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  // StringType.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.JSONString;
36  import com.google.gwt.json.client.JSONValue;
37  import com.google.gwt.user.client.ui.ListBox;
38  import com.google.gwt.user.client.ui.TextBox;
39  
40  public class StringType extends DataTypeBase {
41  	transient ValueDomain valueDomain = null;
42  
43  	public StringType(String name) {
44  		super(name);
45  	}
46  
47  	public StringType(String name, ValueDomain valueDomain) {
48  		super(name);
49  		this.valueDomain = valueDomain;
50  	}
51  
52  	private boolean hasValueDomain() {
53  		return valueDomain != null;
54  	}
55  
56  	public InputForm getInputForm() {
57  		if (!hasValueDomain())
58  			return new StringTypeForm();
59  		else
60  			return new StringTypeListBox(valueDomain);
61  	}
62  
63  	public class StringTypeForm extends InputForm {
64  		TextBox form = new TextBox();
65  
66  		public StringTypeForm() {
67  
68  			form.setWidth("300px");
69  			initWidget(form);
70  		}
71  
72  		public String getUserInput() {
73  			return form.getText();
74  		}
75  
76  		public JSONValue getJSONValue() {
77  			return new JSONString(form.getText());
78  		}
79  
80  		public void setValue(String value) {
81  			form.setText(value);
82  		}
83  
84  		public void addKeyPressHandler(KeyPressHandler listener) {
85  			form.addKeyPressHandler(listener);
86  		}
87  
88  		public void addChangeHandler(ChangeHandler listener) {
89  			form.addChangeHandler(listener);
90  		}
91  
92  	}
93  
94  	public class StringTypeListBox extends InputForm {
95  		ListBox listBox = new ListBox();
96  
97  		public StringTypeListBox(ValueDomain vd) {
98  			listBox.setVisibleItemCount(1);
99  			for (Value v : vd.getValueList()) {
100 				listBox.addItem(v.getLabel(), v.getValue());
101 			}
102 
103 			initWidget(listBox);
104 		}
105 
106 		public JSONValue getJSONValue() {
107 			return new JSONString(getUserInput());
108 		}
109 
110 		public String getUserInput() {
111 			return listBox.getValue(listBox.getSelectedIndex());
112 		}
113 
114 		public void setValue(String value) {
115 			for (int i = 0; i < listBox.getItemCount(); i++) {
116 				if (listBox.getValue(i).equals(value)) {
117 					listBox.setSelectedIndex(i);
118 					return;
119 				}
120 			}
121 			// no entry for the given value is found
122 			GWT.log(value + " is not found in the value domain", new UTGBClientException());
123 		}
124 
125 		public void addKeyPressHandler(KeyPressHandler listener) {
126 			listBox.addKeyPressHandler(listener);
127 		}
128 
129 		public void addChangeHandler(ChangeHandler listener) {
130 			listBox.addChangeHandler(listener);
131 		}
132 
133 	}
134 
135 	public String getTypeName() {
136 		return "string";
137 	}
138 
139 }