--- /dev/null
+/*
+@ITMillApache2LicenseForJavaFiles@
+ */
+
+package com.itmill.toolkit.tests;
+
+import java.util.Random;
+
+import com.itmill.toolkit.data.Item;
+import com.itmill.toolkit.ui.AbstractSelect;
+import com.itmill.toolkit.ui.CustomComponent;
+import com.itmill.toolkit.ui.Panel;
+import com.itmill.toolkit.ui.Select;
+
+public class UsingCustomNewItemHandlerInSelect extends CustomComponent {
+
+ private final Select select = new Select();
+
+ public static Random random = new Random(1);
+
+ private static int sequence = 0;
+
+ public UsingCustomNewItemHandlerInSelect() {
+
+ final Panel panel = new Panel("Select demo");
+ panel.addComponent(select);
+
+ select.setCaption("Select component");
+ select.setImmediate(true);
+ select.addContainerProperty("CAPTION", String.class, "");
+ select.setItemCaptionMode(Select.ITEM_CAPTION_MODE_PROPERTY);
+ select.setItemCaptionPropertyId("CAPTION");
+ select.setNewItemsAllowed(true);
+ select.setNewItemHandler(new MyNewItemHandler());
+
+ populateSelect();
+
+ setCompositionRoot(panel);
+ }
+
+ public void populateSelect() {
+ final String[] names = new String[] { "John", "Mary", "Joe", "Sarah",
+ "Jeff", "Jane", "Peter", "Marc", "Josie", "Linus" };
+ for (int j = 0; j < 4; j++) {
+ Integer id = new Integer(sequence++);
+ Item item = select.addItem(id);
+ item.getItemProperty("CAPTION").setValue(
+ id.toString() + ": "
+ + names[random.nextInt() % names.length]);
+ }
+ }
+
+ public class MyNewItemHandler implements AbstractSelect.NewItemHandler {
+ public void addNewItem(String newItemCaption) {
+ // here could be db insert or other backend operation
+ Integer id = new Integer(sequence++);
+ Item item = select.addItem(id);
+ item.getItemProperty("CAPTION").setValue(
+ id.toString() + ": " + newItemCaption);
+ select.setValue(id);
+ }
+
+ }
+
+}
// Null (empty) selection is enabled by default
private boolean nullSelectionAllowed = true;
+ private NewItemHandler newItemHandler;
/* Constructors ********************************************************* */
// New option entered (and it is allowed)
final String newitem = (String) variables.get("newitem");
if (newitem != null && newitem.length() > 0) {
-
- // Checks for readonly
- if (isReadOnly()) {
- throw new Property.ReadOnlyException();
- }
-
- // Adds new option
- if (addItem(newitem) != null) {
-
- // Sets the caption property, if used
- if (getItemCaptionPropertyId() != null) {
- try {
- getContainerProperty(newitem,
- getItemCaptionPropertyId()).setValue(newitem);
- } catch (final Property.ConversionException ignored) {
- // The conversion exception is safely ignored, the
- // caption is
- // just missing
- }
- }
- }
+ getNewItemHandler().addNewItem(newitem);
}
// Selection change
requestRepaint();
} else if (id != null && containsId(id)) {
s.add(id);
- } else if (itemIdMapper.isNewIdKey(ka[i])
- && newitem != null && newitem.length() > 0) {
- s.add(newitem);
}
}
} else if (id != null
&& id.equals(getNullSelectionItemId())) {
setValue(null, true);
- } else if (itemIdMapper.isNewIdKey(ka[0])) {
- setValue(newitem);
} else {
setValue(id, true);
}
}
}
+ /**
+ * TODO refine doc Setter for new item handler that is called when user adds
+ * new item in newItemAllowed mode.
+ *
+ * @param newItemHandler
+ */
+ public void setNewItemHandler(NewItemHandler newItemHandler) {
+ this.newItemHandler = newItemHandler;
+ }
+
+ /**
+ * TODO refine doc
+ *
+ * @return
+ */
+ public NewItemHandler getNewItemHandler() {
+ if (newItemHandler == null) {
+ newItemHandler = new DefaultNewItemHandler();
+ }
+ return newItemHandler;
+ }
+
+ public interface NewItemHandler {
+ void addNewItem(String newItemCaption);
+ }
+
+ /**
+ * TODO refine doc
+ *
+ * This is a default class that handles adding new items that are typed by
+ * user to selects container.
+ *
+ * By extending this class one may implement some logic on new item addition
+ * like database inserts.
+ *
+ */
+ public class DefaultNewItemHandler implements NewItemHandler {
+ 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) {
+ try {
+ getContainerProperty(newItemCaption,
+ getItemCaptionPropertyId()).setValue(
+ newItemCaption);
+ } catch (final Property.ConversionException ignored) {
+ // The conversion exception is safely ignored, the
+ // caption is
+ // just missing
+ }
+ }
+ if (isMultiSelect()) {
+ Set values = new HashSet((Collection) getValue());
+ values.add(newItemCaption);
+ setValue(values);
+ } else {
+ setValue(newItemCaption);
+ }
+ }
+ }
+ }
+
/**
* Gets the component UIDL tag.
*
import java.util.Set;
import com.itmill.toolkit.data.Container;
-import com.itmill.toolkit.data.Property;
import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;
import com.itmill.toolkit.terminal.Resource;
// New option entered (and it is allowed)
final String newitem = (String) variables.get("newitem");
if (newitem != null && newitem.length() > 0) {
-
- // Checks for readonly
- if (isReadOnly()) {
- throw new Property.ReadOnlyException();
- }
-
- // Adds new option
- if (addItem(newitem) != null) {
-
- // Sets the caption property, if used
- if (getItemCaptionPropertyId() != null) {
- try {
- getContainerProperty(newitem,
- getItemCaptionPropertyId()).setValue(newitem);
- } catch (final Property.ConversionException ignored) {
- // The conversion exception is safely ignored, the
- // caption is
- // just missing
- }
- }
- setValue(newitem);
- // rebuild list
- filterstring = null;
- prevfilterstring = null;
- }
+ getNewItemHandler().addNewItem(newitem);
+ // rebuild list
+ filterstring = null;
+ prevfilterstring = null;
}
// Selection change
final Object id = itemIdMapper.get(ka[i]);
if (id != null && containsId(id)) {
s.add(id);
- } else if (itemIdMapper.isNewIdKey(ka[i])
- && newitem != null && newitem.length() > 0) {
- s.add(newitem);
}
}
final Object id = itemIdMapper.get(ka[0]);
if (id != null && id.equals(getNullSelectionItemId())) {
setValue(null, true);
- } else if (itemIdMapper.isNewIdKey(ka[0])) {
- setValue(newitem);
} else {
setValue(id, true);
}