1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.utgenome.gwt.utgb.client.track.lib;
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.TrackGroupPropertyChange;
36 import org.utgenome.gwt.utgb.client.util.CanonicalProperties;
37 import org.utgenome.gwt.utgb.client.util.JSONUtil;
38
39 import com.google.gwt.event.dom.client.ClickEvent;
40 import com.google.gwt.event.dom.client.ClickHandler;
41 import com.google.gwt.user.client.ui.Anchor;
42 import com.google.gwt.user.client.ui.FlowPanel;
43 import com.google.gwt.user.client.ui.Widget;
44
45
46
47
48
49
50
51
52
53 public class ValueSelectorTrack extends TrackBase {
54 public static TrackFactory factory() {
55 return new TrackFactory() {
56 private String trackName;
57 private String targetProperty;
58 private List<String> linkList = new ArrayList<String>();
59
60 public Track newInstance() {
61 final ValueSelectorTrack track = new ValueSelectorTrack(trackName, targetProperty);
62
63 final int SIZE = linkList.size();
64 for (int i = 0; i < SIZE; i++) {
65 final String value = (linkList.get(i));
66 track.addValue(value);
67 }
68
69 return track;
70 }
71
72 public void setProperty(String key, String value) {
73 if (key.equals("trackName"))
74 trackName = value;
75 else if (key.equals("targetProperty"))
76 targetProperty = value;
77 else if (key.equals("value")) {
78 linkList.add(value);
79 }
80 }
81
82 public void clear() {
83 linkList.clear();
84 }
85
86 };
87 }
88
89 private String _targetProperty;
90 private String _selectedValue;
91
92 private FlowPanel _linkPanel = new FlowPanel();
93 private ArrayList<ValueLink> _linkList = new ArrayList<ValueLink>();
94
95 class ValueLink extends Anchor implements ClickHandler {
96 public ValueLink(String value) {
97 super(value, value);
98 setStyleName("selector-item");
99 addClickHandler(this);
100 }
101
102 public void onClick(ClickEvent e) {
103 getTrackGroup().getPropertyWriter().setProperty(_targetProperty, getText());
104
105 setSelectionStyle(this);
106 }
107
108 public String toString() {
109 return this.getText();
110 }
111 }
112
113 public String getTargetProperty() {
114 return _targetProperty;
115 }
116
117 private void setSelectionStyle(ValueLink link) {
118 if (link == null)
119 return;
120
121 _selectedValue = link.toString();
122 link.setStyleName("selector-item-selected");
123 for (Iterator<ValueLink> it = _linkList.iterator(); it.hasNext();) {
124 ValueLink v = it.next();
125 if (v != link)
126 v.setStyleName("selector-item");
127 }
128 }
129
130 private ValueLink search(String value) {
131 for (Iterator<ValueLink> it = _linkList.iterator(); it.hasNext();) {
132 ValueLink v = it.next();
133 if (v.getText().equals(value))
134 return v;
135 }
136 return null;
137 }
138
139 public ValueSelectorTrack(String trackName, String targetProperty) {
140 super(trackName);
141 _linkPanel.setHeight("15px");
142 _linkPanel.setWidth("100%");
143 this._targetProperty = targetProperty;
144 }
145
146 public void addValue(String value) {
147 _linkList.add(new ValueLink(value));
148 }
149
150 public void clearValues() {
151 _linkList.clear();
152 }
153
154 public void clear() {
155 _linkPanel.clear();
156 }
157
158 public int getDefaultWindowHeight() {
159 return 15;
160 }
161
162 public Widget getWidget() {
163 return _linkPanel;
164 }
165
166 protected void updateSelection() {
167 setSelectionStyle(search(_selectedValue));
168 }
169
170 public void onChangeTrackGroupProperty(TrackGroupPropertyChange change) {
171 if (change.contains(_targetProperty)) {
172 _selectedValue = change.getProperty(_targetProperty);
173 updateSelection();
174 }
175
176 }
177
178 public void draw() {
179 _linkPanel.clear();
180 for (Iterator<ValueLink> it = _linkList.iterator(); it.hasNext();) {
181 ValueLink link = it.next();
182 _linkPanel.add(link);
183 }
184 updateSelection();
185 }
186
187 public void setUp(TrackFrame trackFrame, TrackGroup group) {
188 trackFrame.pack();
189 }
190
191 public void saveProperties(CanonicalProperties saveData) {
192 saveData.add("targetProperty", _targetProperty);
193 saveData.add("valueList", JSONUtil.toJSONArray(_linkList));
194 saveData.add("selectedValue", _selectedValue);
195 }
196
197 public void restoreProperties(CanonicalProperties properties) {
198 _targetProperty = properties.get("targetProperty", _targetProperty);
199 _selectedValue = properties.get("selectedValue", _selectedValue);
200 ArrayList<String> valueList = JSONUtil.parseJSONArray(properties.get("valueList", "[]"));
201 _linkList.clear();
202 for (String value : valueList) {
203 addValue(value);
204 }
205 }
206
207 }