1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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.ChangeEvent;
36 import com.google.gwt.event.dom.client.ChangeHandler;
37 import com.google.gwt.event.dom.client.ClickEvent;
38 import com.google.gwt.event.dom.client.ClickHandler;
39 import com.google.gwt.event.dom.client.KeyPressHandler;
40 import com.google.gwt.json.client.JSONString;
41 import com.google.gwt.json.client.JSONValue;
42 import com.google.gwt.user.client.ui.HorizontalPanel;
43 import com.google.gwt.user.client.ui.ListBox;
44 import com.google.gwt.user.client.ui.RadioButton;
45 import com.google.gwt.user.client.ui.VerticalPanel;
46
47 public class ColorType extends DataTypeBase {
48
49 private final ColorInputForm inputForm;
50
51 public ColorType(final String parameterName, final String[] values, final String prefix) {
52 super(parameterName + " color setting");
53
54 inputForm = new ColorInputForm(parameterName, values, prefix);
55 }
56
57 public InputForm getInputForm() {
58 return inputForm;
59 }
60
61 public String getTypeName() {
62 return "select-color";
63 }
64
65 public void setParameters(Map<String, String> parameterMap) {
66 inputForm.setParameters(parameterMap);
67 }
68
69 class ColorInputForm extends ConfigInputForm implements ClickHandler {
70 private List<ChangeHandler> changeHandlers = new ArrayList<ChangeHandler>();
71
72 private final String parameterName;
73
74 private final ColorSelectPanel[] colorSelectPanels;
75
76 private RadioButton useColorButton;
77
78 private String urlEncode(String s) {
79 return s.replaceAll(" ", "%2B").replaceAll("=", "%3D");
80 }
81
82 ColorInputForm(final String parameterName, final String[] values, final String prefix) {
83 this.parameterName = urlEncode(parameterName);
84 colorSelectPanels = new ColorSelectPanel[values.length];
85
86 final VerticalPanel panel = new VerticalPanel();
87
88 for (int i = 0; i < values.length; i++) {
89 colorSelectPanels[i] = new ColorSelectPanel(values[i]);
90
91 panel.add(colorSelectPanels[i]);
92
93 if (values[i].equals("plus")) {
94 colorSelectPanels[i].firstListBox.setSelectedIndex(2);
95 colorSelectPanels[i].onChange(null);
96 }
97 else if (values[i].equals("minus")) {
98 colorSelectPanels[i].firstListBox.setSelectedIndex(3);
99 colorSelectPanels[i].onChange(null);
100 }
101 }
102
103 {
104 useColorButton = new RadioButton(prefix, "use this color setting");
105 useColorButton.setEnabled(true);
106 useColorButton.addClickHandler(this);
107 panel.add(useColorButton);
108 }
109
110 addWidget(panel);
111 }
112
113 public void onClick(ClickEvent e) {
114 for (int i = 0; i < changeHandlers.size(); i++) {
115 final ChangeHandler changeHandler = (ChangeHandler) (changeHandlers.get(i));
116 changeHandler.onChange(null);
117 }
118 }
119
120 public void addChangeHandler(ChangeHandler Handler) {
121 for (int i = 0; i < colorSelectPanels.length; i++) {
122 colorSelectPanels[i].addChangeHandler(Handler);
123 }
124 changeHandlers.add(Handler);
125 }
126
127 public void addKeyPressHandler(KeyPressHandler handler) {
128 for (int i = 0; i < colorSelectPanels.length; i++) {
129 colorSelectPanels[i].addKeyPressHandler(handler);
130 }
131 useColorButton.addKeyPressHandler(handler);
132 }
133
134 public JSONValue getJSONValue() {
135 return new JSONString(getUserInput());
136 }
137
138 public String getUserInput() {
139 final StringBuffer buf = new StringBuffer();
140
141 for (int i = 0; i < colorSelectPanels.length; i++) {
142 String colorValue = colorSelectPanels[i].getValue();
143 if (colorValue != null) {
144 if (i != 0)
145 buf.append('&');
146 String colorParamName = "color-" + parameterName + "-" + colorSelectPanels[i].getLabel();
147 buf.append(urlEncode(colorParamName) + "=(");
148 buf.append(colorSelectPanels[i].getValue());
149 buf.append(")");
150 }
151 }
152
153 return buf.toString();
154 }
155
156 public void setValue(String value) {
157 }
158
159 public void setParameters(Map<String, String> parameterMap) {
160 boolean colorIsSpecified = false;
161 for (int i = 0; i < colorSelectPanels.length; i++) {
162 String colorValue = colorSelectPanels[i].getValue();
163 if (colorValue != null) {
164 final String key = "color-" + parameterName + "-" + colorSelectPanels[i].getLabel();
165 final String value = "(" + colorSelectPanels[i].getValue() + ")";
166 String encodedParam = urlEncode(key);
167 parameterMap.put(encodedParam, value);
168 colorIsSpecified = true;
169 }
170 }
171 if (useColorButton.getValue() && colorIsSpecified) {
172 parameterMap.put("usecolor", parameterName + "-color");
173 }
174 }
175 }
176
177 private class ColorSelectPanel extends HorizontalPanel implements ChangeHandler {
178 final String label;
179 final ListBox firstListBox = new ListBox();
180
181 final ByteListBox redBox = new ByteListBox();
182 final ByteListBox greenBox = new ByteListBox();
183 final ByteListBox blueBox = new ByteListBox();
184
185 public ColorSelectPanel(final String label) {
186 this.label = label;
187
188 add(new FormLabel(label));
189 add(firstListBox);
190 firstListBox.addChangeHandler(this);
191 setItems(firstListBox);
192
193 add(new FormLabel("R"));
194 add(redBox);
195 add(new FormLabel("G"));
196 add(greenBox);
197 add(new FormLabel("B"));
198 add(blueBox);
199 }
200
201 private final void setItems(final ListBox listBox) {
202 listBox.addItem("(default)", "default");
203 listBox.addItem("black", "0,0,0");
204 listBox.addItem("red", "233,52,71");
205 listBox.addItem("blue", "0,111,171");
206 listBox.addItem("green", "0,154,87");
207 listBox.addItem("yellow", "244,213,0");
208 listBox.addItem("user input");
209 }
210
211 public String getLabel() {
212 return label;
213 }
214
215 public String getValue() {
216 final String firstValue = firstListBox.getValue(firstListBox.getSelectedIndex());
217 if (firstValue.equals("default"))
218 return null;
219
220 final StringBuffer buf = new StringBuffer();
221
222 buf.append(redBox.getValue(redBox.getSelectedIndex()));
223 buf.append(',');
224 buf.append(greenBox.getValue(greenBox.getSelectedIndex()));
225 buf.append(',');
226 buf.append(blueBox.getValue(blueBox.getSelectedIndex()));
227
228 return buf.toString();
229 }
230
231 public void onChange(ChangeEvent e) {
232 final String firstValue = firstListBox.getValue(firstListBox.getSelectedIndex());
233
234 if (firstValue.equals("user input")) {
235 redBox.setEnabled(true);
236 greenBox.setEnabled(true);
237 blueBox.setEnabled(true);
238 }
239 else {
240 if (!firstValue.equals("default")) {
241 final String[] elements = firstValue.split(",");
242
243 redBox.setSelectedIndex(Integer.parseInt(elements[0]));
244 greenBox.setSelectedIndex(Integer.parseInt(elements[1]));
245 blueBox.setSelectedIndex(Integer.parseInt(elements[2]));
246 }
247
248 redBox.setEnabled(false);
249 greenBox.setEnabled(false);
250 blueBox.setEnabled(false);
251 }
252 }
253
254 public void addChangeHandler(ChangeHandler Handler) {
255 firstListBox.addChangeHandler(Handler);
256
257 redBox.addChangeHandler(Handler);
258 greenBox.addChangeHandler(Handler);
259 blueBox.addChangeHandler(Handler);
260 }
261
262 public void addKeyPressHandler(KeyPressHandler handler) {
263 firstListBox.addKeyPressHandler(handler);
264
265 redBox.addKeyPressHandler(handler);
266 greenBox.addKeyPressHandler(handler);
267 blueBox.addKeyPressHandler(handler);
268 }
269
270 }
271
272 private class ByteListBox extends ListBox {
273 private static final int SIZE = 256;
274
275 ByteListBox() {
276 for (int i = 0; i < SIZE; i++) {
277 addItem(Integer.toString(i));
278 }
279 setEnabled(false);
280 }
281 }
282
283 }