aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenri Sara <hesara@vaadin.com>2015-11-09 14:52:46 +0200
committerHenri Sara <hesara@vaadin.com>2016-01-15 14:42:57 +0200
commiteb264da6892a6f382725095eca5b951fbdd89fe1 (patch)
treed62f0a9d7b7d7217e923fa9ac5437983c1a09542
parent9ba4e15b677d42918eae32a08a6d6a2b12ff43e9 (diff)
downloadvaadin-framework-eb264da6892a6f382725095eca5b951fbdd89fe1.tar.gz
vaadin-framework-eb264da6892a6f382725095eca5b951fbdd89fe1.zip
Refactor ComboBox item painting (#19929)
This is an intermediate step towards a different approach to sending items from server to client. An unused null selection item attribute was removed, as the information is currently communicated outside the item. Change-Id: I9c08d627bcf48fd42793595795aeb3ac49c2fef6
-rw-r--r--server/src/com/vaadin/ui/ComboBox.java77
1 files changed, 51 insertions, 26 deletions
diff --git a/server/src/com/vaadin/ui/ComboBox.java b/server/src/com/vaadin/ui/ComboBox.java
index de87ea0d17..1c43e72352 100644
--- a/server/src/com/vaadin/ui/ComboBox.java
+++ b/server/src/com/vaadin/ui/ComboBox.java
@@ -221,6 +221,31 @@ public class ComboBox extends AbstractSelect implements
&& getFilteringMode() != FilteringMode.OFF;
}
+ /**
+ * A class representing an item in a ComboBox for server to client
+ * communication. This class is for internal use only and subject to change.
+ *
+ * @since
+ */
+ private static class ComboBoxItem implements Serializable {
+ String key = "";
+ String caption = "";
+ String style = null;
+ Resource icon = null;
+
+ // constructor for a null item
+ public ComboBoxItem() {
+ }
+
+ public ComboBoxItem(String key, String caption, String style,
+ Resource icon) {
+ this.key = key;
+ this.caption = caption;
+ this.style = style;
+ this.icon = icon;
+ }
+ }
+
@Override
public void paintContent(PaintTarget target) throws PaintException {
isPainting = true;
@@ -254,8 +279,6 @@ public class ComboBox extends AbstractSelect implements
// Paints the options and create array of selected id keys
int keyIndex = 0;
- target.startTag("options");
-
if (currentPage < 0) {
optionRequest = false;
currentPage = 0;
@@ -281,14 +304,12 @@ public class ComboBox extends AbstractSelect implements
final boolean paintNullSelection = needNullSelectOption
&& currentPage == 0 && !nullFilteredOut;
- if (paintNullSelection) {
- target.startTag("so");
- target.addAttribute("caption", "");
- target.addAttribute("key", "");
-
- paintItemStyle(target, null);
+ List<ComboBoxItem> items = new ArrayList<ComboBoxItem>();
- target.endTag("so");
+ if (paintNullSelection) {
+ ComboBoxItem item = new ComboBoxItem();
+ item.style = getItemStyle(null);
+ items.add(item);
}
final Iterator<?> i = options.iterator();
@@ -308,24 +329,31 @@ public class ComboBox extends AbstractSelect implements
final String key = itemIdMapper.key(id);
final String caption = getItemCaption(id);
final Resource icon = getItemIcon(id);
+
getCaptionChangeListener().addNotifierForItem(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);
+ // Prepare to paint the option
+ ComboBoxItem item = new ComboBoxItem(key, caption,
+ getItemStyle(id), icon);
+ items.add(item);
if (keyIndex < selectedKeys.length && isSelected(id)) {
// at most one item can be selected at a time
selectedKeys[keyIndex++] = key;
}
+ }
- paintItemStyle(target, id);
+ // paint the items
+ target.startTag("options");
+ for (ComboBoxItem item : items) {
+ target.startTag("so");
+ if (item.icon != null) {
+ target.addAttribute("icon", item.icon);
+ }
+ target.addAttribute("caption", item.caption);
+ target.addAttribute("key", item.key);
+ if (item.style != null) {
+ target.addAttribute("style", item.style);
+ }
target.endTag("so");
}
@@ -363,14 +391,11 @@ public class ComboBox extends AbstractSelect implements
}
- private void paintItemStyle(PaintTarget target, Object itemId)
- throws PaintException {
+ private String getItemStyle(Object itemId) throws PaintException {
if (itemStyleGenerator != null) {
- String style = itemStyleGenerator.getStyle(this, itemId);
- if (style != null && !style.isEmpty()) {
- target.addAttribute("style", style);
- }
+ return itemStyleGenerator.getStyle(this, itemId);
}
+ return null;
}
/**