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  // TrackGroupPropertyImpl.java
20  // Since: Jun 12, 2007
21  //
22  // $URL$ 
23  // $Author$
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   * {@link TrackGroupPropertyImpl} is a set of (key, value) pairs shared within a {@link TrackGroup} Instead of directly
47   * access {@link TrackGroupPropertyImpl}, you must read and write values through the {@link TrackGroupProperty} and
48   * {@link TrackGroupPropertyWriter}, respectively. This indirection is necessary for broadcasting
49   * {@link Track#onChangeTrackGroupProperty(TrackGroupPropertyChange)} and {@link Track#onChangeTrackWindow(TrackWindow)}
50   * events to all tracks in a group.
51   * 
52   * @author leo
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  	 * Notify the change to the track group and listeners
94  	 * 
95  	 * @param change
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 		// notify the change
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(); // group-properties
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 }