Browse Source

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
feature/combobox-communication
Henri Sara 8 years ago
parent
commit
bc37b9d273
1 changed files with 51 additions and 26 deletions
  1. 51
    26
      server/src/com/vaadin/ui/ComboBox.java

+ 51
- 26
server/src/com/vaadin/ui/ComboBox.java View File

&& getFilteringMode() != FilteringMode.OFF; && 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 @Override
public void paintContent(PaintTarget target) throws PaintException { public void paintContent(PaintTarget target) throws PaintException {
isPainting = true; isPainting = true;
// Paints the options and create array of selected id keys // Paints the options and create array of selected id keys
int keyIndex = 0; int keyIndex = 0;


target.startTag("options");

if (currentPage < 0) { if (currentPage < 0) {
optionRequest = false; optionRequest = false;
currentPage = 0; currentPage = 0;
final boolean paintNullSelection = needNullSelectOption final boolean paintNullSelection = needNullSelectOption
&& currentPage == 0 && !nullFilteredOut; && 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(); final Iterator<?> i = options.iterator();
final String key = itemIdMapper.key(id); final String key = itemIdMapper.key(id);
final String caption = getItemCaption(id); final String caption = getItemCaption(id);
final Resource icon = getItemIcon(id); final Resource icon = getItemIcon(id);

getCaptionChangeListener().addNotifierForItem(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)) { if (keyIndex < selectedKeys.length && isSelected(id)) {
// at most one item can be selected at a time // at most one item can be selected at a time
selectedKeys[keyIndex++] = key; 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"); target.endTag("so");
} }


} }


private void paintItemStyle(PaintTarget target, Object itemId)
throws PaintException {
private String getItemStyle(Object itemId) throws PaintException {
if (itemStyleGenerator != null) { 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;
} }


/** /**

Loading…
Cancel
Save