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  // GradationType.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  import org.utgenome.gwt.utgb.client.ui.FormLabel;
34  
35  import com.google.gwt.event.dom.client.ChangeHandler;
36  import com.google.gwt.event.dom.client.ClickEvent;
37  import com.google.gwt.event.dom.client.ClickHandler;
38  import com.google.gwt.event.dom.client.KeyPressHandler;
39  import com.google.gwt.json.client.JSONString;
40  import com.google.gwt.json.client.JSONValue;
41  import com.google.gwt.user.client.ui.DockPanel;
42  import com.google.gwt.user.client.ui.HorizontalPanel;
43  import com.google.gwt.user.client.ui.RadioButton;
44  import com.google.gwt.user.client.ui.TextBox;
45  
46  public class GradationType extends DataTypeBase {
47  	private final GradationInputForm inputForm;
48  
49  	public GradationType(final String parameterName, final double minValue, final double maxValue, final String prefix) {
50  		super(parameterName + " color gradation setting");
51  
52  		inputForm = new GradationInputForm(parameterName, minValue, maxValue, prefix);
53  	}
54  
55  	public InputForm getInputForm() {
56  		return inputForm;
57  	}
58  
59  	public String getTypeName() {
60  		return "real-gradation";
61  	}
62  
63  	public void setParameters(Map<String, String> parameterMap) {
64  		inputForm.setParameters(parameterMap);
65  	}
66  
67  	class GradationInputForm extends InputForm implements ClickHandler {
68  		private List<ChangeHandler> changeHandlers = new ArrayList<ChangeHandler>();
69  
70  		private final String parameterName;
71  
72  		private final TextBox minBox = new TextBox();
73  		private final TextBox maxBox = new TextBox();
74  
75  		private final double minValue;
76  		private final double maxValue;
77  
78  		private RadioButton useColorButton;
79  
80  		GradationInputForm(final String parameterName, final double minValue, final double maxValue, final String prefix) {
81  			this.parameterName = parameterName;
82  			this.minValue = minValue;
83  			this.maxValue = maxValue;
84  
85  			final DockPanel _panel = new DockPanel();
86  			_panel.setHorizontalAlignment(DockPanel.ALIGN_LEFT);
87  
88  			final HorizontalPanel panel = new HorizontalPanel();
89  			panel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
90  
91  			panel.add(new FormLabel("min[" + minValue + "]", false));
92  			panel.add(minBox);
93  			minBox.setText(Double.toString(minValue));
94  
95  			panel.add(new FormLabel("max[" + maxValue + "]", false));
96  			panel.add(maxBox);
97  			maxBox.setText(Double.toString(maxValue));
98  
99  			minBox.setWidth("50px");
100 			maxBox.setWidth("50px");
101 
102 			_panel.add(panel, DockPanel.NORTH);
103 
104 			useColorButton = new RadioButton(prefix, "use this color setting");
105 			useColorButton.addClickHandler(this);
106 			_panel.add(useColorButton, DockPanel.CENTER);
107 
108 			initWidget(_panel);
109 		}
110 
111 		public void onClick(ClickEvent sender) {
112 			for (int i = 0; i < changeHandlers.size(); i++) {
113 				final ChangeHandler changeHandler = (ChangeHandler) (changeHandlers.get(i));
114 				changeHandler.onChange(null);
115 			}
116 		}
117 
118 		public void addChangeHandler(ChangeHandler listener) {
119 			minBox.addChangeHandler(listener);
120 			maxBox.addChangeHandler(listener);
121 			changeHandlers.add(listener);
122 		}
123 
124 		public void addKeyPressHandler(KeyPressHandler listener) {
125 			minBox.addKeyPressHandler(listener);
126 			maxBox.addKeyPressHandler(listener);
127 			useColorButton.addKeyPressHandler(listener);
128 		}
129 
130 		public JSONValue getJSONValue() {
131 			return new JSONString(getUserInput());
132 		}
133 
134 		public String getUserInput() {
135 			return "gradation-" + parameterName + "-min=" + getInputMinValue() + "&gradation-" + parameterName + "-max=" + getInputMaxValue();
136 		}
137 
138 		public void setValue(String value) {
139 		}
140 
141 		public void setParameters(Map<String, String> parameterMap) {
142 			final String minKey = "gradation-" + parameterName + "-min";
143 			parameterMap.put(minKey, Double.toString(getInputMinValue()));
144 
145 			final String maxKey = "gradation-" + parameterName + "-max";
146 			parameterMap.put(maxKey, Double.toString(getInputMaxValue()));
147 
148 			if (useColorButton.getValue()) {
149 				parameterMap.put("usecolor", parameterName + "-gradation");
150 			}
151 		}
152 
153 		private double getInputMinValue() {
154 			final double inputMinValue = Double.parseDouble(minBox.getText());
155 
156 			return Math.max(inputMinValue, minValue);
157 		}
158 
159 		private double getInputMaxValue() {
160 			final double inputMaxValue = Double.parseDouble(maxBox.getText());
161 
162 			return Math.min(inputMaxValue, maxValue);
163 		}
164 	}
165 }