]> source.dussan.org Git - vaadin-framework.git/commitdiff
Added feature to allow custom logic when user adds new item to select
authorMatti Tahvonen <matti.tahvonen@itmill.com>
Fri, 18 Jan 2008 09:03:23 +0000 (09:03 +0000)
committerMatti Tahvonen <matti.tahvonen@itmill.com>
Fri, 18 Jan 2008 09:03:23 +0000 (09:03 +0000)
svn changeset:3578/svn branch:trunk

src/com/itmill/toolkit/tests/UsingCustomNewItemHandlerInSelect.java [new file with mode: 0644]
src/com/itmill/toolkit/ui/AbstractSelect.java
src/com/itmill/toolkit/ui/Select.java

diff --git a/src/com/itmill/toolkit/tests/UsingCustomNewItemHandlerInSelect.java b/src/com/itmill/toolkit/tests/UsingCustomNewItemHandlerInSelect.java
new file mode 100644 (file)
index 0000000..7acc6b3
--- /dev/null
@@ -0,0 +1,65 @@
+/* 
+@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);
+        }
+
+    }
+
+}
index c10ad10fa40aa83fe97539ba95931005abc5f39e..47d06104e75808581092cc71b0b0ec4ea04e3271 100644 (file)
@@ -179,6 +179,7 @@ public abstract class AbstractSelect extends AbstractField implements
 
     // Null (empty) selection is enabled by default
     private boolean nullSelectionAllowed = true;
+    private NewItemHandler newItemHandler;
 
     /* Constructors ********************************************************* */
 
@@ -351,27 +352,7 @@ public abstract class AbstractSelect extends AbstractField implements
         // 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
@@ -393,9 +374,6 @@ public abstract class AbstractSelect extends AbstractField implements
                         requestRepaint();
                     } else if (id != null && containsId(id)) {
                         s.add(id);
-                    } else if (itemIdMapper.isNewIdKey(ka[i])
-                            && newitem != null && newitem.length() > 0) {
-                        s.add(newitem);
                     }
                 }
 
@@ -441,8 +419,6 @@ public abstract class AbstractSelect extends AbstractField implements
                     } else if (id != null
                             && id.equals(getNullSelectionItemId())) {
                         setValue(null, true);
-                    } else if (itemIdMapper.isNewIdKey(ka[0])) {
-                        setValue(newitem);
                     } else {
                         setValue(id, true);
                     }
@@ -451,6 +427,75 @@ public abstract class AbstractSelect extends AbstractField implements
         }
     }
 
+    /**
+     * 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.
      * 
index 0eb18f0929b8852128755bf89bfd2d43f960576d..12f833bbb0671c98d8ccc2e334bd7abd51d75377 100644 (file)
@@ -13,7 +13,6 @@ import java.util.Map;
 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;
@@ -288,31 +287,10 @@ public class Select extends AbstractSelect implements AbstractSelect.Filtering {
         // 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
@@ -330,9 +308,6 @@ public class Select extends AbstractSelect implements AbstractSelect.Filtering {
                     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);
                     }
                 }
 
@@ -366,8 +341,6 @@ public class Select extends AbstractSelect implements AbstractSelect.Filtering {
                     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);
                     }