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  // OperationTrack.java
20  // Since: 2007/06/11
21  //
22  // This track supports operation xml.
23  //
24  // $URL$ 
25  // $Author$ ssksn
26  //--------------------------------------
27  package org.utgenome.gwt.utgb.client.track.lib.old;
28  
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import org.utgenome.gwt.utgb.client.track.Design;
33  import org.utgenome.gwt.utgb.client.track.Track;
34  import org.utgenome.gwt.utgb.client.track.TrackBase;
35  import org.utgenome.gwt.utgb.client.track.TrackFrame;
36  import org.utgenome.gwt.utgb.client.track.TrackGroup;
37  import org.utgenome.gwt.utgb.client.track.TrackGroupPropertyChange;
38  import org.utgenome.gwt.utgb.client.track.TrackGroupPropertyWriter;
39  import org.utgenome.gwt.utgb.client.track.TrackInfo;
40  import org.utgenome.gwt.utgb.client.track.TrackWindow;
41  import org.utgenome.gwt.utgb.client.track.lib.ParameterTrack;
42  import org.utgenome.gwt.utgb.client.track.operation.OperationParser;
43  
44  import com.google.gwt.core.client.GWT;
45  import com.google.gwt.event.dom.client.ClickEvent;
46  import com.google.gwt.event.dom.client.ClickHandler;
47  import com.google.gwt.event.dom.client.ErrorEvent;
48  import com.google.gwt.event.dom.client.ErrorHandler;
49  import com.google.gwt.event.dom.client.LoadEvent;
50  import com.google.gwt.event.dom.client.LoadHandler;
51  import com.google.gwt.user.client.rpc.AsyncCallback;
52  import com.google.gwt.user.client.ui.AbsolutePanel;
53  import com.google.gwt.user.client.ui.Anchor;
54  import com.google.gwt.user.client.ui.DockPanel;
55  import com.google.gwt.user.client.ui.Image;
56  import com.google.gwt.user.client.ui.Label;
57  import com.google.gwt.user.client.ui.VerticalPanel;
58  import com.google.gwt.user.client.ui.Widget;
59  import com.google.gwt.xml.client.Document;
60  import com.google.gwt.xml.client.NamedNodeMap;
61  import com.google.gwt.xml.client.Node;
62  import com.google.gwt.xml.client.NodeList;
63  import com.google.gwt.xml.client.XMLParser;
64  
65  public class OperationTrack extends TrackBase {
66  	public static TrackFactory factory() {
67  		return new TrackFactory() {
68  			public Track newInstance() {
69  				return new OperationTrack();
70  			}
71  		};
72  	}
73  
74  	public static TrackFactory factory(final String descriptionXML) {
75  		return new TrackFactory() {
76  			public Track newInstance() {
77  				final OperationTrack track = new OperationTrack();
78  				track.setDescriptionXML(descriptionXML);
79  				return track;
80  			}
81  		};
82  	}
83  
84  	final VerticalPanel _panel = new VerticalPanel();
85  
86  	String _descriptionXMLURL;
87  
88  	String species = "medaka";
89  	String build = "version1.0";
90  	String target = "scaffold1";
91  	String startIndex = "1";
92  	String endIndex = "1000000";
93  
94  	String graphicLayerURL = "";
95  	String indexLayerURL = "";
96  	String operationLayerURL = "";
97  
98  	final AbsolutePanel _absolutePanel = new AbsolutePanel();
99  	final Image graphicPanel = new Image();
100 
101 	final Anchor parameterButton = new Anchor();
102 	final Label _label = new Label();
103 
104 	public OperationTrack() {
105 		super("Operation Track");
106 		_panel.setWidth("100%");
107 
108 		final DockPanel infoPanel = new DockPanel();
109 		infoPanel.setStyleName("operation-track-label");
110 		infoPanel.setWidth("100%");
111 
112 		parameterButton.setText("[show parameters]");
113 		parameterButton.addClickHandler(new ClickHandler() {
114 			public void onClick(ClickEvent e) {
115 				final int frameIndex = getTrackGroup().getTrackIndex(OperationTrack.this);
116 				final ParameterTrack _parameterTrack = getParameterTrack();
117 				getTrackGroup().insertTrack(_parameterTrack, frameIndex);
118 			}
119 
120 		});
121 		infoPanel.add(parameterButton, DockPanel.WEST);
122 		infoPanel.add(_label, DockPanel.EAST);
123 
124 		_panel.add(infoPanel);
125 		_panel.add(_absolutePanel);
126 		_absolutePanel.add(graphicPanel);
127 
128 	}
129 
130 	public void setStartIndex(int start) {
131 		startIndex = Integer.toString(start);
132 	}
133 
134 	public void setBuild(String build) {
135 		this.build = build;
136 	}
137 
138 	private void setParameters() {
139 
140 		if (getTrackGroup() == null)
141 			return;
142 
143 		final TrackGroupPropertyWriter propertyWriter = getTrackGroup().getPropertyWriter();
144 
145 		final Map<String, String> properties = new HashMap<String, String>();
146 
147 		properties.put("descURL", _descriptionXMLURL);
148 		properties.put("species", species);
149 		properties.put("build", build);
150 		properties.put("target", target);
151 		properties.put("startIndex", startIndex);
152 		properties.put("endIndex", endIndex);
153 
154 		properties.put("graphicURL", graphicLayerURL);
155 		properties.put("indexURL", indexLayerURL);
156 		properties.put("operationURL", operationLayerURL);
157 
158 		propertyWriter.setProperty(properties);
159 	}
160 
161 	public ParameterTrack getParameterTrack() {
162 		final ParameterTrack parameterTrack = new ParameterTrack();
163 
164 		parameterTrack.addParameter("descURL", "description XML URL");
165 		parameterTrack.addParameter("species", "species");
166 		parameterTrack.addParameter("build", "build");
167 		parameterTrack.addParameter("target", "target");
168 		parameterTrack.addParameter("startIndex", "startIndex");
169 		parameterTrack.addParameter("endIndex", "endIndex");
170 
171 		parameterTrack.addParameter("graphicURL", "graphic layer URL");
172 		parameterTrack.addParameter("indexURL", "index layer URL");
173 		parameterTrack.addParameter("operationURL", "operation layer URL");
174 
175 		return parameterTrack;
176 	}
177 
178 	public void setDescriptionXML(final String descriptionXMLURL) {
179 		this._descriptionXMLURL = descriptionXMLURL;
180 
181 		parseDescriptionXML(_descriptionXMLURL);
182 	}
183 
184 	private void parseDescriptionXML(final String _descriptionXMLURL) {
185 		getBrowserService().getHTTPContent(_descriptionXMLURL, new AsyncCallback<String>() {
186 			public void onFailure(Throwable caught) {
187 				GWT.log("cannot retrieve: " + _descriptionXMLURL, caught);
188 			}
189 
190 			public void onSuccess(String text) {
191 				final Document dom = XMLParser.parse(text);
192 
193 				{
194 					final NodeList topLevelTrackNodeList = dom.getElementsByTagName("track");
195 					if (topLevelTrackNodeList.getLength() != 1) {
196 						throw new AssertionError();
197 					}
198 
199 					final Node topLevelTrackNode = topLevelTrackNodeList.item(0);
200 
201 					TrackInfo info = getTrackInfo();
202 					info.setTrackName(Utilities.getAttributeValue(topLevelTrackNode, "name"));
203 					info.setDescription(Utilities.getAttributeValue(topLevelTrackNode, "comment", ""));
204 					final String trackDescriptionURL = Utilities.getAttributeValue(topLevelTrackNode, "description_url");
205 					if (trackDescriptionURL != null) {
206 						info.setLinkURL(trackDescriptionURL);
207 					}
208 				}
209 
210 				final NodeList layerNodes = dom.getElementsByTagName("layer");
211 				for (int i = 0; i < layerNodes.getLength(); i++) {
212 					final Node layerNode = layerNodes.item(i);
213 
214 					final NamedNodeMap attributes = layerNode.getAttributes();
215 					final Node kindNode = attributes.getNamedItem("kind");
216 					final Node urlNode = attributes.getNamedItem("url");
217 
218 					final String kindValue = kindNode.getNodeValue();
219 					final String urlValue = urlNode.getNodeValue();
220 
221 					if (kindValue.equals("graphic")) {
222 						graphicLayerURL = urlValue;
223 					}
224 					if (kindValue.equals("index")) {
225 						indexLayerURL = urlValue;
226 					}
227 					if (kindValue.equals("operation")) {
228 						operationLayerURL = urlValue;
229 					}
230 				}
231 				{
232 					if (getTrackGroup() != null) {
233 						final TrackGroupPropertyWriter propertyWriter = getTrackGroup().getPropertyWriter();
234 
235 						HashMap<String, String> newProperty = new HashMap<String, String>();
236 						newProperty.put("graphicURL", graphicLayerURL);
237 						newProperty.put("indexURL", indexLayerURL);
238 						newProperty.put("operationURL", operationLayerURL);
239 						propertyWriter.setProperty(newProperty);
240 					}
241 				}
242 				// draw();
243 			}
244 		});
245 	}
246 
247 	public int getDefaultWindowHeight() {
248 		return 200;
249 	}
250 
251 	public Widget getWidget() {
252 		return _panel;
253 	}
254 
255 	public void onChangeTrackWindow(final TrackWindow newWindow) {
256 		// draw();
257 	}
258 
259 	public void draw() {
260 
261 		{ // remove an old image panel and operationAreas.
262 			while (_absolutePanel.getWidgetCount() > 0) {
263 				_absolutePanel.remove(0);
264 			}
265 			_absolutePanel.add(graphicPanel); // re-add image panel
266 		}
267 		{ // update image panel
268 			final String graphicURL = getGraphicURL();
269 			if (graphicURL != null) {
270 				Image.prefetch(graphicURL);
271 				graphicPanel.setUrl(graphicURL);
272 			}
273 		}
274 		{ // process operation xml
275 			parseOperationXML();
276 		}
277 
278 	}
279 
280 	private String getOperationURL() {
281 		if (operationLayerURL == null)
282 			return null;
283 		String url = operationLayerURL;
284 		if (url.indexOf('?') == -1)
285 			url += '?';
286 		else if ((url.indexOf('?') != (url.length() - 1)) && (url.indexOf('&') != (url.length() - 1)))
287 			url += '&';
288 
289 		final int windowWidth = getTrackGroup().getPropertyReader().getTrackWindow().getPixelWidth();
290 
291 		return url
292 				+ join(new String[] { "species", "revision", "target", "start", "end", "width" }, new String[] { species, build, target, startIndex, endIndex,
293 						Integer.toString(windowWidth) });
294 	}
295 
296 	private String getGraphicURL() {
297 		if (graphicLayerURL == null)
298 			return null;
299 		String url = graphicLayerURL;
300 		if (url.indexOf('?') == -1)
301 			url += '?';
302 		else if ((url.indexOf('?') != (url.length() - 1)) && (url.indexOf('&') != (url.length() - 1)))
303 			url += '&';
304 
305 		final int windowWidth = getTrackGroup().getPropertyReader().getTrackWindow().getPixelWidth();
306 
307 		return url
308 				+ join(new String[] { "species", "revision", "target", "start", "end", "width" }, new String[] { species, build, target, startIndex, endIndex,
309 						Integer.toString(windowWidth) });
310 	}
311 
312 	public static String join(String[] keys, String[] value) {
313 		String result = new String();
314 		for (int i = 0; i < keys.length; i++) {
315 			result += keys[i] + "=" + value[i];
316 			if (i != (keys.length - 1))
317 				result += "&";
318 		}
319 		return result;
320 	}
321 
322 	public final void parseOperationXML() {
323 		final String operationURL = getOperationURL();
324 
325 		if (operationURL == null)
326 			return;
327 		if (!operationLayerURL.startsWith("http"))
328 			return;
329 
330 		_label.setText("Now Operation XML parsing ...");
331 
332 		getBrowserService().getHTTPContent(operationURL, new AsyncCallback<String>() {
333 			public void onFailure(Throwable caught) {
334 			}
335 
336 			public void onSuccess(String text) {
337 				if (text.length() <= 0) {
338 					_label.setText("");
339 					return;
340 				}
341 				{
342 					final Document dom = XMLParser.parse(text);
343 					final OperationParser parser = OperationParser.getParser();
344 					parser.parse(dom, _absolutePanel, OperationTrack.this);
345 					_label.setText("");
346 				}
347 			}
348 		});
349 	}
350 
351 	public void onChangeTrackGroupProperty(TrackGroupPropertyChange change) {
352 		boolean drawFlag = false;
353 
354 		{
355 			{
356 				final String key = "descURL";
357 				if (change.contains(key)) {
358 					_descriptionXMLURL = change.getProperty(key);
359 					drawFlag = true;
360 				}
361 			}
362 			{
363 				final String key = "species";
364 				if (change.contains(key)) {
365 					species = change.getProperty(key);
366 					drawFlag = true;
367 				}
368 			}
369 			{
370 				final String key = "build";
371 				if (change.contains(key)) {
372 					build = change.getProperty(key);
373 					drawFlag = true;
374 				}
375 			}
376 			{
377 				final String key = "target";
378 				if (change.contains(key)) {
379 					target = change.getProperty(key);
380 					drawFlag = true;
381 				}
382 			}
383 			{
384 				final String key = "startIndex";
385 				if (change.contains(key)) {
386 					startIndex = change.getProperty(key);
387 					drawFlag = true;
388 				}
389 			}
390 			{
391 				final String key = "endIndex";
392 				if (change.contains(key)) {
393 					endIndex = change.getProperty(key);
394 					drawFlag = true;
395 				}
396 			}
397 
398 			{
399 				final String key = "graphicURL";
400 				if (change.contains(key)) {
401 					graphicLayerURL = change.getProperty(key);
402 					drawFlag = true;
403 				}
404 			}
405 			{
406 				final String key = "indexURL";
407 				if (change.contains(key)) {
408 					indexLayerURL = change.getProperty(key);
409 					drawFlag = true;
410 				}
411 			}
412 			{
413 				final String key = "operationURL";
414 				if (change.contains(key)) {
415 					operationLayerURL = change.getProperty(key);
416 					drawFlag = true;
417 				}
418 			}
419 		}
420 		if (drawFlag)
421 			draw();
422 	}
423 
424 	public void setUp(TrackFrame trackFrame, TrackGroup group) {
425 		trackFrame.pack();
426 		setParameters();
427 		graphicPanel.addLoadHandler(new LoadHandler() {
428 
429 			public void onLoad(LoadEvent arg0) {
430 				getFrame().onUpdateTrackWidget();
431 
432 			}
433 		});
434 
435 		graphicPanel.addErrorHandler(new ErrorHandler() {
436 
437 			public void onError(ErrorEvent e) {
438 				graphicPanel.setUrl(Design.IMAGE_NOT_AVAILABLE);
439 			}
440 		});
441 
442 	}
443 
444 }