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  // TrackConfig.java
20  // Since: Jun 19, 2007a
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track;
26  
27  import java.util.HashMap;
28  
29  import org.utgenome.gwt.utgb.client.db.datatype.BooleanType;
30  import org.utgenome.gwt.utgb.client.db.datatype.DataType;
31  import org.utgenome.gwt.utgb.client.db.datatype.DoubleType;
32  import org.utgenome.gwt.utgb.client.db.datatype.InputForm;
33  import org.utgenome.gwt.utgb.client.db.datatype.IntegerType;
34  import org.utgenome.gwt.utgb.client.db.datatype.StringType;
35  import org.utgenome.gwt.utgb.client.track.impl.TrackConfigChangeImpl;
36  import org.utgenome.gwt.utgb.client.ui.DraggableTable;
37  import org.utgenome.gwt.utgb.client.ui.Icon;
38  import org.utgenome.gwt.utgb.client.ui.MouseMoveListener;
39  import org.utgenome.gwt.utgb.client.util.CanonicalProperties;
40  
41  import com.google.gwt.core.client.GWT;
42  import com.google.gwt.event.dom.client.ChangeEvent;
43  import com.google.gwt.event.dom.client.ChangeHandler;
44  import com.google.gwt.event.dom.client.ClickEvent;
45  import com.google.gwt.event.dom.client.ClickHandler;
46  import com.google.gwt.event.dom.client.KeyPressEvent;
47  import com.google.gwt.event.dom.client.KeyPressHandler;
48  import com.google.gwt.user.client.DOM;
49  import com.google.gwt.user.client.ui.Composite;
50  import com.google.gwt.user.client.ui.DockPanel;
51  import com.google.gwt.user.client.ui.Grid;
52  import com.google.gwt.user.client.ui.Label;
53  import com.google.gwt.user.client.ui.PopupPanel;
54  import com.google.gwt.user.client.ui.ScrollPanel;
55  import com.google.gwt.widgetideas.client.event.KeyboardHandler;
56  
57  /**
58   * Track configuration panel.
59   * 
60   * @author leo
61   * 
62   */
63  public class TrackConfig extends PopupPanel {
64  
65  	class CloseButton extends Icon {
66  		public CloseButton(final PopupPanel popup) {
67  			super(Design.getIconImage(Design.ICON_CLOSE));
68  			setStyleName("track-icon");
69  
70  			addClickHandler(new ClickHandler() {
71  				public void onClick(ClickEvent e) {
72  					popup.hide();
73  				}
74  			});
75  		}
76  	}
77  
78  	private Track _track;
79  	private DockPanel _panel = new DockPanel();
80  	private ConfigurationTable _configTable = new ConfigurationTable();
81  	private Label _label;
82  	CanonicalProperties properties = new CanonicalProperties();
83  	CanonicalProperties defaultValueTable = new CanonicalProperties();
84  
85  	public TrackConfig(Track track) {
86  		super(true);
87  		init(track);
88  	}
89  
90  	public void init(Track track) {
91  		this._track = track;
92  
93  		_label = new Label(track.getTrackInfo().getTrackName() + " Configuration");
94  		_label.setStyleName("config-label");
95  		new MouseMoveListener(this).register(_label);
96  
97  		DOM.setStyleAttribute(_label.getElement(), "cursor", "move");
98  		DOM.setStyleAttribute(_label.getElement(), "display", "block");
99  		DOM.setStyleAttribute(_label.getElement(), "width", "100%");
100 		CloseButton cb = new CloseButton(this);
101 
102 		Grid grid = new Grid(1, 2);
103 		grid.getColumnFormatter().setWidth(0, "15px");
104 		grid.getColumnFormatter().setWidth(1, "100%");
105 		grid.setWidth("100%");
106 		grid.setStyleName("config-frame");
107 		grid.setWidget(0, 0, cb);
108 		grid.setWidget(0, 1, _label);
109 
110 		final DockPanel simplePanel = new DockPanel();
111 		simplePanel.add(_configTable, DockPanel.CENTER);
112 		final ScrollPanel scrollPanel = new ScrollPanel(simplePanel) {
113 			@Override
114 			protected void onAttach() {
115 				super.onAttach();
116 				final int height = getOffsetHeight();
117 				if (height > 500) {
118 					// setHeight("500px");
119 				}
120 			}
121 		};
122 		this.setStyleName("config");
123 		_panel.add(grid, DockPanel.NORTH);
124 		_panel.add(scrollPanel, DockPanel.CENTER);
125 		this.setWidget(_panel);
126 		setWidth("500px");
127 	}
128 
129 	public void addConfigString(String label, String paramName, String defaultValue) {
130 		addConfig(label, new StringType(paramName), defaultValue);
131 	}
132 
133 	public void addConfigInteger(String label, String paramName, int defaultValue) {
134 		addConfig(label, new IntegerType(paramName), Integer.toString(defaultValue));
135 	}
136 
137 	public void addConfigBoolean(String label, String paramName, boolean defaultValue) {
138 		addConfig(label, new BooleanType(paramName), Boolean.toString(defaultValue));
139 	}
140 
141 	public void addConfigDouble(String label, String paramName, double defaultValue) {
142 		addConfig(label, new DoubleType(paramName), Double.toString(defaultValue));
143 	}
144 
145 	public void addConfig(DataType dataType, String defaultValue) {
146 		addConfig(dataType.getName(), dataType, defaultValue);
147 	}
148 
149 	public void addConfig(String label, DataType dataType, String defaultValue) {
150 		_configTable.addConfiguration(dataType, label, defaultValue);
151 	}
152 
153 	public void addHiddenConfig(String paramName, String defaultValue) {
154 		properties.put(paramName, properties.get(paramName, defaultValue));
155 	}
156 
157 	public String getParameter(String parameterName) {
158 		return properties.get(parameterName);
159 	}
160 
161 	/**
162 	 * Set the parameter value. This method does not notify the configuration change to the {@link TrackConfig}
163 	 * 
164 	 * @param parameterName
165 	 * @param value
166 	 */
167 	public void setParameter(String parameterName, String value) {
168 		properties.put(parameterName, value);
169 		_configTable.setValue(parameterName, value);
170 	}
171 
172 	public String getString(String parameterName, String defaultValue) {
173 		return properties.get(parameterName, defaultValue);
174 	}
175 
176 	public int getInt(String parameterName, int devaultValue) {
177 		return properties.getInt(parameterName, devaultValue);
178 	}
179 
180 	public float getFloat(String parameterName, float defaultValue) {
181 		return properties.getFloat(parameterName, defaultValue);
182 	}
183 
184 	public boolean getBoolean(String parameterName, boolean devaultValue) {
185 		return properties.getBoolean(parameterName, devaultValue);
186 	}
187 
188 	public void notifyConfigChange(String parameterName) {
189 		_track.onChangeTrackConfig(new TrackConfigChangeImpl(this, parameterName));
190 	}
191 
192 	public void saveProperties(CanonicalProperties toSave) {
193 
194 		for (String key : properties.keySet()) {
195 			String nKey = CanonicalProperties.toNaturalName(key);
196 			toSave.put(nKey, properties.get(key));
197 		}
198 	}
199 
200 	public void restoreProperties(CanonicalProperties forLoad) {
201 		properties.putAll(forLoad);
202 	}
203 
204 	public boolean hasProperties() {
205 		return !properties.isEmpty();
206 	}
207 
208 	class ConfigurationTable extends Composite {
209 		private DraggableTable _table = new DraggableTable();
210 		private HashMap<String, Entry> _paramToEntryMap = new HashMap<String, Entry>();
211 
212 		class Entry extends Composite {
213 			class InputChangeListener implements KeyPressHandler, ChangeHandler {
214 
215 				public void onKeyPress(KeyPressEvent e) {
216 					if (e.getCharCode() == KeyboardHandler.KEY_ENTER) {
217 						properties.put(_parameterName, _form.getUserInput());
218 						notifyConfigChange(_parameterName);
219 					}
220 					else {
221 						// resize();
222 					}
223 				}
224 
225 				public void onChange(ChangeEvent e) {
226 					properties.put(_parameterName, _form.getUserInput());
227 					notifyConfigChange(_parameterName);
228 				}
229 
230 			}
231 
232 			private String _parameterName;
233 			private Label _label;
234 			private InputForm _form;
235 			private DockPanel _layoutPanel = new DockPanel();
236 
237 			/**
238 			 * @param label
239 			 * @param form
240 			 */
241 			public Entry(String parameterLabel, String parameterName, InputForm form) {
242 				this._parameterName = parameterName;
243 				this._label = new Label(parameterLabel + ":");
244 				this._form = form;
245 
246 				_label.setStyleName("form-label");
247 				_form.setStyleName("form-field");
248 				// resize();
249 
250 				_layoutPanel.setStyleName("form");
251 				_layoutPanel.setVerticalAlignment(DockPanel.ALIGN_MIDDLE);
252 
253 				_layoutPanel.add(_label, DockPanel.WEST);
254 				_layoutPanel.add(_form, DockPanel.CENTER);
255 
256 				InputChangeListener listener = new InputChangeListener();
257 				_form.addKeyPressHandler(listener);
258 				_form.addChangeHandler(listener);
259 
260 				initWidget(_layoutPanel);
261 			}
262 
263 			public void resize() {
264 				String input = _form.getUserInput();
265 				int widgetWidth = _form.getOffsetWidth();
266 				if (input != null) {
267 					int width = input.length() * 8;
268 					width = width > widgetWidth ? width : widgetWidth;
269 					if (width > 800)
270 						width = 800;
271 					_form.setWidth(width + "px");
272 				}
273 			}
274 
275 			public Label getDragEdge() {
276 				return _label;
277 			}
278 
279 			public InputForm getForm() {
280 				return _form;
281 			}
282 
283 		}
284 
285 		public ConfigurationTable() {
286 			initWidget(_table);
287 		}
288 
289 		public void addConfiguration(DataType dataType, String label, String defaultValue) {
290 			InputForm form = dataType.getInputForm();
291 			String currentValue = getParameter(dataType.getName());
292 			if (currentValue == null)
293 				currentValue = defaultValue;
294 
295 			String cKey = CanonicalProperties.toCanonicalName(dataType.getName());
296 			properties.put(cKey, currentValue);
297 			form.setValue(currentValue);
298 			Entry entry = new Entry(label, cKey, form);
299 			_table.add(entry, entry.getDragEdge());
300 			_paramToEntryMap.put(cKey, entry);
301 
302 			if (form.getOffsetWidth() > _table.getOffsetWidth())
303 				_table.setWidth((form.getOffsetWidth() + 10) + "px");
304 		}
305 
306 		private boolean isValidParameterName(String cKey) {
307 			if (_paramToEntryMap.containsKey(cKey))
308 				return true;
309 			else {
310 				GWT.log("[WARN] no input form for the given parameter name " + cKey + " is found", null);
311 				return false;
312 			}
313 		}
314 
315 		public String getValue(String parameterName) {
316 			String cKey = CanonicalProperties.toCanonicalName(parameterName);
317 			if (!isValidParameterName(cKey))
318 				return "";
319 
320 			Entry entry = _paramToEntryMap.get(cKey);
321 			return entry.getForm().getUserInput();
322 		}
323 
324 		public void setValue(String parameterName, String value) {
325 			String cKey = CanonicalProperties.toCanonicalName(parameterName);
326 
327 			if (!isValidParameterName(cKey))
328 				return;
329 
330 			Entry entry = _paramToEntryMap.get(cKey);
331 			entry.getForm().setValue(value);
332 		}
333 
334 	}
335 
336 }