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 org.eclipse.jface.resource.JFaceResources;
  37 import org.eclipse.swt.SWT;
  38 import org.eclipse.swt.layout.GridData;
  39 import org.eclipse.swt.layout.GridLayout;
  40 import org.eclipse.swt.widgets.Button;
  41 import org.eclipse.swt.widgets.Composite;
  42 import org.eclipse.swt.widgets.Display;
  43 import org.eclipse.swt.widgets.Event;
  44 import org.eclipse.swt.widgets.Label;
  45 import org.eclipse.swt.widgets.Listener;
  46 import org.openjdk.jmc.common.unit.IQuantity;
  47 import org.openjdk.jmc.common.unit.IRange;
  48 import org.openjdk.jmc.common.unit.UnitLookup;
  49 import org.openjdk.jmc.ui.charts.XYChart;
  50 
  51 public class TimeFilter extends Composite {
  52 
  53         private ChartCanvas chartCanvas;
  54         private XYChart chart;
  55         private TimeDisplay startDisplay;
  56         private TimeDisplay endDisplay;
  57 
  58         public TimeFilter(Composite parent, IRange<IQuantity> recordingRange, Listener resetListener) {
  59                 super(parent, SWT.NO_BACKGROUND);
  60                 this.setLayout(new GridLayout(7, false));
  61                 Label eventsLabel = new Label(this, SWT.LEFT);
  62                 eventsLabel.setText(Messages.TimeFilter_FILTER_EVENTS);
  63                 eventsLabel.setFont(JFaceResources.getFontRegistry().get(JFaceResources.BANNER_FONT));
  64 
  65                 Label from = new Label(this, SWT.CENTER);
  66                 from.setText(Messages.TimeFilter_FROM);
  67 
  68                 startDisplay = new TimeDisplay(this);
  69 
  70                 Label to = new Label(this, SWT.CENTER);
  71                 to.setText(Messages.TimeFilter_TO);
  72 
  73                 endDisplay = new TimeDisplay(this);
  74 
  75                 Button filterBtn = new Button(this, SWT.PUSH);
  76                 filterBtn.setText(Messages.TimeFilter_FILTER);
  77                 filterBtn.addListener(SWT.Selection, new Listener() {
  78                         @Override
  79                         public void handleEvent(Event event) {
  80                                 if (startDisplay.isFormatValid() && endDisplay.isFormatValid()) {
  81                                         Long startDisplayEpoch = startDisplay.getDisplayTime().in(UnitLookup.EPOCH_MS).longValue();
  82                                         Long endDisplayEpoch = endDisplay.getDisplayTime().in(UnitLookup.EPOCH_MS).longValue();
  83                                         Long endEpoch = recordingRange.getEnd().in(UnitLookup.EPOCH_MS).longValue();
  84                                         Long startEpoch = recordingRange.getStart().in(UnitLookup.EPOCH_MS).longValue();
  85                                         if (startDisplayEpoch < startEpoch) {
  86                                                 DialogToolkit.showWarning(Display.getCurrent().getActiveShell(),
  87                                                                 Messages.TimeFilter_ERROR, Messages.TimeFilter_START_TIME_PRECEEDS_ERROR);
  88                                         } else if (endDisplayEpoch > endEpoch) {
  89                                                 DialogToolkit.showWarning(Display.getCurrent().getActiveShell(),
  90                                                                 Messages.TimeFilter_ERROR, Messages.TimeFilter_END_TIME_EXCEEDS_ERROR);
  91                                         } else if (startDisplayEpoch > endDisplayEpoch) {
  92                                                 DialogToolkit.showWarning(Display.getCurrent().getActiveShell(),
  93                                                                 Messages.TimeFilter_ERROR, Messages.TimeFilter_START_TIME_LONGER_THAN_END_ERROR);
  94                                         } else {
  95                                                 chart.setVisibleRange(startDisplay.getDisplayTime(), endDisplay.getDisplayTime());
  96                                                 chartCanvas.redrawChart();
  97                                         }
  98                                 } else {
  99                                         DialogToolkit.showWarning(Display.getCurrent().getActiveShell(),
 100                                                         Messages.TimeFilter_ERROR, Messages.TimeFilter_INVALID_FORMAT_ERROR);
 101                                 }
 102                         }
 103                 });
 104 
 105                 Button resetBtn = new Button(this, SWT.PUSH);
 106                 resetBtn.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false));
 107                 resetBtn.setText(Messages.TimeFilter_RESET);
 108                 resetBtn.addListener(SWT.Selection, resetListener);
 109         }
 110 
 111         public void setChart(XYChart chart) {
 112                 this.chart = chart;
 113         }
 114 
 115         public void setChartCanvas(ChartCanvas canvas) {
 116                 this.chartCanvas = canvas;
 117         }
 118         
 119         public boolean setStartTime(IQuantity time) {
 120                 startDisplay.setTime(time);
 121                 return true;
 122         }
 123 
 124         public void setEndTime(IQuantity time) {
 125                 endDisplay.setTime(time);
 126         }
 127 }