BrightSide Workbench Full Report + Source Code
GenericComboModel.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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.Arrays;
21 import java.util.Comparator;
22 import java.util.LinkedList;
23 import java.util.List;
24 import org.zkoss.zul.AbstractListModel;
25 import org.zkoss.zul.ListModel;
26 import org.zkoss.zul.ListSubModel;
27 import org.zkoss.zul.event.ListDataEvent;
28 import org.zkoss.zul.ext.Sortable;
29 
34 public abstract class GenericComboModel extends AbstractListModel implements Sortable<Object>, ListSubModel, java.io.Serializable {
35 
36  private static final long serialVersionUID = 20090814L;
37 
38  private Object[] data;
39  private Object lastValue;
40 
41  public GenericComboModel(Object[] data) {
42  this.data = data;
43  }
44 
45  public GenericComboModel(List data) {
46  this.data = data.toArray(new Object[data.size()]);
47  }
48 
49  @Override
50  public Object getElementAt(int index) {
51  return data != null && index >= 0 && index < data.length ? data[index] : null;
52  }
53 
54  @Override
55  public int getSize() {
56  return data.length;
57  }
58 
59  @Override
60  public void sort(Comparator cmpr, boolean ascending) {
61  Arrays.sort(data, cmpr);
62  fireEvent(ListDataEvent.CONTENTS_CHANGED, -1, -1);
63  }
64 
65  @Override
66  public String getSortDirection(Comparator<Object> cmpr) {
67  return "natural";
68  }
69 
70  @Override
71  public synchronized ListModel getSubModel(Object value, int nRows) {
72  if(value != null && value.equals(lastValue)) return this;
73  lastValue = value;
74  LinkedList list = new LinkedList();
75  if(nRows < 1) nRows = 20;
76  populateList(value.toString(), list, nRows);
77  return new GenericComboModel(list) {
78  @Override
79  public void populateList(String value, LinkedList list, int nRows) {
80  throw new UnsupportedOperationException("Submodel does not populate items");
81  }
82  };
83  }
84 
85  public abstract void populateList(String value, LinkedList list, int nRows);
86 
87 }
88 
void sort(Comparator cmpr, boolean ascending)
abstract void populateList(String value, LinkedList list, int nRows)
synchronized ListModel getSubModel(Object value, int nRows)
String getSortDirection(Comparator< Object > cmpr)