BrightSide Workbench Full Report + Source Code
ConceptCombobox.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.financials.document;
19 
20 import java.util.Collection;
21 import java.util.LinkedList;
22 import org.turro.command.Command;
23 import org.turro.command.Context;
24 import org.turro.elephant.db.WhereClause;
25 import org.turro.elephant.util.Messages;
26 import org.turro.financials.db.FinancialsPU;
27 import org.turro.financials.entity.DocumentDefinition;
28 import org.turro.i18n.I_;
29 import org.turro.jpa.Dao;
30 import org.turro.util.Chars;
31 import org.turro.zkoss.dialog.InputDialog;
32 import org.turro.zkoss.dialog.Listeners;
33 import org.turro.zkoss.input.GenericCombobox;
34 import org.zkoss.zk.ui.WrongValueException;
35 import org.zkoss.zk.ui.event.Event;
36 import org.zkoss.zk.ui.event.EventListener;
37 import org.zkoss.zk.ui.event.Events;
38 import org.zkoss.zk.ui.ext.AfterCompose;
39 
44 public class ConceptCombobox extends GenericCombobox<String> implements AfterCompose, EventListener {
45 
46  private DocumentDefinition documentDefinition;
47 
49  return documentDefinition;
50  }
51 
52  public void setDocumentDefinition(DocumentDefinition documentDefinition) {
53  this.documentDefinition = documentDefinition;
54  }
55 
56  @Override
57  public void populateList(String value, LinkedList list, int nRows) {
58  Dao dao = new FinancialsPU();
59  WhereClause wc = new WhereClause();
60  wc.addClause("select distinct concept from DocumentLine as documentLine");
61  wc.addClause("where documentLine.document.documentDefinition.id = :id");
62  wc.addNamedValue("id", documentDefinition.getId());
63  wc.addLikeFields(new String[] { "documentLine.concept" }, value);
64  wc.addClause("order by documentLine.concept");
65  list.addAll(dao.getResultList(wc, nRows));
66  }
67 
68  @Override
69  public String getTextFromObject(String value) {
70  return value;
71  }
72 
73  @Override
74  public void setObjectValue(String value) throws WrongValueException {
75  if(value != null && value.contains("\n")) {
76  super.setValue(formatValue(value));
77  } else {
78  super.setValue(value);
79  }
80  }
81 
82  @Override
83  public String getObjectValue() throws WrongValueException {
84  String tmp = super.getObjectValue();
85  return tmp == null ? null : tmp.replaceAll(Chars.nl().regexp().toString(), "\n");
86  }
87 
88  public static String formatValue(String value) {
89  return value == null ? null : ((String) value).replaceAll("\\n", Chars.nl().toString());
90  }
91 
92  @Override
93  public void afterCompose() {
94  addEventListener(Events.ON_DOUBLE_CLICK, this);
95  }
96 
97  @Override
98  public void onEvent(Event event) throws Exception {
99  if(isReadonly()) {
100  Collection c = Listeners.cancelListener(this, Events.ON_BLUR);
101  Messages.info(I_.get("Concept")).add(this.getObjectValue()).show();
102  Listeners.activateListeners(this, Events.ON_BLUR, c);
103  } else {
104  final Collection c = Listeners.cancelListener(this, Events.ON_BLUR);
105  InputDialog.getInput(getPage(), I_.get("Concept"), "Description",
106  this.getObjectValue(), null, 10, new Command() {
107  @Override
108  public Object execute(Context context) {
109  String value = (String) context.get("value");
110  if(value != null) {
111  ConceptCombobox.this.setObjectValue(value);
112  }
113  return null;
114  }
115  }, new Command() {
116  @Override
117  public Object execute(Context context) {
118  Listeners.activateListeners(ConceptCombobox.this, Events.ON_BLUR, c);
119  return null;
120  }
121  });
122  }
123  }
124 
125 }
void addLikeFields(String[] fields, String value)
void addNamedValue(String name, Object value)
Messages add(String word)
Definition: Messages.java:50
void setDocumentDefinition(DocumentDefinition documentDefinition)
void populateList(String value, LinkedList list, int nRows)
static String get(String msg)
Definition: I_.java:41
static void getInput(Page page, String title, String label, Object value, String format, int scale, final Command onOk)
static void activateListeners(Component component, String eventType, Collection< EventListener > listeners)
Definition: Listeners.java:42
static Collection< EventListener > cancelListener(Component component, String eventType)
Definition: Listeners.java:32