BrightSide Workbench Full Report + Source Code
JpaListbox.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.jpa.input;
19 
20 import java.util.List;
21 import org.turro.i18n.I_;
22 import org.turro.jpa.Dao;
23 import org.turro.zkoss.input.GenericListbox;
24 import org.zkoss.zk.ui.event.Event;
25 import org.zkoss.zk.ui.event.EventListener;
26 import org.zkoss.zk.ui.event.Events;
27 import org.zkoss.zul.Listitem;
28 
33 public abstract class JpaListbox<V> extends GenericListbox<V> {
34 
35  private Dao dao;
36  private String query;
37  private boolean autoSave = true;
38  private boolean allowNull;
39  private String nullLabel = "None";
40 
41  public JpaListbox() {
42  }
43 
44  public JpaListbox(Dao dao, String query, boolean checkmark, boolean autoSave) {
45  this.dao = dao;
46  this.query = query;
47  this.autoSave = autoSave;
48  setCheckmark(checkmark);
49  }
50 
51  public boolean isAutoSave() {
52  return autoSave;
53  }
54 
55  public void setAutoSave(boolean autoSave) {
56  this.autoSave = autoSave;
57  }
58 
59  public Dao getDao() {
60  return dao;
61  }
62 
63  public void setDao(Dao dao) {
64  this.dao = dao;
65  }
66 
67  public String getQuery() {
68  return query;
69  }
70 
71  public void setQuery(String query) {
72  this.query = query;
73  }
74 
75  @Override
76  protected void populateList() {
77  if(dao == null) return;
78  if(allowNull) {
79  Listitem li = new Listitem(I_.get(nullLabel), null);
80  appendChild(li);
81  }
82  for(Object o : dao.getResultList(query)) {
83  Listitem li = new Listitem(convertToString((V) o), o);
84  if(isCheckmark()) {
85  li.setSelected(getChecked((V) o));
86  }
87  appendChild(li);
88  }
89  addEventListener(Events.ON_SELECT, new EventListener() {
90  @Override
91  public void onEvent(Event event) throws Exception {
92  for(Listitem li : (List<Listitem>) getItems()) {
93  if(getChecked((V) li.getValue()) != li.isSelected()) {
94  setChecked((V) li.getValue(), li.isSelected());
95  if(autoSave) {
96  dao.saveObject(li.getValue());
97  }
98  }
99  }
100  }
101  });
102  }
103 
104  public abstract String convertToString(V v);
105  public abstract boolean getChecked(V v);
106  public abstract void setChecked(V v, boolean checked);
107 
108  public boolean isAllowNull() {
109  return allowNull;
110  }
111 
112  public void setAllowNull(boolean allowNull) {
113  this.allowNull = allowNull;
114  }
115 
116  public String getNullLabel() {
117  return nullLabel;
118  }
119 
120  public void setNullLabel(String nullLabel) {
121  this.nullLabel = nullLabel;
122  }
123 
124  @Override
125  protected boolean equals(V value, V obj) {
126  String v1 = value == null ? null : convertToString(value);
127  String v2 = obj == null ? null : convertToString(obj);
128  return (v1 == null && v2 == null) ||
129  (v1 != null && v1.equals(v2));
130  }
131 
132 }
static String get(String msg)
Definition: I_.java:41
abstract void setChecked(V v, boolean checked)
void setAutoSave(boolean autoSave)
Definition: JpaListbox.java:55
void setQuery(String query)
Definition: JpaListbox.java:71
abstract boolean getChecked(V v)
abstract String convertToString(V v)
void setNullLabel(String nullLabel)
void setAllowNull(boolean allowNull)
boolean equals(V value, V obj)
JpaListbox(Dao dao, String query, boolean checkmark, boolean autoSave)
Definition: JpaListbox.java:44