BrightSide Workbench Full Report + Source Code
DualSelectorListbox.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.zkoss.input;
19 
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.LinkedHashSet;
23 import java.util.List;
24 import java.util.Set;
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;
39 
50 public abstract class DualSelectorListbox<T> extends Div implements IdSpace, ListitemRenderer<T> {
51 
52  @Wire
53  private Listbox candidateLb;
54  @Wire
55  private Listbox chosenLb;
56 
57  private ListModelList<T> optionsModel;
58  private ListModelList<T> selectedModel;
59 
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);
67  }
68  candidateLb.setItemRenderer(this);
69  chosenLb.setItemRenderer(this);
70  }
71 
72  @Listen("onClick = #chooseBtn")
73  public void chooseItem() {
74  Events.postEvent(new ChooseEvent(this, chooseOne()));
75  }
76 
77  @Listen("onClick = #removeBtn")
78  public void unchooseItem() {
79  Events.postEvent(new ChooseEvent(this, unchooseOne()));
80  }
81 
82  @Listen("onClick = #chooseAllBtn")
83  public void chooseAllItem() {
84  for (int i = 0, j = optionsModel.getSize(); i < j; i++) {
85  selectedModel.add(optionsModel.getElementAt(i));
86  }
87  optionsModel.clear();
88  }
89 
90  @Listen("onClick = #removeAllBtn")
91  public void unchooseAll() {
92  for (int i = 0, j = selectedModel.getSize(); i < j; i++) {
93  optionsModel.add(selectedModel.getElementAt(i));
94  }
95  selectedModel.clear();
96  }
97 
98  @Listen("onClick = #topBtn")
99  public void top() {
100  int i = 0;
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);
107  }
108  Events.postEvent(new ChooseEvent(this, null));
109  }
110 
111  @Listen("onClick = #upBtn")
112  public void up() {
113  Set<T> selected = selectedModel.getSelection();
114  if (selected.isEmpty()) {
115  return;
116  }
117  int index = selectedModel.indexOf(selected.iterator().next());
118  if (index == 0 || index < 0) {
119  return;
120  }
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);
126  }
127  Events.postEvent(new ChooseEvent(this, null));
128  }
129 
130  @Listen("onClick = #downBtn")
131  public void down() {
132  Set<T> selected = selectedModel.getSelection();
133  if (selected.isEmpty()) {
134  return;
135  }
136  int index = selectedModel.indexOf(selected.iterator().next());
137  if (index == selectedModel.size() - selected.size() || index < 0) {
138  return;
139  }
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);
145  }
146  Events.postEvent(new ChooseEvent(this, null));
147  }
148 
149  @Listen("onClick = #bottomBtn")
150  public void bottom() {
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);
157  }
158  Events.postEvent(new ChooseEvent(this, null));
159  }
160 
166  public void setModel(List<T> candidate) {
167  candidateLb.setModel(this.optionsModel = new ListModelList<T>(candidate));
168  selectedModel.clear();
169  if(candidateLb.getModel() instanceof Selectable) {
170  ((Selectable) candidateLb.getModel()).setMultiple(true);
171  }
172  }
173 
177  public List<T> getChosenDataList() {
178  return new ArrayList<T>(selectedModel);
179  }
180 
181  private Set<T> chooseOne() {
182  Set<T> set = optionsModel.getSelection();
183  selectedModel.addAll(set);
184  optionsModel.removeAll(set);
185  return set;
186  }
187 
188  private Set<T> unchooseOne() {
189  Set<T> set = selectedModel.getSelection();
190  optionsModel.addAll(set);
191  selectedModel.removeAll(set);
192  return set;
193  }
194 
195  @Override
196  public void render(Listitem item, T data, int index) throws Exception {
197  doRender(item, data, index);
198  }
199 
200  protected abstract void doRender(Listitem item, T data, int index);
201 
202  // Customized Event
203  public class ChooseEvent extends Event {
204  public ChooseEvent(Component target, Set<T> data) {
205  super("onChoose", target, data);
206  }
207  }
208 }
abstract void doRender(Listitem item, T data, int index)
void render(Listitem item, T data, int index)