BrightSide Workbench Full Report + Source Code
DialogField.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2022 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 
19 package org.turro.zkoss.dialog;
20 
21 import java.io.File;
22 import java.util.Date;
23 import java.util.function.Supplier;
24 import org.turro.string.Strings;
25 import org.turro.elephant.impl.util.StringParser;
26 import org.turro.i18n.I_;
27 import org.turro.zkoss.input.DateboxShort;
28 import org.turro.zkoss.input.ExpressionInput;
29 import org.turro.zkoss.input.FileListbox;
30 import org.turro.zkoss.input.GenericBandbox;
31 import org.turro.zkoss.input.GenericCombobox;
32 import org.turro.zkoss.input.GenericListbox;
33 import org.turro.zkoss.label.Labels;
34 import org.zkoss.zk.ui.HtmlBasedComponent;
35 import org.zkoss.zul.A;
36 import org.zkoss.zul.Checkbox;
37 import org.zkoss.zul.Combobox;
38 import org.zkoss.zul.Datebox;
39 import org.zkoss.zul.Decimalbox;
40 import org.zkoss.zul.Hlayout;
41 import org.zkoss.zul.Label;
42 import org.zkoss.zul.Listbox;
43 import org.zkoss.zul.Space;
44 import org.zkoss.zul.Textbox;
45 import org.zkoss.zul.Vlayout;
46 
51 public class DialogField extends Vlayout {
52 
53  private final String label;
54  private final Object value;
55 
56  private boolean onlyLabel;
57  private String format, help;
58  private int scale, maxLength = 255;
59  private Enum[] choices;
60 
61  protected boolean labelOnEditor = false;
62  protected HtmlBasedComponent editor;
63  private Supplier<HtmlBasedComponent> onEditor;
64 
66  this.onlyLabel = true;
67  return this;
68  }
69 
70  public DialogField format(String format) {
71  this.format = format;
72  return this;
73  }
74 
75  public DialogField help(String help) {
76  this.help = help;
77  return this;
78  }
79 
80  public DialogField scale(int scale) {
81  this.scale = scale;
82  return this;
83  }
84 
85  public DialogField lines(int lines) {
86  this.scale = lines;
87  return this;
88  }
89 
90  public DialogField maxLength(int maxLength) {
91  this.maxLength = maxLength;
92  return this;
93  }
94 
95  public DialogField choices(Enum[] choices) {
96  this.choices = choices;
97  return this;
98  }
99 
100  public DialogField onEditor(Supplier<HtmlBasedComponent> onEditor) {
101  this.onEditor = onEditor;
102  return this;
103  }
104 
106  if(onlyLabel) {
107  if(Strings.isBlank(label)) {
108  appendChild(new Space());
109  } else {
110  appendChild(labelComponent());
111  }
112  } else {
113  editor = createEditor();
115  if (!labelOnEditor) {
116  appendChild(labelComponent());
117  }
118  appendChild(editor);
119  }
120  return this;
121  }
122 
123  public String getLabel() {
124  return label;
125  }
126 
127  public Object getValue() {
128  return getEditorValue();
129  }
130 
131  public HtmlBasedComponent getEditor() {
132  return editor;
133  }
134 
135  protected HtmlBasedComponent createEditor() {
136  if(onEditor != null) {
137  return onEditor.get();
138  } else if(value instanceof Boolean) {
139  Checkbox cb = new Checkbox();
140  cb.setChecked((Boolean) value);
141  cb.setLabel(I_.get(label));
142  labelOnEditor = true;
143  return cb;
144  } else if(value instanceof Number) {
145  ExpressionInput ei = new ExpressionInput();
146  ei.setValue(value);
147  return ei;
148  } else if(value instanceof Date) {
149  Datebox db = new Datebox((Date) value);
150  db.setFormat(format);
151  return db;
152  } else if(value instanceof Enum) {
153  InputEnum ie = new InputEnum(choices);
154  ie.setObjectValue((Enum) value);
155  labelOnEditor = true;
156  return ie;
157  } else if(value instanceof File) {
158  FileListbox fl = new FileListbox();
159  fl.setMold("select");
160  fl.setFilter(format);
161  fl.setDir((File) value);
162  fl.setSelectFirst(true);
163  return fl;
164  } else {
165  Textbox tb = new Textbox((String) value);
166  if(scale != 0) {
167  tb.setRows(scale);
168  tb.setCols(150);
169  tb.setMaxlength(maxLength);
170  }
171  return tb;
172  }
173  }
174 
175  protected Object getEditorValue() {
176  if(editor instanceof Checkbox) {
177  return Boolean.valueOf(((Checkbox) editor).isChecked());
178  } else if(editor instanceof Decimalbox) {
179  return StringParser.convertToClass(value.getClass(), ((Decimalbox) editor).getValue());
180  } else if(editor instanceof ExpressionInput) {
181  return StringParser.convertToClass(value.getClass(), ((ExpressionInput) editor).getNumber(scale));
182  } else if(editor instanceof Datebox) {
183  return ((Datebox) editor).getValue();
184  } else if(editor instanceof GenericListbox) {
185  return ((GenericListbox) editor).getObjectValue();
186  } else if(editor instanceof GenericCombobox) {
187  return ((GenericCombobox) editor).getObjectValue();
188  } else if(editor instanceof GenericBandbox) {
189  return ((GenericBandbox) editor).getObjectValue();
190  } else if(editor instanceof Listbox) {
191  return ((Listbox) editor).getSelectedItem().getValue();
192  } else if(editor instanceof InputEnum) {
193  return ((InputEnum) editor).getObjectValue();
194  } else {
195  return ((Textbox) editor).getText();
196  }
197  }
198 
199  protected void initiateEditor(HtmlBasedComponent editor) {
200  editor.setHflex("true");
201  if(editor instanceof Checkbox) {
202  // nothing
203  } else if(editor instanceof InputEnum) {
204  } else if(editor instanceof Combobox) {
205  } else if(editor instanceof DateboxShort) {
206  } else {
207  editor.setStyle("padding:0px;margin:0px;");
208  }
209  }
210 
211  protected HtmlBasedComponent labelComponent() {
212  Label l = Labels.text(I_.get(label)).style("font-size:1.1em").get();
213  if(!Strings.isBlank(help)) {
214  Hlayout hbox = new Hlayout();
215  hbox.setSclass("z-valign-middle");
216  hbox.setValign("middle");
217  hbox.appendChild(l);
218  A b = new A();
219  b.setIconSclass("z-icon-question");
220  b.setTarget("_blank");
221  b.setHref(help);
222  hbox.appendChild(b);
223  return hbox;
224  }
225  return l;
226  }
227 
228  /* Factory */
229 
230  public static DialogField space() {
231  return field(null, null).onlyLabel();
232  }
233 
234  public static DialogField label(String label) {
235  return field(label, null).onlyLabel();
236  }
237 
238  public static DialogField field(String label) {
239  return field(label, null);
240  }
241 
242  public static DialogField field(String label, Object value) {
243  return new DialogField(label, value);
244  }
245 
246  private DialogField(String label, Object value) {
247  this.label = label;
248  this.value = value;
249  setWidth("100%");
250  }
251 
252 }
static Object convertToClass(Class javaClass, Object value)
static String get(String msg)
Definition: I_.java:41
HtmlBasedComponent createEditor()
DialogField scale(int scale)
void initiateEditor(HtmlBasedComponent editor)
DialogField onEditor(Supplier< HtmlBasedComponent > onEditor)
DialogField choices(Enum[] choices)
static DialogField field(String label, Object value)
DialogField maxLength(int maxLength)
static DialogField label(String label)
DialogField help(String help)
DialogField format(String format)
HtmlBasedComponent labelComponent()
DialogField lines(int lines)
static DialogField field(String label)
static Labels text(String text)
Definition: Labels.java:119
Labels style(String style)
Definition: Labels.java:51