/** * 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); }
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"); } } }
/** * 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); } } }
@Override protected void setInternalValue(T newValue) { super.setInternalValue(newValue); getSelect().setValue(newValue); }
/** * 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); } }
public void selectFirst() { if (bic != null && bic.size() > 0) { getSelect().setValue(bic.getIdByIndex(0)); } }
/** * 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; }
@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); } } } }
@Test public void doNotAddNonVisibleField() { // GIVEN createField(); AbstractSelect select = getSubFieldWithType(AbstractSelect.class); // WHEN select.setValue("text"); assertEquals("has the select field and the selected textfield", 2, getVisibleFieldNb()); // WHEN select.setValue("hidden"); // THEN assertEquals("has only the select field as the hidden is not visible", 1, getVisibleFieldNb()); }
super.setValue(newValue, repaintIsNotNeeded);
@Test public void switchingWritesToItemWithDefaultValue() throws Exception { // GIVEN definition.getFields().get(0).setDefaultValue("hop!"); baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName()); createField(); AbstractSelect select = getSubFieldWithType(AbstractSelect.class); // WHEN select.setValue("text"); baseNode = ((JcrNewNodeAdapter) baseItem).applyChanges(); // THEN assertEquals("text", baseNode.getProperty(propertyName).getString()); assertTrue(baseNode.hasProperty(propertyName + "text")); assertEquals("hop!", baseNode.getProperty(propertyName + "text").getString()); }
@Test public void validationOfRequiredSwitchableSucceedsIfOptionIsSelected() { // GIVEN definition.setRequired(true); createField(); AbstractSelect select = getSubFieldWithType(AbstractSelect.class); select.setValue("text"); // WHEN boolean isValid = field.isValid(); // THEN assertTrue(isValid); }
@Test public void switchingWritesToItem() throws Exception { // GIVEN baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName()) { @Override public boolean addItemProperty(Object propertyId, Property property) throws UnsupportedOperationException { return super.addItemProperty(propertyId, property); } }; createField(); AbstractSelect select = getSubFieldWithType(AbstractSelect.class); // WHEN select.setValue("text"); baseNode = ((JcrNewNodeAdapter) baseItem).applyChanges(); // THEN assertEquals("text", baseItem.getItemProperty(propertyName).getValue()); assertEquals("text", baseNode.getProperty(propertyName).getString()); assertFalse(baseNode.hasProperty(propertyName + "text")); }
@Test public void validationIsTriggeredOnlyForVisibleField() { // GIVEN textFieldDefinition.setRequired(true); codeFieldDefinition.setRequired(true); createField(); AbstractSelect select = getSubFieldWithType(AbstractSelect.class); select.setValue("text"); TextField textField = getSubFieldWithType(TextField.class); textField.setValue("foo"); // WHEN boolean isValid = field.isValid(); // THEN assertTrue(isValid); }
@Test public void validationOfSelectedOptionFails() { // GIVEN textFieldDefinition.setRequired(true); codeFieldDefinition.setRequired(true); createField(); AbstractSelect select = getSubFieldWithType(AbstractSelect.class); select.setValue("code"); // default value for AceEditor is empty string, and AceEditor doesn't consider this an empty value in #isEmpty(), as it should (see AbstractTextField#isEmpty) AceEditor codeField = getSubFieldWithType(AceEditor.class); codeField.setValue(null); // WHEN boolean isValid = field.isValid(); // THEN assertFalse(isValid); }