BrightSide Workbench Full Report + Source Code
ConnectorNameComboModel.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.contacts.zul.contact;
19 
20 import java.util.Arrays;
21 import java.util.Comparator;
22 import java.util.List;
23 import org.turro.contacts.db.ContactsPU;
24 import org.turro.elephant.db.WhereClause;
25 import org.turro.jpa.Dao;
26 import org.zkoss.zul.AbstractListModel;
27 import org.zkoss.zul.ListModel;
28 import org.zkoss.zul.ListSubModel;
29 import org.zkoss.zul.event.ListDataEvent;
30 import org.zkoss.zul.ext.Sortable;
31 
36 public class ConnectorNameComboModel extends AbstractListModel implements Sortable<String>, ListSubModel, java.io.Serializable {
37 
38  private static final long serialVersionUID = 20081029L;
39 
40  private Object[] data;
41 
42  public ConnectorNameComboModel(Object[] data) {
43  this.data = data;
44  }
45 
46  public ConnectorNameComboModel(List data) {
47  this.data = data.toArray(new Object[data.size()]);
48  }
49 
50  @Override
51  public Object getElementAt(int index) {
52  if(data == null || data.length <= index) return null;
53  return data[index];
54  }
55 
56  @Override
57  public int getSize() {
58  return data.length;
59  }
60 
61  @Override
62  public void sort(Comparator cmpr, boolean ascending) {
63  Arrays.sort(data, cmpr);
64  fireEvent(ListDataEvent.CONTENTS_CHANGED, -1, -1);
65  }
66 
67  @Override
68  public String getSortDirection(Comparator<String> cmpr) {
69  return "natural";
70  }
71 
72  @Override
73  public ListModel getSubModel(Object value, int nRows) {
74  Dao dao = new ContactsPU();
75  WhereClause wc = new WhereClause();
76  wc.addClause("select distinct connector.description");
77  wc.addClause("from Connector as connector");
78  wc.addClause("where 1=1");
79  wc.addLikeFields(new String[]{ "connector.description" }, value.toString());
80  wc.addClause("order by connector.description");
81  if(nRows < 1) nRows = 10;
82  return new AddressNameComboModel(dao.getResultList(wc, nRows));
83  }
84 
85 }
void addLikeFields(String[] fields, String value)