From 0d66b296eee63715b02fd38c2def0f431947493e Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Fri, 18 Jan 2008 09:03:23 +0000 Subject: [PATCH] Added feature to allow custom logic when user adds new item to select svn changeset:3578/svn branch:trunk --- .../UsingCustomNewItemHandlerInSelect.java | 65 +++++++++++++ src/com/itmill/toolkit/ui/AbstractSelect.java | 97 ++++++++++++++----- src/com/itmill/toolkit/ui/Select.java | 35 +------ 3 files changed, 140 insertions(+), 57 deletions(-) create mode 100644 src/com/itmill/toolkit/tests/UsingCustomNewItemHandlerInSelect.java diff --git a/src/com/itmill/toolkit/tests/UsingCustomNewItemHandlerInSelect.java b/src/com/itmill/toolkit/tests/UsingCustomNewItemHandlerInSelect.java new file mode 100644 index 0000000000..7acc6b3976 --- /dev/null +++ b/src/com/itmill/toolkit/tests/UsingCustomNewItemHandlerInSelect.java @@ -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); + } + + } + +} diff --git a/src/com/itmill/toolkit/ui/AbstractSelect.java b/src/com/itmill/toolkit/ui/AbstractSelect.java index c10ad10fa4..47d06104e7 100644 --- a/src/com/itmill/toolkit/ui/AbstractSelect.java +++ b/src/com/itmill/toolkit/ui/AbstractSelect.java @@ -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. * diff --git a/src/com/itmill/toolkit/ui/Select.java b/src/com/itmill/toolkit/ui/Select.java index 0eb18f0929..12f833bbb0 100644 --- a/src/com/itmill/toolkit/ui/Select.java +++ b/src/com/itmill/toolkit/ui/Select.java @@ -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); } -- 2.39.5