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  // PropertyEditTrack.java
20  // Since: Jun 14, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.track.lib.debug;
26  
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  
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.TrackEntry;
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.TrackGroupPropertyChangeListener;
38  import org.utgenome.gwt.utgb.client.track.TrackGroupPropertyChangeListenerAdapter;
39  import org.utgenome.gwt.utgb.client.track.TrackGroupPropertyWriter;
40  import org.utgenome.gwt.utgb.client.track.TrackUpdateListenerAdapter;
41  import org.utgenome.gwt.utgb.client.ui.EditableTable;
42  import org.utgenome.gwt.utgb.client.ui.TableChangeListener;
43  
44  import com.google.gwt.event.dom.client.ChangeEvent;
45  import com.google.gwt.event.dom.client.ChangeHandler;
46  import com.google.gwt.user.client.DOM;
47  import com.google.gwt.user.client.ui.HorizontalPanel;
48  import com.google.gwt.user.client.ui.Label;
49  import com.google.gwt.user.client.ui.ListBox;
50  import com.google.gwt.user.client.ui.VerticalPanel;
51  import com.google.gwt.user.client.ui.Widget;
52  
53  /**
54   * A track for editing {@link TrackGroupProperty}
55   * 
56   * @author leo
57   * 
58   */
59  public class PropertyEditTrack extends TrackBase {
60  	private final VerticalPanel _layoutPanel = new VerticalPanel();
61  	private final HorizontalPanel _panel = new HorizontalPanel();
62  	private final Label _changeNotification = new Label();
63  
64  	// listen target
65  	private TrackGroup _peekingGroup;
66  	private TrackGroupPropertyChangeListener _propertyListener;
67  	private final ListBox _groupListBox = new ListBox();
68  	private final ArrayList<TrackGroup> _groupList = new ArrayList<TrackGroup>();
69  
70  	private final EditableTable _propertyTable = new EditableTable(new String[] { "key", "value" }, 0);
71  
72  	public static TrackFactory factory() {
73  		return new TrackFactory() {
74  			public Track newInstance() {
75  				return new PropertyEditTrack();
76  			}
77  		};
78  	}
79  
80  	public PropertyEditTrack() {
81  		super("Property");
82  
83  		_panel.setStyleName("selector");
84  
85  		DOM.setStyleAttribute(_changeNotification.getElement(), "color", "#FF9999");
86  
87  		HorizontalPanel hp = new HorizontalPanel();
88  		hp.add(new Label("track group: "));
89  		hp.add(_groupListBox);
90  		hp.setStyleName("selector");
91  
92  		_layoutPanel.add(hp);
93  		_layoutPanel.add(_panel);
94  		_layoutPanel.add(_propertyTable);
95  		_layoutPanel.setWidth("100%");
96  
97  		_propertyTable.enableInsertionOfNewRow();
98  		_propertyTable.addTableChangeListener(new TableChangeListener() {
99  			private String getKey(Object key) {
100 				return key.toString();
101 			}
102 
103 			public void onDeleteRow(Object rowKey) {
104 				// TODO implementation
105 			}
106 
107 			public void onInsertNewRow(Object[] newRowData) {
108 				TrackGroupPropertyWriter writer = getTrackGroup().getPropertyWriter();
109 				writer.setProperty(getKey(newRowData[0]), newRowData[1].toString());
110 			}
111 
112 			public void onUpdateRow(Object rowKey, String columnName, String newData) {
113 				TrackGroupPropertyWriter writer = getTrackGroup().getPropertyWriter();
114 				writer.setProperty(getKey(rowKey), newData);
115 			}
116 		});
117 	}
118 
119 	public Widget getWidget() {
120 		return _layoutPanel;
121 	}
122 
123 	public int getDefaultWindowHeight() {
124 		return 25;
125 	}
126 
127 	public void draw() {
128 		_propertyTable.removeAllRows();
129 
130 		TrackGroupProperty reader = _peekingGroup.getPropertyReader();
131 		int rowId = 0;
132 		for (Iterator<String> it = reader.keySet().iterator(); it.hasNext(); rowId++) {
133 			String key = (String) it.next();
134 			String value = reader.getProperty(key, "");
135 			_propertyTable.addRow(new Object[] { key, value });
136 		}
137 	}
138 
139 	private void updateGroupList() {
140 		// get the root track group
141 		TrackGroup rootGroup = getTrackGroup().getRootTrackGroup();
142 
143 		// list up track groups
144 		assert (rootGroup != null);
145 		ArrayList<GroupEntry> trackGroupList = new ArrayList<GroupEntry>();
146 		findTrackGroups(rootGroup, "", trackGroupList);
147 
148 		// update track group list
149 		_groupList.clear();
150 		_groupListBox.clear();
151 		for (Iterator<GroupEntry> it = trackGroupList.iterator(); it.hasNext();) {
152 			GroupEntry entry = it.next();
153 			_groupListBox.addItem(entry.label);
154 			_groupList.add(entry.group);
155 		}
156 	}
157 
158 	class GroupEntry {
159 		String label;
160 		TrackGroup group;
161 
162 		public GroupEntry(TrackGroup group, String label) {
163 			super();
164 			this.label = label;
165 			this.group = group;
166 		}
167 
168 	}
169 
170 	public void setUp(TrackFrame trackFrame, TrackGroup group) {
171 		trackFrame.pack();
172 		_peekingGroup = group;
173 
174 		// set up the track group change listener
175 		_propertyListener = new TrackGroupPropertyChangeListenerAdapter() {
176 			public void onChangeTrackGroupProperty(TrackGroupPropertyChange change) {
177 				refresh();
178 			}
179 		};
180 		_peekingGroup.addTrackGroupPropertyChangeListener(_propertyListener);
181 
182 		_groupListBox.addChangeHandler(new ChangeHandler() {
183 			public void onChange(ChangeEvent e) {
184 				int index = _groupListBox.getSelectedIndex();
185 				if (index >= 0 && index < _groupList.size()) {
186 					TrackGroup selectedGroup = _groupList.get(index);
187 
188 					// remove the previous change listener
189 					_peekingGroup.removeTrackGroupPropertyChangeListener(_propertyListener);
190 
191 					// switch the listening target
192 					_peekingGroup = selectedGroup;
193 					_peekingGroup.addTrackGroupPropertyChangeListener(_propertyListener);
194 					refresh();
195 				}
196 			}
197 		});
198 
199 		updateGroupList();
200 
201 		getTrackGroup().getRootTrackGroup().addTrackUpdateListener(new TrackUpdateListenerAdapter() {
202 			public void onAddTrackGroup(TrackGroup trackGroup) {
203 				updateGroupList();
204 			}
205 
206 			public void onRemoveTrackGroup(TrackGroup trackGroup) {
207 				updateGroupList();
208 			}
209 		});
210 
211 	}
212 
213 	private void findTrackGroups(TrackGroup group, String groupPrefix, ArrayList<GroupEntry> result) {
214 		if (group == null)
215 			return;
216 
217 		String newPrefix = groupPrefix + group.getName();
218 		result.add(new GroupEntry(group, newPrefix));
219 
220 		for (TrackEntry entry : group.getTrackEntryList()) {
221 			if (entry.isTrackGroup())
222 				findTrackGroups((TrackGroup) entry, newPrefix + ".", result);
223 		}
224 	}
225 
226 }