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  // GenomeBrowser Project
18  //
19  // SelectType.java
20  // Since: 2007/07/11
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib.old.datatype;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.utgenome.gwt.utgb.client.db.datatype.DataTypeBase;
32  import org.utgenome.gwt.utgb.client.db.datatype.InputForm;
33  
34  import com.google.gwt.event.dom.client.ChangeHandler;
35  import com.google.gwt.event.dom.client.ClickEvent;
36  import com.google.gwt.event.dom.client.ClickHandler;
37  import com.google.gwt.event.dom.client.KeyPressHandler;
38  import com.google.gwt.json.client.JSONString;
39  import com.google.gwt.json.client.JSONValue;
40  import com.google.gwt.user.client.Random;
41  import com.google.gwt.user.client.ui.FlowPanel;
42  import com.google.gwt.user.client.ui.RadioButton;
43  
44  public class SelectType extends DataTypeBase {
45  
46  	private final SelectInputForm inputForm;
47  
48  	public SelectType(final String parameterName, final String[] values) {
49  		super(parameterName + " select setting");
50  
51  		inputForm = new SelectInputForm(parameterName, values);
52  	}
53  
54  	public InputForm getInputForm() {
55  		return inputForm;
56  	}
57  
58  	public String getTypeName() {
59  		return "select-select";
60  	}
61  
62  	public void setParameters(Map<String, String> parameterMap) {
63  		inputForm.setParameters(parameterMap);
64  	}
65  
66  	class SelectInputForm extends ConfigInputForm implements ClickHandler {
67  		private List<ChangeHandler> changeHandlers = new ArrayList<ChangeHandler>();
68  
69  		private final String parameterName;
70  
71  		private final String[] values;
72  		private RadioButton[] radioButtons;
73  
74  		SelectInputForm(final String parameterName, final String[] values) {
75  			this.parameterName = parameterName;
76  			this.values = values;
77  
78  			final String prefix = Integer.toString(Random.nextInt());
79  
80  			radioButtons = new RadioButton[values.length];
81  
82  			final FlowPanel panel = new FlowPanel();
83  
84  			for (int i = 0; i < values.length; i++) {
85  				radioButtons[i] = new RadioButton(prefix, values[i]);
86  				radioButtons[i].addClickHandler(this);
87  				panel.add(radioButtons[i]);
88  			}
89  			radioButtons[0].setValue(true);
90  
91  			addWidget(panel);
92  		}
93  
94  		public void onClick(ClickEvent e) {
95  			for (int i = 0; i < changeHandlers.size(); i++) {
96  				final ChangeHandler changeHandler = (ChangeHandler) (changeHandlers.get(i));
97  				changeHandler.onChange(null);
98  			}
99  		}
100 
101 		public void addChangeHandler(ChangeHandler listener) {
102 			changeHandlers.add(listener);
103 		}
104 
105 		public void addKeyPressHandler(KeyPressHandler listener) {
106 			for (int i = 0; i < radioButtons.length; i++) {
107 				radioButtons[i].addKeyPressHandler(listener);
108 			}
109 		}
110 
111 		public JSONValue getJSONValue() {
112 			return new JSONString(getUserInput());
113 		}
114 
115 		public String getUserInput() {
116 			final StringBuffer buf = new StringBuffer();
117 
118 			for (int i = 0; i < radioButtons.length; i++) {
119 				if (radioButtons[i].getValue()) {
120 					buf.append("select-" + parameterName + "=" + radioButtons[i].getText());
121 					break;
122 				}
123 			}
124 
125 			return buf.toString();
126 		}
127 
128 		public void setValue(String value) {
129 		}
130 
131 		public void setParameters(Map<String, String> parameterMap) {
132 			for (int i = 0; i < radioButtons.length; i++) {
133 				if (radioButtons[i].getValue()) {
134 					final String key = "select-" + parameterName;
135 					final String value = radioButtons[i].getText();
136 					parameterMap.put(key, value);
137 					break;
138 				}
139 			}
140 		}
141 	}
142 }