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.impl;
26
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.Map;
31 import java.util.Set;
32
33 import org.utgenome.gwt.utgb.client.track.Track;
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.TrackGroupPropertyWriter;
39 import org.utgenome.gwt.utgb.client.track.TrackWindow;
40 import org.utgenome.gwt.utgb.client.util.CanonicalProperties;
41 import org.utgenome.gwt.utgb.client.util.xml.XMLAttribute;
42 import org.utgenome.gwt.utgb.client.util.xml.XMLUtil;
43 import org.utgenome.gwt.utgb.client.util.xml.XMLWriter;
44
45
46
47
48
49
50
51
52
53
54
55 public class TrackGroupPropertyImpl implements TrackGroupProperty, TrackGroupPropertyWriter {
56 private final TrackGroup _trackGroup;
57 private TrackWindow _trackWindow = new TrackWindow(700, 1, 100);
58 private CanonicalProperties _properties = new CanonicalProperties();
59 private ArrayList<TrackGroupPropertyChangeListener> _changeListener = new ArrayList<TrackGroupPropertyChangeListener>();
60 private boolean enableNotifiaction = true;
61
62 public TrackGroupPropertyImpl(TrackGroup trackGroup) {
63 this._trackGroup = trackGroup;
64 }
65
66 public void setProperyChangeNotifaction(boolean enable) {
67 this.enableNotifiaction = enable;
68 }
69
70 public String getProperty(String key) {
71 Object value = _properties.get(key);
72 if (value != null)
73 return value.toString();
74 else
75 return null;
76 }
77
78 public String getProperty(String key, String defaultValue) {
79 Object value = _properties.get(key);
80 return (value != null) ? value.toString() : defaultValue;
81 }
82
83 public TrackWindow getTrackWindow() {
84 return _trackWindow;
85 }
86
87 public void clear() {
88 _properties.clear();
89 _changeListener.clear();
90 }
91
92
93
94
95
96
97 protected void notifyTheChange(TrackGroupPropertyChange change, TrackWindow newWindow) {
98 if (!enableNotifiaction)
99 return;
100
101 _trackGroup.setResizeNotification(false);
102
103 _trackGroup.broadcastChange(change, newWindow);
104 for (TrackGroupPropertyChangeListener listener : _changeListener) {
105 listener.onChange(change, newWindow);
106 }
107
108 _trackGroup.setResizeNotification(true);
109 _trackGroup.notifyResize();
110 }
111
112 public void setProperty(String key, String value) {
113 _properties.put(key, value);
114
115
116 notifyTheChange(new TrackPropertyChangeImpl(this, key), null);
117 }
118
119 public void setProperty(Map<String, String> property) {
120 if (property == null || property.size() == 0)
121 return;
122 _properties.putAll(property);
123 notifyTheChange(new TrackPropertyChangeImpl(this, property.keySet()), null);
124 }
125
126 public void setProperty(Map<String, String> properties, TrackWindow newWindow) {
127
128 boolean notifyPropChange = false;
129 if (properties != null && properties.size() > 0) {
130 _properties.putAll(properties);
131 notifyPropChange = true;
132 }
133
134 _trackWindow = newWindow;
135 notifyTheChange(notifyPropChange ? new TrackPropertyChangeImpl(this, properties.keySet()) : null, newWindow);
136 }
137
138 public void setTrackWindow(int startOnGenome, int endOnGenome) {
139 if (_trackWindow != null)
140 _trackWindow = _trackWindow.newWindow(startOnGenome, endOnGenome);
141 else
142 _trackWindow = new TrackWindow(700, startOnGenome, endOnGenome);
143 notifyTheChange(null, _trackWindow);
144 }
145
146 public void setTrackWindowSize(int windowWidth) {
147 _trackWindow = _trackWindow.newPixelWidthWindow(windowWidth);
148 notifyTheChange(null, _trackWindow);
149 }
150
151 public Set<String> keySet() {
152 return _properties.keySet();
153 }
154
155 public void setTrackWindow(TrackWindow newWindow) {
156 if (newWindow == null)
157 throw new NullPointerException("window is null");
158 _trackWindow = newWindow;
159 notifyTheChange(null, _trackWindow);
160 }
161
162 public void apply(TrackGroupPropertyChange change) {
163 HashMap<String, String> map = new HashMap<String, String>();
164 for (Iterator<String> it = change.changedKeySet().iterator(); it.hasNext();) {
165 String key = it.next();
166 String value = change.getProperty(key);
167 map.put(key, value);
168 }
169 this.setProperty(map);
170 }
171
172 public void addTrackGroupPropertyChangeListener(TrackGroupPropertyChangeListener listener) {
173 _changeListener.add(listener);
174 }
175
176 public void removeTrackGroupPropertyChangeListener(TrackGroupPropertyChangeListener listener) {
177 _changeListener.remove(listener);
178 }
179
180 public void toXML(XMLWriter xmlWriter) {
181 xmlWriter.start("groupProperties");
182 XMLUtil.toCanonicalXML(_properties, xmlWriter);
183 toXML(_trackWindow, xmlWriter);
184 xmlWriter.end();
185 }
186
187 public static XMLWriter toXML(TrackWindow w, XMLWriter writer) {
188 writer.element("trackWindow", new XMLAttribute().add("start", w.getStartOnGenome()).add("end", w.getEndOnGenome()).add("width", w.getPixelWidth()));
189 return writer;
190 }
191
192 public void scrollTrackWindow(double scrollPercentage) {
193 _trackGroup.broadCastScrollTrackWindow(scrollPercentage);
194 }
195
196 public void scaleDownTrackWindow() {
197 _trackGroup.broadCastWindowSizeChange(-1);
198 }
199
200 public void scaleUpTrackWindow() {
201 _trackGroup.broadCastWindowSizeChange(1);
202 }
203
204 }