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  // OldUTGBPropertyTrack.java
20  // Since: 2007/06/20
21  //
22  // $URL$ 
23  // $Author$ ssksn
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib.old;
26  
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  import org.utgenome.gwt.utgb.client.track.Track;
32  import org.utgenome.gwt.utgb.client.track.TrackBase;
33  import org.utgenome.gwt.utgb.client.track.TrackFrame;
34  import org.utgenome.gwt.utgb.client.track.TrackGroup;
35  import org.utgenome.gwt.utgb.client.track.TrackGroupProperty;
36  import org.utgenome.gwt.utgb.client.track.TrackGroupPropertyChange;
37  import org.utgenome.gwt.utgb.client.track.TrackGroupPropertyWriter;
38  import org.utgenome.gwt.utgb.client.track.TrackWindow;
39  
40  import com.google.gwt.event.dom.client.ChangeEvent;
41  import com.google.gwt.event.dom.client.ChangeHandler;
42  import com.google.gwt.event.dom.client.KeyCodes;
43  import com.google.gwt.event.dom.client.KeyPressEvent;
44  import com.google.gwt.event.dom.client.KeyPressHandler;
45  import com.google.gwt.user.client.ui.DockPanel;
46  import com.google.gwt.user.client.ui.FlexTable;
47  import com.google.gwt.user.client.ui.Label;
48  import com.google.gwt.user.client.ui.ListBox;
49  import com.google.gwt.user.client.ui.TextBox;
50  import com.google.gwt.user.client.ui.Widget;
51  
52  /**
53   * @author ssksn
54   * 
55   */
56  public class OldUTGBPropertyTrack extends TrackBase {
57  
58  	public static TrackFactory factory() {
59  		return new TrackFactory() {
60  			public Track newInstance() {
61  				return new OldUTGBPropertyTrack();
62  			}
63  		};
64  	}
65  
66  	private final DockPanel _panel = new DockPanel();
67  
68  	private final Label _label = new Label();
69  
70  	private final FlexTable _tablePanel = new FlexTable();
71  
72  	private final ListBox _speciesListBox = new ListBox();
73  	private final ListBox _revisionListBox = new ListBox();
74  
75  	private final TextBox _targetTextBox = new TextBox();
76  
77  	private final TextBox _startTextBox = new TextBox();
78  	private final TextBox _endTextBox = new TextBox();
79  
80  	private final TextBox _widthTextBox = new TextBox();
81  
82  	private final List<String> _speciesList = new ArrayList<String>();
83  	private final List<List<String>> _species_revisionMapList = new ArrayList<List<String>>();
84  
85  	public OldUTGBPropertyTrack() {
86  		super("Property Viewer");
87  
88  		_panel.add(_tablePanel, DockPanel.CENTER);
89  		_panel.setWidth("100%");
90  
91  		_label.setStyleName("operation-track-label");
92  		_label.setWidth("100%");
93  		_label.setHorizontalAlignment(Label.ALIGN_CENTER);
94  
95  		_tablePanel.setStyleName("parameter-track");
96  		_tablePanel.setWidth("100%");
97  
98  		{
99  			_speciesList.add("medaka");
100 			_speciesList.add("medaka-HNI");
101 
102 			{
103 				final List<String> medakaRevisionList = new ArrayList<String>();
104 				medakaRevisionList.add("version1.0");
105 				medakaRevisionList.add("version0.9");
106 				medakaRevisionList.add("200506");
107 				medakaRevisionList.add("200406");
108 
109 				_species_revisionMapList.add(medakaRevisionList);
110 			}
111 			{
112 				final List<String> medakaHNIRevisionList = new ArrayList<String>();
113 				medakaHNIRevisionList.add("version1.0");
114 
115 				_species_revisionMapList.add(medakaHNIRevisionList);
116 			}
117 		}
118 
119 		_tablePanel.setText(0, 0, OldUTGBProperty.SPECIES.toUpperCase());
120 		_tablePanel.setWidget(0, 1, _speciesListBox);
121 		{
122 			setItems(_speciesListBox, _speciesList);
123 
124 			_speciesListBox.setVisibleItemCount(1);
125 
126 			_speciesListBox.addChangeHandler(new ChangeHandler() {
127 				public void onChange(ChangeEvent e) {
128 					final TrackGroupPropertyWriter propertyWriter = _trackGroup.getPropertyWriter();
129 
130 					final int selectedSpeciesIndex = _speciesListBox.getSelectedIndex();
131 					final String selectedValue = _speciesListBox.getValue(selectedSpeciesIndex);
132 
133 					if (selectedValue.length() > 0) {
134 						propertyWriter.setProperty(OldUTGBProperty.SPECIES, selectedValue);
135 
136 						removeAllItems(_revisionListBox);
137 
138 						final List<String> revisionList = _species_revisionMapList.get(selectedSpeciesIndex);
139 						setItems(_revisionListBox, revisionList);
140 					}
141 				}
142 
143 			});
144 		}
145 
146 		_tablePanel.setText(0, 2, OldUTGBProperty.REVISION.toUpperCase());
147 		_tablePanel.setWidget(0, 3, _revisionListBox);
148 		{
149 			final List<String> revisionList = _species_revisionMapList.get(0);
150 			setItems(_revisionListBox, revisionList);
151 
152 			_revisionListBox.setVisibleItemCount(1);
153 			_revisionListBox.setSelectedIndex(1);
154 
155 			_revisionListBox.addChangeHandler(new ChangeHandler() {
156 				public void onChange(ChangeEvent e) {
157 					final TrackGroupPropertyWriter propertyWriter = _trackGroup.getPropertyWriter();
158 
159 					final int selectedSpeciesIndex = _revisionListBox.getSelectedIndex();
160 					final String selectedValue = _revisionListBox.getValue(selectedSpeciesIndex);
161 
162 					if (selectedValue.length() > 0)
163 						propertyWriter.setProperty(OldUTGBProperty.REVISION, _revisionListBox.getValue(selectedSpeciesIndex));
164 				}
165 			});
166 		}
167 
168 		_tablePanel.setText(0, 4, OldUTGBProperty.TARGET.toUpperCase());
169 		_tablePanel.setWidget(0, 5, _targetTextBox);
170 		{
171 			_targetTextBox.addKeyPressHandler(new KeyPressHandler() {
172 
173 				public void onKeyPress(KeyPressEvent e) {
174 					char keyCode = e.getCharCode();
175 					if (keyCode == KeyCodes.KEY_ENTER || keyCode == KeyCodes.KEY_TAB) {
176 						final TrackGroupPropertyWriter propertyWriter = _trackGroup.getPropertyWriter();
177 						propertyWriter.setProperty(OldUTGBProperty.TARGET, _targetTextBox.getText());
178 					}
179 
180 				}
181 			});
182 		}
183 
184 		_tablePanel.setText(1, 0, "START");
185 		_tablePanel.setWidget(1, 1, _startTextBox);
186 		{
187 			_startTextBox.addKeyPressHandler(new KeyPressHandler() {
188 				public void onKeyPress(KeyPressEvent e) {
189 					char keyCode = e.getCharCode();
190 					if (keyCode == KeyCodes.KEY_ENTER || keyCode == KeyCodes.KEY_TAB) {
191 						final TrackWindow trackWindow = _trackGroup.getTrackWindow();
192 						_trackGroup.setTrackWindowLocation(Integer.parseInt(_startTextBox.getText()), trackWindow.getEndOnGenome());
193 					}
194 
195 				}
196 			});
197 		}
198 
199 		_tablePanel.setText(1, 2, "END");
200 		_tablePanel.setWidget(1, 3, _endTextBox);
201 		{
202 			_endTextBox.addKeyPressHandler(new KeyPressHandler() {
203 				public void onKeyPress(KeyPressEvent e) {
204 					char keyCode = e.getCharCode();
205 					if (keyCode == KeyCodes.KEY_ENTER || keyCode == KeyCodes.KEY_TAB) {
206 						final TrackWindow trackWindow = _trackGroup.getTrackWindow();
207 						_trackGroup.setTrackWindowLocation(trackWindow.getStartOnGenome(), Integer.parseInt(_endTextBox.getText()));
208 					}
209 
210 				}
211 			});
212 		}
213 
214 		_tablePanel.setText(1, 4, "WIDTH");
215 		_tablePanel.setWidget(1, 5, _widthTextBox);
216 		{
217 			_widthTextBox.setEnabled(false);
218 		}
219 	}
220 
221 	private static final void setItems(final ListBox listBox, final List<String> itemList) {
222 		final Iterator<String> speciesIterator = itemList.iterator();
223 		while (speciesIterator.hasNext()) {
224 			final String str = speciesIterator.next();
225 			listBox.addItem(str, str);
226 		}
227 	}
228 
229 	private static final void removeAllItems(final ListBox listBox) {
230 		while (listBox.getItemCount() > 0) {
231 			listBox.removeItem(0);
232 		}
233 	}
234 
235 	public Widget getWidget() {
236 		return _panel;
237 	}
238 
239 	public void onChangeTrackGroupProperty(TrackGroupPropertyChange change) {
240 		String currentSelectedSpecies = _speciesListBox.getItemText(_speciesListBox.getSelectedIndex());
241 		final String newSpecies = change.getProperty(OldUTGBProperty.SPECIES);
242 		if (change.contains(OldUTGBProperty.SPECIES) && !(newSpecies.equals(currentSelectedSpecies))) {
243 			final int _index = _speciesList.indexOf(newSpecies);
244 			if (_index == -1) {
245 				removeAllItems(_speciesListBox);
246 				setItems(_speciesListBox, _speciesList);
247 				_speciesListBox.addItem("(" + newSpecies + ")", "");
248 				_speciesListBox.setSelectedIndex(_speciesList.size());
249 
250 				removeAllItems(_revisionListBox);
251 				_revisionListBox.setEnabled(false);
252 				_targetTextBox.setEnabled(false);
253 				_startTextBox.setEnabled(false);
254 				_endTextBox.setEnabled(false);
255 
256 				writeMessage("This species/revision setting is not valid for the track. Please select a valid setting.");
257 				_frame.onUpdateTrackWidget();
258 			}
259 			else {
260 				removeAllItems(_speciesListBox);
261 				setItems(_speciesListBox, _speciesList);
262 				_speciesListBox.setSelectedIndex(_index);
263 
264 				final List<String> _revisionList = _species_revisionMapList.get(_index);
265 				removeAllItems(_revisionListBox);
266 				setItems(_revisionListBox, _revisionList);
267 
268 				_revisionListBox.setEnabled(true);
269 				_targetTextBox.setEnabled(true);
270 				_startTextBox.setEnabled(true);
271 				_endTextBox.setEnabled(true);
272 
273 				eraseMassege();
274 				_frame.onUpdateTrackWidget();
275 			}
276 		}
277 
278 		if (change.contains(OldUTGBProperty.REVISION) && _revisionListBox.isEnabled()) {
279 			final String newRevision = change.getProperty(OldUTGBProperty.REVISION);
280 			final int selectedIndex = _speciesListBox.getSelectedIndex();
281 
282 			final List<String> _revisionList = _species_revisionMapList.get(selectedIndex);
283 
284 			final int _index = _revisionList.indexOf(newRevision);
285 			if (_index == -1) {
286 				removeAllItems(_revisionListBox);
287 				setItems(_revisionListBox, _revisionList);
288 				_revisionListBox.addItem("(" + newRevision + ")", "");
289 				_revisionListBox.setSelectedIndex(_revisionList.size());
290 
291 				_revisionListBox.setEnabled(false);
292 				_targetTextBox.setEnabled(false);
293 				_startTextBox.setEnabled(false);
294 				_endTextBox.setEnabled(false);
295 
296 				writeMessage("This species/revision setting is not valid for the track. Please select a valid setting.");
297 				_frame.onUpdateTrackWidget();
298 			}
299 			else {
300 				removeAllItems(_revisionListBox);
301 				setItems(_revisionListBox, _revisionList);
302 				_revisionListBox.setSelectedIndex(_index);
303 
304 				_revisionListBox.setEnabled(true);
305 				_targetTextBox.setEnabled(true);
306 				_startTextBox.setEnabled(true);
307 				_endTextBox.setEnabled(true);
308 
309 				eraseMassege();
310 				_frame.onUpdateTrackWidget();
311 			}
312 		}
313 
314 		if (change.contains(OldUTGBProperty.TARGET)) {
315 			_targetTextBox.setText(change.getProperty(OldUTGBProperty.TARGET));
316 		}
317 	}
318 
319 	public void onChangeTrackWindow(TrackWindow newWindow) {
320 		_startTextBox.setText(Long.toString(newWindow.getStartOnGenome()));
321 		_endTextBox.setText(Long.toString(newWindow.getEndOnGenome()));
322 		_widthTextBox.setText(Integer.toString(newWindow.getPixelWidth()));
323 	}
324 
325 	public void setUp(TrackFrame trackFrame, TrackGroup group) {
326 		trackFrame.pack();
327 		final TrackWindow trackWindow = group.getTrackWindow();
328 
329 		_widthTextBox.setText(Integer.toString(trackWindow.getPixelWidth()));
330 		_startTextBox.setText(Long.toString(trackWindow.getStartOnGenome()));
331 		_endTextBox.setText(Long.toString(trackWindow.getEndOnGenome()));
332 
333 		final TrackGroupProperty propertyReader = group.getPropertyReader();
334 
335 		_targetTextBox.setText(propertyReader.getProperty(OldUTGBProperty.TARGET));
336 	}
337 
338 	private final void writeMessage(final String message) {
339 		eraseMassege();
340 
341 		_panel.add(_label, DockPanel.NORTH);
342 		_label.setText(message);
343 	}
344 
345 	private final void eraseMassege() {
346 		final int index = _panel.getWidgetIndex(_label);
347 		if (index != -1) {
348 			_panel.remove(_label);
349 		}
350 	}
351 
352 }