if (isNewItemsAllowed()) { final String newitem = (String) variables.get("newitem"); if (newitem != null && !newitem.isEmpty()) { getNewItemHandler().addNewItem(newitem); if (isMultiSelect()) { for (String key : clientSideSelectedKeys) { final Object id = itemIdMapper.get(key); if (!isNullSelectionAllowed() && (id == null || id == getNullSelectionItemId())) { markAsDirty(); } else if (id != null && containsId(id)) { acceptedSelections.add(id); if (!isNullSelectionAllowed() && acceptedSelections.isEmpty()) { markAsDirty(); return; Collection<?> visibleNotSelected = getVisibleItemIds(); if (visibleNotSelected != null) { visibleNotSelected = new HashSet<Object>( Set<Object> newsel = (Set<Object>) getValue(); if (newsel == null) { newsel = new LinkedHashSet<Object>();
/** * Writes the data source items to a design. Hierarchical select components * should override this method to only write the root items. * * @since 7.5.0 * @param design * the element into which to insert the items * @param context * the DesignContext instance used in writing */ protected void writeItems(Element design, DesignContext context) { for (Object itemId : getItemIds()) { writeItem(design, itemId, context); } }
/** * Creates a new select that is connected to a data-source. * * @param caption * the Caption of the component. * @param dataSource * the Container datasource to be selected from by this select. */ public AbstractSelect(String caption, Container dataSource) { setCaption(caption); setContainerDataSource(dataSource); }
/** * Sets the visible value of the property. * * <p> * The value of the select is the selected item id. If the select is in * multiselect-mode, the value is a set of selected item keys. In * multiselect mode all collections of id:s can be assigned. * </p> * * @param newValue * the New selected item or collection of selected items. * @see AbstractField#setValue(java.lang.Object) */ @Override public void setValue(Object newValue) throws Property.ReadOnlyException { if (newValue == getNullSelectionItemId()) { newValue = null; } setValue(newValue, false); }
if (isMultiSelect()) { target.addAttribute("selectmode", "multi"); if (isNewItemsAllowed()) { target.addAttribute("allownewitem", true); if (isNullSelectionAllowed()) { target.addAttribute("nullselect", true); if (getNullSelectionItemId() != null) { target.addAttribute("nullselectitem", true); if (isMultiSelect()) { selectedKeys = new String[((Set<?>) getValue()).size()]; } else { selectedKeys = new String[(getValue() == null && getNullSelectionItemId() == null ? 0 : 1)]; getCaptionChangeListener().clear(); final Collection<?> ids = getItemIds(); if (isNullSelectionAllowed() && getNullSelectionItemId() != null && !ids.contains(getNullSelectionItemId())) { final Object id = getNullSelectionItemId(); paintItem(target, id); if (isSelected(id)) { selectedKeys[keyIndex++] = itemIdMapper.key(id); for (final Object id : getItemIds()) {
@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); } } } }
newField.setCaption(oldField.getCaption()); newField.setReadOnly(oldField.isReadOnly()); newField.setBuffered(oldField.isBuffered()); newField.addContainerProperty("desc", String.class, ""); newField.setItemCaptionPropertyId("desc"); for (int i = 0; i < values.length; i++) { Object id = values[i]; final Item item; if (id == null) { id = newField.addItem(); item = newField.getItem(id); newField.setNullSelectionItemId(id); } else { item = newField.addItem(id); newField.setPropertyDataSource(property); newField.addListener(fieldValueChangeListener); oldField.removeListener(fieldValueChangeListener);
/** * 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); } } }
Object value = getValue(); if (value == null) { return; if (isMultiSelect()) { Collection<Object> valueAsCollection = (Collection<Object>) value; List<Object> newSelection = new ArrayList<Object>( valueAsCollection.size()); for (Object subValue : valueAsCollection) { if (containsId(subValue)) { newSelection.add(subValue); } else { setValue(newSelection); if (!containsId(value)) { setValue(null);
@Test public void createFieldSelectsFirstOptionIfItemIsNew() throws Exception { // GIVEN definition.setSortOptions(false); baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName()); initializeSelectFieldFactory(); // WHEN AbstractSelect field = (AbstractSelect) dialogSelect.createField(); // THEN Collection<String> items = (Collection<String>) field.getItemIds(); assertThat(items, contains("1", "2", "3")); assertThat(field.getValue().toString(), is("1")); }
if (multiSelect && getNullSelectionItemId() != null) { throw new IllegalStateException( "Multiselect and NullSelectionItemId can not be set at the same time."); final Object oldValue = getValue(); s.add(oldValue); setValue(s); } else { final Set<?> s = (Set<?>) oldValue; if (s == null || s.isEmpty()) { setValue(null); } else { setValue(s.iterator().next()); markAsDirty();
@Override protected void setInternalValue(T newValue) { super.setInternalValue(newValue); getSelect().setValue(newValue); }
@Test public void selectHasNoDefaultValueIfNotConfigured() { // GIVEN // WHEN createField(); // THEN AbstractSelect select = getSubFieldWithType(AbstractSelect.class); assertTrue(select.isNullSelectionAllowed()); assertNull(select.getValue()); }
/** * 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); } }
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"); } } }
getCaptionChangeListener().clear(); setValue(null); markAsDirty();
/** * For multi-selectable fields, also an empty collection of values is * considered to be an empty field. * * @see LegacyAbstractField#isEmpty(). */ @Override public boolean isEmpty() { if (!multiSelect) { return super.isEmpty(); } else { Object value = getValue(); return super.isEmpty() || (value instanceof Collection && ((Collection<?>) value).isEmpty()); } }
/** * Creates an empty Select. The caption is not used. */ public AbstractSelect() { setContainerDataSource(new IndexedContainer()); }
/** * Removes all items from the container. * * This functionality is optional. If the function is unsupported, it always * returns false. * * @return True if the operation succeeded. * @see Container#removeAllItems() */ @Override public boolean removeAllItems() throws UnsupportedOperationException { final boolean retval = items.removeAllItems(); itemIdMapper.removeAll(); if (retval) { setValue(null); if (!(items instanceof Container.ItemSetChangeNotifier)) { fireItemSetChange(); } } return retval; }
protected AbstractSelect createCompatibleSelect( Class<? extends AbstractSelect> fieldType) { AbstractSelect select; if (fieldType.isAssignableFrom(ListSelect.class)) { select = new ListSelect(); select.setMultiSelect(false); } else if (fieldType.isAssignableFrom(NativeSelect.class)) { select = new NativeSelect(); } else if (fieldType.isAssignableFrom(OptionGroup.class)) { select = new OptionGroup(); select.setMultiSelect(false); } else if (fieldType.isAssignableFrom(Table.class)) { Table t = new Table(); t.setSelectable(true); select = t; } else { select = new ComboBox(null); } select.setImmediate(true); select.setNullSelectionAllowed(false); return select; }