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  // DBSelectTrack.java
20  // Since: Oct 15, 2009
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib;
26  
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.utgenome.gwt.utgb.client.bean.DatabaseEntry;
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.TrackConfig;
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.UTGBProperty;
38  import org.utgenome.gwt.utgb.client.ui.FormLabel;
39  import org.utgenome.gwt.utgb.client.util.CanonicalProperties;
40  import org.utgenome.gwt.utgb.client.util.JSONUtil;
41  import org.utgenome.gwt.widget.client.Style;
42  
43  import com.google.gwt.core.client.GWT;
44  import com.google.gwt.event.dom.client.ChangeEvent;
45  import com.google.gwt.event.dom.client.ChangeHandler;
46  import com.google.gwt.event.dom.client.ClickEvent;
47  import com.google.gwt.event.dom.client.ClickHandler;
48  import com.google.gwt.user.client.rpc.AsyncCallback;
49  import com.google.gwt.user.client.ui.FlexTable;
50  import com.google.gwt.user.client.ui.HorizontalPanel;
51  import com.google.gwt.user.client.ui.Label;
52  import com.google.gwt.user.client.ui.ListBox;
53  import com.google.gwt.user.client.ui.Widget;
54  
55  public class DBSelectTrack extends TrackBase {
56  
57  	public static TrackFactory factory() {
58  		return new TrackFactory() {
59  			@Override
60  			public Track newInstance() {
61  				return new DBSelectTrack();
62  			}
63  		};
64  	}
65  
66  	TrackConfig config = new TrackConfig(this);
67  
68  	//VerticalPanel globalLayout = new VerticalPanel();
69  	FlexTable layout = new FlexTable();
70  	ListBox groupSelector = new ListBox();
71  	ListBox dbSelector = new ListBox();
72  	HorizontalPanel favoritePanel = new HorizontalPanel();
73  
74  	private List<DatabaseEntry> currentEntry;
75  	private List<String> favoriteGroup = new ArrayList<String>();
76  
77  	private String parentDBGroup = "org/utgenome";
78  
79  	public DBSelectTrack() {
80  		super("DB Selector");
81  
82  		layout.setWidget(0, 0, new FormLabel("DB Group: "));
83  		layout.setWidget(0, 1, groupSelector);
84  		layout.setWidget(1, 0, new FormLabel("DB Name: "));
85  		layout.setWidget(1, 1, dbSelector);
86  		layout.setWidget(2, 1, favoritePanel);
87  
88  		//globalLayout.add(layout);
89  		//globalLayout.add(favoritePanel);
90  
91  		groupSelector.addChangeHandler(new ChangeHandler() {
92  			public void onChange(ChangeEvent e) {
93  				selectDBGroup(groupSelector.getValue(groupSelector.getSelectedIndex()));
94  			}
95  		});
96  
97  		dbSelector.addChangeHandler(new ChangeHandler() {
98  			public void onChange(ChangeEvent e) {
99  				selectDBName(dbSelector.getValue(dbSelector.getSelectedIndex()));
100 			}
101 		});
102 	}
103 
104 	private void selectDBGroup(String dbGroup) {
105 		if (dbGroup != null) {
106 			for (int i = 0; i < groupSelector.getItemCount(); ++i) {
107 				String itemText = groupSelector.getItemText(i);
108 				if (itemText != null && itemText.equals(dbGroup)) {
109 					groupSelector.setSelectedIndex(i);
110 					break;
111 				}
112 			}
113 		}
114 		else
115 			groupSelector.setSelectedIndex(0);
116 
117 		setTrackGroupProperty(UTGBProperty.DB_GROUP, groupSelector.getValue(groupSelector.getSelectedIndex()));
118 	}
119 
120 	private void selectDBName(String dbName) {
121 
122 		if (dbName != null) {
123 			for (int i = 1; i < dbSelector.getItemCount(); ++i) {
124 				String itemText = dbSelector.getItemText(i);
125 				if (itemText != null && itemText.equals(dbName)) {
126 					dbSelector.setSelectedIndex(i);
127 					break;
128 				}
129 			}
130 
131 		}
132 		else
133 			dbSelector.setSelectedIndex(0);
134 
135 		if (dbSelector.getSelectedIndex() == 0)
136 			return;
137 
138 		String selectedDBName = dbSelector.getValue(dbSelector.getSelectedIndex());
139 
140 		for (DatabaseEntry each : currentEntry) {
141 			if (each.path.equals(selectedDBName)) {
142 				if (each.isFile())
143 					setTrackGroupProperty(UTGBProperty.DB_NAME, selectedDBName);
144 				else
145 					setTrackGroupProperty(UTGBProperty.DB_GROUP, DatabaseEntry.newFolder(getTrackGroupProperty(UTGBProperty.DB_GROUP)).dbPath(selectedDBName));
146 			}
147 		}
148 
149 	}
150 
151 	public Widget getWidget() {
152 		return layout;
153 	}
154 
155 	@Override
156 	public TrackConfig getConfig() {
157 		return config;
158 	}
159 
160 	@Override
161 	public void setUp(TrackFrame trackFrame, TrackGroup group) {
162 
163 		//config.addConfigParameter("Parent DB Group", new StringType(PARENT_DB_GROUP), getTrackGroupProperty(PARENT_DB_GROUP));
164 
165 		//		if (getTrackGroupProperty(PARENT_DB_GROUP) != null) {
166 		//			updateDBGroupList();
167 		//		}
168 
169 		String parentDBGroup = DatabaseEntry.parent(getTrackGroupProperty(UTGBProperty.DB_GROUP));
170 		if (!this.parentDBGroup.equals(parentDBGroup))
171 			updateDBGroupList();
172 
173 	}
174 
175 	private void updateDBGroupList() {
176 		parentDBGroup = DatabaseEntry.parent(getTrackGroupProperty(UTGBProperty.DB_GROUP));
177 		getBrowserService().getChildDBGroups(parentDBGroup, new AsyncCallback<List<String>>() {
178 			public void onFailure(Throwable e) {
179 				GWT.log(e.getMessage(), e);
180 			}
181 
182 			public void onSuccess(List<String> result) {
183 				groupSelector.clear();
184 
185 				for (String each : result)
186 					groupSelector.addItem(each);
187 
188 				DatabaseEntry d = DatabaseEntry.newFolder(getTrackGroupProperty(UTGBProperty.DB_GROUP));
189 				d = d.parent();
190 				while (d != null) {
191 					groupSelector.addItem(d.path);
192 					d = d.parent();
193 				}
194 
195 				if (!result.isEmpty()) {
196 					selectDBGroup(getTrackGroupProperty(UTGBProperty.DB_GROUP));
197 				}
198 			}
199 		});
200 
201 	}
202 
203 	private void updateDBNameList() {
204 		getBrowserService().getDBEntry(getTrackGroupProperty(UTGBProperty.DB_GROUP), new AsyncCallback<List<DatabaseEntry>>() {
205 			public void onFailure(Throwable e) {
206 				GWT.log(e.getMessage(), e);
207 			}
208 
209 			public void onSuccess(List<DatabaseEntry> result) {
210 				dbSelector.clear();
211 
212 				DBSelectTrack.this.currentEntry = result;
213 				dbSelector.addItem("(select)");
214 				for (DatabaseEntry each : result) {
215 					dbSelector.addItem(each.leaf());
216 				}
217 
218 				if (!result.isEmpty()) {
219 					selectDBName(getTrackGroupProperty(UTGBProperty.DB_NAME));
220 				}
221 			}
222 		});
223 	}
224 
225 	@Override
226 	public void onChangeTrackGroupProperty(TrackGroupPropertyChange change) {
227 		if (change.contains(UTGBProperty.DB_GROUP)) {
228 			String p = DatabaseEntry.parent(change.getProperty(UTGBProperty.DB_GROUP));
229 
230 			if (this.parentDBGroup != null && !this.parentDBGroup.equals(p)) {
231 				updateDBGroupList();
232 			}
233 		}
234 
235 		if (change.contains(UTGBProperty.DB_GROUP)) {
236 			updateDBNameList();
237 		}
238 
239 	}
240 
241 	private class FavGroupClickHandler implements ClickHandler {
242 		private final String group;
243 
244 		public FavGroupClickHandler(String group) {
245 			this.group = group;
246 		}
247 
248 		public void onClick(ClickEvent arg0) {
249 			setTrackGroupProperty(UTGBProperty.DB_GROUP, group);
250 		}
251 
252 	}
253 
254 	public void updateFavoriteGroup() {
255 		favoritePanel.clear();
256 
257 		for (String each : favoriteGroup) {
258 			Label l = new Label(each);
259 			Style.fontSize(l, 11);
260 			Style.fontColor(l, "#336699");
261 			Style.set(l, "textDecoration", "underline");
262 			Style.cursor(l, Style.CURSOR_POINTER);
263 			Style.margin(l, Style.LEFT, 5);
264 			l.addClickHandler(new FavGroupClickHandler(each));
265 			favoritePanel.add(l);
266 		}
267 	}
268 
269 	@Override
270 	public void restoreProperties(CanonicalProperties properties) {
271 
272 		favoriteGroup.clear();
273 
274 		String f = properties.get("favoriteGroup", "[]");
275 		for (String each : JSONUtil.parseJSONArray(f)) {
276 			favoriteGroup.add(each);
277 		}
278 
279 		updateFavoriteGroup();
280 
281 	}
282 
283 	@Override
284 	public void saveProperties(CanonicalProperties saveData) {
285 		saveData.add("favoriteGroup", JSONUtil.toJSONArray(favoriteGroup));
286 	}
287 
288 }