1 | /* |
---|
2 | * $Id: LineAttribute.java,v 1.20 2003/09/17 20:32:10 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 java.awt.*; |
---|
16 | import java.beans.PropertyChangeListener; |
---|
17 | import java.beans.PropertyChangeSupport; |
---|
18 | |
---|
19 | /** |
---|
20 | * Sets the rendering style for line data. |
---|
21 | * <code>Color</code>, width, and dash characteristics are |
---|
22 | * <code>LineAttribute</code> properties. |
---|
23 | * |
---|
24 | * @author Donald Denbo |
---|
25 | * @version $Revision: 1.20 $, $Date: 2003/09/17 20:32:10 $ |
---|
26 | * @since 1.0 |
---|
27 | * @see LineCartesianRenderer |
---|
28 | * @see ContourLevels |
---|
29 | */ |
---|
30 | public class LineAttribute implements Attribute, Cloneable { |
---|
31 | protected transient PropertyChangeSupport changes_ = new PropertyChangeSupport(this); |
---|
32 | |
---|
33 | private boolean batch_ = false; |
---|
34 | private boolean local_ = true; |
---|
35 | private boolean modified_ = false; |
---|
36 | private String id_ = null; |
---|
37 | private Color color_ = Color.black; |
---|
38 | private int style_; |
---|
39 | private int mark_ = 1; |
---|
40 | private double markHeightP_ = 0.2; |
---|
41 | private float width_ = 2.0f; |
---|
42 | private float dashes_[] = {12.0f, 12.0f}; |
---|
43 | private float dashPhase_ = 0.0f; |
---|
44 | private int cap_style_ = CAP_SQUARE; |
---|
45 | private int miter_style_ = JOIN_MITER; |
---|
46 | private float miter_limit_ = 10.0f; |
---|
47 | |
---|
48 | private static float HEAVY_WIDTH = 2.0f; |
---|
49 | /** |
---|
50 | * Solid line style. |
---|
51 | */ |
---|
52 | public static final int SOLID = 0; |
---|
53 | /** |
---|
54 | * Dashed line style. |
---|
55 | */ |
---|
56 | public static final int DASHED = 1; |
---|
57 | /** |
---|
58 | * Heavy line style |
---|
59 | * @since 2.0 |
---|
60 | */ |
---|
61 | public static final int HEAVY = 2; |
---|
62 | /** |
---|
63 | * Highlighted line style. Accomplished by drawing |
---|
64 | * the line over a contrasting polygon of the same shape. |
---|
65 | */ |
---|
66 | public static final int HIGHLIGHT = 3; |
---|
67 | /** |
---|
68 | * Mark line style. |
---|
69 | * |
---|
70 | * @see PlotMark |
---|
71 | **/ |
---|
72 | public static final int MARK = 4; |
---|
73 | /** |
---|
74 | * Mark with connecting lines style. |
---|
75 | */ |
---|
76 | public static final int MARK_LINE = 5; |
---|
77 | /** |
---|
78 | * Stroke. |
---|
79 | */ |
---|
80 | public static final int STROKE = 6; |
---|
81 | /** |
---|
82 | * Cap styles |
---|
83 | */ |
---|
84 | public static final int CAP_BUTT = 0; |
---|
85 | public static final int CAP_ROUND = 1; |
---|
86 | public static final int CAP_SQUARE = 2; |
---|
87 | /** |
---|
88 | * Join styles |
---|
89 | */ |
---|
90 | public static final int JOIN_MITER = 0; |
---|
91 | public static final int JOIN_ROUND = 1; |
---|
92 | public static final int JOIN_BEVEL = 2; |
---|
93 | |
---|
94 | |
---|
95 | /** |
---|
96 | * Default constructor. Default style is SOLID and |
---|
97 | * default color is red. |
---|
98 | **/ |
---|
99 | public LineAttribute() { |
---|
100 | this(SOLID, Color.red); |
---|
101 | } |
---|
102 | /** |
---|
103 | * Construct <code>LineAttribute</code> with <code>Color.black</code>. |
---|
104 | */ |
---|
105 | public LineAttribute(int style) { |
---|
106 | style_ = style; |
---|
107 | if(style_ == HEAVY) width_ = HEAVY_WIDTH; |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * <code>LineAttribute</code> constructor. |
---|
112 | * |
---|
113 | * @param style line style |
---|
114 | * @param color line <code>Color</code> |
---|
115 | * @see java.awt.Color |
---|
116 | **/ |
---|
117 | public LineAttribute(int style,Color color) { |
---|
118 | this(style, 1, color); |
---|
119 | } |
---|
120 | /** |
---|
121 | * <code>LineAttribute</code> constructor for plot marks. |
---|
122 | * |
---|
123 | * @param style line sytle |
---|
124 | * @param mark plot mark |
---|
125 | * @param color line <code>Color</code> |
---|
126 | **/ |
---|
127 | public LineAttribute(int style,int mark,Color color) { |
---|
128 | style_ = style; |
---|
129 | mark_ = mark; |
---|
130 | color_ = color; |
---|
131 | } |
---|
132 | /** |
---|
133 | * Copy the <code>LineAttribute</code>. |
---|
134 | * |
---|
135 | * @return new <code>LineAttribute</code> |
---|
136 | */ |
---|
137 | public Object copy() { |
---|
138 | LineAttribute newLine; |
---|
139 | try { |
---|
140 | newLine = (LineAttribute)clone(); |
---|
141 | } catch (CloneNotSupportedException e) { |
---|
142 | newLine = new LineAttribute(); |
---|
143 | } |
---|
144 | return newLine; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * @since 3.0 |
---|
149 | */ |
---|
150 | public boolean equals(Object obj) { |
---|
151 | if(obj == null || !(obj instanceof LineAttribute)) return false; |
---|
152 | LineAttribute attr = (LineAttribute)obj; |
---|
153 | if((id_ != attr.getId()) || |
---|
154 | (!color_.equals(attr.getColor())) || |
---|
155 | (style_ != attr.getStyle())) return false; |
---|
156 | if(style_ == MARK || style_ == MARK_LINE) { |
---|
157 | if((mark_ != attr.getMark()) || (markHeightP_ != attr.getMarkHeightP())) |
---|
158 | return false; |
---|
159 | } |
---|
160 | if(style_ == HEAVY) { |
---|
161 | if(width_ != attr.getWidth()) return false; |
---|
162 | } |
---|
163 | if(style_ == STROKE) { |
---|
164 | if(width_ != attr.getWidth()) return false; |
---|
165 | if(dashes_.length != attr.getDashArray().length) { |
---|
166 | return false; |
---|
167 | } else { |
---|
168 | float[] dar = attr.getDashArray(); |
---|
169 | for(int i=0; i < dashes_.length; i++) { |
---|
170 | if(dashes_[i] != dar[i]) return false; |
---|
171 | } |
---|
172 | } |
---|
173 | if((dashPhase_ != attr.getDashPhase()) || |
---|
174 | (cap_style_ != attr.getCapStyle()) || |
---|
175 | (miter_style_ != attr.getMiterStyle()) || |
---|
176 | (miter_limit_ != attr.getMiterLimit())) return false; |
---|
177 | } |
---|
178 | return true; |
---|
179 | } |
---|
180 | /** |
---|
181 | * Set mark height. |
---|
182 | * <BR><B>Property Change:</B> <code>markHeightP</code>. |
---|
183 | * |
---|
184 | * @param markh mark height |
---|
185 | **/ |
---|
186 | public void setMarkHeightP(double markh) { |
---|
187 | if(markHeightP_ != markh) { |
---|
188 | Double tempOld = new Double(markHeightP_); |
---|
189 | markHeightP_ = markh; |
---|
190 | firePropertyChange("markHeightP", |
---|
191 | tempOld, |
---|
192 | new Double(markHeightP_)); |
---|
193 | } |
---|
194 | } |
---|
195 | /** |
---|
196 | * Get mark height |
---|
197 | * |
---|
198 | * @return mark height |
---|
199 | **/ |
---|
200 | public double getMarkHeightP() { |
---|
201 | return markHeightP_; |
---|
202 | } |
---|
203 | /** |
---|
204 | * Set the line style. |
---|
205 | * <BR><B>Property Change:</B> <code>style</code>. |
---|
206 | * |
---|
207 | * @param st line style |
---|
208 | **/ |
---|
209 | public void setStyle(int st) { |
---|
210 | if(style_ != st) { |
---|
211 | Integer tempOld = new Integer(style_); |
---|
212 | style_ = st; |
---|
213 | firePropertyChange("style", |
---|
214 | tempOld, |
---|
215 | new Integer(style_)); |
---|
216 | } |
---|
217 | } |
---|
218 | /** |
---|
219 | * Set the line <code>Color</code>. |
---|
220 | * <BR><B>Property Change:</B> <code>color</code>. |
---|
221 | * |
---|
222 | * @param c line <code>Color</code> |
---|
223 | **/ |
---|
224 | public void setColor(Color c) { |
---|
225 | if(!color_.equals(c)) { |
---|
226 | Color tempOld = color_; |
---|
227 | color_ = c; |
---|
228 | firePropertyChange("color", |
---|
229 | tempOld, |
---|
230 | color_); |
---|
231 | } |
---|
232 | } |
---|
233 | /** |
---|
234 | * Set the line width in physical units. |
---|
235 | * <BR><B>Property Change:</B> <code>width</code>. |
---|
236 | * |
---|
237 | * @param t line width |
---|
238 | **/ |
---|
239 | public void setWidth(float t) { |
---|
240 | if(width_ != t) { |
---|
241 | Float tempOld = new Float(width_); |
---|
242 | width_ = t; |
---|
243 | firePropertyChange("width", |
---|
244 | tempOld, |
---|
245 | new Float(width_)); |
---|
246 | } |
---|
247 | } |
---|
248 | /** |
---|
249 | * Set the dash characteristics. Lengths are in physical units. |
---|
250 | * <BR><B>Property Change:</B> <code>dashArray</code>. |
---|
251 | * |
---|
252 | **/ |
---|
253 | public void setDashArray(float[] dashes) { |
---|
254 | if(dashes == null) return; |
---|
255 | boolean changed = false; |
---|
256 | if(dashes_.length != dashes.length) { |
---|
257 | changed = true; |
---|
258 | } else { |
---|
259 | for(int i = 0; i < dashes_.length; i++) { |
---|
260 | if(dashes_[i] != dashes[i]) { |
---|
261 | changed = true; |
---|
262 | break; |
---|
263 | } |
---|
264 | } |
---|
265 | } |
---|
266 | if(changed) { |
---|
267 | float[] tempOld = dashes_; |
---|
268 | dashes_ = dashes; |
---|
269 | firePropertyChange("dashArray", |
---|
270 | tempOld, |
---|
271 | dashes_); |
---|
272 | } |
---|
273 | } |
---|
274 | /** |
---|
275 | * Get line dash array. |
---|
276 | * @since 2.0 |
---|
277 | */ |
---|
278 | public float[] getDashArray() { |
---|
279 | return dashes_; |
---|
280 | } |
---|
281 | /** |
---|
282 | * Set line dash phase. |
---|
283 | * <BR><B>Property Change:</B> <code>dashPhase</code>. |
---|
284 | * @since 2.0 |
---|
285 | */ |
---|
286 | public void setDashPhase(float phase) { |
---|
287 | if(dashPhase_ != phase) { |
---|
288 | Float tempOld = new Float(dashPhase_); |
---|
289 | dashPhase_ = phase; |
---|
290 | firePropertyChange("dashPhase", |
---|
291 | tempOld, |
---|
292 | new Float(dashPhase_)); |
---|
293 | } |
---|
294 | } |
---|
295 | /** |
---|
296 | * Get line dash phase. |
---|
297 | * @since 2.0 |
---|
298 | */ |
---|
299 | public float getDashPhase() { |
---|
300 | return dashPhase_; |
---|
301 | } |
---|
302 | /** |
---|
303 | * Get line style. |
---|
304 | * |
---|
305 | * @return line style |
---|
306 | **/ |
---|
307 | public int getStyle() { |
---|
308 | return style_; |
---|
309 | } |
---|
310 | /** |
---|
311 | * Get line <code>Color</code>. |
---|
312 | * |
---|
313 | * @return line <code>Color</code> |
---|
314 | **/ |
---|
315 | public Color getColor() { |
---|
316 | return color_; |
---|
317 | } |
---|
318 | /** |
---|
319 | * Get line width. |
---|
320 | * |
---|
321 | * @return line width in physcial coordinates. |
---|
322 | **/ |
---|
323 | public float getWidth() { |
---|
324 | return width_; |
---|
325 | } |
---|
326 | /** |
---|
327 | * Set plot mark |
---|
328 | * <BR><B>Property Change:</B> <code>mark</code>. |
---|
329 | * |
---|
330 | * @param mark the plot mark |
---|
331 | **/ |
---|
332 | public void setMark(int mark) { |
---|
333 | if(mark_ != mark) { |
---|
334 | Integer tempOld = new Integer(mark_); |
---|
335 | if(mark <= 0) mark = 1; |
---|
336 | if(mark > 51) mark = 51; |
---|
337 | mark_ = mark; |
---|
338 | firePropertyChange("mark", |
---|
339 | tempOld, |
---|
340 | new Integer(mark_)); |
---|
341 | } |
---|
342 | } |
---|
343 | /** |
---|
344 | * Get plot mark |
---|
345 | * |
---|
346 | * @return plot mark |
---|
347 | **/ |
---|
348 | public int getMark() { |
---|
349 | return mark_; |
---|
350 | } |
---|
351 | /** |
---|
352 | * Set the current line cap style. Cap styles include |
---|
353 | * <code>CAP_BUTT</code>, <code>CAP_ROUND</code>, and |
---|
354 | * <code>CAP_SQUARE</code>. |
---|
355 | * <BR><B>Property Change:</B> <code>capStyle</code>. |
---|
356 | */ |
---|
357 | public void setCapStyle(int style) { |
---|
358 | if(cap_style_ != style) { |
---|
359 | Integer tempOld = new Integer(cap_style_); |
---|
360 | cap_style_ = style; |
---|
361 | firePropertyChange("capStyle", |
---|
362 | tempOld, |
---|
363 | new Integer(cap_style_)); |
---|
364 | } |
---|
365 | } |
---|
366 | /** |
---|
367 | * Get the current line cap style. |
---|
368 | */ |
---|
369 | public int getCapStyle() { |
---|
370 | return cap_style_; |
---|
371 | } |
---|
372 | /** |
---|
373 | * Set the current miter style. Styles include |
---|
374 | * <code>JOIN_MITER</code>, <code>JOIN_ROUND</code>, and |
---|
375 | * <code>JOIN_BEVEL</code>. |
---|
376 | * <BR><B>Property Change:</B> <code>miterStyle</code>. */ |
---|
377 | public void setMiterStyle(int style) { |
---|
378 | if(miter_style_ != style) { |
---|
379 | Integer tempOld = new Integer(miter_style_); |
---|
380 | miter_style_ = style; |
---|
381 | firePropertyChange("miterStyle", |
---|
382 | tempOld, |
---|
383 | new Integer(miter_style_)); |
---|
384 | } |
---|
385 | } |
---|
386 | /** |
---|
387 | * Get the current miter sytle. |
---|
388 | */ |
---|
389 | public int getMiterStyle() { |
---|
390 | return miter_style_; |
---|
391 | } |
---|
392 | /** |
---|
393 | * Set the miter limit. |
---|
394 | * <BR><B>Property Change:</B> <code>miterLimit</code>. |
---|
395 | */ |
---|
396 | public void setMiterLimit(float limit) { |
---|
397 | if(miter_limit_ != limit) { |
---|
398 | Float tempOld = new Float(miter_limit_); |
---|
399 | miter_limit_ = limit; |
---|
400 | firePropertyChange("miterLimit", |
---|
401 | tempOld, |
---|
402 | new Float(miter_limit_)); |
---|
403 | } |
---|
404 | } |
---|
405 | /** |
---|
406 | * Get the current miter limit. |
---|
407 | */ |
---|
408 | public float getMiterLimit() { |
---|
409 | return miter_limit_; |
---|
410 | } |
---|
411 | /** |
---|
412 | * Get a <code>String</code> representation of the |
---|
413 | * <code>LineAttribute</code>. |
---|
414 | * |
---|
415 | * @return <code>String</code> representation |
---|
416 | */ |
---|
417 | public String toString() { |
---|
418 | String name = getClass().getName(); |
---|
419 | return name.substring(name.lastIndexOf(".")+1); |
---|
420 | } |
---|
421 | /** |
---|
422 | * Add listener to changes in <code>LineAttribute</code> properties. |
---|
423 | */ |
---|
424 | public void addPropertyChangeListener(PropertyChangeListener listener) { |
---|
425 | if(changes_ == null) changes_ = new PropertyChangeSupport(this); |
---|
426 | changes_.addPropertyChangeListener(listener); |
---|
427 | } |
---|
428 | public void removePropertyChangeListener(PropertyChangeListener listener) { |
---|
429 | changes_.removePropertyChangeListener(listener); |
---|
430 | } |
---|
431 | /** |
---|
432 | * @since 3.0 |
---|
433 | */ |
---|
434 | public void setId(String id) { |
---|
435 | id_ = id; |
---|
436 | } |
---|
437 | /** |
---|
438 | * @since 3.0 |
---|
439 | */ |
---|
440 | public String getId() { |
---|
441 | return id_; |
---|
442 | } |
---|
443 | |
---|
444 | protected void firePropertyChange(String name, Object oldValue, Object newValue) { |
---|
445 | if(batch_) { |
---|
446 | modified_ = true; |
---|
447 | return; |
---|
448 | } |
---|
449 | AttributeChangeEvent ace = new AttributeChangeEvent(this, name, |
---|
450 | oldValue, newValue, |
---|
451 | local_); |
---|
452 | changes_.firePropertyChange(ace); |
---|
453 | modified_ = false; |
---|
454 | } |
---|
455 | /** |
---|
456 | * @since 3.0 |
---|
457 | */ |
---|
458 | public void setBatch(boolean batch) { |
---|
459 | setBatch(batch, true); |
---|
460 | } |
---|
461 | /** |
---|
462 | * @since 3.0 |
---|
463 | */ |
---|
464 | public void setBatch(boolean batch, boolean local) { |
---|
465 | local_ = local; |
---|
466 | batch_ = batch; |
---|
467 | if(!batch && modified_) firePropertyChange("batch", Boolean.TRUE, Boolean.FALSE); |
---|
468 | } |
---|
469 | /** |
---|
470 | * @since 3.0 |
---|
471 | */ |
---|
472 | public boolean isBatch() { |
---|
473 | return batch_; |
---|
474 | } |
---|
475 | } |
---|