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  // DispType.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.ui.CheckBox;
41  import com.google.gwt.user.client.ui.FlowPanel;
42  
43  /**
44   * @author ssksn
45   * 
46   */
47  public class DispType extends DataTypeBase {
48  
49  	private final DispInputForm inputForm;
50  
51  	public DispType(final String parameterName, final String[] values) {
52  		super(parameterName + " display setting");
53  
54  		inputForm = new DispInputForm(parameterName, values);
55  	}
56  
57  	public InputForm getInputForm() {
58  		return inputForm;
59  	}
60  
61  	public String getTypeName() {
62  		return "select-disp";
63  	}
64  
65  	public void setParameters(Map<String, String> parameterMap) {
66  		inputForm.setParameters(parameterMap);
67  	}
68  
69  	private class DispInputForm extends ConfigInputForm implements ClickHandler {
70  		private List<ChangeHandler> changeHandlers = new ArrayList<ChangeHandler>();
71  
72  		private final String parameterName;
73  
74  		private CheckBox[] checkBoxs;
75  
76  		DispInputForm(final String parameterName, final String[] values) {
77  			this.parameterName = parameterName;
78  			checkBoxs = new CheckBox[values.length];
79  
80  			final FlowPanel panel = new FlowPanel();
81  
82  			for (int i = 0; i < values.length; i++) {
83  				checkBoxs[i] = new CheckBox(values[i]);
84  				checkBoxs[i].addClickHandler(this);
85  				checkBoxs[i].setValue(true);
86  				panel.add(checkBoxs[i]);
87  			}
88  
89  			addWidget(panel);
90  		}
91  
92  		public void onClick(ClickEvent e) {
93  			for (int i = 0; i < changeHandlers.size(); i++) {
94  				final ChangeHandler changeHandler = (ChangeHandler) (changeHandlers.get(i));
95  				changeHandler.onChange(null);
96  			}
97  		}
98  
99  		public void addChangeHandler(ChangeHandler listener) {
100 			changeHandlers.add(listener);
101 		}
102 
103 		public void addKeyPressHandler(KeyPressHandler listener) {
104 			for (int i = 0; i < checkBoxs.length; i++) {
105 				checkBoxs[i].addKeyPressHandler(listener);
106 			}
107 		}
108 
109 		public JSONValue getJSONValue() {
110 			return new JSONString(getUserInput());
111 		}
112 
113 		public String getUserInput() {
114 			final StringBuffer buf = new StringBuffer();
115 
116 			for (int i = 0; i < checkBoxs.length; i++) {
117 				if (i != 0)
118 					buf.append('&');
119 				buf.append("disp-" + parameterName + "-" + checkBoxs[i].getText() + "=");
120 				if (checkBoxs[i].getValue()) {
121 					buf.append("on");
122 				}
123 				else {
124 					buf.append("off");
125 				}
126 			}
127 
128 			return buf.toString();
129 		}
130 
131 		public void setValue(String value) {
132 		}
133 
134 		public void setParameters(Map<String, String> parameterMap) {
135 			for (int i = 0; i < checkBoxs.length; i++) {
136 				final String key = "disp-" + parameterName + "-" + checkBoxs[i].getText();
137 				if (checkBoxs[i].getValue()) {
138 					parameterMap.put(key, "on");
139 				}
140 				else {
141 					parameterMap.put(key, "off");
142 				}
143 			}
144 		}
145 	}
146 
147 }