/** * Returns the type of the property. <code>getValue</code> and * <code>setValue</code> methods must be compatible with this type: one can * safely cast <code>getValue</code> to given type and pass any variable * assignable to this type as a parameter to <code>setValue</code>. * * @return the Type of the property. */ @Override public Class<?> getType() { if (isMultiSelect()) { return Set.class; } else { return Object.class; } }
/** * Sets the item id that represents null value of this select. * * <p> * Data interface does not support nulls as item ids. Selecting the item * identified by this id is the same as selecting no items at all. This * setting only affects the single select mode. * </p> * * @param nullSelectionItemId * the nullSelectionItemId to set. * @see #getNullSelectionItemId() * @see #isSelected(Object) * @see #select(Object) */ public void setNullSelectionItemId(Object nullSelectionItemId) { if (nullSelectionItemId != null && isMultiSelect()) { throw new IllegalStateException( "Multiselect and NullSelectionItemId can not be set at the same time."); } this.nullSelectionItemId = nullSelectionItemId; }
/** * Gets the selected item id or in multiselect mode a set of selected ids. * * @see AbstractField#getValue() */ @Override public Object getValue() { final Object retValue = super.getValue(); if (isMultiSelect()) { // If the return value is not a set if (retValue == null) { return new HashSet<Object>(); } if (retValue instanceof Set) { return Collections.unmodifiableSet((Set<?>) retValue); } else if (retValue instanceof Collection) { return new HashSet<Object>((Collection<?>) retValue); } else { final Set<Object> s = new HashSet<Object>(); if (items.containsId(retValue)) { s.add(retValue); } return s; } } else { return retValue; } }
protected void readItems(Element design, DesignContext context) { Set<String> selected = new HashSet<String>(); for (Element child : design.children()) { readItem(child, selected, context); } if (!selected.isEmpty()) { if (isMultiSelect()) { setValue(selected, false, true); } else if (selected.size() == 1) { setValue(selected.iterator().next(), false, true); } else { throw new DesignException( "Multiple values selected for a single select component"); } } }
/** * Tests if an item is selected. * * <p> * In single select mode testing selection status of the item identified by * {@link #getNullSelectionItemId()} returns true if the value of the * property is null. * </p> * * @param itemId * the Id the of the item to be tested. * @see #getNullSelectionItemId() * @see #setNullSelectionItemId(Object) * */ public boolean isSelected(Object itemId) { if (itemId == null) { return false; } if (isMultiSelect()) { return ((Set<?>) getValue()).contains(itemId); } else { final Object value = getValue(); return itemId .equals(value == null ? getNullSelectionItemId() : value); } }
/** * Unselects an item. * * @param itemId * the identifier of the Item to be unselected. * @see #getNullSelectionItemId() * @see #setNullSelectionItemId(Object) * */ public void unselect(Object itemId) { if (isSelected(itemId)) { if (isMultiSelect()) { final Set<Object> s = new HashSet<Object>((Set<?>) getValue()); s.remove(itemId); setValue(s); } else { setValue(null); } } }
boolean ignoreReadOnly) throws Property.ReadOnlyException, ConversionException, InvalidValueException { if (isMultiSelect()) { if (newFieldValue == null) { super.setValue(new LinkedHashSet<Object>(), repaintIsNotNeeded,
/** * Selects an item. * * <p> * In single select mode selecting item identified by * {@link #getNullSelectionItemId()} sets the value of the property to null. * </p> * * @param itemId * the identifier of Item to be selected. * @see #getNullSelectionItemId() * @see #setNullSelectionItemId(Object) * */ public void select(Object itemId) { if (!isMultiSelect()) { setValue(itemId); } else if (!isSelected(itemId) && itemId != null && items.containsId(itemId)) { final Set<Object> s = new HashSet<Object>((Set<?>) getValue()); s.add(itemId); setValue(s); } }
if (isMultiSelect()) { Collection<Object> valueAsCollection = (Collection<Object>) value; List<Object> newSelection = new ArrayList<Object>(
@Override public void addNewItem(String newItemCaption) { // Checks for readonly if (isReadOnly()) { throw new Property.ReadOnlyException(); } // Adds new option if (addItem(newItemCaption) != null) { // Sets the caption property, if used if (getItemCaptionPropertyId() != null) { getContainerProperty(newItemCaption, getItemCaptionPropertyId()) .setValue(newItemCaption); } if (isMultiSelect()) { Set values = new HashSet((Collection) getValue()); values.add(newItemCaption); setValue(values); } else { setValue(newItemCaption); } } } }
if (isMultiSelect()) { target.addAttribute("selectmode", "multi"); if (isMultiSelect()) { selectedKeys = new String[((Set<?>) getValue()).size()]; } else {