/** * Returns date range from picker. * * @return The date range. */ protected DateRange getDateRange() { return dateRangePicker == null ? null : dateRangePicker.getSelectedRange(); }
public DateRangePicker() { super(); setReadonly(true); customItem = new Dateitem(); customItem.setLabel("Custom..."); addChild(customItem); setAllowCustom(false); loadChoices(DEFAULT_CHOICES); }
/** * Load choices from a list. * * @param choices A list of choices. */ public void loadChoices(Iterable<String> choices) { clear(); if (choices != null) { for (String choice : choices) { addChoice(choice, false); } } checkSelection(true); }
/** * Updates the visual appearance of the current selection. */ private void updateSelection() { Dateitem selectedItem = getSelectedItem(); if (selectedItem == null) { addStyle("color", "gray"); } else { addStyle("color", "inherit"); } setFocus(false); }
/** * Initializes Controller. */ @Override protected void initializeController() { super.initializeController(); if (dateRangePicker != null) { String deflt = getPropertyValue(Constants.PROPERTY_ID_DATE_RANGE, String.class, "Last Two Years"); dateRangePicker.setSelectedItem(dateRangePicker.findMatchingItem(deflt)); initDateFilter().setDateRange(dateRangePicker.getSelectedRange()); } if (dateTypePicker != null) { for (DateType dt : DateType.values()) { String lbl = getLabel(Constants.LABEL_ID_SORT_MODE.replace("$", dt.name().toLowerCase())); Comboitem item = new Comboitem(lbl); item.setData(dt); dateTypePicker.addChild(item); } DateType sortModePref = getPropertyValue(Constants.PROPERTY_ID_SORT_MODE, DateType.class, DateType.MEASURED); Comboitem item = (Comboitem) dateTypePicker.findChildByData(sortModePref); dateTypePicker.setSelectedItem(item == null ? (Comboitem) dateTypePicker.getFirstChild() : item); dateTypePicker.setReadonly(true); initDateFilter().setDateType(sortModePref); } if (dateFilter != null) { registerQueryFilter(dateFilter); } }
item = findMatchingItem(range); item.setLabel(range.getLabel()); item.setData(range); addChild(item, isCustom ? null : customItem); setSelectedItem(item);
/** * Check the current selection. If nothing is selected, display a prompt message in gray text. * Also, remembers the last selection made. * * @param suppressEvent If true, onSelectRange event is not fired. */ private void checkSelection(boolean suppressEvent) { Dateitem selectedItem = getSelectedItem(); if (selectedItem == null) { selectedItem = lastSelectedItem; setSelectedItem(selectedItem); } else if (selectedItem != customItem && lastSelectedItem != selectedItem) { lastSelectedItem = selectedItem; if (!suppressEvent) { EventUtil.send(new Event(ON_SELECT_RANGE, this)); } } updateSelection(); }
/** * Change event handler. * * @param event The change event. */ @EventHandler("change") private void onChange(ChangeEvent event) { // When the custom range item is selected, triggers the display of the date range dialog. if (event.getRelatedTarget() == customItem) { event.stopPropagation(); DateRangeDialog.show((range) -> { setSelectedItem(range == null ? lastSelectedItem : addChoice(range, true)); checkSelection(false); }); } else { checkSelection(false); } }
/** * Removes all items (except for "custom") from the item list. */ @Override public void clear() { List<BaseComponent> items = getChildren(); for (int i = items.size() - 1; i >= 0; i--) { if (items.get(i) != customItem) { items.get(i).destroy(); } } }
/** * Adds a date range item to the choice list from its string representation (see DateRange class * for format). * * @param range String representation of date range. * @param isCustom If true, range is a custom item. In this case, if another matching custom * item exists, it will not be added. * @return combo box item that was added (or found if duplicate custom item). */ public Dateitem addChoice(String range, boolean isCustom) { return addChoice(new DateRange(range), isCustom); }
/** * Returns the date range item that is currently selected. * * @return Selected date range item, or null if none selected. */ public DateRange getSelectedRange() { Dateitem selected = getSelectedItem(); return selected == null ? null : (DateRange) selected.getData(); }
/** * Searches for a comboitem that has a label that matches the specified value. The search is * case insensitive. * * @param label Label text to find. * @return A comboitem with a matching label., or null if not found. */ public Dateitem findMatchingItem(String label) { for (BaseComponent child : getChildren()) { Dateitem item = (Dateitem) child; if (label.equalsIgnoreCase(item.getLabel())) { return item; } } return null; }
/** * Returns the selected start date. This may be null if there is no active selection or if the * selected date range has no start date. * * @return Starting date of range, or null. */ public Date getStartDate() { DateRange range = getSelectedRange(); return range == null ? null : range.getStartDate(); }
/** * Searches for a comboitem that has a date range equivalent to the specified range. * * @param range The date range to locate. * @return A comboitem containing the date range, or null if not found. */ public Dateitem findMatchingItem(DateRange range) { for (BaseComponent item : getChildren()) { if (range.equals(item.getData())) { return (Dateitem) item; } } return null; }
/** * Returns the selected end date. This may be null if there is no active selection or if the * selected date range has no end date. * * @return Ending date of range, or null. */ public Date getEndDate() { DateRange range = getSelectedRange(); return range == null ? null : range.getEndDate(); }