BrightSide Workbench Full Report + Source Code
DossierCtrl.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.dossier.zul.dossier;
19 
20 import org.turro.dossier.db.DossierPU;
21 import org.turro.dossier.entity.Dossier;
22 import org.turro.dossier.search.DossierResults;
23 import org.turro.zkoss.label.LabelExtended;
24 import org.zkoss.lang.Strings;
25 import org.zkoss.zk.ui.Component;
26 import org.zkoss.zk.ui.WrongValueException;
27 import org.zkoss.zk.ui.event.Event;
28 import org.zkoss.zk.ui.event.EventListener;
29 import org.zkoss.zk.ui.event.Events;
30 import org.zkoss.zk.ui.ext.AfterCompose;
31 import org.zkoss.zul.Bandbox;
32 import org.zkoss.zul.Bandpopup;
33 import org.zkoss.zul.Listbox;
34 import org.zkoss.zul.Listcell;
35 import org.zkoss.zul.Listitem;
36 
41 public class DossierCtrl extends Bandbox implements AfterCompose {
42 
43  private LabelExtended description;
44  private Dossier dossier;
45  private int wordWrap = 0, maxChars = 0, parentCount = 1;
46  private boolean showDescription = false;
47 
48  public DossierCtrl() {
49  addPopup();
50  }
51 
52  public boolean isShowDescription() {
53  return showDescription;
54  }
55 
56  public void setShowDescription(boolean showDescription) {
57  this.showDescription = showDescription;
58  if(showDescription) {
59  addEventListener(Events.ON_CHANGE, new EventListener() {
60  @Override
61  public void onEvent(Event event) throws Exception {
62  description.setValue(loadValue());
63  }
64  });
65  }
66  }
67 
68  public Dossier getDossier() {
69  return dossier;
70  }
71 
72  public void setDossier(Dossier dossier) {
73  this.dossier = dossier;
74  if(dossier != null) {
75  setValue(dossier.getId() + "");
76  }
77  }
78 
79  public int getMaxChars() {
80  return maxChars;
81  }
82 
83  public void setMaxChars(int maxChars) {
84  this.maxChars = maxChars;
85  }
86 
87  public int getParentCount() {
88  return parentCount;
89  }
90 
91  public void setParentCount(int parentCount) {
92  this.parentCount = parentCount;
93  }
94 
95  public int getWordWrap() {
96  return wordWrap;
97  }
98 
99  public void setWordWrap(int wordWrap) {
100  this.wordWrap = wordWrap;
101  }
102 
103  @Override
104  public void afterCompose() {
105  if(!showDescription) return;
106  description = new LabelExtended();
107  description.setValue(loadValue());
108  description.setWordWrap(wordWrap);
109  description.setMaxChars(maxChars);
110  Component comp = getParent();
111  for(int i = 1; i < parentCount; i++) {
112  comp = comp.getParent();
113  }
114  comp.appendChild(description);
115  }
116 
117  @Override
118  public String getValue() throws WrongValueException {
119  return Strings.isBlank(super.getValue()) ? "0" : super.getValue();
120  }
121 
122  @Override
123  public void setValue(String value) throws WrongValueException {
124  if(Strings.isEmpty(value)) value = "0";
125  super.setValue(value);
126  }
127 
128  private void addPopup() {
129  Bandpopup popup = new Bandpopup();
130  appendChild(popup);
131 
132  final Listbox dossiers = new Listbox();
133  dossiers.addEventListener(Events.ON_SELECT, new EventListener() {
134  @Override
135  public void onEvent(Event event) throws Exception {
136  DossierCtrl.this.setValue(dossiers.getSelectedItem().getLabel());
137  DossierCtrl.this.loadValue();
138  Events.postEvent(new Event(Events.ON_CHANGE, DossierCtrl.this));
139  DossierCtrl.this.close();
140  }
141  });
142  popup.appendChild(dossiers);
143 
144  DossierResults results = new DossierResults();
145 
146  for(Dossier d : results.getDossierList()) {
147  Listitem item = new Listitem();
148  dossiers.appendChild(item);
149 
150  Listcell cell = new Listcell(d.getId() + "");
151  item.appendChild(cell);
152 
153  cell = new Listcell(d.getFullDescription());
154  item.appendChild(cell);
155  }
156 
157  }
158 
159  private String loadValue() {
160  if(!Strings.isEmpty(getValue()) && Long.valueOf(getValue()) > 0) {
161  dossier = new DossierPU().find(Dossier.class, Long.valueOf(getValue()));
162  if(dossier != null) {
163  return dossier.getDescription();
164  }
165  }
166  return "";
167  }
168 
169 }
void setShowDescription(boolean showDescription)
LabelExtended setWordWrap(int wordWrap)
LabelExtended setMaxChars(int maxChars)