package com.onaro.util.jfc.date; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.text.DateFormat; import java.util.Calendar; import java.util.Date; import javax.swing.BorderFactory; import javax.swing.JLabel; import javax.swing.JPanel; import org.apache.commons.lang3.time.DateUtils; import com.jidesoft.combobox.DateComboBox; import com.onaro.client.DateFormats; public class CustomFilterPanel extends JPanel { protected final DateFormat TIME_DATE_FORMAT = DateFormats.getShortDateTimeFormat(); protected final DateFormat HOUR_ONLY_TIME_DATE_FORMAT = DateFormats.getShortDateTimeFormat(); protected final DateFormat DATE_FORMAT = DateFormats.getShortDateFormat(); // Restrict them to only specifying on the hour private static final String SPINNER_HOURS_ONLY_FORMAT_STR = DateFormats.FORMATS_RB.getString("HOURS_ONLY_FORMAT"); //$NON-NLS-1$ private static final String SPINNER_FULL_FORMAT_STR = DateFormats.FORMATS_RB.getString("SHORT_TIME_FORMAT"); //$NON-NLS-1$ private static final long serialVersionUID = 1L; private JLabel fromLabel = null; private JLabel toLabel = null; private DateComboBox fromDateCombo = null; private DateComboBox toDateCombo = null; private JLabel headerLabel = null; private Date minDate = null; // @jve:decl-index=0: private Date maxDate = null; // @jve:decl-index=0: private boolean hoursOnly; /** * This is the default constructor */ public CustomFilterPanel() { this(null, null); } public CustomFilterPanel(Date from, Date to) { this(from, to, true); } public CustomFilterPanel(Date from, Date to, boolean showTime) { this(from, to, showTime, null); } public CustomFilterPanel(Date from, Date to, boolean showTime, boolean hoursOnly) { this(from, to, showTime, hoursOnly, null); } public CustomFilterPanel(Date from, Date to, boolean showTime, TimeSpan allowedRange) { this(from, to, showTime, false, allowedRange); } public CustomFilterPanel(Date from, Date to, boolean showTime, boolean hoursOnly, TimeSpan allowedRange) { this.hoursOnly = hoursOnly; // If working with top of the hour only, clear minutes/seconds if(this.hoursOnly) { from = DateUtils.truncate(from, Calendar.HOUR); to = DateUtils.truncate(to, Calendar.HOUR); } setShowTime(showTime); setAllowedRange(allowedRange); setFrom(from); setTo(to); initialize(); } public String getHeaderMessage() { return getHeaderLabel().getText(); } public void setHeaderMessage(String headerMessage) { getHeaderLabel().setText(headerMessage); } public TimeSpan getAllowedRange() { return new TimeSpan(getMinDate(), getMaxDate()); } public void setAllowedRange(TimeSpan allowedRange) { if (allowedRange == null) { setMinDate(null); setMaxDate(null); } else { setMinDate(allowedRange.getFromTime()); setMaxDate(allowedRange.getToTime()); } } public Date getMaxDate() { return maxDate; } public void setMaxDate(Date maxDate) { this.maxDate = maxDate; Calendar max = null; if (maxDate != null) { max = Calendar.getInstance(); max.setTime(maxDate); } getFromDateCombo().getDateModel().setMaxDate(max); getToDateCombo().getDateModel().setMaxDate(max); } public Date getMinDate() { return minDate; } public void setMinDate(Date minDate) { this.minDate = minDate; Calendar min = null; if (minDate != null) { min = Calendar.getInstance(); min.setTime(minDate); } getFromDateCombo().getDateModel().setMinDate(min); getToDateCombo().getDateModel().setMinDate(min); } public boolean isShowTime() { return getFromDateCombo().isTimeDisplayed(); } public void setShowTime(boolean showTime) { DateFormat format; String timeFormat = null; DateComboBox fromCombo = getFromDateCombo(); DateComboBox toCombo = getToDateCombo(); if(showTime) { format = hoursOnly ? HOUR_ONLY_TIME_DATE_FORMAT : TIME_DATE_FORMAT; timeFormat = hoursOnly ? SPINNER_HOURS_ONLY_FORMAT_STR : SPINNER_FULL_FORMAT_STR; fromCombo.setTimeFormat(timeFormat); toCombo.setTimeFormat(timeFormat); } else { format = DATE_FORMAT; } fromCombo.setTimeDisplayed(showTime); fromCombo.setFormat(format); toCombo.setTimeDisplayed(showTime); toCombo.setFormat(format); } public Date getFrom() { Date result = getFromDateCombo().getDate(); if(hoursOnly) { result = DateUtils.truncate(result, Calendar.HOUR); } return result; } public void setFrom(Date from) { if(hoursOnly) { from = DateUtils.truncate(from, Calendar.HOUR); } getFromDateCombo().setDate(from); getToDateCombo().setPrototypeDisplayValue(from); } public Date getTo() { Date result = getToDateCombo().getDate(); if(hoursOnly) { result = DateUtils.truncate(result, Calendar.HOUR); } return result; } public void setTo(Date to) { if(hoursOnly) { to = DateUtils.truncate(to, Calendar.HOUR); } getToDateCombo().setDate(to); getToDateCombo().setPrototypeDisplayValue(to); } /** * This method initializes this * * @return void */ private void initialize() { GridBagConstraints headerConstraints = new GridBagConstraints(); headerConstraints.gridx = 0; headerConstraints.gridwidth = 2; headerConstraints.gridy = 0; GridBagConstraints fromLabelConstraints = new GridBagConstraints(); fromLabelConstraints.gridx = 0; fromLabelConstraints.anchor = GridBagConstraints.EAST; fromLabelConstraints.insets = new Insets(10, 0, 5, 5); fromLabelConstraints.gridy = 1; GridBagConstraints fromComboConstraints = new GridBagConstraints(); fromComboConstraints.gridx = 1; fromComboConstraints.insets = new Insets(10, 0, 5, 0); fromComboConstraints.fill = GridBagConstraints.HORIZONTAL; fromComboConstraints.weightx = 1; fromComboConstraints.gridy = 1; GridBagConstraints toLabelConstraints = new GridBagConstraints(); toLabelConstraints.gridx = 0; toLabelConstraints.gridy = 2; toLabelConstraints.anchor = GridBagConstraints.EAST; toLabelConstraints.insets = new Insets(0, 0, 0, 5); GridBagConstraints toComboConstraints = new GridBagConstraints(); toComboConstraints.gridx = 1; toComboConstraints.fill = GridBagConstraints.HORIZONTAL; toComboConstraints.weightx = 1; toComboConstraints.gridy = 2; fromLabel = new JLabel(); fromLabel.setText(Messages.INSTANCE.getFromLabel()); toLabel = new JLabel(); toLabel.setText(Messages.INSTANCE.getToLabel()); this.setLayout(new GridBagLayout()); this.setName("customFilterPanel"); //$NON-NLS-1$ this.add(fromLabel, fromLabelConstraints); this.add(toLabel, toLabelConstraints); this.add(getFromDateCombo(), fromComboConstraints); this.add(getToDateCombo(), toComboConstraints); this.add(getHeaderLabel(), headerConstraints); this.setBorder(BorderFactory.createEmptyBorder(0, 30, 0, 30)); } /** * This method initializes fromDateCombo * * @return com.jidesoft.combobox.DateComboBox */ private DateComboBox getFromDateCombo() { if (fromDateCombo == null) { fromDateCombo = new DateComboBox(); fromDateCombo.setShowNoneButton(false); fromDateCombo.setEditable(false); fromDateCombo.setShowTodayButton(true); // Default from value is one day ago Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); //day diff fromDateCombo.setDate(calendar.getTime()); } return fromDateCombo; } /** * This method initializes toDateCombo * * @return com.jidesoft.combobox.DateComboBox */ private DateComboBox getToDateCombo() { if (toDateCombo == null) { toDateCombo = new DateComboBox(); toDateCombo.setShowNoneButton(false); toDateCombo.setEditable(false); toDateCombo.setShowTodayButton(true); // Default to value is now toDateCombo.setDate(new Date()); } return toDateCombo; } /** * This method initializes headerLabel * * @return javax.swing.JLabel */ private JLabel getHeaderLabel() { if (headerLabel == null) { headerLabel = new JLabel(); headerLabel.setText(null); } return headerLabel; } }