18 package org.turro.zkoss.input;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.LinkedHashSet;
23 import java.util.List;
25 import org.zkoss.zk.ui.Component;
26 import org.zkoss.zk.ui.Executions;
27 import org.zkoss.zk.ui.IdSpace;
28 import org.zkoss.zk.ui.event.Event;
29 import org.zkoss.zk.ui.event.Events;
30 import org.zkoss.zk.ui.select.Selectors;
31 import org.zkoss.zk.ui.select.annotation.Listen;
32 import org.zkoss.zk.ui.select.annotation.Wire;
33 import org.zkoss.zul.Div;
34 import org.zkoss.zul.ListModelList;
35 import org.zkoss.zul.Listbox;
36 import org.zkoss.zul.Listitem;
37 import org.zkoss.zul.ListitemRenderer;
38 import org.zkoss.zul.ext.Selectable;
53 private Listbox candidateLb;
55 private Listbox chosenLb;
57 private ListModelList<T> optionsModel;
58 private ListModelList<T> selectedModel;
61 Executions.createComponents(
"/WEB-INF/_zul/bs/comps/dualSelector/dualSelectorListbox.zul",
this,
null);
62 Selectors.wireComponents(
this,
this,
false);
63 Selectors.wireEventListeners(
this,
this);
64 chosenLb.setModel(selectedModel =
new ListModelList<T>());
65 if(chosenLb.getModel() instanceof Selectable) {
66 ((Selectable) chosenLb.getModel()).setMultiple(
true);
68 candidateLb.setItemRenderer(
this);
69 chosenLb.setItemRenderer(
this);
72 @Listen(
"onClick = #chooseBtn")
74 Events.postEvent(
new ChooseEvent(
this, chooseOne()));
77 @Listen(
"onClick = #removeBtn")
79 Events.postEvent(
new ChooseEvent(
this, unchooseOne()));
82 @Listen(
"onClick = #chooseAllBtn")
84 for (
int i = 0, j = optionsModel.getSize(); i < j; i++) {
85 selectedModel.add(optionsModel.getElementAt(i));
90 @Listen(
"onClick = #removeAllBtn")
92 for (
int i = 0, j = selectedModel.getSize(); i < j; i++) {
93 optionsModel.add(selectedModel.getElementAt(i));
95 selectedModel.clear();
98 @Listen(
"onClick = #topBtn")
101 Iterator<T> iterator =
new LinkedHashSet<T>(selectedModel.getSelection()).iterator();
102 while (iterator.hasNext()) {
103 T selectedItem = iterator.next();
104 selectedModel.remove(selectedItem);
105 selectedModel.add(i++, selectedItem);
106 selectedModel.addToSelection(selectedItem);
111 @Listen(
"onClick = #upBtn")
113 Set<T> selected = selectedModel.getSelection();
114 if (selected.isEmpty()) {
117 int index = selectedModel.indexOf(selected.iterator().next());
118 if (index == 0 || index < 0) {
121 ArrayList<T> clone = (ArrayList<T>)
new ArrayList<T>(selected).clone();
122 selectedModel.removeAll(selected);
123 selectedModel.addAll(--index, clone);
124 for(T selectedItem : clone) {
125 selectedModel.addToSelection(selectedItem);
130 @Listen(
"onClick = #downBtn")
132 Set<T> selected = selectedModel.getSelection();
133 if (selected.isEmpty()) {
136 int index = selectedModel.indexOf(selected.iterator().next());
137 if (index == selectedModel.size() - selected.size() || index < 0) {
140 ArrayList<T> clone = (ArrayList<T>)
new ArrayList<T>(selected).clone();
141 selectedModel.removeAll(selected);
142 selectedModel.addAll(++index, clone);
143 for(T selectedItem : clone) {
144 selectedModel.addToSelection(selectedItem);
149 @Listen(
"onClick = #bottomBtn")
151 Iterator<T> iterator =
new LinkedHashSet<T>(selectedModel.getSelection()).iterator();
152 while (iterator.hasNext()) {
153 T selectedItem = iterator.next();
154 selectedModel.remove(selectedItem);
155 selectedModel.add(selectedItem);
156 selectedModel.addToSelection(selectedItem);
167 candidateLb.setModel(this.optionsModel =
new ListModelList<T>(candidate));
168 selectedModel.clear();
169 if(candidateLb.getModel() instanceof Selectable) {
170 ((Selectable) candidateLb.getModel()).setMultiple(
true);
178 return new ArrayList<T>(selectedModel);
181 private Set<T> chooseOne() {
182 Set<T>
set = optionsModel.getSelection();
183 selectedModel.addAll(
set);
184 optionsModel.removeAll(
set);
188 private Set<T> unchooseOne() {
189 Set<T>
set = selectedModel.getSelection();
190 optionsModel.addAll(
set);
191 selectedModel.removeAll(
set);
196 public void render(Listitem item, T data,
int index)
throws Exception {
200 protected abstract void doRender(Listitem item, T data,
int index);
205 super(
"onChoose", target, data);