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  // IntegerType.java
20  // Since: 2007/04/13
21  //
22  // $Date$
23  // $URL$ 
24  // $Author$
25  //--------------------------------------
26  package org.utgenome.gwt.utgb.client.db.datatype;
27  
28  import org.utgenome.gwt.utgb.client.UTGBClientException;
29  import org.utgenome.gwt.utgb.client.db.Value;
30  import org.utgenome.gwt.utgb.client.db.ValueDomain;
31  
32  import com.google.gwt.core.client.GWT;
33  import com.google.gwt.event.dom.client.ChangeHandler;
34  import com.google.gwt.event.dom.client.KeyPressHandler;
35  import com.google.gwt.json.client.JSONNumber;
36  import com.google.gwt.json.client.JSONString;
37  import com.google.gwt.json.client.JSONValue;
38  import com.google.gwt.user.client.ui.ListBox;
39  import com.google.gwt.user.client.ui.TextBox;
40  
41  public class IntegerType extends DataTypeBase {
42  	private ValueDomain _valueDomain = null;
43  
44  	public IntegerType(String name) {
45  		super(name);
46  	}
47  
48  	public IntegerType(String name, ValueDomain valueDomain) {
49  		super(name);
50  		_valueDomain = valueDomain;
51  	}
52  
53  	public IntegerType(String name, boolean isPrimaryKey, boolean isNotNull) {
54  		super(name, isPrimaryKey, isNotNull);
55  	}
56  
57  	public InputForm getInputForm() {
58  		if (_valueDomain == null)
59  			return new IntegerTypeForm();
60  		else
61  			return new IntegerTypeListBox(_valueDomain);
62  	}
63  
64  	public class IntegerTypeForm extends InputForm {
65  
66  		private TextBox form = new TextBox();
67  
68  		public IntegerTypeForm() {
69  			initWidget(form);
70  		}
71  
72  		public String getUserInput() {
73  			return form.getText();
74  		}
75  
76  		public JSONValue getJSONValue() {
77  			try {
78  				int value = Integer.parseInt(form.getText());
79  				return new JSONNumber(value);
80  			}
81  			catch (NumberFormatException e) {
82  				return new JSONString("");
83  			}
84  		}
85  
86  		public void setValue(String value) {
87  			try {
88  				int v = Integer.parseInt(value);
89  				form.setText(value);
90  			}
91  			catch (NumberFormatException e) {
92  				GWT.log(value + " is not a integer type", e);
93  			}
94  		}
95  
96  		public void addKeyPressHandler(KeyPressHandler listener) {
97  			form.addKeyPressHandler(listener);
98  		}
99  
100 		public void addChangeHandler(ChangeHandler listener) {
101 			form.addChangeHandler(listener);
102 		}
103 
104 	}
105 
106 	public class IntegerTypeListBox extends InputForm {
107 		ListBox listBox = new ListBox();
108 
109 		public IntegerTypeListBox(ValueDomain vd) {
110 			listBox.setVisibleItemCount(1);
111 			for (Value v : vd.getValueList()) {
112 				listBox.addItem(v.getLabel(), v.getValue());
113 			}
114 
115 			initWidget(listBox);
116 		}
117 
118 		public JSONValue getJSONValue() {
119 			try {
120 				int value = Integer.parseInt(getUserInput());
121 				return new JSONNumber(value);
122 			}
123 			catch (NumberFormatException e) {
124 				return new JSONString("");
125 			}
126 		}
127 
128 		public String getUserInput() {
129 			return listBox.getValue(listBox.getSelectedIndex());
130 		}
131 
132 		public void setValue(String value) {
133 			for (int i = 0; i < listBox.getItemCount(); i++) {
134 				if (listBox.getValue(i).equals(value)) {
135 					listBox.setSelectedIndex(i);
136 					return;
137 				}
138 			}
139 			// no entry for the given value is found
140 			GWT.log(value + " is not found in the value domain", new UTGBClientException());
141 		}
142 
143 		public void addKeyPressHandler(KeyPressHandler listener) {
144 			listBox.addKeyPressHandler(listener);
145 		}
146 
147 		public void addChangeHandler(ChangeHandler listener) {
148 			listBox.addChangeHandler(listener);
149 		}
150 	}
151 
152 	public String toString(JSONValue value) {
153 		JSONNumber n = value.isNumber();
154 		if (n != null)
155 			return Integer.toString((int) n.doubleValue());
156 		else {
157 			return super.toString(value);
158 		}
159 
160 	}
161 
162 	public String getTypeName() {
163 		return "integer";
164 	}
165 
166 }