18 package org.turro.zkoss.filter;
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.*;
51 public abstract class FilterGrid extends Grid implements AfterCompose, Serializable {
53 protected List<FilterField>
fields =
new ArrayList<>();
55 private Row newFilterRow;
56 private String preferenceKey;
59 setSclass(
"filterGrid");
69 if(label.equals(ff.getLabel())) {
79 if(getRows() !=
null) {
81 for(Component row : getRows().getChildren()) {
125 this.preferenceKey = preference;
127 List<FilterField> lff = (List<FilterField>) app.
getPreference(preference);
137 if(preferenceKey !=
null) {
143 private void addColumns() {
144 Columns cols =
new Columns();
148 Column col =
new Column();
150 cols.appendChild(col);
154 cols.appendChild(col);
158 cols.appendChild(col);
162 cols.appendChild(col);
166 cols.appendChild(col);
169 private void addRows() {
170 Rows rows =
new Rows();
173 newFilterRow =
new Row();
174 rows.appendChild(newFilterRow);
175 newFilterRow.appendChild(LabelTypes.getEmptyLabel());
176 final FilterFieldListbox ffl =
new FilterFieldListbox(
fields);
177 ffl.setMold(
"select");
179 newFilterRow.appendChild(ffl);
180 Toolbarbutton ins =
new Toolbarbutton(
null,
"/_zul/images/list-add.png");
181 ins.addEventListener(Events.ON_CLICK,
new EventListener() {
183 public void onEvent(Event event)
throws Exception {
184 addRow((FilterField) ffl.getObjectValue().clone());
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() {
195 public void onEvent(Event event)
throws Exception {
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() {
204 public void onEvent(Event event)
throws Exception {
208 hbox.appendChild(save);
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() {
214 public void onEvent(Event event)
throws Exception {
218 hbox.appendChild(del);
220 newFilterRow.appendChild(LabelTypes.getEmptyLabel());
230 private void addRow(FilterField field) {
231 Rows rows = getRows();
233 final Row row =
new Row();
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() {
245 public void onEvent(Event event)
throws Exception {
250 row.appendChild(del);
251 if(field instanceof AfterCompose) {
252 ((AfterCompose) field).afterCompose();
257 private void checkFields() {
259 fields =
new ArrayList<>();
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();
270 ff.getChainEditor().setObjectValue(ChainOperator.AND);
271 ff.getChainEditor().setDisabled(
true);
274 ff.getChainEditor().setDisabled(
false);
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"))) {
288 protected String convertToString(String v) {
291 },
"500px",
null,
new Command() {
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"});
299 for(String[] s : list) {
303 if(getRows() !=
null) {
314 private void saveFields() {
315 final ConfigurationSet cs =
new ConfigurationSet(preferenceKey);
316 InputDialog.getInput(
320 new InputField(
"Name", null, null, 0) {
322 protected HtmlBasedComponent createEditor() {
323 FilterNameCombobox ccb = new FilterNameCombobox(cs);
329 public Object execute(Context context) {
330 InputField[]
fields = (InputField[]) context.get(
"fields");
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());
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"))) {
352 protected String convertToString(String v) {
355 },
"500px",
null,
new Command() {
357 public Object execute(Context context) {
358 CollectionListbox<String> cl = (CollectionListbox<String>) context.get(
"component");
359 if(cl !=
null && cl.getObjectValue() !=
null) {
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);
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());
380 list.add(l.toArray(
new String[0]));
void putPreference(String key, Object value)
Object getPreference(String key)
static Application getApplication()
void setCurrentFields(List< FilterField > currentFields)
List< FilterField > getCurrentFields()
FilterField addField(FilterField filterField)
List< FilterField > getFields()
boolean loadPreferences(String preference)
List< FilterField > fields
FilterField addCurrentField(String label)
void setFields(List< FilterField > fields)
List< FilterField > currentFields
List< IFilterValue > getValues()