BrightSide Workbench Full Report + Source Code
ExpressionInput.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.input;
19 
20 import org.turro.script.Script;
21 import org.zkoss.zk.ui.WrongValueException;
22 import org.zkoss.zul.Textbox;
23 
28 public class ExpressionInput extends Textbox {
29 
30  private String format;
31 
32  public Object getNumber() throws WrongValueException {
33  return new Script().evalToDouble(getText());
34  }
35 
36  public Object getNumber(int precision) throws WrongValueException {
37  return new Script().evalToDouble(getText(), precision);
38  }
39 
40  public double getDoubleValue() {
41  Object n = (Object) getNumber();
42  if(n instanceof Number) {
43  return ((Number) n).doubleValue();
44  }
45  return 0.0d;
46  }
47 
48  public void setValue(Object value) throws WrongValueException {
49  setText(coerceToString(value == null ? "" : value));
50  }
51 
52  @Override
53  protected Object coerceFromString(String value) throws WrongValueException {
54  return Script.formatInput(new Script().evalToDouble(value), format == null ? "0.########" : format);
55  }
56 
57  @Override
58  protected String coerceToString(Object value) {
59  return Script.formatInput(value, format == null ? "0.########" : format);
60  }
61 
62  public String getFormat() {
63  return format;
64  }
65 
66  public void setFormat(String format) {
67  this.format = format;
68  }
69 
70 }