1 | /** |
---|
2 | * $Id: SimpleLine.java,v 1.15 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 | |
---|
13 | package gov.noaa.pmel.sgt.dm; |
---|
14 | |
---|
15 | import gov.noaa.pmel.sgt.SGLabel; |
---|
16 | import gov.noaa.pmel.util.GeoDate; |
---|
17 | import gov.noaa.pmel.util.GeoDateArray; |
---|
18 | import gov.noaa.pmel.util.SoTRange; |
---|
19 | |
---|
20 | import java.io.Serializable; |
---|
21 | import java.beans.PropertyChangeSupport; |
---|
22 | import java.beans.PropertyChangeListener; |
---|
23 | |
---|
24 | /** |
---|
25 | * <code>SimpleLine</code> provides an implementation of the |
---|
26 | * <code>SGTLine</code> and <code>Cartesian</code> interfaces. |
---|
27 | * |
---|
28 | * @author Donald Denbo |
---|
29 | * @version $Revision: 1.15 $, $Date: 2003/08/22 23:02:38 $ |
---|
30 | * @since 1.0 |
---|
31 | * @see SGTLine |
---|
32 | * @see Cartesian |
---|
33 | */ |
---|
34 | public class SimpleLine implements SGTLine, Cartesian, |
---|
35 | Serializable, Cloneable { |
---|
36 | protected double[] xloc_; |
---|
37 | protected double[] yloc_; |
---|
38 | // protected GeoDate[] tloc_; |
---|
39 | protected GeoDateArray tloc_; |
---|
40 | protected boolean xTime_; |
---|
41 | protected boolean yTime_; |
---|
42 | protected String title_; |
---|
43 | protected SGLabel keyTitle_ = null; |
---|
44 | protected String id_ = null; |
---|
45 | /**@shapeType AggregationLink |
---|
46 | * @clientRole x*/ |
---|
47 | protected SGTMetaData xMetaData_ = null; |
---|
48 | /**@shapeType AggregationLink |
---|
49 | * @clientRole y*/ |
---|
50 | protected SGTMetaData yMetaData_ = null; |
---|
51 | /**@shapeType AggregationLink |
---|
52 | * @clientRole associated data*/ |
---|
53 | protected SGTLine associatedData_ = null; |
---|
54 | private SoTRange xRange_ = null; |
---|
55 | private SoTRange yRange_ = null; |
---|
56 | private PropertyChangeSupport changes_ = new PropertyChangeSupport(this); |
---|
57 | /** |
---|
58 | * Default constuctor. |
---|
59 | */ |
---|
60 | public SimpleLine() { |
---|
61 | this((double[])null, (double[])null, ""); |
---|
62 | } |
---|
63 | /** |
---|
64 | * Constructor for X and Y double. |
---|
65 | * |
---|
66 | * @param xloc X coordinates |
---|
67 | * @param yloc Y coordinates |
---|
68 | * @param title the Title |
---|
69 | */ |
---|
70 | public SimpleLine(double[] xloc,double[] yloc,String title) { |
---|
71 | xloc_ = xloc; |
---|
72 | yloc_ = yloc; |
---|
73 | title_ = title; |
---|
74 | xTime_ = false; |
---|
75 | yTime_ = false; |
---|
76 | xRange_ = computeSoTRange(xloc); |
---|
77 | yRange_ = computeSoTRange(yloc); |
---|
78 | } |
---|
79 | /** |
---|
80 | * Constructor for X Time and Y double. |
---|
81 | * |
---|
82 | * @param tloc Time coordinates |
---|
83 | * @param yloc Y coordinates |
---|
84 | * @param title the Title |
---|
85 | */ |
---|
86 | public SimpleLine(GeoDate[] tloc, double[] yloc, String title) { |
---|
87 | tloc_ = new GeoDateArray(tloc); |
---|
88 | yloc_ = yloc; |
---|
89 | title_ = title; |
---|
90 | xTime_ = true; |
---|
91 | yTime_ = false; |
---|
92 | xRange_ = computeSoTRange(tloc_); |
---|
93 | yRange_ = computeSoTRange(yloc); |
---|
94 | } |
---|
95 | /** |
---|
96 | * Constructor for X Time and Y double. |
---|
97 | * |
---|
98 | * @since 3.0 |
---|
99 | * @param tloc Time coordinates |
---|
100 | * @param yloc Y coordinates |
---|
101 | * @param title the Title |
---|
102 | */ |
---|
103 | public SimpleLine(GeoDateArray tloc, double[] yloc, String title) { |
---|
104 | tloc_ = tloc; |
---|
105 | yloc_ = yloc; |
---|
106 | title_ = title; |
---|
107 | xTime_ = true; |
---|
108 | yTime_ = false; |
---|
109 | xRange_ = computeSoTRange(tloc_); |
---|
110 | yRange_ = computeSoTRange(yloc); |
---|
111 | } |
---|
112 | /** |
---|
113 | * Constructor for X double and Y Time. |
---|
114 | * |
---|
115 | * @since 3.0 |
---|
116 | * @param xloc X coordinates |
---|
117 | * @param tloc Time coordinates |
---|
118 | * @param title the Title |
---|
119 | */ |
---|
120 | public SimpleLine(double[] xloc, GeoDateArray tloc, String title) { |
---|
121 | xloc_ = xloc; |
---|
122 | tloc_ = tloc; |
---|
123 | title_ = title; |
---|
124 | xTime_ = false; |
---|
125 | yTime_ = true; |
---|
126 | xRange_ = computeSoTRange(xloc); |
---|
127 | yRange_ = computeSoTRange(tloc_); |
---|
128 | } |
---|
129 | /** |
---|
130 | * Constructor for X double and Y Time. |
---|
131 | * |
---|
132 | * @param xloc X coordinates |
---|
133 | * @param tloc Time coordinates |
---|
134 | * @param title the Title |
---|
135 | */ |
---|
136 | public SimpleLine(double[] xloc, GeoDate[] tloc, String title) { |
---|
137 | xloc_ = xloc; |
---|
138 | tloc_ = new GeoDateArray(tloc); |
---|
139 | title_ = title; |
---|
140 | xTime_ = false; |
---|
141 | yTime_ = true; |
---|
142 | xRange_ = computeSoTRange(xloc); |
---|
143 | yRange_ = computeSoTRange(tloc_); |
---|
144 | } |
---|
145 | /** |
---|
146 | * Create a shallow copy. |
---|
147 | * |
---|
148 | * @since 2.0 |
---|
149 | * @see SGTData |
---|
150 | */ |
---|
151 | public SGTData copy(){ |
---|
152 | SGTLine newLine; |
---|
153 | try { |
---|
154 | newLine = (SGTLine)clone(); |
---|
155 | } catch (CloneNotSupportedException e) { |
---|
156 | newLine = new SimpleLine(); |
---|
157 | } |
---|
158 | return (SGTData)newLine; |
---|
159 | } |
---|
160 | /** |
---|
161 | * Get the X coordinate array. |
---|
162 | */ |
---|
163 | public double[] getXArray() { |
---|
164 | return xloc_; |
---|
165 | } |
---|
166 | /** |
---|
167 | * Get the Y coordinate array. |
---|
168 | */ |
---|
169 | public double[] getYArray() { |
---|
170 | return yloc_; |
---|
171 | } |
---|
172 | /** |
---|
173 | * Get the Time coordinate array. |
---|
174 | */ |
---|
175 | public GeoDate[] getTimeArray() { |
---|
176 | return tloc_.getGeoDate(); |
---|
177 | } |
---|
178 | /** |
---|
179 | * Get the <code>GeoDateArray</code> object. |
---|
180 | * |
---|
181 | * @since 3.0 |
---|
182 | */ |
---|
183 | public GeoDateArray getGeoDateArray() { |
---|
184 | return tloc_; |
---|
185 | } |
---|
186 | /** |
---|
187 | * Is the X coordinate Time? |
---|
188 | */ |
---|
189 | public boolean isXTime() { |
---|
190 | return xTime_; |
---|
191 | } |
---|
192 | /** |
---|
193 | * Is the Y coordinate Time? |
---|
194 | */ |
---|
195 | public boolean isYTime() { |
---|
196 | return yTime_; |
---|
197 | } |
---|
198 | /** |
---|
199 | * Get the X coordinate metadata. |
---|
200 | */ |
---|
201 | public SGTMetaData getXMetaData() { |
---|
202 | return xMetaData_; |
---|
203 | } |
---|
204 | /** |
---|
205 | * Get the Y coordinate metadata |
---|
206 | */ |
---|
207 | public SGTMetaData getYMetaData() { |
---|
208 | return yMetaData_; |
---|
209 | } |
---|
210 | /** |
---|
211 | * Get the Title. |
---|
212 | */ |
---|
213 | public String getTitle() { |
---|
214 | return title_; |
---|
215 | } |
---|
216 | /** |
---|
217 | * Get the unique identifier. The presence of the identifier |
---|
218 | * is optional, but if it is present it should be unique. This |
---|
219 | * field is used to search for the layer that contains the data. |
---|
220 | * |
---|
221 | * @since 2.0 |
---|
222 | * @return unique identifier |
---|
223 | * @see gov.noaa.pmel.sgt.JPane |
---|
224 | * @see gov.noaa.pmel.sgt.Layer |
---|
225 | */ |
---|
226 | public String getId() { |
---|
227 | return id_; |
---|
228 | } |
---|
229 | /** |
---|
230 | * Set the unique identifier. |
---|
231 | */ |
---|
232 | public void setId(String ident) { |
---|
233 | id_ = ident; |
---|
234 | } |
---|
235 | /** |
---|
236 | * Set the data that will be associated with <code>SGTLine</code> |
---|
237 | * <BR><B>Property Change:</B> <code>associatedDataModified</code>. |
---|
238 | * |
---|
239 | * @since 2.0 |
---|
240 | */ |
---|
241 | public void setAssociatedData(SGTLine assoc) { |
---|
242 | associatedData_ = assoc; |
---|
243 | changes_.firePropertyChange("associatedDataModified", |
---|
244 | null, |
---|
245 | assoc); |
---|
246 | } |
---|
247 | /** |
---|
248 | * Get the associated data. |
---|
249 | */ |
---|
250 | public SGTLine getAssociatedData() { |
---|
251 | return associatedData_; |
---|
252 | } |
---|
253 | /** |
---|
254 | * Is there associated data? |
---|
255 | */ |
---|
256 | public boolean hasAssociatedData() { |
---|
257 | return (associatedData_ != null); |
---|
258 | } |
---|
259 | /** |
---|
260 | * Set the X coordinate metadata. |
---|
261 | */ |
---|
262 | public void setXMetaData(SGTMetaData md) { |
---|
263 | xMetaData_ = md; |
---|
264 | } |
---|
265 | /** |
---|
266 | * Set the Y coordinate metadata. |
---|
267 | */ |
---|
268 | public void setYMetaData(SGTMetaData md) { |
---|
269 | yMetaData_ = md; |
---|
270 | } |
---|
271 | /** |
---|
272 | * Set the title. |
---|
273 | */ |
---|
274 | public void setTitle(String title) { |
---|
275 | title_ = title; |
---|
276 | } |
---|
277 | public SGLabel getKeyTitle() { |
---|
278 | return keyTitle_; |
---|
279 | } |
---|
280 | /** Set the title formatted for the <code>VectorKey</code>. */ |
---|
281 | public void setKeyTitle(SGLabel title) { |
---|
282 | keyTitle_ = title; |
---|
283 | } |
---|
284 | /** |
---|
285 | * Set the X coordinate array. |
---|
286 | * <BR><B>Property Change:</B> <code>dataModified</code>. |
---|
287 | */ |
---|
288 | public void setXArray(double[] xloc) { |
---|
289 | xloc_ = xloc; |
---|
290 | xTime_ = false; |
---|
291 | xRange_ = computeSoTRange(xloc); |
---|
292 | changes_.firePropertyChange("dataModified", |
---|
293 | new Integer(0), |
---|
294 | new Integer(xloc.length)); |
---|
295 | } |
---|
296 | /** |
---|
297 | * Set the Y coordinate array |
---|
298 | * <BR><B>Property Change:</B> <code>dataModified</code>. |
---|
299 | */ |
---|
300 | public void setYArray(double[] yloc) { |
---|
301 | yloc_ = yloc; |
---|
302 | yTime_ = false; |
---|
303 | yRange_ = computeSoTRange(yloc); |
---|
304 | changes_.firePropertyChange("dataModified", |
---|
305 | new Integer(0), |
---|
306 | new Integer(yloc.length)); |
---|
307 | } |
---|
308 | /** |
---|
309 | * Set the Time coordinate array |
---|
310 | * <BR><B>Property Change:</B> <code>dataModified</code>. |
---|
311 | */ |
---|
312 | public void setTimeArray(GeoDate[] tloc) { |
---|
313 | setTimeArray(new GeoDateArray(tloc)); |
---|
314 | } |
---|
315 | /** |
---|
316 | * @since 3.0 |
---|
317 | */ |
---|
318 | public void setTimeArray(GeoDateArray tarray) { |
---|
319 | tloc_ = tarray; |
---|
320 | if(xTime_) { |
---|
321 | xRange_ = computeSoTRange(tarray); |
---|
322 | } else if(yTime_) { |
---|
323 | yRange_ = computeSoTRange(tarray); |
---|
324 | } |
---|
325 | changes_.firePropertyChange("dataModified", |
---|
326 | new Integer(0), |
---|
327 | new Integer(tarray.getLength())); |
---|
328 | } |
---|
329 | public SoTRange getXRange() { |
---|
330 | return xRange_.copy(); |
---|
331 | } |
---|
332 | public SoTRange getYRange() { |
---|
333 | return yRange_.copy(); |
---|
334 | } |
---|
335 | public void addPropertyChangeListener(PropertyChangeListener l) { |
---|
336 | changes_.addPropertyChangeListener(l); |
---|
337 | } |
---|
338 | public void removePropertyChangeListener(PropertyChangeListener l) { |
---|
339 | changes_.removePropertyChangeListener(l); |
---|
340 | } |
---|
341 | private SoTRange computeSoTRange(double[] array) { |
---|
342 | double dstart = Double.POSITIVE_INFINITY; |
---|
343 | double dend = Double.NEGATIVE_INFINITY; |
---|
344 | int count = 0; |
---|
345 | for(int i=0; i < array.length; i++) { |
---|
346 | if(!Double.isNaN(array[i])) { |
---|
347 | dstart = Math.min(dstart, array[i]); |
---|
348 | dend = Math.max(dend, array[i]); |
---|
349 | count++; |
---|
350 | } |
---|
351 | } |
---|
352 | if(count == 0) { |
---|
353 | return new SoTRange.Double(Double.NaN, Double.NaN); |
---|
354 | } else { |
---|
355 | return new SoTRange.Double(dstart, dend); |
---|
356 | } |
---|
357 | } |
---|
358 | private SoTRange computeSoTRange(GeoDateArray tarray) { |
---|
359 | long tstart = Long.MAX_VALUE; |
---|
360 | long tend = Long.MIN_VALUE; |
---|
361 | long[] tar = tarray.getTime(); |
---|
362 | int count = 0; |
---|
363 | for(int i=0; i < tar.length; i++) { |
---|
364 | if(!(tar[i] == Long.MAX_VALUE)) { |
---|
365 | tstart = Math.min(tstart, tar[i]); |
---|
366 | tend = Math.max(tend, tar[i]); |
---|
367 | count++; |
---|
368 | } |
---|
369 | } |
---|
370 | if(count == 0) { |
---|
371 | return new SoTRange.Time(Long.MAX_VALUE, |
---|
372 | Long.MAX_VALUE); |
---|
373 | } else { |
---|
374 | return new SoTRange.Time(tstart, tend); |
---|
375 | } |
---|
376 | } |
---|
377 | } |
---|