1 | /* |
---|
2 | * $Id: Annotation.java,v 1.8 2003/08/22 23:02:38 dwd Exp $ |
---|
3 | * |
---|
4 | * This software is provided by NOAA for full, free and open release. It is |
---|
5 | * understood by the recipient/user that NOAA assumes no liability for any |
---|
6 | * errors contained in the code. Although this software is released without |
---|
7 | * conditions or restrictions in its use, it is expected that appropriate |
---|
8 | * credit be given to its author and to the National Oceanic and Atmospheric |
---|
9 | * Administration should the software be included by the recipient as an |
---|
10 | * element in other product development. |
---|
11 | */ |
---|
12 | package gov.noaa.pmel.sgt.dm; |
---|
13 | |
---|
14 | import gov.noaa.pmel.sgt.SGLabel; |
---|
15 | import gov.noaa.pmel.sgt.LineAttribute; |
---|
16 | import gov.noaa.pmel.sgt.PointAttribute; |
---|
17 | import gov.noaa.pmel.sgt.SGException; |
---|
18 | |
---|
19 | import gov.noaa.pmel.util.SoTRange; |
---|
20 | import gov.noaa.pmel.util.SoTPoint; |
---|
21 | |
---|
22 | import java.util.List; |
---|
23 | import java.util.Vector; |
---|
24 | import java.util.Iterator; |
---|
25 | import java.awt.Color; |
---|
26 | |
---|
27 | import java.beans.PropertyChangeListener; |
---|
28 | import java.beans.PropertyChangeSupport; |
---|
29 | import java.beans.PropertyChangeEvent; |
---|
30 | |
---|
31 | /** |
---|
32 | * A container to hold <code>Annote</code> objects. |
---|
33 | * @author Donald Denbo |
---|
34 | * @version $Revision: 1.8 $ |
---|
35 | * @since 3.0 |
---|
36 | */ |
---|
37 | public class Annotation implements SGTData, PropertyChangeListener { |
---|
38 | private PropertyChangeSupport changes_ = new PropertyChangeSupport(this); |
---|
39 | private String title_ = null; |
---|
40 | private String id_ = null; |
---|
41 | |
---|
42 | /** |
---|
43 | * @label x |
---|
44 | * @link aggregation |
---|
45 | */ |
---|
46 | private SGTMetaData xMeta_ = null; |
---|
47 | |
---|
48 | /** |
---|
49 | * @link aggregation |
---|
50 | * @label y |
---|
51 | */ |
---|
52 | private SGTMetaData yMeta_ = null; |
---|
53 | private List text_ = new Vector(); |
---|
54 | private List line_ = new Vector(); |
---|
55 | private List point_ = new Vector(); |
---|
56 | private List oval_ = new Vector(); |
---|
57 | private List rect_ = new Vector(); |
---|
58 | |
---|
59 | private SoTRange xRange_ = null; |
---|
60 | private SoTRange yRange_ = null; |
---|
61 | private boolean xTime_ = false; |
---|
62 | private boolean yTime_ = false; |
---|
63 | |
---|
64 | public Annotation() { |
---|
65 | this(null, false, false); |
---|
66 | } |
---|
67 | |
---|
68 | public Annotation(String title) { |
---|
69 | this(title, false, false); |
---|
70 | } |
---|
71 | |
---|
72 | public Annotation(String title, boolean xTime, boolean yTime) { |
---|
73 | title_ = title; |
---|
74 | xTime_ = xTime; |
---|
75 | yTime_ = yTime; |
---|
76 | } |
---|
77 | |
---|
78 | public boolean remove(String id) { |
---|
79 | Annote ann = findAnnote(id); |
---|
80 | if(ann == null) return false; |
---|
81 | return remove(ann); |
---|
82 | } |
---|
83 | |
---|
84 | public boolean remove(Annote ann) { |
---|
85 | if(ann instanceof Annote.Line) { |
---|
86 | return removeLine(ann); |
---|
87 | } else if(ann instanceof Annote.Oval) { |
---|
88 | return removeOval(ann); |
---|
89 | } else if(ann instanceof Annote.Point) { |
---|
90 | return removePoint(ann); |
---|
91 | } else if(ann instanceof Annote.Rect) { |
---|
92 | return removeRect(ann); |
---|
93 | } else if(ann instanceof Annote.Text) { |
---|
94 | return removeText(ann); |
---|
95 | } |
---|
96 | return false; |
---|
97 | } |
---|
98 | |
---|
99 | public void add(Annote ann) throws SGException { |
---|
100 | if(ann instanceof Annote.Line) { |
---|
101 | SGTLine line = ((Annote.Line)ann).getLine(); |
---|
102 | if(xTime_ != line.isXTime() || yTime_ != line.isYTime()) |
---|
103 | throw new SGException("Time axes do not match"); |
---|
104 | ann.addPropertyChangeListener(this); |
---|
105 | line_.add(ann); |
---|
106 | changes_.firePropertyChange("lineAdded", true, false); |
---|
107 | } else if(ann instanceof Annote.Oval) { |
---|
108 | SoTPoint pt1 = ((Annote.Oval)ann).getUpperLeft(); |
---|
109 | if(xTime_ != pt1.isXTime() || yTime_ != pt1.isYTime()) |
---|
110 | throw new SGException("Time axes do not match"); |
---|
111 | ann.addPropertyChangeListener(this); |
---|
112 | oval_.add(ann); |
---|
113 | changes_.firePropertyChange("ovalAdded", true, false); |
---|
114 | } else if(ann instanceof Annote.Point) { |
---|
115 | SGTPoint point = ((Annote.Point)ann).getPoint(); |
---|
116 | if(xTime_ != point.isXTime() || yTime_ != point.isYTime()) |
---|
117 | throw new SGException("Time axes do not match"); |
---|
118 | ann.addPropertyChangeListener(this); |
---|
119 | point_.add(ann); |
---|
120 | changes_.firePropertyChange("pointAdded", true, false); |
---|
121 | } else if(ann instanceof Annote.Rect) { |
---|
122 | SoTPoint pt1 = ((Annote.Rect)ann).getUpperLeft(); |
---|
123 | if(xTime_ != pt1.isXTime() || yTime_ != pt1.isYTime()) |
---|
124 | throw new SGException("Time axes do not match"); |
---|
125 | ann.addPropertyChangeListener(this); |
---|
126 | rect_.add(ann); |
---|
127 | changes_.firePropertyChange("rectAdded", true, false); |
---|
128 | } else if(ann instanceof Annote.Text) { |
---|
129 | SoTPoint loc = ((Annote.Text)ann).getLocation(); |
---|
130 | if(xTime_ != loc.isXTime() || yTime_ != loc.isYTime()) |
---|
131 | throw new SGException("Time axes do not match"); |
---|
132 | ann.addPropertyChangeListener(this); |
---|
133 | text_.add(ann); |
---|
134 | changes_.firePropertyChange("textAdded", true, false); |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | public Annote addLine(String id, SGTLine line, LineAttribute attr) |
---|
139 | throws SGException { |
---|
140 | if(xTime_ != line.isXTime() || yTime_ != line.isYTime()) |
---|
141 | throw new SGException("Time axes do not match"); |
---|
142 | Annote.Line aLine = new Annote.Line(id, line, attr); |
---|
143 | aLine.addPropertyChangeListener(this); |
---|
144 | line_.add(aLine); |
---|
145 | |
---|
146 | changes_.firePropertyChange("lineAdded", true, false); |
---|
147 | return aLine; |
---|
148 | } |
---|
149 | |
---|
150 | private boolean removeLine(Annote line) { |
---|
151 | boolean result = false; |
---|
152 | if(line instanceof Annote.Line) { |
---|
153 | line.removePropertyChangeListener(this); |
---|
154 | result = line_.remove(line); |
---|
155 | if(result) changes_.firePropertyChange("lineRemoved", true, false); |
---|
156 | } |
---|
157 | return result; |
---|
158 | } |
---|
159 | |
---|
160 | public Iterator getLineIterator() { |
---|
161 | return line_.iterator(); |
---|
162 | } |
---|
163 | |
---|
164 | public boolean hasLine() { |
---|
165 | return !line_.isEmpty(); |
---|
166 | } |
---|
167 | |
---|
168 | public Annote addPoint(String id, SGTPoint point, PointAttribute attr) |
---|
169 | throws SGException { |
---|
170 | if(xTime_ != point.isXTime() || yTime_ != point.isYTime()) |
---|
171 | throw new SGException("Time axes do not match"); |
---|
172 | Annote.Point aPoint = new Annote.Point(id, point, attr); |
---|
173 | aPoint.addPropertyChangeListener(this); |
---|
174 | point_.add(aPoint); |
---|
175 | |
---|
176 | changes_.firePropertyChange("pointAdded", true, false); |
---|
177 | return aPoint; |
---|
178 | } |
---|
179 | |
---|
180 | private boolean removePoint(Annote point) { |
---|
181 | boolean result = false; |
---|
182 | if(point instanceof Annote.Point) { |
---|
183 | point.removePropertyChangeListener(this); |
---|
184 | result = point_.remove(point); |
---|
185 | if(result) changes_.firePropertyChange("pointRemoved", true, false); |
---|
186 | } |
---|
187 | return result; |
---|
188 | } |
---|
189 | |
---|
190 | public Iterator getPointIterator() { |
---|
191 | return point_.iterator(); |
---|
192 | } |
---|
193 | |
---|
194 | public boolean hasPoint() { |
---|
195 | return !point_.isEmpty(); |
---|
196 | } |
---|
197 | |
---|
198 | public Annote addText(String id, SoTPoint loc, SGLabel text) |
---|
199 | throws SGException { |
---|
200 | if(xTime_ != loc.isXTime() || yTime_ != loc.isYTime()) |
---|
201 | throw new SGException("Time axes do not match"); |
---|
202 | Annote.Text aText = new Annote.Text(id, loc, text); |
---|
203 | aText.addPropertyChangeListener(this); |
---|
204 | text_.add(aText); |
---|
205 | |
---|
206 | changes_.firePropertyChange("textAdded", true, false); |
---|
207 | return aText; |
---|
208 | } |
---|
209 | |
---|
210 | private boolean removeText(Annote text) { |
---|
211 | boolean result = false; |
---|
212 | if(text instanceof Annote.Text) { |
---|
213 | text.removePropertyChangeListener(this); |
---|
214 | result = text_.remove(text); |
---|
215 | if(result) changes_.firePropertyChange("textRemoved", true, false); |
---|
216 | } |
---|
217 | return result; |
---|
218 | } |
---|
219 | |
---|
220 | public Iterator getTextIterator() { |
---|
221 | return text_.iterator(); |
---|
222 | } |
---|
223 | |
---|
224 | public boolean hasText() { |
---|
225 | return !text_.isEmpty(); |
---|
226 | } |
---|
227 | /** |
---|
228 | * Add an oval to the <code>Annotation</code>. If attr is non-null an oval |
---|
229 | * outline will be drawn, if color is non-null it will be filled. |
---|
230 | */ |
---|
231 | public Annote addOval(String id, SoTPoint pt1, SoTPoint pt2, |
---|
232 | LineAttribute attr, Color color) throws SGException { |
---|
233 | if(xTime_ != pt1.isXTime() || yTime_ != pt1.isYTime()) |
---|
234 | throw new SGException("Time axes do not match"); |
---|
235 | Annote.Oval aOval = new Annote.Oval(id, pt1, pt2, attr, color); |
---|
236 | aOval.addPropertyChangeListener(this); |
---|
237 | oval_.add(aOval); |
---|
238 | |
---|
239 | changes_.firePropertyChange("ovalAdded", true, false); |
---|
240 | return aOval; |
---|
241 | } |
---|
242 | |
---|
243 | private boolean removeOval(Annote oval) { |
---|
244 | boolean result = false; |
---|
245 | if(oval instanceof Annote.Oval) { |
---|
246 | oval.removePropertyChangeListener(this); |
---|
247 | result = oval_.remove(oval); |
---|
248 | if(result) changes_.firePropertyChange("ovalRemoved", true, false); |
---|
249 | } |
---|
250 | return result; |
---|
251 | } |
---|
252 | |
---|
253 | public Iterator getOvalIterator() { |
---|
254 | return oval_.iterator(); |
---|
255 | } |
---|
256 | |
---|
257 | public boolean hasOval() { |
---|
258 | return !oval_.isEmpty(); |
---|
259 | } |
---|
260 | /** |
---|
261 | * Add an rectangle to the <code>Annotation</code>. If attr is non-null an rectangle |
---|
262 | * outline will be drawn, if color is non-null it will be filled. |
---|
263 | */ |
---|
264 | public Annote addRect(String id, SoTPoint pt1, SoTPoint pt2, |
---|
265 | LineAttribute attr, Color color) throws SGException { |
---|
266 | if(xTime_ != pt1.isXTime() || yTime_ != pt1.isYTime()) |
---|
267 | throw new SGException("Time axes do not match"); |
---|
268 | Annote.Rect aRect = new Annote.Rect(id, pt1, pt2, attr, color); |
---|
269 | aRect.addPropertyChangeListener(this); |
---|
270 | rect_.add(aRect); |
---|
271 | |
---|
272 | changes_.firePropertyChange("rectAdded", true, false); |
---|
273 | return aRect; |
---|
274 | } |
---|
275 | |
---|
276 | private boolean removeRect(Annote rect) { |
---|
277 | boolean result = false; |
---|
278 | if(rect instanceof Annote.Rect) { |
---|
279 | rect.removePropertyChangeListener(this); |
---|
280 | result = rect_.remove(rect); |
---|
281 | if(result) changes_.firePropertyChange("rectRemoved", true, false); |
---|
282 | } |
---|
283 | return result; |
---|
284 | } |
---|
285 | |
---|
286 | public Iterator getRectIterator() { |
---|
287 | return rect_.iterator(); |
---|
288 | } |
---|
289 | |
---|
290 | public boolean hasRect() { |
---|
291 | return !rect_.isEmpty(); |
---|
292 | } |
---|
293 | |
---|
294 | public Annote findAnnote(String id) { |
---|
295 | Annote tmp = null; |
---|
296 | Iterator iter; |
---|
297 | if(!line_.isEmpty()) { |
---|
298 | iter = line_.iterator(); |
---|
299 | while(iter.hasNext()) { |
---|
300 | tmp = (Annote)iter.next(); |
---|
301 | if(tmp.getAnnoteId().equals(id)) return tmp; |
---|
302 | } |
---|
303 | } |
---|
304 | if(!point_.isEmpty()) { |
---|
305 | iter = point_.iterator(); |
---|
306 | while(iter.hasNext()) { |
---|
307 | tmp = (Annote)iter.next(); |
---|
308 | if(tmp.getAnnoteId().equals(id)) return tmp; |
---|
309 | } |
---|
310 | } |
---|
311 | if(!oval_.isEmpty()) { |
---|
312 | iter = oval_.iterator(); |
---|
313 | while(iter.hasNext()) { |
---|
314 | tmp = (Annote)iter.next(); |
---|
315 | if(tmp.getAnnoteId().equals(id)) return tmp; |
---|
316 | } |
---|
317 | } |
---|
318 | if(!rect_.isEmpty()) { |
---|
319 | iter = rect_.iterator(); |
---|
320 | while(iter.hasNext()) { |
---|
321 | tmp = (Annote)iter.next(); |
---|
322 | if(tmp.getAnnoteId().equals(id)) return tmp; |
---|
323 | } |
---|
324 | } |
---|
325 | if(!text_.isEmpty()) { |
---|
326 | iter = text_.iterator(); |
---|
327 | while(iter.hasNext()) { |
---|
328 | tmp = (Annote)iter.next(); |
---|
329 | if(tmp.getAnnoteId().equals(id)) return tmp; |
---|
330 | } |
---|
331 | } |
---|
332 | return null; |
---|
333 | } |
---|
334 | |
---|
335 | public void setTitle(String title) { |
---|
336 | title_ = title; |
---|
337 | } |
---|
338 | |
---|
339 | public String getTitle() { |
---|
340 | return title_; |
---|
341 | } |
---|
342 | |
---|
343 | public SGLabel getKeyTitle() { |
---|
344 | return null; |
---|
345 | } |
---|
346 | |
---|
347 | public void setId(String id) { |
---|
348 | id_ = id; |
---|
349 | } |
---|
350 | |
---|
351 | public String getId() { |
---|
352 | return id_; |
---|
353 | } |
---|
354 | |
---|
355 | public SGTData copy() { |
---|
356 | /**@todo: Implement this gov.noaa.pmel.sgt.dm.SGTData method*/ |
---|
357 | throw new java.lang.UnsupportedOperationException("Method copy() not yet implemented."); |
---|
358 | } |
---|
359 | |
---|
360 | public boolean isXTime() { |
---|
361 | return xTime_; |
---|
362 | } |
---|
363 | |
---|
364 | public boolean isYTime() { |
---|
365 | return yTime_; |
---|
366 | } |
---|
367 | |
---|
368 | public void setXMetaData(SGTMetaData meta) { |
---|
369 | xMeta_ = meta; |
---|
370 | } |
---|
371 | |
---|
372 | public SGTMetaData getXMetaData() { |
---|
373 | return xMeta_; |
---|
374 | } |
---|
375 | |
---|
376 | public void setYMetaData(SGTMetaData meta) { |
---|
377 | yMeta_ = meta; |
---|
378 | } |
---|
379 | |
---|
380 | public SGTMetaData getYMetaData() { |
---|
381 | return yMeta_; |
---|
382 | } |
---|
383 | |
---|
384 | public SoTRange getXRange() { |
---|
385 | return xRange_; |
---|
386 | } |
---|
387 | |
---|
388 | public SoTRange getYRange() { |
---|
389 | return yRange_; |
---|
390 | } |
---|
391 | |
---|
392 | public void addPropertyChangeListener(PropertyChangeListener l) { |
---|
393 | changes_.addPropertyChangeListener(l); |
---|
394 | } |
---|
395 | |
---|
396 | public void removePropertyChangeListener(PropertyChangeListener l) { |
---|
397 | changes_.removePropertyChangeListener(l); |
---|
398 | } |
---|
399 | public void propertyChange(PropertyChangeEvent evt) { |
---|
400 | changes_.firePropertyChange(evt); |
---|
401 | } |
---|
402 | } |
---|