summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/ui/Select.java
diff options
context:
space:
mode:
authorMarc Englund <marc.englund@itmill.com>2007-11-14 15:01:30 +0000
committerMarc Englund <marc.englund@itmill.com>2007-11-14 15:01:30 +0000
commit0f494df92022c454b807fa87cf66eed91ef0956c (patch)
tree8148c6559334cdd1254d8483247fe1e38163fc31 /src/com/itmill/toolkit/ui/Select.java
parentb7d60ab6dbc0f38528925b048f15bc1bd7763f3d (diff)
downloadvaadin-framework-0f494df92022c454b807fa87cf66eed91ef0956c.tar.gz
vaadin-framework-0f494df92022c454b807fa87cf66eed91ef0956c.zip
add "nullselectitem" attribute if a nullselectionitem is in the uidl
svn changeset:2814/svn branch:trunk
Diffstat (limited to 'src/com/itmill/toolkit/ui/Select.java')
-rw-r--r--src/com/itmill/toolkit/ui/Select.java715
1 files changed, 359 insertions, 356 deletions
diff --git a/src/com/itmill/toolkit/ui/Select.java b/src/com/itmill/toolkit/ui/Select.java
index 562dd30109..12f8757096 100644
--- a/src/com/itmill/toolkit/ui/Select.java
+++ b/src/com/itmill/toolkit/ui/Select.java
@@ -62,360 +62,363 @@ import com.itmill.toolkit.terminal.Resource;
*/
public class Select extends AbstractSelect implements AbstractSelect.Filtering {
- /**
- * Holds value of property pageLength. 0 disables paging.
- */
- protected int pageLength = 10;
-
- // current page when the user is 'paging' trough options
- private int currentPage;
-
- private int filteringMode = FILTERINGMODE_STARTSWITH;
-
- private String filterstring;
- private String prevfilterstring;
- private List filteredOptions;
-
- /* Constructors ********************************************************* */
-
- /* Component methods **************************************************** */
-
- public Select() {
- super();
- }
-
- public Select(String caption, Collection options) {
- super(caption, options);
- }
-
- public Select(String caption, Container dataSource) {
- super(caption, dataSource);
- }
-
- public Select(String caption) {
- super(caption);
- }
-
- /**
- * Paints the content of this component.
- *
- * @param target
- * the Paint Event.
- * @throws PaintException
- * if the paint operation failed.
- */
- public void paintContent(PaintTarget target) throws PaintException {
- // Focus control id
- if (getFocusableId() > 0) {
- target.addAttribute("focusid", getFocusableId());
- }
-
- // The tab ordering number
- if (getTabIndex() > 0) {
- target.addAttribute("tabindex", getTabIndex());
- }
-
- // If the field is modified, but not committed, set modified attribute
- if (isModified()) {
- target.addAttribute("modified", true);
- }
-
- // Adds the required attribute
- if (isRequired()) {
- target.addAttribute("required", true);
- }
-
- // Paints select attributes
- if (isMultiSelect()) {
- target.addAttribute("selectmode", "multi");
- }
- if (isNewItemsAllowed()) {
- target.addAttribute("allownewitem", true);
- }
-
- boolean needNullSelectOption = false;
- if (isNullSelectionAllowed()) {
- target.addAttribute("nullselect", true);
- needNullSelectOption = getNullSelectionItemId() == null;
- }
-
- // Constructs selected keys array
- String[] selectedKeys;
- if (isMultiSelect()) {
- selectedKeys = new String[((Set) getValue()).size()];
- } else {
- selectedKeys = new String[(getValue() == null
- && getNullSelectionItemId() == null ? 0 : 1)];
- }
-
- target.addAttribute("filteringmode", getFilteringMode());
-
- // Paints the options and create array of selected id keys
- // TODO Also use conventional rendering if lazy loading is not supported
- // by terminal
- int keyIndex = 0;
-
- /*
- * if (!isLazyLoading()) { // Support for external null selection item
- * id Collection ids = getItemIds(); if (getNullSelectionItemId() !=
- * null && (!ids.contains(getNullSelectionItemId()))) { // Gets the
- * option attribute values Object id = getNullSelectionItemId(); String
- * key = this.itemIdMapper.key(id); String caption = getItemCaption(id);
- * Resource icon = getItemIcon(id); // Paints option
- * target.startTag("so"); if (icon != null) {
- * target.addAttribute("icon", icon); } target.addAttribute("caption",
- * caption); target.addAttribute("nullselection", true);
- * target.addAttribute("key", key); if (isSelected(id)) {
- * target.addAttribute("selected", true); selectedKeys[keyIndex++] =
- * key; } target.endTag("so"); } }
- */
- /*
- * Iterator i; if (this.filterstring != null) { i =
- * this.optionFilter.filter(this.filterstring,
- * this.lazyLoadingPageLength, this.page).iterator();
- * target.addAttribute("totalMatches", this.optionFilter
- * .getMatchCount()); } else { i = getItemIds().iterator(); }
- */
- target.startTag("options");
-
- boolean paintNullSelection = needNullSelectOption
- && (currentPage == 0 && (filterstring == null
- || filterstring.equals("") || filterstring.equals("-")));
-
- if (paintNullSelection) {
- target.startTag("so");
- target.addAttribute("caption", "-");
- target.addAttribute("key", "");
- target.endTag("so");
- }
-
- List options = getFilteredOptions();
- if (options.size() > this.pageLength) {
- int first = this.currentPage * this.pageLength;
- int last = first + this.pageLength;
- if(needNullSelectOption) {
- if(currentPage > 0) {
- first--;
- }
- last--;
- }
- if (options.size() < last) {
- last = options.size();
- }
- options = options.subList(first, last);
- }
- Iterator i = options.iterator();
- // Paints the available selection options from data source
-
- while (i.hasNext()) {
-
- // Gets the option attribute values
- Object id = i.next();
- String key = this.itemIdMapper.key(id);
- String caption = getItemCaption(id);
- Resource icon = getItemIcon(id);
-
- // Paints the option
- target.startTag("so");
- if (icon != null) {
- target.addAttribute("icon", icon);
- }
- target.addAttribute("caption", caption);
- if (id != null && id.equals(getNullSelectionItemId())) {
- target.addAttribute("nullselection", true);
- }
- target.addAttribute("key", key);
- if (isSelected(id) && keyIndex < selectedKeys.length) {
- target.addAttribute("selected", true);
- selectedKeys[keyIndex++] = key;
- }
- target.endTag("so");
- }
- target.endTag("options");
-
- target.addAttribute("totalitems", size()
- + (needNullSelectOption ? 1 : 0));
- if (this.filteredOptions != null) {
- target.addAttribute("totalMatches", this.filteredOptions.size()
- + (needNullSelectOption ? 1 : 0));
- }
-
- // Paint variables
- target.addVariable(this, "selected", selectedKeys);
- if (isNewItemsAllowed()) {
- target.addVariable(this, "newitem", "");
- }
-
- target.addVariable(this, "filter", this.filterstring);
- target.addVariable(this, "page", this.currentPage);
-
- }
-
- protected List getFilteredOptions() {
- if (this.filterstring == null || this.filterstring.equals("")
- || this.filteringMode == FILTERINGMODE_OFF) {
- this.filteredOptions = new LinkedList(getItemIds());
- return this.filteredOptions;
- }
-
- if (this.filterstring.equals(this.prevfilterstring)) {
- return this.filteredOptions;
- }
-
- Collection items;
- if (prevfilterstring != null
- && filterstring.startsWith(this.prevfilterstring)) {
- items = filteredOptions;
- } else {
- items = getItemIds();
- }
- prevfilterstring = filterstring;
-
- this.filteredOptions = new LinkedList();
- for (Iterator it = items.iterator(); it.hasNext();) {
- Object itemId = it.next();
- String caption = getItemCaption(itemId);
- if (caption == null || caption.equals("")) {
- continue;
- }
- switch (this.filteringMode) {
- case FILTERINGMODE_CONTAINS:
- if (caption.indexOf(this.filterstring) > -1) {
- this.filteredOptions.add(itemId);
- }
- break;
- case FILTERINGMODE_STARTSWITH:
- default:
- if (caption.startsWith(this.filterstring)) {
- this.filteredOptions.add(itemId);
- }
- break;
- }
- }
-
- return this.filteredOptions;
- }
-
- /**
- * Invoked when the value of a variable has changed.
- *
- * @see com.itmill.toolkit.ui.AbstractComponent#changeVariables(java.lang.Object,
- * java.util.Map)
- */
- public void changeVariables(Object source, Map variables) {
- String newFilter;
- if ((newFilter = (String) variables.get("filter")) != null) {
- // this is a filter request
- this.currentPage = ((Integer) variables.get("page")).intValue();
- this.filterstring = newFilter;
- requestRepaint();
- return;
- }
-
- // Try to set the property value
-
- // New option entered (and it is allowed)
- 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 (Property.ConversionException ignored) {
- // The conversion exception is safely ignored, the
- // caption is
- // just missing
- }
- }
- setValue(newitem);
- }
- }
-
- // Selection change
- if (variables.containsKey("selected")) {
- String[] ka = (String[]) variables.get("selected");
-
- // Multiselect mode
- if (isMultiSelect()) {
-
- // TODO Optimize by adding repaintNotNeeded whan applicaple
-
- // Converts the key-array to id-set
- LinkedList s = new LinkedList();
- for (int i = 0; i < ka.length; i++) {
- Object id = this.itemIdMapper.get(ka[i]);
- if (id != null && containsId(id)) {
- s.add(id);
- } else if (this.itemIdMapper.isNewIdKey(ka[i])
- && newitem != null && newitem.length() > 0) {
- s.add(newitem);
- }
- }
-
- // Limits the deselection to the set of visible items
- // (non-visible items can not be deselected)
- Collection visible = getVisibleItemIds();
- if (visible != null) {
- Set newsel = (Set) getValue();
- if (newsel == null) {
- newsel = new HashSet();
- } else {
- newsel = new HashSet(newsel);
- }
- newsel.removeAll(visible);
- newsel.addAll(s);
- setValue(newsel, true);
- }
- }
-
- // Single select mode
- else {
- if (ka.length == 0) {
-
- // Allows deselection only if the deselected item is visible
- Object current = getValue();
- Collection visible = getVisibleItemIds();
- if (visible != null && visible.contains(current)) {
- setValue(null, true);
- }
- } else {
- Object id = this.itemIdMapper.get(ka[0]);
- if (id != null && id.equals(getNullSelectionItemId())) {
- setValue(null, true);
- } else if (this.itemIdMapper.isNewIdKey(ka[0])) {
- setValue(newitem);
- } else {
- setValue(id, true);
- }
- }
- }
- }
- }
-
- /**
- * Gets the component UIDL tag.
- *
- * @return the Component UIDL tag as string.
- */
- public String getTag() {
- return "select";
- }
-
- public void setFilteringMode(int filteringMode) {
- this.filteringMode = filteringMode;
- }
-
- public int getFilteringMode() {
- return this.filteringMode;
- }
+ /**
+ * Holds value of property pageLength. 0 disables paging.
+ */
+ protected int pageLength = 10;
+
+ // current page when the user is 'paging' trough options
+ private int currentPage;
+
+ private int filteringMode = FILTERINGMODE_STARTSWITH;
+
+ private String filterstring;
+ private String prevfilterstring;
+ private List filteredOptions;
+
+ /* Constructors ********************************************************* */
+
+ /* Component methods **************************************************** */
+
+ public Select() {
+ super();
+ }
+
+ public Select(String caption, Collection options) {
+ super(caption, options);
+ }
+
+ public Select(String caption, Container dataSource) {
+ super(caption, dataSource);
+ }
+
+ public Select(String caption) {
+ super(caption);
+ }
+
+ /**
+ * Paints the content of this component.
+ *
+ * @param target
+ * the Paint Event.
+ * @throws PaintException
+ * if the paint operation failed.
+ */
+ public void paintContent(PaintTarget target) throws PaintException {
+ // Focus control id
+ if (getFocusableId() > 0) {
+ target.addAttribute("focusid", getFocusableId());
+ }
+
+ // The tab ordering number
+ if (getTabIndex() > 0) {
+ target.addAttribute("tabindex", getTabIndex());
+ }
+
+ // If the field is modified, but not committed, set modified attribute
+ if (isModified()) {
+ target.addAttribute("modified", true);
+ }
+
+ // Adds the required attribute
+ if (isRequired()) {
+ target.addAttribute("required", true);
+ }
+
+ // Paints select attributes
+ if (isMultiSelect()) {
+ target.addAttribute("selectmode", "multi");
+ }
+ if (isNewItemsAllowed()) {
+ target.addAttribute("allownewitem", true);
+ }
+
+ boolean needNullSelectOption = false;
+ if (isNullSelectionAllowed()) {
+ target.addAttribute("nullselect", true);
+ needNullSelectOption = (getNullSelectionItemId() == null);
+ if (!needNullSelectOption) {
+ target.addAttribute("nullselectitem", true);
+ }
+ }
+
+ // Constructs selected keys array
+ String[] selectedKeys;
+ if (isMultiSelect()) {
+ selectedKeys = new String[((Set) getValue()).size()];
+ } else {
+ selectedKeys = new String[(getValue() == null
+ && getNullSelectionItemId() == null ? 0 : 1)];
+ }
+
+ target.addAttribute("filteringmode", getFilteringMode());
+
+ // Paints the options and create array of selected id keys
+ // TODO Also use conventional rendering if lazy loading is not supported
+ // by terminal
+ int keyIndex = 0;
+
+ /*
+ * if (!isLazyLoading()) { // Support for external null selection item
+ * id Collection ids = getItemIds(); if (getNullSelectionItemId() !=
+ * null && (!ids.contains(getNullSelectionItemId()))) { // Gets the
+ * option attribute values Object id = getNullSelectionItemId(); String
+ * key = this.itemIdMapper.key(id); String caption = getItemCaption(id);
+ * Resource icon = getItemIcon(id); // Paints option
+ * target.startTag("so"); if (icon != null) {
+ * target.addAttribute("icon", icon); } target.addAttribute("caption",
+ * caption); target.addAttribute("nullselection", true);
+ * target.addAttribute("key", key); if (isSelected(id)) {
+ * target.addAttribute("selected", true); selectedKeys[keyIndex++] =
+ * key; } target.endTag("so"); } }
+ */
+ /*
+ * Iterator i; if (this.filterstring != null) { i =
+ * this.optionFilter.filter(this.filterstring,
+ * this.lazyLoadingPageLength, this.page).iterator();
+ * target.addAttribute("totalMatches", this.optionFilter
+ * .getMatchCount()); } else { i = getItemIds().iterator(); }
+ */
+ target.startTag("options");
+
+ boolean paintNullSelection = needNullSelectOption
+ && (currentPage == 0 && (filterstring == null
+ || filterstring.equals("") || filterstring.equals("-")));
+
+ if (paintNullSelection) {
+ target.startTag("so");
+ target.addAttribute("caption", "-");
+ target.addAttribute("key", "");
+ target.endTag("so");
+ }
+
+ List options = getFilteredOptions();
+ if (options.size() > pageLength) {
+ int first = currentPage * pageLength;
+ int last = first + pageLength;
+ if (needNullSelectOption) {
+ if (currentPage > 0) {
+ first--;
+ }
+ last--;
+ }
+ if (options.size() < last) {
+ last = options.size();
+ }
+ options = options.subList(first, last);
+ }
+ Iterator i = options.iterator();
+ // Paints the available selection options from data source
+
+ while (i.hasNext()) {
+
+ // Gets the option attribute values
+ Object id = i.next();
+ String key = itemIdMapper.key(id);
+ String caption = getItemCaption(id);
+ Resource icon = getItemIcon(id);
+
+ // Paints the option
+ target.startTag("so");
+ if (icon != null) {
+ target.addAttribute("icon", icon);
+ }
+ target.addAttribute("caption", caption);
+ if (id != null && id.equals(getNullSelectionItemId())) {
+ target.addAttribute("nullselection", true);
+ }
+ target.addAttribute("key", key);
+ if (isSelected(id) && keyIndex < selectedKeys.length) {
+ target.addAttribute("selected", true);
+ selectedKeys[keyIndex++] = key;
+ }
+ target.endTag("so");
+ }
+ target.endTag("options");
+
+ target.addAttribute("totalitems", size()
+ + (needNullSelectOption ? 1 : 0));
+ if (filteredOptions != null) {
+ target.addAttribute("totalMatches", filteredOptions.size()
+ + (needNullSelectOption ? 1 : 0));
+ }
+
+ // Paint variables
+ target.addVariable(this, "selected", selectedKeys);
+ if (isNewItemsAllowed()) {
+ target.addVariable(this, "newitem", "");
+ }
+
+ target.addVariable(this, "filter", filterstring);
+ target.addVariable(this, "page", currentPage);
+
+ }
+
+ protected List getFilteredOptions() {
+ if (filterstring == null || filterstring.equals("")
+ || filteringMode == FILTERINGMODE_OFF) {
+ filteredOptions = new LinkedList(getItemIds());
+ return filteredOptions;
+ }
+
+ if (filterstring.equals(prevfilterstring)) {
+ return filteredOptions;
+ }
+
+ Collection items;
+ if (prevfilterstring != null
+ && filterstring.startsWith(prevfilterstring)) {
+ items = filteredOptions;
+ } else {
+ items = getItemIds();
+ }
+ prevfilterstring = filterstring;
+
+ filteredOptions = new LinkedList();
+ for (Iterator it = items.iterator(); it.hasNext();) {
+ Object itemId = it.next();
+ String caption = getItemCaption(itemId);
+ if (caption == null || caption.equals("")) {
+ continue;
+ }
+ switch (filteringMode) {
+ case FILTERINGMODE_CONTAINS:
+ if (caption.indexOf(filterstring) > -1) {
+ filteredOptions.add(itemId);
+ }
+ break;
+ case FILTERINGMODE_STARTSWITH:
+ default:
+ if (caption.startsWith(filterstring)) {
+ filteredOptions.add(itemId);
+ }
+ break;
+ }
+ }
+
+ return filteredOptions;
+ }
+
+ /**
+ * Invoked when the value of a variable has changed.
+ *
+ * @see com.itmill.toolkit.ui.AbstractComponent#changeVariables(java.lang.Object,
+ * java.util.Map)
+ */
+ public void changeVariables(Object source, Map variables) {
+ String newFilter;
+ if ((newFilter = (String) variables.get("filter")) != null) {
+ // this is a filter request
+ currentPage = ((Integer) variables.get("page")).intValue();
+ filterstring = newFilter;
+ requestRepaint();
+ return;
+ }
+
+ // Try to set the property value
+
+ // New option entered (and it is allowed)
+ 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 (Property.ConversionException ignored) {
+ // The conversion exception is safely ignored, the
+ // caption is
+ // just missing
+ }
+ }
+ setValue(newitem);
+ }
+ }
+
+ // Selection change
+ if (variables.containsKey("selected")) {
+ String[] ka = (String[]) variables.get("selected");
+
+ // Multiselect mode
+ if (isMultiSelect()) {
+
+ // TODO Optimize by adding repaintNotNeeded whan applicaple
+
+ // Converts the key-array to id-set
+ LinkedList s = new LinkedList();
+ for (int i = 0; i < ka.length; i++) {
+ 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);
+ }
+ }
+
+ // Limits the deselection to the set of visible items
+ // (non-visible items can not be deselected)
+ Collection visible = getVisibleItemIds();
+ if (visible != null) {
+ Set newsel = (Set) getValue();
+ if (newsel == null) {
+ newsel = new HashSet();
+ } else {
+ newsel = new HashSet(newsel);
+ }
+ newsel.removeAll(visible);
+ newsel.addAll(s);
+ setValue(newsel, true);
+ }
+ }
+
+ // Single select mode
+ else {
+ if (ka.length == 0) {
+
+ // Allows deselection only if the deselected item is visible
+ Object current = getValue();
+ Collection visible = getVisibleItemIds();
+ if (visible != null && visible.contains(current)) {
+ setValue(null, true);
+ }
+ } else {
+ 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);
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * Gets the component UIDL tag.
+ *
+ * @return the Component UIDL tag as string.
+ */
+ public String getTag() {
+ return "select";
+ }
+
+ public void setFilteringMode(int filteringMode) {
+ this.filteringMode = filteringMode;
+ }
+
+ public int getFilteringMode() {
+ return filteringMode;
+ }
}