BrightSide Workbench Full Report + Source Code
FilterGrid.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.filter;
19 
20 import java.io.Serializable;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.TreeSet;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import org.jdom.JDOMException;
27 import org.turro.command.Command;
28 import org.turro.command.Context;
29 import org.turro.configuration.ConfigurationSet;
30 import org.turro.elephant.context.Application;
31 import org.turro.elephant.context.ElephantContext;
32 import org.turro.i18n.I_;
33 import org.turro.plugin.filter.IFilterValue;
34 import org.turro.zkoss.dialog.InputDialog;
35 import org.turro.zkoss.dialog.InputField;
36 import org.turro.zkoss.dialog.SelectionDialog;
37 import org.turro.zkoss.input.CollectionListbox;
38 import org.turro.zkoss.label.LabelTypes;
39 import org.zkoss.zk.ui.Component;
40 import org.zkoss.zk.ui.HtmlBasedComponent;
41 import org.zkoss.zk.ui.event.Event;
42 import org.zkoss.zk.ui.event.EventListener;
43 import org.zkoss.zk.ui.event.Events;
44 import org.zkoss.zk.ui.ext.AfterCompose;
45 import org.zkoss.zul.*;
46 
51 public abstract class FilterGrid extends Grid implements AfterCompose, Serializable {
52 
53  protected List<FilterField> fields = new ArrayList<>();
54  protected List<FilterField> currentFields = new ArrayList<>();
55  private Row newFilterRow;
56  private String preferenceKey;
57 
58  public FilterGrid() {
59  setSclass("filterGrid");
60  }
61 
62  public FilterField addField(FilterField filterField) {
63  fields.add(filterField);
64  return filterField;
65  }
66 
67  public FilterField addCurrentField(String label) {
68  for(FilterField ff : fields) {
69  if(label.equals(ff.getLabel())) {
70  FilterField ffc = (FilterField) ff.clone();
71  currentFields.add(ffc);
72  return ffc;
73  }
74  }
75  return null;
76  }
77 
78  public List<FilterField> getCurrentFields() {
79  if(getRows() != null) {
80  currentFields = new ArrayList<>();
81  for(Component row : getRows().getChildren()) {
82  if(((Row)row).getValue() instanceof FilterField) {
83  currentFields.add((FilterField) ((Row)row).getValue());
84  }
85  }
86  }
88  return currentFields;
89  }
90 
91  public void setCurrentFields(List<FilterField> currentFields) {
92  this.currentFields = currentFields;
93  }
94 
95  public List<FilterField> getFields() {
96  return fields;
97  }
98 
99  public void setFields(List<FilterField> fields) {
100  this.fields = fields;
101  }
102 
103  public List<IFilterValue> getValues() {
104  return new ArrayList<IFilterValue>(getCurrentFields());
105  }
106 
107  public boolean hasValues() {
108  for(IFilterValue v : getValues()) {
109  if(v.hasValue()) {
110  return true;
111  }
112  }
113  return false;
114  }
115 
116  @Override
117  public void afterCompose() {
118  checkFields();
119  setSpan(true);
120  addColumns();
121  addRows();
122  }
123 
124  protected boolean loadPreferences(String preference) {
125  this.preferenceKey = preference;
127  List<FilterField> lff = (List<FilterField>) app.getPreference(preference);
128  if(lff != null) {
129  currentFields = lff;
130  return true;
131  } else {
132  return false;
133  }
134  }
135 
136  protected void savePreferences() {
137  if(preferenceKey != null) {
139  app.putPreference(preferenceKey, currentFields);
140  }
141  }
142 
143  private void addColumns() {
144  Columns cols = new Columns();
145  //cols.setVisible(false);
146  appendChild(cols);
147 
148  Column col = new Column();
149  col.setHflex("min");
150  cols.appendChild(col);
151 
152  col = new Column();
153  col.setHflex("min");
154  cols.appendChild(col);
155 
156  col = new Column();
157  col.setHflex("min");
158  cols.appendChild(col);
159 
160  col = new Column();
161  col.setHflex("1");
162  cols.appendChild(col);
163 
164  col = new Column();
165  col.setHflex("min");
166  cols.appendChild(col);
167  }
168 
169  private void addRows() {
170  Rows rows = new Rows();
171  appendChild(rows);
172 
173  newFilterRow = new Row();
174  rows.appendChild(newFilterRow);
175  newFilterRow.appendChild(LabelTypes.getEmptyLabel());
176  final FilterFieldListbox ffl = new FilterFieldListbox(fields);
177  ffl.setMold("select");
178  ffl.afterCompose();
179  newFilterRow.appendChild(ffl);
180  Toolbarbutton ins = new Toolbarbutton(null, "/_zul/images/list-add.png");
181  ins.addEventListener(Events.ON_CLICK, new EventListener() {
182  @Override
183  public void onEvent(Event event) throws Exception {
184  addRow((FilterField) ffl.getObjectValue().clone());
185  checkFirstChain();
186  }
187  });
188  newFilterRow.appendChild(ins);
189  Hbox hbox = new Hbox();
190  hbox.setSpacing("20px");
191  newFilterRow.appendChild(hbox);
192  Toolbarbutton open = new Toolbarbutton(null, "/_zul/images/open.png");
193  open.addEventListener(Events.ON_CLICK, new EventListener() {
194  @Override
195  public void onEvent(Event event) throws Exception {
196  loadFields();
197  }
198  });
199  hbox.appendChild(open);
200  if(Application.getApplication().isInRole("filter:save")) {
201  Toolbarbutton save = new Toolbarbutton(null, "/_zul/images/save.png");
202  save.addEventListener(Events.ON_CLICK, new EventListener() {
203  @Override
204  public void onEvent(Event event) throws Exception {
205  saveFields();
206  }
207  });
208  hbox.appendChild(save);
209  }
210  if(Application.getApplication().isInRole("filter:delete")) {
211  Toolbarbutton del = new Toolbarbutton(null, "/_zul/images/trash-empty.png");
212  del.addEventListener(Events.ON_CLICK, new EventListener() {
213  @Override
214  public void onEvent(Event event) throws Exception {
215  deleteFields();
216  }
217  });
218  hbox.appendChild(del);
219  }
220  newFilterRow.appendChild(LabelTypes.getEmptyLabel());
221 
222  if(currentFields != null) {
223  for(FilterField field : currentFields) {
224  addRow(field);
225  }
226  }
227  checkFirstChain();
228  }
229 
230  private void addRow(FilterField field) {
231  Rows rows = getRows();
232  if(field != null) {
233  final Row row = new Row();
234  row.setValue(field);
235  row.setVisible(!field.isFixed());
236  rows.insertBefore(row, newFilterRow);
237  row.appendChild(field.getChainEditor());
238  row.appendChild(new Label(I_.get(field.label)));
239  row.appendChild(field.getOperatorEditor());
240  row.appendChild(field.getEditor());
241  Toolbarbutton del = new Toolbarbutton(null, "/_zul/images/list-remove.png");
242  del.setWidth("25px");
243  del.addEventListener(Events.ON_CLICK, new EventListener() {
244  @Override
245  public void onEvent(Event event) throws Exception {
246  row.detach();
247  checkFirstChain();
248  }
249  });
250  row.appendChild(del);
251  if(field instanceof AfterCompose) {
252  ((AfterCompose) field).afterCompose();
253  }
254  }
255  }
256 
257  private void checkFields() {
258  if(fields == null) {
259  fields = new ArrayList<>();
260  }
261  }
262 
263  private void checkFirstChain() {
264  boolean done = false;
265  if(getRows() != null) {
266  for(Component row : getRows().getChildren()) {
267  if(((Row)row).getValue() instanceof FilterField) {
268  FilterField ff = (FilterField) ((Row)row).getValue();
269  if(!done) {
270  ff.getChainEditor().setObjectValue(ChainOperator.AND);
271  ff.getChainEditor().setDisabled(true);
272  done = true;
273  } else {
274  ff.getChainEditor().setDisabled(false);
275  }
276  }
277  }
278  }
279  invalidate();
280  }
281 
282  private void loadFields() {
283  final ConfigurationSet cs = new ConfigurationSet(preferenceKey);
284  SelectionDialog.getComponent(
285  getPage(), I_.get("Open"),
286  new CollectionListbox<String>(new TreeSet<>(cs.getChildrenValue(new FilterNamePath(), "filter", "name"))) {
287  @Override
288  protected String convertToString(String v) {
289  return v;
290  }
291  }, "500px", null, new Command() {
292  @Override
293  public Object execute(Context context) {
294  CollectionListbox<String> cl = (CollectionListbox<String>) context.get("component");
295  if(cl != null && cl.getObjectValue() != null) {
296  List<String[]> list = cs.getChildrenValues(
297  new FilterPath(cl.getObjectValue()), "fields", new String[] { "name", "value"});
298  currentFields.clear();
299  for(String[] s : list) {
300  FilterField ff = addCurrentField(s[0]);
301  // ff.setValue()
302  }
303  if(getRows() != null) {
304  getRows().detach();
305  }
306  addRows();
307  checkFirstChain();
308  }
309  return null;
310  }
311  });
312  }
313 
314  private void saveFields() {
315  final ConfigurationSet cs = new ConfigurationSet(preferenceKey);
316  InputDialog.getInput(
317  getPage(),
318  I_.get("Save"),
319  new InputField[] {
320  new InputField("Name", null, null, 0) {
321  @Override
322  protected HtmlBasedComponent createEditor() {
323  FilterNameCombobox ccb = new FilterNameCombobox(cs);
324  return ccb;
325  }
326  }
327  }, new Command() {
328  @Override
329  public Object execute(Context context) {
330  InputField[] fields = (InputField[]) context.get("fields");
331  if(fields.length > 0) {
332  for(InputField f : fields) {
333  if("Name".equals(f.getLabel())) {
334  cs.setChildrenValues(
335  new FilterPath((String) f.getValue()), "fields", new String[] { "name", "value"},
336  getCurrentFieldsValues());
337  }
338  }
339  checkFirstChain();
340  }
341  return null;
342  }
343  });
344  }
345 
346  private void deleteFields() {
347  final ConfigurationSet cs = new ConfigurationSet(preferenceKey);
348  SelectionDialog.getComponent(
349  getPage(), I_.get("Delete"),
350  new CollectionListbox<String>(new TreeSet<>(cs.getChildrenValue(new FilterNamePath(), "filter", "name"))) {
351  @Override
352  protected String convertToString(String v) {
353  return v;
354  }
355  }, "500px", null, new Command() {
356  @Override
357  public Object execute(Context context) {
358  CollectionListbox<String> cl = (CollectionListbox<String>) context.get("component");
359  if(cl != null && cl.getObjectValue() != null) {
360  try {
361  FilterNamePath fnp = new FilterNamePath();
362  fnp.setDoc(cs.getDocument());
363  fnp.delFilter(cl.getObjectValue());
364  cs.setDocument(fnp.getDoc());
365  } catch (JDOMException ex) {
366  Logger.getLogger(FilterGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
367  }
368  }
369  return null;
370  }
371  });
372  }
373 
374  private List<String[]> getCurrentFieldsValues() {
375  ArrayList<String[]> list = new ArrayList<>();
376  for(FilterField ff : getCurrentFields()) {
377  ArrayList<String> l = new ArrayList<>();
378  l.add(ff.getLabel());
379  l.add("");
380  list.add(l.toArray(new String[0]));
381  }
382  return list;
383  }
384 }
void putPreference(String key, Object value)
void setCurrentFields(List< FilterField > currentFields)
Definition: FilterGrid.java:91
List< FilterField > getCurrentFields()
Definition: FilterGrid.java:78
FilterField addField(FilterField filterField)
Definition: FilterGrid.java:62
List< FilterField > getFields()
Definition: FilterGrid.java:95
boolean loadPreferences(String preference)
FilterField addCurrentField(String label)
Definition: FilterGrid.java:67
void setFields(List< FilterField > fields)
Definition: FilterGrid.java:99
List< FilterField > currentFields
Definition: FilterGrid.java:54
List< IFilterValue > getValues()