1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2019, Red Hat Inc. All rights reserved.
   4  *
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * The contents of this file are subject to the terms of either the Universal Permissive License
   8  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   9  *
  10  * or the following license:
  11  *
  12  * Redistribution and use in source and binary forms, with or without modification, are permitted
  13  * provided that the following conditions are met:
  14  *
  15  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  16  * and the following disclaimer.
  17  *
  18  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  19  * conditions and the following disclaimer in the documentation and/or other materials provided with
  20  * the distribution.
  21  *
  22  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  23  * endorse or promote products derived from this software without specific prior written permission.
  24  *
  25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  27  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  28  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  32  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33  */
  34 package org.openjdk.jmc.ui.misc;
  35 
  36 import java.util.Calendar;
  37 import java.util.Date;
  38 import java.util.regex.Matcher;
  39 import java.util.regex.Pattern;
  40 
  41 import org.eclipse.swt.SWT;
  42 import org.eclipse.swt.events.ModifyEvent;
  43 import org.eclipse.swt.events.ModifyListener;
  44 import org.eclipse.swt.layout.GridLayout;
  45 import org.eclipse.swt.widgets.Composite;
  46 import org.eclipse.swt.widgets.Text;
  47 import org.openjdk.jmc.common.unit.IQuantity;
  48 import org.openjdk.jmc.common.unit.UnitLookup;
  49 import org.openjdk.jmc.ui.misc.PatternFly.Palette;
  50 
  51 public class TimeDisplay extends Composite {
  52 
  53                 private static final String TIME_FORMAT_REGEX = "\\d{2}\\:\\d{2}\\:\\d{2}\\:\\d{3}";
  54                 private static final String DIGIT_FORMAT_REGEX = "\\d{3}|\\d{2}";
  55                 private final Pattern timePattern = Pattern.compile(TIME_FORMAT_REGEX);
  56                 private final Pattern digitPattern = Pattern.compile(DIGIT_FORMAT_REGEX);
  57                 private Calendar currentCalendar; // currently active time in Calendar form
  58                 private IQuantity currentTime; // currently active time
  59                 private IQuantity displayTime; // time shown in the widget (may or not be what's active)
  60                 private StringBuilder sb;
  61                 private Text timeText;
  62 
  63                 public TimeDisplay(Composite parent) {
  64                         super(parent, SWT.NO_BACKGROUND);
  65                         this.setLayout(new GridLayout());
  66                         timeText = new Text(this, SWT.SEARCH | SWT.SINGLE);
  67                         timeText.setTextLimit(12);
  68                         timeText.addModifyListener(new ModifyListener() {
  69                                 @Override
  70                                 public void modifyText(ModifyEvent e) {
  71                                         displayTime = null;
  72                                         if (isFormatValid() && isValidTime()) {
  73                                                 timeText.setForeground(Palette.PF_BLACK.getSWTColor());
  74                                         } else {
  75                                                 timeText.setForeground(Palette.PF_RED_100.getSWTColor());
  76                                         }
  77                                 }
  78                         });
  79                 }
  80 
  81                 // Convert epoch ms timestamp to Calendar object
  82                 private Calendar convertEpochToCalendar(long epoch) {
  83                         currentCalendar = Calendar.getInstance();
  84                         currentCalendar.setTime(new Date(epoch));
  85                         return currentCalendar;
  86                 }
  87 
  88                 // Locally store the new time, and format it for displaying in the Text widget
  89                 public void setTime(IQuantity time) {
  90                         this.currentTime = time;
  91                         displayTime(formatTimeString(convertEpochToCalendar(time.in(UnitLookup.EPOCH_MS).longValue())));
  92                 }
  93 
  94                 // Returns the IQuantity time stamp of the time displayed in the widget
  95                 public IQuantity getDisplayTime() {
  96                         if (displayTime != null) {
  97                                 formatTimeString(convertEpochToCalendar(displayTime.in(UnitLookup.EPOCH_MS).longValue()));
  98                                 return displayTime;
  99                         }
 100                         if (isFormatValid() && isValidTime()) {
 101                                 IQuantity time = currentTime;
 102                                 Matcher m = digitPattern.matcher(timeText.getText());
 103                                 int i = 0;
 104                                 while(m.find()) {
 105                                         int value = Integer.parseInt(m.group());
 106                                         switch(i) {
 107                                         case 0:
 108                                                 value = value - currentCalendar.get(Calendar.HOUR);
 109                                                 time = time.in(UnitLookup.EPOCH_NS).add(UnitLookup.HOUR.quantity(value).in(UnitLookup.NANOSECOND));
 110                                                 break;
 111                                         case 1:
 112                                                 value = value - currentCalendar.get(Calendar.MINUTE);
 113                                                 time = time.in(UnitLookup.EPOCH_NS).add(UnitLookup.MINUTE.quantity(value).in(UnitLookup.NANOSECOND));
 114                                                 break;
 115                                         case 2:
 116                                                 value = value - currentCalendar.get(Calendar.SECOND);
 117                                                 time = time.in(UnitLookup.EPOCH_NS).add(UnitLookup.SECOND.quantity(value).in(UnitLookup.NANOSECOND));
 118                                                 break;
 119                                         case 3:
 120                                                 value = value - currentCalendar.get(Calendar.MILLISECOND);
 121                                                 time = time.in(UnitLookup.EPOCH_NS).add(UnitLookup.MILLISECOND.quantity(value).in(UnitLookup.NANOSECOND));
 122                                                 break;
 123                                         }
 124                                         i++;
 125                                 }
 126                                 this.displayTime = time;
 127                                 return time;
 128                         }
 129                         return null;
 130                 }
 131 
 132                 // Format the calendar time to a string HH:mm:ss:SSS
 133                 private String formatTimeString(Calendar cal) {
 134                         sb = new StringBuilder();
 135                         sb.append(String.format("%02d", cal.get(Calendar.HOUR)));
 136                         sb.append(":");
 137                         sb.append(String.format("%02d", cal.get(Calendar.MINUTE)));
 138                         sb.append(":");
 139                         sb.append(String.format("%02d", cal.get(Calendar.SECOND)));
 140                         sb.append(":");
 141                         sb.append(String.format("%03d", cal.get(Calendar.MILLISECOND)));
 142                         return sb.toString();
 143                 }
 144 
 145                 public void displayTime(String time) {
 146                         timeText.setText(time);
 147                 }
 148 
 149                 /**
 150                  * Verify that the time string inside the text widget matches the
 151                  * expected time format of HH:mm:ss:SSS
 152                  * @return true if the text corresponds to a HH:mm:ss:SSS format
 153                  */
 154                 protected boolean isFormatValid() {
 155                         if (!timePattern.matcher(timeText.getText()).matches()) {
 156                                 // not in HH:mm:ss:SSS format
 157                                 return false;
 158                         }
 159                         return true;
 160                 }
 161 
 162                 /**
 163                  * Verify that the string inside the text widget is a valid
 164                  * 24-hour clock time
 165                  * @return true if the text corresponds to a valid 24-hour time
 166                  */
 167                 private boolean isValidTime() {
 168                         Matcher m = digitPattern.matcher(timeText.getText());
 169                         int i = 0;
 170                         while(m.find()) {
 171                                 int value = Integer.parseInt(m.group());
 172                                 if (i == 0 && value >= 24) {
 173                                         return false;
 174                                 } else if ((i == 1 || i == 2) && value >= 60) {
 175                                         return false;
 176                                 }
 177                                 i++;
 178                         }
 179                         return true;
 180                 }
 181         }