package com.onaro.util.jfc.date; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.text.DateFormat; import java.util.Date; import java.util.EnumSet; import javax.swing.JDialog; import javax.swing.JOptionPane; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import com.onaro.client.DateFormats; import com.onaro.sanscreen.client.Messages; import com.onaro.sanscreen.client.view.performance.DataSamples; import com.onaro.util.jfc.enumeration.EnumComboBox; import com.onaro.util.jfc.enumeration.EnumDecorationRepository; public class TimeRangeCombo extends EnumComboBox { private static final long serialVersionUID = 1L; /** * Constant passed to constructor indicating that the times of the time range * should be displayed, not just the dates. */ public static final boolean SHOW_TIME = true; /** * Constant passed to constructor indicating that only the hours of the time range * should be displayed in addition to the dates. */ public static final boolean SHOW_HOURS_ONLY = true; private final boolean isTimeDisplayable; private final boolean isHoursOnly; private TimeSpan timeSpan = null; private TimeSpan customSpan = null; private String customTimeRangeHeaderMessage = null; private boolean valueIsAdjusting = false; /** * The logger to use with this class. */ static final Logger logger = LogManager.getLogger(TimeRangeCombo.class); public TimeRangeCombo(EnumSet allowedRanges) { this (allowedRanges, ! SHOW_TIME, ! SHOW_HOURS_ONLY); } public TimeRangeCombo(EnumSet allowedRanges, boolean doShowTime) { this (allowedRanges, doShowTime, ! SHOW_HOURS_ONLY); } public TimeRangeCombo(EnumSet allowedRanges, boolean doShowTime, boolean doHoursOnly) { super(allowedRanges, null, null, EnumDecorationRepository.NO_COLOR_INSTANCE); setName ("timeRangeCombo"); //$NON-NLS-1$ isTimeDisplayable = doShowTime; isHoursOnly = doHoursOnly; addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!valueIsAdjusting) { doSelectionChanged(false); } } }); doSelectionChanged(true); } public void setSelectedTimeSpan(TimeSpan timeSpan) { DataSamples dataSamples = timeSpan.getDataSamples(); if (dataSamples.isCustom()) { setCustomTimeSpan(timeSpan); } valueIsAdjusting = true; setSelectedItem(dataSamples); valueIsAdjusting = false; } public DataSamples getDataSamples() { return getSelectedEnum(); } public void recalcTimeSpan() { //recalculate the actual time DataSamples dataSamples = getDataSamples(); if (dataSamples != null && ! dataSamples.isCustom()) { setStandardTimeSpan(dataSamples); } } public TimeSpan getTimeSpan() { if (timeSpan == null) { // We don't store value in timeSpan because we want value determined when called. return DataSamples.getDefaultTimeSpan(); } else { return timeSpan; } } public Date getFromTime() { return getTimeSpan().getFromTime(); } public Date getToTime() { return getTimeSpan().getToTime(); } private void setStandardTimeSpan(DataSamples dataSamples) { timeSpan = dataSamples.getTimeSpan(); } private void setCustomTimeSpan(TimeSpan newSpan) { timeSpan = new TimeSpan (newSpan.getFromTime(), newSpan.getToTime(), DataSamples.CUSTOM); customSpan = timeSpan; } public String getCustomTimeRangeHeaderMessage() { return customTimeRangeHeaderMessage; } public void setCustomTimeRangeHeaderMessage(String customTimeRangeHeaderMessage) { this.customTimeRangeHeaderMessage = customTimeRangeHeaderMessage; } private void doSelectionChanged(boolean isInit) { DataSamples dataSamples = getDataSamples(); if (dataSamples != null && ! dataSamples.isCustom()) { setStandardTimeSpan(dataSamples); } else if (dataSamples == DataSamples.CUSTOM && !isInit) { // Let the user choose a time span. TimeSpan selection = selectCustomTimeRange(); if (selection != null) { setCustomTimeSpan(selection); } } updateToolTip(); fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, getSelectedItem(), ItemEvent.SELECTED)); } /** * This method adds the tool tip with exact from and to times for the selected time span */ private void updateToolTip(){ // MM/dd/yy hh:mm a - This is the date and time format we use in the tool tip DateFormat format = DateFormats.getShortDateTimeFormat(); if(getDataSamples().isAll()){ setToolTipText(com.onaro.util.jfc.date.Messages.INSTANCE.getTimeSpanToolTipForAllData( format.format(getToTime()))); }else{ setToolTipText(com.onaro.util.jfc.date.Messages.INSTANCE.getTimeSpanToolTip( format.format(getFromTime()), format.format(getToTime()))); } } /** * Prompt for the custom toTime and fromTime */ @SuppressWarnings("null") // False alarm by Eclipse private TimeSpan selectCustomTimeRange() { TimeSpan newSpan; // Setup the initial time to reflect the previously selected custom span // if custom has been previously set, otherwise the current time in the combo. if (customSpan == null) { newSpan = getTimeSpan(); // If the current span is All (0 to now), use default span. if (newSpan.getDataSamples().isAll()) { newSpan = DataSamples.getDefaultTimeSpan(); } } else { newSpan = customSpan; } // Repeatedly show the time filter dialog until a valid selection is made boolean validSelection = false; while (!validSelection) { CustomFilterPanel customFilterPanel = new CustomFilterPanel(newSpan.getFromTime(), newSpan.getToTime(), isTimeDisplayable, isHoursOnly); if (StringUtils.isNotBlank(getCustomTimeRangeHeaderMessage())) { customFilterPanel.setHeaderMessage(getCustomTimeRangeHeaderMessage()); } JOptionPane pane = new JOptionPane(customFilterPanel, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION); JDialog dialog = pane.createDialog(this.getRootPane(), Messages.INSTANCE.getCustomTimeChooserDialogTitle()); dialog.setVisible(true); Integer rv = (Integer) pane.getValue(); if (rv != null && rv.intValue() == JOptionPane.OK_OPTION) { Date newFrom = customFilterPanel.getFrom(); Date newTo = customFilterPanel.getTo(); // If a date wasn't specified somehow, prompt again if (newFrom == null || newTo == null) continue; if (newFrom.after(newTo)) { JOptionPane.showMessageDialog(this.getRootPane(), Messages.INSTANCE.getInvalidTimeRangeMessage(), Messages.INSTANCE.getInvalidTimeRangeTitle(), JOptionPane.ERROR_MESSAGE); } else { if(logger.isDebugEnabled()) { logger.debug("Times from custom time combobox newFrom: " + newFrom //$NON-NLS-1$ + "(" + newFrom.getTime() //$NON-NLS-1$ + ") newTo: " + newTo //$NON-NLS-1$ + "(" + newTo.getTime() + ")"); //$NON-NLS-1$ //$NON-NLS-2$ } newSpan = new TimeSpan(newFrom, newTo); // break out of the loop validSelection = true; } } else { // Canceling is a valid selection newSpan = null; validSelection = true; } } return newSpan; } }