BrightSide Workbench Full Report + Source Code
InputField.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.dialog;
19 
20 import java.io.File;
21 import java.util.Date;
22 import org.turro.elephant.impl.util.StringParser;
23 import org.turro.i18n.I_;
24 import org.turro.zkoss.input.*;
25 import org.zkoss.zk.ui.HtmlBasedComponent;
26 import org.zkoss.zul.*;
27 
32 public class InputField extends Vlayout {
33 
34  private final String label, format;
35  private final Object value;
36  private final int scale;
37  private Enum[] choices;
38 
39  protected boolean labelOnEditor = false;
40  protected HtmlBasedComponent editor;
41 
42  public InputField(String label, Object value, String format, int scale) {
43  this.label = label;
44  this.value = value;
45  this.format = format;
46  this.scale = scale;
47  this.choices = null;
48  setWidth("100%");
49  editor = createEditor();
51  if(!labelOnEditor) {
52  appendChild(new Label(I_.get(label)));
53  }
54  appendChild(editor);
55  }
56 
57  public InputField(String label, Object value, Enum[] choices) {
58  this.label = label;
59  this.value = value;
60  this.format = null;
61  this.scale = 0;
62  this.choices = choices;
63  setWidth("100%");
64  editor = createEditor();
66  if(!labelOnEditor) {
67  appendChild(new Label(I_.get(label)));
68  }
69  appendChild(editor);
70  }
71 
72  public InputField(String label, HtmlBasedComponent editor, Object value) {
73  this.label = label;
74  this.value = value;
75  this.format = null;
76  this.scale = 0;
77  setWidth("100%");
78  this.editor = editor;
80  if(!labelOnEditor) {
81  appendChild(new Label(I_.get(label)));
82  }
83  appendChild(editor);
84  }
85 
86  public String getLabel() {
87  return label;
88  }
89 
90  public Object getValue() {
91  return getEditorValue();
92  }
93 
94  public HtmlBasedComponent getEditor() {
95  return editor;
96  }
97 
98  protected HtmlBasedComponent createEditor() {
99  if(value instanceof Boolean) {
100  Checkbox cb = new Checkbox();
101  cb.setChecked((Boolean) value);
102  cb.setLabel(I_.get(label));
103  labelOnEditor = true;
104  return cb;
105  } else if(value instanceof Number) {
106  ExpressionInput ei = new ExpressionInput();
107  ei.setValue(value);
108  return ei;
109  } else if(value instanceof Date) {
110  Datebox db = new Datebox((Date) value);
111  db.setFormat(format);
112  return db;
113  } else if(value instanceof Enum) {
114  InputEnum ie = new InputEnum(choices);
115  ie.setObjectValue((Enum) value);
116  labelOnEditor = true;
117  return ie;
118  } else if(value instanceof File) {
119  FileListbox fl = new FileListbox();
120  fl.setMold("select");
121  fl.setFilter(format);
122  fl.setDir((File) value);
123  fl.setSelectFirst(true);
124  return fl;
125  } else {
126  Textbox tb = new Textbox((String) value);
127  if(scale != 0) {
128  tb.setRows(scale);
129  tb.setCols(150);
130  }
131  return tb;
132  }
133  }
134 
135  protected Object getEditorValue() {
136  if(editor instanceof Checkbox) {
137  return Boolean.valueOf(((Checkbox) editor).isChecked());
138  } else if(editor instanceof Decimalbox) {
139  return StringParser.convertToClass(value.getClass(), ((Decimalbox) editor).getValue());
140  } else if(editor instanceof ExpressionInput) {
141  return StringParser.convertToClass(value.getClass(), ((ExpressionInput) editor).getNumber(scale));
142  } else if(editor instanceof Datebox) {
143  return ((Datebox) editor).getValue();
144  } else if(editor instanceof GenericListbox) {
145  return ((GenericListbox) editor).getObjectValue();
146  } else if(editor instanceof GenericCombobox) {
147  return ((GenericCombobox) editor).getObjectValue();
148  } else if(editor instanceof GenericBandbox) {
149  return ((GenericBandbox) editor).getObjectValue();
150  } else if(editor instanceof Listbox) {
151  return ((Listbox) editor).getSelectedItem().getValue();
152  } else if(editor instanceof InputEnum) {
153  return ((InputEnum) editor).getObjectValue();
154  } else {
155  return ((Textbox) editor).getText();
156  }
157  }
158 
159  protected void initiateEditor(HtmlBasedComponent editor) {
160  editor.setHflex("true");
161  if(editor instanceof Checkbox) {
162  // nothing
163  } else if(editor instanceof InputEnum) {
164  } else if(editor instanceof Combobox) {
165  } else if(editor instanceof DateboxShort) {
166  } else {
167  editor.setStyle("padding:0px;margin:0px;");
168  }
169  }
170 
171  public static InputField getByLabel(String label, InputField[] fields) {
172  for(InputField f : fields) {
173  if(f.getLabel().equals(label)) {
174  return f;
175  }
176  }
177  return null;
178  }
179 
180 }
static Object convertToClass(Class javaClass, Object value)
static String get(String msg)
Definition: I_.java:41
HtmlBasedComponent createEditor()
Definition: InputField.java:98
HtmlBasedComponent getEditor()
Definition: InputField.java:94
InputField(String label, Object value, String format, int scale)
Definition: InputField.java:42
void initiateEditor(HtmlBasedComponent editor)
InputField(String label, HtmlBasedComponent editor, Object value)
Definition: InputField.java:72
static InputField getByLabel(String label, InputField[] fields)
InputField(String label, Object value, Enum[] choices)
Definition: InputField.java:57