From cbcf59df3b8faddf9e2e967b43ece2f0f0987ab2 Mon Sep 17 00:00:00 2001 From: Marc Englund Date: Fri, 2 Nov 2007 14:13:07 +0000 Subject: [PATCH] Null selection changes (#574) svn changeset:2694/svn branch:trunk --- src/com/itmill/toolkit/ui/AbstractSelect.java | 25 ++++++++++++++++--- src/com/itmill/toolkit/ui/Table.java | 16 +++++++++++- src/com/itmill/toolkit/ui/Tree.java | 8 ++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/com/itmill/toolkit/ui/AbstractSelect.java b/src/com/itmill/toolkit/ui/AbstractSelect.java index 939cdd248d..aa928548d7 100644 --- a/src/com/itmill/toolkit/ui/AbstractSelect.java +++ b/src/com/itmill/toolkit/ui/AbstractSelect.java @@ -407,7 +407,11 @@ public abstract class AbstractSelect extends AbstractField implements LinkedList s = new LinkedList(); for (int i = 0; i < ka.length; i++) { Object id = this.itemIdMapper.get(ka[i]); - if (id != null && containsId(id)) { + if (!isNullSelectionAllowed() + && (id == null || id == getNullSelectionItemId())) { + // skip empty selection if nullselection is not allowed + requestRepaint(); + } else if (id != null && containsId(id)) { s.add(id); } else if (this.itemIdMapper.isNewIdKey(ka[i]) && newitem != null && newitem.length() > 0) { @@ -415,6 +419,12 @@ public abstract class AbstractSelect extends AbstractField implements } } + if (!isNullSelectionAllowed() && s.size() < 1) { + // empty selection not allowed, keep old value + requestRepaint(); + return; + } + // Limits the deselection to the set of visible items // (non-visible items can not be deselected) Collection visible = getVisibleItemIds(); @@ -431,8 +441,14 @@ public abstract class AbstractSelect extends AbstractField implements } } else { // Single select mode + if (!isNullSelectionAllowed() + && (ka.length == 0 || ka[0] == null || ka[0] == getNullSelectionItemId())) { + requestRepaint(); + return; + } if (ka.length == 0) { - // Allows deselection only if the deselected item is visible + // Allows deselection only if the deselected item is + // visible Object current = getValue(); Collection visible = getVisibleItemIds(); if (visible != null && visible.contains(current)) { @@ -440,7 +456,10 @@ public abstract class AbstractSelect extends AbstractField implements } } else { Object id = this.itemIdMapper.get(ka[0]); - if (id != null && id.equals(getNullSelectionItemId())) { + if (!isNullSelectionAllowed() && id == null) { + requestRepaint(); + } else if (id != null + && id.equals(getNullSelectionItemId())) { setValue(null, true); } else if (this.itemIdMapper.isNewIdKey(ka[0])) { setValue(newitem); diff --git a/src/com/itmill/toolkit/ui/Table.java b/src/com/itmill/toolkit/ui/Table.java index 6d5a699116..5a9762db9e 100644 --- a/src/com/itmill/toolkit/ui/Table.java +++ b/src/com/itmill/toolkit/ui/Table.java @@ -1322,6 +1322,13 @@ public class Table extends AbstractSelect implements Action.Container, */ public void changeVariables(Object source, Map variables) { + if (!isSelectable() && variables.containsKey("selected")) { + // Not-selectable is a special case, AbstractSelect does not support + // TODO could be optimized. + variables = new HashMap(variables); + variables.remove("selected"); + } + super.changeVariables(source, variables); // Page start index @@ -1522,9 +1529,16 @@ public class Table extends AbstractSelect implements Action.Container, } target.startTag("rows"); for (int i = 0; i < cells[0].length; i++) { - target.startTag("tr"); Object itemId = cells[CELL_ITEMID][i]; + if (!isNullSelectionAllowed() && getNullSelectionItemId() != null + && itemId == getNullSelectionItemId()) { + // Remove null selection item if null selection is not allowed + continue; + } + + target.startTag("tr"); + // tr attributes if (rowheads) { if (cells[CELL_ICON][i] != null) { diff --git a/src/com/itmill/toolkit/ui/Tree.java b/src/com/itmill/toolkit/ui/Tree.java index 0aaa22a3c7..3c5552c256 100644 --- a/src/com/itmill/toolkit/ui/Tree.java +++ b/src/com/itmill/toolkit/ui/Tree.java @@ -31,6 +31,7 @@ package com.itmill.toolkit.ui; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashSet; @@ -336,6 +337,13 @@ public class Tree extends AbstractSelect implements Container.Hierarchical, */ public void changeVariables(Object source, Map variables) { + if (!isSelectable() && variables.containsKey("selected")) { + // Not-selectable is a special case, AbstractSelect does not support + // TODO could be optimized. + variables = new HashMap(variables); + variables.remove("selected"); + } + // Collapses the nodes if (variables.containsKey("collapse")) { String[] keys = (String[]) variables.get("collapse"); -- 2.39.5