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