View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // UTGB Common Project
18  //
19  // DoubleType.java
20  // Since: 2009/12/14
21  //
22  // $Date$
23  // $URL$ 
24  // $Author$
25  //--------------------------------------
26  package org.utgenome.gwt.utgb.client.db.datatype;
27  
28  import com.google.gwt.core.client.GWT;
29  import com.google.gwt.event.dom.client.ChangeHandler;
30  import com.google.gwt.event.dom.client.KeyPressHandler;
31  import com.google.gwt.json.client.JSONNumber;
32  import com.google.gwt.json.client.JSONString;
33  import com.google.gwt.json.client.JSONValue;
34  import com.google.gwt.user.client.ui.TextBox;
35  
36  public class FloatType extends DataTypeBase {
37  	public FloatType(String name) {
38  		super(name);
39  	}
40  
41  	public InputForm getInputForm() {
42  		return new FloatTypeForm();
43  	}
44  
45  	public class FloatTypeForm extends InputForm {
46  		private TextBox form = new TextBox();
47  
48  		public FloatTypeForm() {
49  			initWidget(form);
50  		}
51  
52  		public String getUserInput() {
53  			return form.getText();
54  		}
55  
56  		public JSONValue getJSONValue() {
57  			try {
58  				double value = Float.parseFloat(form.getText());
59  				return new JSONNumber(value);
60  			}
61  			catch (NumberFormatException e) {
62  				return new JSONString("");
63  			}
64  		}
65  
66  		public void setValue(String value) {
67  			try {
68  				float v = Float.parseFloat(value);
69  				form.setText(value);
70  			}
71  			catch (NumberFormatException e) {
72  				GWT.log(value + " is not a float type", e);
73  			}
74  		}
75  
76  		public void addKeyPressHandler(KeyPressHandler listener) {
77  			form.addKeyPressHandler(listener);
78  		}
79  
80  		public void addChangeHandler(ChangeHandler listener) {
81  			form.addChangeHandler(listener);
82  		}
83  	}
84  
85  	public String getTypeName() {
86  		return "float";
87  	}
88  
89  }