public DateRangeChooser() { super(); customItem = new Listitem(); customItem.setLabel("Custom..."); addChild(customItem); setAllowCustom(false); loadChoices(null); }
/** * onSelect 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 (getSelectedItem() == customItem) { event.stopPropagation(); DateRangeDialog.show((range) -> { if (range == null) { setSelectedItem(lastSelectedItem); } else { setSelectedItem(addChoice(range, true)); } checkSelection(false); }); } else { checkSelection(false); } } }
/** * Load choices from the named property * * @param propertyName Name of the property containing the choices. Must be in the acceptable * format. If the property name is null, loads a default set of choices. */ public void loadChoices(String propertyName) { clear(); if (propertyName == null || propertyName.isEmpty()) { addChoice("All Dates", false); addChoice("Today|T|T", false); addChoice("Last Week|T|T-7", false); addChoice("Last Month|T|T-30|1", false); addChoice("Last Year|T|T-365", false); addChoice("Last Two Years|T|T-730", false); } else { for (String value : PropertyUtil.getValues(propertyName, null)) { addChoice(value, false); } } checkSelection(true); }
/** * 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) { Listitem 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(); }
item = findMatchingItem(range); item.setLabel(range.getLabel()); item.setData(range); addChild(item, isCustom ? null : customItem); setSelectedItem(item);
/** * Removes all items (except for "custom") from the item list. */ public void clear() { List<?> items = getChildren(); for (int i = items.size() - 1; i >= 0; i--) { if (items.get(i) != customItem) { items.remove(i); } } }
/** * 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(); }
/** * 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 List item that was added (or found if duplicate custom item). */ public Listitem 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() { Listitem selected = getSelectedItem(); return selected == null ? null : (DateRange) selected.getData(); }
/** * If set to true, the user may select "Custom..." from the choice list and enter a custom date * range. If set to false, this choice is hidden and any existing custom entries are removed * from the list. * * @param allowCustom Determines whether or not custom entries are allowed. */ public void setAllowCustom(boolean allowCustom) { customItem.setVisible(allowCustom); if (!allowCustom) { BaseComponent sibling; while ((sibling = customItem.getNextSibling()) != null) { removeChild(sibling); } } }
/** * Searches for a Listitem that has a label that matches the specified value. The search is case * insensitive. * * @param label Label text to find. * @return A Listitem with a matching label., or null if not found. */ public Listitem findMatchingItem(String label) { for (Listitem item : getChildren(Listitem.class)) { if (label.equalsIgnoreCase(item.getLabel())) { return 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(); }
/** * Searches for a Listitem that has a date range equivalent to the specified range. * * @param range The date range to locate. * @return A Listitem containing the date range, or null if not found. */ public Listitem findMatchingItem(DateRange range) { for (BaseComponent item : getChildren()) { if (range.equals(item.getData())) { return (Listitem) item; } } return null; }