View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2009 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  // utgb-core Project
18  //
19  // GenomeTrack.java
20  // Since: Feb 17, 2009
21  //
22  // $URL: http://svn.utgenome.org/utgb/trunk/utgb/utgb-core/src/main/java/org/utgenome/gwt/utgb/client/track/lib/GenomeTrack.java $ 
23  // $Author: leo $
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib;
26  
27  import org.utgenome.gwt.utgb.client.bio.Coordinate;
28  import org.utgenome.gwt.utgb.client.db.datatype.StringType;
29  import org.utgenome.gwt.utgb.client.track.Design;
30  import org.utgenome.gwt.utgb.client.track.Track;
31  import org.utgenome.gwt.utgb.client.track.TrackBase;
32  import org.utgenome.gwt.utgb.client.track.TrackConfig;
33  import org.utgenome.gwt.utgb.client.track.TrackConfigChange;
34  import org.utgenome.gwt.utgb.client.track.TrackFrame;
35  import org.utgenome.gwt.utgb.client.track.TrackGroup;
36  import org.utgenome.gwt.utgb.client.track.TrackGroupPropertyChange;
37  import org.utgenome.gwt.utgb.client.track.TrackWindow;
38  import org.utgenome.gwt.utgb.client.track.UTGBProperty;
39  import org.utgenome.gwt.utgb.client.util.CanonicalProperties;
40  import org.utgenome.gwt.utgb.client.util.Properties;
41  import org.utgenome.gwt.widget.client.Style;
42  
43  import com.google.gwt.event.dom.client.ErrorEvent;
44  import com.google.gwt.event.dom.client.ErrorHandler;
45  import com.google.gwt.event.dom.client.LoadEvent;
46  import com.google.gwt.event.dom.client.LoadHandler;
47  import com.google.gwt.user.client.DOM;
48  import com.google.gwt.user.client.Event;
49  import com.google.gwt.user.client.ui.FlexTable;
50  import com.google.gwt.user.client.ui.Frame;
51  import com.google.gwt.user.client.ui.Image;
52  import com.google.gwt.user.client.ui.Widget;
53  
54  /**
55   * GenomeTrack is for visualizing data that can be located on genome sequences.
56   * 
57   * @author yoshimura
58   * 
59   */
60  
61  public class GenomeTrackTextTest extends TrackBase {
62  
63  	private String type = "image";
64  	private String trackBaseURL;
65  	private int leftMargin = 0;
66  	private FlexTable layoutPanel = new FlexTable();
67  	private Image trackImage = new Image();
68  	private Frame frame = new Frame();
69  	private boolean isWidgetReady = false;
70  
71  	private TrackConfig config = new TrackConfig(this);
72  
73  	public static TrackFactory factory() {
74  		return new TrackFactory() {
75  			public Track newInstance() {
76  				return new GenomeTrackTextTest();
77  			}
78  		};
79  	}
80  
81  	private class ContentFrame extends Frame {
82  		@Override
83  		protected void onLoad() {
84  		}
85  
86  		@Override
87  		public void onBrowserEvent(Event event) {
88  
89  			if (event.getTypeInt() == Event.ONCHANGE) {
90  				TrackFrame frame = getFrame();
91  				if (frame != null) {
92  					frame.loadingDone();
93  				}
94  			}
95  		}
96  	}
97  
98  	public GenomeTrackTextTest() {
99  		super("Genome Track");
100 
101 		Style.fullWidth(layoutPanel);
102 		layoutPanel.setCellPadding(0);
103 		layoutPanel.setCellSpacing(0);
104 		Style.fontSize(layoutPanel, 0);
105 
106 		Style.fullWidth(frame);
107 		Style.margin(frame, 0);
108 		Style.padding(frame, 0);
109 		DOM.setElementProperty(frame.getElement(), "align", "left");
110 		DOM.setElementPropertyInt(frame.getElement(), "marginHight", 0);
111 		DOM.setElementPropertyInt(frame.getElement(), "marginWidth", 0);
112 		DOM.setElementPropertyInt(frame.getElement(), "frameBorder", 0);
113 
114 	}
115 
116 	public Widget getWidget() {
117 		return layoutPanel;
118 	}
119 
120 	@Override
121 	public void draw() {
122 
123 		layoutPanel.getCellFormatter().setWidth(0, 0, leftMargin + "px");
124 
125 		String trackURL = getTrackURL();
126 		if (type.equals("frame")) {
127 			if (!isWidgetReady) {
128 				layoutPanel.setWidget(0, 1, frame);
129 				isWidgetReady = true;
130 			}
131 			frame.setUrl(trackURL);
132 		}
133 		else if (type.equals("image")) {
134 			// use image 
135 			if (!isWidgetReady) {
136 				layoutPanel.setWidget(0, 1, trackImage);
137 				isWidgetReady = true;
138 			}
139 			trackImage.setUrl(getTrackURL());
140 			getFrame().setNowLoading();
141 		}
142 	}
143 
144 	private String getTrackURL() {
145 		Coordinate c = getCoordinate();
146 
147 		Properties p = new Properties();
148 		TrackWindow w = getTrackGroup().getTrackWindow();
149 		p.add("start", w.getStartOnGenome());
150 		p.add("end", w.getEndOnGenome());
151 		p.add("width", w.getPixelWidth() - leftMargin);
152 
153 		return c.getTrackURL(trackBaseURL, p);
154 	}
155 
156 	@Override
157 	public TrackConfig getConfig() {
158 		return config;
159 	}
160 
161 	@Override
162 	public void setUp(TrackFrame trackFrame, TrackGroup group) {
163 
164 		trackImage.addLoadHandler(new LoadHandler() {
165 			public void onLoad(LoadEvent arg0) {
166 				getFrame().onUpdateTrackWidget();
167 				getFrame().loadingDone();
168 			}
169 		});
170 		trackImage.addErrorHandler(new ErrorHandler() {
171 			public void onError(ErrorEvent arg0) {
172 				trackImage.setUrl(Design.IMAGE_NOT_AVAILABLE);
173 				getFrame().loadingDone();
174 			}
175 		});
176 
177 		int height = this.getDefaultWindowHeight();
178 		if (height > 0) {
179 			frame.setHeight(height + "px");
180 			layoutPanel.setHeight(height + "px");
181 		}
182 
183 		// set up the configuration panel
184 		config.addConfig("Track Base URL", new StringType("baseURL"), trackBaseURL);
185 
186 	}
187 
188 	@Override
189 	public void onChangeTrackGroupProperty(TrackGroupPropertyChange change) {
190 
191 		if (change.containsOneOf(UTGBProperty.coordinateParameters)) {
192 			draw();
193 		}
194 	}
195 
196 	@Override
197 	public void onChangeTrackWindow(TrackWindow newWindow) {
198 		draw();
199 	}
200 
201 	@Override
202 	public void onChangeTrackConfig(TrackConfigChange change) {
203 		if (change.contains("baseURL")) {
204 			trackBaseURL = change.getValue("baseURL");
205 			draw();
206 		}
207 	}
208 
209 	@Override
210 	public void saveProperties(CanonicalProperties saveData) {
211 		saveData.add("type", type);
212 		saveData.add("trackBaseURL", trackBaseURL);
213 		saveData.add("leftMargin", leftMargin);
214 
215 	}
216 
217 	@Override
218 	public void restoreProperties(CanonicalProperties properties) {
219 		trackBaseURL = properties.get("trackBaseURL", trackBaseURL);
220 		leftMargin = properties.getInt("leftMargin", leftMargin);
221 		type = properties.get("type", type);
222 	}
223 
224 }