1 | /* |
---|
2 | * $Id: ContourLevels.java,v 1.20 2002/02/25 19:01:11 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; |
---|
14 | |
---|
15 | import gov.noaa.pmel.util.Range2D; |
---|
16 | |
---|
17 | import java.util.Vector; |
---|
18 | import java.util.Hashtable; |
---|
19 | import java.util.Enumeration; |
---|
20 | import java.awt.Color; |
---|
21 | |
---|
22 | import java.beans.PropertyChangeListener; |
---|
23 | import java.beans.PropertyChangeSupport; |
---|
24 | |
---|
25 | /** |
---|
26 | * Contains levels and line styles for contour graphics. |
---|
27 | * |
---|
28 | * @author Donald Denbo |
---|
29 | * @version $Revision: 1.20 $, $Date: 2002/02/25 19:01:11 $ |
---|
30 | * @since 2.0 |
---|
31 | */ |
---|
32 | public class ContourLevels implements Cloneable { |
---|
33 | /** |
---|
34 | * @label defaultAttr |
---|
35 | * @link aggregationByValue |
---|
36 | * @supplierCardinality 1 |
---|
37 | */ |
---|
38 | private DefaultContourLineAttribute defaultAttr_ = |
---|
39 | new DefaultContourLineAttribute(); |
---|
40 | private Vector levels_ = new Vector(); |
---|
41 | private Hashtable lineAttrMap_ = new Hashtable(); |
---|
42 | private boolean sorted_ = false; |
---|
43 | /** |
---|
44 | * @label solid |
---|
45 | * @link aggregationByValue |
---|
46 | */ |
---|
47 | static private ContourLineAttribute solid_ = |
---|
48 | new ContourLineAttribute(ContourLineAttribute.SOLID); |
---|
49 | |
---|
50 | /** |
---|
51 | * @label heavy |
---|
52 | * @link aggregationByValue |
---|
53 | */ |
---|
54 | static private ContourLineAttribute heavy_ = |
---|
55 | new ContourLineAttribute(ContourLineAttribute.HEAVY); |
---|
56 | |
---|
57 | /** |
---|
58 | * @label dashed |
---|
59 | * @link aggregationByValue |
---|
60 | */ |
---|
61 | static private ContourLineAttribute dashed_ = |
---|
62 | new ContourLineAttribute(ContourLineAttribute.DASHED); |
---|
63 | |
---|
64 | private PropertyChangeSupport changes_ = |
---|
65 | new PropertyChangeSupport(this); |
---|
66 | |
---|
67 | /** |
---|
68 | * @directed |
---|
69 | * @label lineAttrMap |
---|
70 | * @link aggregation |
---|
71 | * @supplierCardinality 1..* |
---|
72 | */ |
---|
73 | private ContourLineAttribute lnkLineAttribute; |
---|
74 | /** |
---|
75 | * Construct a default <code>ContourLevels</code> object from a double[]. |
---|
76 | */ |
---|
77 | static public ContourLevels getDefault(double[] array) { |
---|
78 | ContourLevels cl = new ContourLevels(); |
---|
79 | double val = 0.0; |
---|
80 | for(int i=0; i < array.length; i++) { |
---|
81 | cl.addLevel(array[i]); |
---|
82 | } |
---|
83 | return cl; |
---|
84 | } |
---|
85 | /** |
---|
86 | * Construct a default <code>ContourLevels</code> object from a |
---|
87 | * <code>Range2D</code>. |
---|
88 | */ |
---|
89 | static public ContourLevels getDefault(Range2D range) { |
---|
90 | ContourLevels cl = new ContourLevels(); |
---|
91 | double val = range.start; |
---|
92 | while(val <= range.end) { |
---|
93 | cl.addLevel(val); |
---|
94 | val = val + range.delta; |
---|
95 | } |
---|
96 | return cl; |
---|
97 | } |
---|
98 | /** |
---|
99 | * Create a deep copy. |
---|
100 | */ |
---|
101 | public ContourLevels copy() { |
---|
102 | ContourLevels newcls; |
---|
103 | try { |
---|
104 | newcls = (ContourLevels)clone(); |
---|
105 | // newcls.defaultAttr_ = |
---|
106 | // (DefaultContourLineAttribute)this.defaultAttr_.copy(); |
---|
107 | newcls.levels_ = (Vector)this.levels_.clone(); |
---|
108 | newcls.lineAttrMap_ = (Hashtable)this.lineAttrMap_.clone(); |
---|
109 | } catch (CloneNotSupportedException e) { |
---|
110 | newcls = null; |
---|
111 | } |
---|
112 | return newcls; |
---|
113 | } |
---|
114 | /** |
---|
115 | * Get the contour level elements. |
---|
116 | */ |
---|
117 | public Enumeration levelElements() { |
---|
118 | if(!sorted_) sort(); |
---|
119 | return levels_.elements(); |
---|
120 | } |
---|
121 | /** |
---|
122 | * Set a the <code>ContourLineAttribute</code> for a value. |
---|
123 | */ |
---|
124 | public void setContourLineAttribute(double val, ContourLineAttribute l) |
---|
125 | throws ContourLevelNotFoundException { |
---|
126 | throw new MethodNotImplementedError(); |
---|
127 | } |
---|
128 | /** |
---|
129 | * Set a the <code>ContourLineAttribute</code> for an index. |
---|
130 | */ |
---|
131 | public void setContourLineAttribute(int indx, ContourLineAttribute l) |
---|
132 | throws ContourLevelNotFoundException { |
---|
133 | throw new MethodNotImplementedError(); |
---|
134 | } |
---|
135 | /** |
---|
136 | * Get the <code>ContourLineAttribute</code> for a value. |
---|
137 | */ |
---|
138 | public ContourLineAttribute getContourLineAttribute(double val) |
---|
139 | throws ContourLevelNotFoundException { |
---|
140 | ContourLineAttribute attr = |
---|
141 | (ContourLineAttribute)lineAttrMap_.get(new Double(val)); |
---|
142 | if(attr == null) { |
---|
143 | throw new ContourLevelNotFoundException(); |
---|
144 | } |
---|
145 | return attr; |
---|
146 | } |
---|
147 | /** |
---|
148 | * Get the <code>ContourLineAttribute</code> for an index. |
---|
149 | */ |
---|
150 | public ContourLineAttribute getContourLineAttribute(int indx) |
---|
151 | throws ContourLevelNotFoundException { |
---|
152 | if(!sorted_) sort(); |
---|
153 | return getContourLineAttribute(getLevel(indx)); |
---|
154 | } |
---|
155 | /** |
---|
156 | * Get the <code>DefaultContourLineAtrribute</code> |
---|
157 | */ |
---|
158 | public DefaultContourLineAttribute getDefaultContourLineAttribute() { |
---|
159 | return defaultAttr_; |
---|
160 | } |
---|
161 | /** |
---|
162 | * Get the <code>DefaultContourLineAttribute</code> for index. |
---|
163 | */ |
---|
164 | public DefaultContourLineAttribute getDefaultContourLineAttribute(int indx) |
---|
165 | throws ContourLevelNotFoundException { |
---|
166 | if(!sorted_) sort(); |
---|
167 | return |
---|
168 | defaultAttr_.setContourLineAttribute(getContourLineAttribute(getLevel(indx))); |
---|
169 | } |
---|
170 | /** |
---|
171 | * Get the <code>DefaultContourLineAttribute</code> for value. |
---|
172 | */ |
---|
173 | public DefaultContourLineAttribute getDefaultContourLineAttribute(double val) |
---|
174 | throws ContourLevelNotFoundException { |
---|
175 | if(!sorted_) sort(); |
---|
176 | return |
---|
177 | defaultAttr_.setContourLineAttribute(getContourLineAttribute(val)); |
---|
178 | } |
---|
179 | /** |
---|
180 | * Set the <code>DefaultContourLineAttribute</code> |
---|
181 | */ |
---|
182 | public void setDefaultContourLineAttribute(DefaultContourLineAttribute attr) { |
---|
183 | defaultAttr_ = attr; |
---|
184 | } |
---|
185 | /** |
---|
186 | * Add a contour level with default <code>ContourLineAttribute</code>. |
---|
187 | */ |
---|
188 | public void addLevel(double val) { |
---|
189 | ContourLineAttribute attr = null; |
---|
190 | if(val < 0.0) { |
---|
191 | attr = (ContourLineAttribute)dashed_.copy(); |
---|
192 | } else if (val > 0.0) { |
---|
193 | attr = (ContourLineAttribute)solid_.copy(); |
---|
194 | } else { |
---|
195 | attr = (ContourLineAttribute)heavy_.copy(); |
---|
196 | } |
---|
197 | attr.setStyleOverridden(true); |
---|
198 | addLevel(val, attr); |
---|
199 | } |
---|
200 | /** |
---|
201 | * Add a contour level with a specified |
---|
202 | * <code>ContourLineAttribute</code>. |
---|
203 | */ |
---|
204 | public void addLevel(double val, ContourLineAttribute l) { |
---|
205 | Double value = new Double(val); |
---|
206 | levels_.addElement(value); |
---|
207 | lineAttrMap_.put(value, l); |
---|
208 | sorted_ = false; |
---|
209 | } |
---|
210 | /** |
---|
211 | * Get the value of level by index. |
---|
212 | */ |
---|
213 | public double getLevel(int indx) |
---|
214 | throws ContourLevelNotFoundException { |
---|
215 | if(indx < 0 || indx >= levels_.size()) |
---|
216 | throw new ContourLevelNotFoundException(); |
---|
217 | if(!sorted_) sort(); |
---|
218 | Double value = (Double)levels_.elementAt(indx); |
---|
219 | return value.doubleValue(); |
---|
220 | } |
---|
221 | /** |
---|
222 | * Remove a level by value. |
---|
223 | */ |
---|
224 | public void removeLevel(double val) |
---|
225 | throws ContourLevelNotFoundException { |
---|
226 | throw new MethodNotImplementedError(); |
---|
227 | } |
---|
228 | /** |
---|
229 | * Remove a level by index. |
---|
230 | */ |
---|
231 | public void removeLevel(int indx) |
---|
232 | throws ContourLevelNotFoundException { |
---|
233 | throw new MethodNotImplementedError(); |
---|
234 | } |
---|
235 | /** |
---|
236 | * Get the index of a level by value |
---|
237 | */ |
---|
238 | public int getIndex(Double dval) { |
---|
239 | if(!sorted_) sort(); |
---|
240 | return levels_.indexOf(dval); |
---|
241 | } |
---|
242 | /** |
---|
243 | * Get the index of a level by value |
---|
244 | */ |
---|
245 | public int getIndex(double val) { |
---|
246 | if(!sorted_) sort(); |
---|
247 | return getIndex(new Double(val)); |
---|
248 | } |
---|
249 | /** |
---|
250 | * Get the maximum level index. |
---|
251 | */ |
---|
252 | public int getMaximumIndex() { |
---|
253 | return levels_.size() - 1; |
---|
254 | } |
---|
255 | /** |
---|
256 | * Get the range of levels |
---|
257 | */ |
---|
258 | public Range2D getRange() { |
---|
259 | double min = Double.MAX_VALUE; |
---|
260 | double max = Double.MIN_VALUE; |
---|
261 | double value; |
---|
262 | for(int i=0; i < levels_.size(); i++) { |
---|
263 | value = ((Double)levels_.get(i)).doubleValue(); |
---|
264 | min = Math.min(min, value); |
---|
265 | max = Math.max(max, value); |
---|
266 | } |
---|
267 | return new Range2D(min, max); |
---|
268 | } |
---|
269 | /** |
---|
270 | * Get the number of levels. |
---|
271 | */ |
---|
272 | public int size() { |
---|
273 | return levels_.size(); |
---|
274 | } |
---|
275 | |
---|
276 | private void sort() { |
---|
277 | // |
---|
278 | // use brain-dead bubble sort (there will be few lines) |
---|
279 | // |
---|
280 | int i, temp; |
---|
281 | int size = levels_.size(); |
---|
282 | Double a, b; |
---|
283 | int[] index = new int[size]; |
---|
284 | boolean flipped = true; |
---|
285 | for(i=0; i < size; i++) { |
---|
286 | index[i] = i; |
---|
287 | } |
---|
288 | while(flipped) { |
---|
289 | flipped = false; |
---|
290 | for(i=0; i < size-1; i++) { |
---|
291 | a = (Double)levels_.elementAt(index[i]); |
---|
292 | b = (Double)levels_.elementAt(index[i+1]); |
---|
293 | if(a.doubleValue() > b.doubleValue()) { |
---|
294 | // if(a.compareTo(b) > 0) { // jdk1.2 |
---|
295 | temp = index[i]; |
---|
296 | index[i] = index[i+1]; |
---|
297 | index[i+1] = temp; |
---|
298 | flipped = true; |
---|
299 | } |
---|
300 | } |
---|
301 | } |
---|
302 | Vector oldValues = levels_; |
---|
303 | levels_ = new Vector(size); |
---|
304 | for(i=0; i < size; i++) { |
---|
305 | levels_.addElement(oldValues.elementAt(index[i])); |
---|
306 | } |
---|
307 | sorted_ = true; |
---|
308 | } |
---|
309 | |
---|
310 | /** |
---|
311 | * Add listener to changes in <code>ColorMap</code> properties. |
---|
312 | */ |
---|
313 | public void addPropertyChangeListener(PropertyChangeListener listener) { |
---|
314 | changes_.addPropertyChangeListener(listener); |
---|
315 | } |
---|
316 | public void removePropertyChangeListener(PropertyChangeListener listener) { |
---|
317 | changes_.removePropertyChangeListener(listener); |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|