1 | /* |
---|
2 | * $Id: AnnotationCartesianRenderer.java,v 1.12 2003/08/22 23:02:31 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; |
---|
13 | |
---|
14 | import java.awt.*; |
---|
15 | import java.awt.geom.Ellipse2D; |
---|
16 | import java.awt.geom.Rectangle2D; |
---|
17 | import java.awt.geom.GeneralPath; |
---|
18 | //import java.awt.geom.*; |
---|
19 | |
---|
20 | import java.beans.PropertyChangeEvent; |
---|
21 | import java.util.Iterator; |
---|
22 | |
---|
23 | import gov.noaa.pmel.sgt.dm.Annotation; |
---|
24 | import gov.noaa.pmel.sgt.dm.Annote; |
---|
25 | import gov.noaa.pmel.sgt.dm.SGTLine; |
---|
26 | import gov.noaa.pmel.sgt.dm.SGTPoint; |
---|
27 | import gov.noaa.pmel.sgt.dm.SGTData; |
---|
28 | |
---|
29 | import gov.noaa.pmel.util.SoTPoint; |
---|
30 | //import gov.noaa.pmel.util.GeoDate; |
---|
31 | import gov.noaa.pmel.util.SoTValue; |
---|
32 | import gov.noaa.pmel.util.Point2D; |
---|
33 | |
---|
34 | /** |
---|
35 | * Renders <code>Annote</code> and <code>Annotation</code> objects. |
---|
36 | * |
---|
37 | * @author Donald Denbo |
---|
38 | * @version $Revision: 1.12 $ |
---|
39 | * @since 3.0 |
---|
40 | */ |
---|
41 | public class AnnotationCartesianRenderer extends CartesianRenderer { |
---|
42 | /** |
---|
43 | * @link aggregation |
---|
44 | * @undirected |
---|
45 | * @label annotation |
---|
46 | * @supplierCardinality 1 |
---|
47 | */ |
---|
48 | private Annotation data_ = null; |
---|
49 | |
---|
50 | public AnnotationCartesianRenderer(CartesianGraph cg, |
---|
51 | Annotation annotation, |
---|
52 | Attribute attr) { |
---|
53 | cg_ = cg; |
---|
54 | data_ = annotation; |
---|
55 | } |
---|
56 | |
---|
57 | public Attribute getAttribute() { |
---|
58 | return null; |
---|
59 | } |
---|
60 | |
---|
61 | public CartesianGraph getCartesianGraph() { |
---|
62 | return cg_; |
---|
63 | } |
---|
64 | |
---|
65 | public SGTData getDataAt(Point pt) { |
---|
66 | SGTData data = null; |
---|
67 | Annote note = null; |
---|
68 | Iterator iter; |
---|
69 | // System.out.println("point: " + pt); |
---|
70 | Rectangle bnds; |
---|
71 | |
---|
72 | if(data_.hasLine()) { |
---|
73 | iter = data_.getLineIterator(); |
---|
74 | while(iter.hasNext()) { |
---|
75 | note = (Annote)iter.next(); |
---|
76 | bnds = note.getBounds(cg_); |
---|
77 | // System.out.println("Line.Bounds: " + bnds); |
---|
78 | if(note.getBounds(cg_).contains(pt)) { |
---|
79 | return note; |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | if(data_.hasPoint()) { |
---|
84 | iter = data_.getPointIterator(); |
---|
85 | while(iter.hasNext()) { |
---|
86 | note = (Annote)iter.next(); |
---|
87 | bnds = note.getBounds(cg_); |
---|
88 | // System.out.println("Point.Bounds: " + bnds); |
---|
89 | if(bnds.contains(pt)) { |
---|
90 | return note; |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | if(data_.hasText()) { |
---|
95 | iter = data_.getTextIterator(); |
---|
96 | while(iter.hasNext()) { |
---|
97 | note = (Annote)iter.next(); |
---|
98 | bnds = note.getBounds(cg_); |
---|
99 | // System.out.println("Text.Bounds: " + bnds); |
---|
100 | if(bnds.contains(pt)) { |
---|
101 | return note; |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | if(data_.hasOval()) { |
---|
106 | iter = data_.getOvalIterator(); |
---|
107 | while(iter.hasNext()) { |
---|
108 | note = (Annote)iter.next(); |
---|
109 | bnds = note.getBounds(cg_); |
---|
110 | // System.out.println("Oval.Bounds: " + bnds); |
---|
111 | if(bnds.contains(pt)) { |
---|
112 | return note; |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | if(data_.hasRect()) { |
---|
117 | iter = data_.getRectIterator(); |
---|
118 | while(iter.hasNext()) { |
---|
119 | note = (Annote)iter.next(); |
---|
120 | bnds = note.getBounds(cg_); |
---|
121 | // System.out.println("Rect.Bounds: " + bnds); |
---|
122 | if(bnds.contains(pt)) { |
---|
123 | return note; |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | return data; |
---|
128 | } |
---|
129 | /** |
---|
130 | * Render Annotation using java.awt.Graphic2D primatives. |
---|
131 | */ |
---|
132 | public void draw(Graphics g) { |
---|
133 | if(cg_.clipping_) { |
---|
134 | // System.out.println("clipping: on"); |
---|
135 | int xmin, xmax, ymin, ymax; |
---|
136 | int x, y, width, height; |
---|
137 | if(cg_.xTransform_.isSpace()) { |
---|
138 | xmin = cg_.getXUtoD(cg_.xClipRange_.start); |
---|
139 | xmax = cg_.getXUtoD(cg_.xClipRange_.end); |
---|
140 | } else { |
---|
141 | xmin = cg_.getXUtoD(cg_.tClipRange_.start); |
---|
142 | xmax = cg_.getXUtoD(cg_.tClipRange_.end); |
---|
143 | } |
---|
144 | if(cg_.yTransform_.isSpace()) { |
---|
145 | ymin = cg_.getYUtoD(cg_.yClipRange_.start); |
---|
146 | ymax = cg_.getYUtoD(cg_.yClipRange_.end); |
---|
147 | } else { |
---|
148 | ymin = cg_.getYUtoD(cg_.tClipRange_.start); |
---|
149 | ymax = cg_.getYUtoD(cg_.tClipRange_.end); |
---|
150 | } |
---|
151 | if(xmin < xmax) { |
---|
152 | x = xmin; |
---|
153 | width = xmax - xmin; |
---|
154 | } else { |
---|
155 | x=xmax; |
---|
156 | width = xmin - xmax; |
---|
157 | } |
---|
158 | if(ymin < ymax) { |
---|
159 | y = ymin; |
---|
160 | height = ymax - ymin; |
---|
161 | } else { |
---|
162 | y = ymax; |
---|
163 | height = ymin - ymax; |
---|
164 | } |
---|
165 | g.setClip(x, y, width, height); |
---|
166 | } |
---|
167 | Graphics2D g2 = (Graphics2D)g; |
---|
168 | Iterator iter; |
---|
169 | if(data_.hasLine()) { |
---|
170 | SGTLine line; |
---|
171 | LineAttribute attr; |
---|
172 | Annote.Line pLine; |
---|
173 | iter = data_.getLineIterator(); |
---|
174 | while(iter.hasNext()) { |
---|
175 | pLine = (Annote.Line)iter.next(); |
---|
176 | line = pLine.getLine(); |
---|
177 | attr = pLine.getAttribute(); |
---|
178 | // |
---|
179 | renderLine(g2, line, attr); |
---|
180 | } |
---|
181 | } |
---|
182 | if(data_.hasPoint()) { |
---|
183 | SGTPoint point; |
---|
184 | PointAttribute attr; |
---|
185 | Annote.Point pPoint; |
---|
186 | iter = data_.getPointIterator(); |
---|
187 | while(iter.hasNext()) { |
---|
188 | pPoint = (Annote.Point)iter.next(); |
---|
189 | point = pPoint.getPoint(); |
---|
190 | attr = pPoint.getAttribute(); |
---|
191 | // |
---|
192 | renderPoint(g2, point, attr); |
---|
193 | } |
---|
194 | } |
---|
195 | if(data_.hasText()) { |
---|
196 | SGLabel text; |
---|
197 | SoTPoint location; |
---|
198 | Annote.Text pText; |
---|
199 | iter = data_.getTextIterator(); |
---|
200 | while(iter.hasNext()) { |
---|
201 | pText = (Annote.Text)iter.next(); |
---|
202 | text = pText.getText(); |
---|
203 | location = pText.getLocation(); |
---|
204 | // |
---|
205 | renderText(g2, location, text); |
---|
206 | } |
---|
207 | } |
---|
208 | if(data_.hasOval()) { |
---|
209 | SoTPoint pt1; |
---|
210 | SoTPoint pt2; |
---|
211 | LineAttribute attr; |
---|
212 | Color color; |
---|
213 | Annote.Oval pOval; |
---|
214 | iter = data_.getOvalIterator(); |
---|
215 | while(iter.hasNext()) { |
---|
216 | pOval = (Annote.Oval)iter.next(); |
---|
217 | pt1 = pOval.getUpperLeft(); |
---|
218 | pt2 = pOval.getLowerRight(); |
---|
219 | attr = pOval.getLineAttribute(); |
---|
220 | color = pOval.getFillColor(); |
---|
221 | // |
---|
222 | renderOval(g2, pt1, pt2, attr, color); |
---|
223 | } |
---|
224 | } |
---|
225 | if(data_.hasRect()) { |
---|
226 | SoTPoint pt1; |
---|
227 | SoTPoint pt2; |
---|
228 | LineAttribute attr; |
---|
229 | Color color; |
---|
230 | Annote.Rect pRect; |
---|
231 | iter = data_.getRectIterator(); |
---|
232 | while(iter.hasNext()) { |
---|
233 | pRect = (Annote.Rect)iter.next(); |
---|
234 | pt1 = pRect.getUpperLeft(); |
---|
235 | pt2 = pRect.getLowerRight(); |
---|
236 | attr = pRect.getLineAttribute(); |
---|
237 | color = pRect.getFillColor(); |
---|
238 | // |
---|
239 | renderRect(g2, pt1, pt2, attr, color); |
---|
240 | } |
---|
241 | } |
---|
242 | /**@todo: implement this gov.noaa.pmel.sgt.CartesianRenderer abstract method*/ |
---|
243 | // |
---|
244 | // reset clip |
---|
245 | // |
---|
246 | Rectangle rect = cg_.getLayer().getPane().getBounds(); |
---|
247 | g.setClip(rect); |
---|
248 | } |
---|
249 | |
---|
250 | public void propertyChange(PropertyChangeEvent evt) { |
---|
251 | modified("AnnotationCartesianRenderer: propertyChange(" + |
---|
252 | evt.getSource().toString() + "[" + |
---|
253 | evt.getPropertyName() + "]" + ")"); |
---|
254 | } |
---|
255 | /** |
---|
256 | * Render oval using Graphics2D |
---|
257 | */ |
---|
258 | private void renderOval(Graphics2D g2, SoTPoint pt1, SoTPoint pt2, |
---|
259 | LineAttribute attr, Color color) { |
---|
260 | Stroke stroke = getStrokeFromLineAttribute(attr); |
---|
261 | // |
---|
262 | float xPt1 = (float)cg_.getXUtoD2(pt1.getX()); |
---|
263 | float yPt1 = (float)cg_.getYUtoD2(pt1.getY()); |
---|
264 | float xPt2 = (float)cg_.getXUtoD2(pt2.getX()); |
---|
265 | float yPt2 = (float)cg_.getYUtoD2(pt2.getY()); |
---|
266 | float width = xPt2 - xPt1; |
---|
267 | float height = yPt2 - yPt1; |
---|
268 | // |
---|
269 | Shape oval = new Ellipse2D.Float(xPt1, yPt1, width, height); |
---|
270 | Paint savedPaint = g2.getPaint(); |
---|
271 | Stroke savedStroke = g2.getStroke(); |
---|
272 | Color savedColor = g2.getColor(); |
---|
273 | // |
---|
274 | if(color != null) { |
---|
275 | g2.setPaint(color); |
---|
276 | g2.fill(oval); |
---|
277 | } |
---|
278 | if(stroke != null) { |
---|
279 | g2.setStroke(stroke); |
---|
280 | g2.setColor(attr.getColor()); |
---|
281 | g2.draw(oval); |
---|
282 | } |
---|
283 | g2.setPaint(savedPaint); |
---|
284 | g2.setStroke(savedStroke); |
---|
285 | g2.setColor(savedColor); |
---|
286 | } |
---|
287 | /** |
---|
288 | * Render rect using Graphics2D |
---|
289 | */ |
---|
290 | private void renderRect(Graphics2D g2, SoTPoint pt1, SoTPoint pt2, |
---|
291 | LineAttribute attr, Color color) { |
---|
292 | Stroke stroke = getStrokeFromLineAttribute(attr); |
---|
293 | // |
---|
294 | float xPt1 = (float)cg_.getXUtoD2(pt1.getX()); |
---|
295 | float yPt1 = (float)cg_.getYUtoD2(pt1.getY()); |
---|
296 | float xPt2 = (float)cg_.getXUtoD2(pt2.getX()); |
---|
297 | float yPt2 = (float)cg_.getYUtoD2(pt2.getY()); |
---|
298 | float width = xPt2 - xPt1; |
---|
299 | float height = yPt2 - yPt1; |
---|
300 | // |
---|
301 | Shape rect = new Rectangle2D.Float(xPt1, yPt1, width, height); |
---|
302 | Paint savedPaint = g2.getPaint(); |
---|
303 | Stroke savedStroke = g2.getStroke(); |
---|
304 | Color savedColor = g2.getColor(); |
---|
305 | // |
---|
306 | if(color != null) { |
---|
307 | g2.setPaint(color); |
---|
308 | g2.fill(rect); |
---|
309 | } |
---|
310 | if(stroke != null) { |
---|
311 | g2.setStroke(stroke); |
---|
312 | g2.setColor(attr.getColor()); |
---|
313 | g2.draw(rect); |
---|
314 | } |
---|
315 | g2.setPaint(savedPaint); |
---|
316 | g2.setStroke(savedStroke); |
---|
317 | g2.setColor(savedColor); |
---|
318 | } |
---|
319 | /** |
---|
320 | * Render line using Graphics2D |
---|
321 | */ |
---|
322 | private void renderLine(Graphics2D g2, SGTLine line, LineAttribute attr) { |
---|
323 | LineCartesianRenderer lcr = new LineCartesianRenderer(cg_, line, attr); |
---|
324 | lcr.draw(g2); |
---|
325 | /* |
---|
326 | Stroke savedStroke = g2.getStroke(); |
---|
327 | Color savedColor = g2.getColor(); |
---|
328 | // |
---|
329 | Stroke stroke = getStrokeFromLineAttribute(attr); |
---|
330 | // build line |
---|
331 | float[] xd; |
---|
332 | float[] yd; |
---|
333 | if(line.isXTime()) { |
---|
334 | long[] xu = line.getGeoDateArray().getTime(); |
---|
335 | xd = new float[xu.length]; |
---|
336 | for(int i=0; i < xu.length; i++) { |
---|
337 | xd[i] = (float)cg_.getXUtoD2(xu[i]); |
---|
338 | } |
---|
339 | } else { |
---|
340 | double[] xu = line.getXArray(); |
---|
341 | xd = new float[xu.length]; |
---|
342 | for(int i=0; i < xu.length; i++) { |
---|
343 | xd[i] = (float)cg_.getXUtoD2(xu[i]); |
---|
344 | } |
---|
345 | } |
---|
346 | if(line.isYTime()) { |
---|
347 | long[] yu = line.getGeoDateArray().getTime(); |
---|
348 | yd = new float[yu.length]; |
---|
349 | for(int i=0; i < yu.length; i++) { |
---|
350 | yd[i] = (float)cg_.getYUtoD2(yu[i]); |
---|
351 | } |
---|
352 | } else { |
---|
353 | double[] yu = line.getYArray(); |
---|
354 | yd = new float[yu.length]; |
---|
355 | for(int i=0; i < yu.length; i++) { |
---|
356 | yd[i] = (float)cg_.getYUtoD2(yu[i]); |
---|
357 | } |
---|
358 | } |
---|
359 | GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, xd.length); |
---|
360 | // |
---|
361 | int start = 0; |
---|
362 | for(int i=0; i < xd.length; i++) { |
---|
363 | if(!(Float.isNaN(xd[i]) || Float.isNaN(yd[i]))) { |
---|
364 | path.moveTo(xd[i], yd[i]); |
---|
365 | start = i+1; |
---|
366 | break; |
---|
367 | } |
---|
368 | } |
---|
369 | boolean move = false; |
---|
370 | for(int i=start; i < xd.length; i++) { |
---|
371 | if(Float.isNaN(xd[i]) || Float.isNaN(yd[i])) { |
---|
372 | move = true; |
---|
373 | continue; |
---|
374 | } |
---|
375 | if(move) { |
---|
376 | path.moveTo(xd[i], yd[i]); |
---|
377 | move = false; |
---|
378 | } else { |
---|
379 | path.lineTo(xd[i], yd[i]); |
---|
380 | } |
---|
381 | } |
---|
382 | if(stroke != null) g2.setStroke(stroke); |
---|
383 | g2.setColor(attr.getColor()); |
---|
384 | g2.draw(path); |
---|
385 | g2.setColor(savedColor); |
---|
386 | g2.setStroke(savedStroke); */ |
---|
387 | } |
---|
388 | /** |
---|
389 | * Render point using Graphics2D |
---|
390 | */ |
---|
391 | private void renderPoint(Graphics2D g, SGTPoint point, PointAttribute attr) { |
---|
392 | PointCartesianRenderer pcr = new PointCartesianRenderer(cg_, point, attr); |
---|
393 | pcr.draw(g); |
---|
394 | } |
---|
395 | /** |
---|
396 | * Render label using Graphics2D |
---|
397 | */ |
---|
398 | private void renderText(Graphics2D g, SoTPoint loc, SGLabel text) { |
---|
399 | double xp; |
---|
400 | double yp; |
---|
401 | if(loc.getX().isTime()) { |
---|
402 | xp = cg_.getXUtoP(loc.getX().getLongTime()); |
---|
403 | } else { |
---|
404 | xp = cg_.getXUtoP(((Number)loc.getX().getObjectValue()).doubleValue()); |
---|
405 | } |
---|
406 | if(loc.getY().isTime()) { |
---|
407 | yp = cg_.getYUtoP(loc.getY().getLongTime()); |
---|
408 | } else { |
---|
409 | yp = cg_.getYUtoP(((Number)loc.getY().getObjectValue()).doubleValue()); |
---|
410 | } |
---|
411 | text.setLocationP(new Point2D.Double(xp, yp)); |
---|
412 | text.setLayer(cg_.getLayer()); |
---|
413 | try { |
---|
414 | text.draw(g); |
---|
415 | } catch (LayerNotFoundException ex) { |
---|
416 | ex.printStackTrace(); |
---|
417 | } |
---|
418 | } |
---|
419 | |
---|
420 | |
---|
421 | private Stroke getStrokeFromLineAttribute(LineAttribute attr) { |
---|
422 | BasicStroke stroke = null; |
---|
423 | if(attr == null) return stroke; |
---|
424 | switch(attr.getStyle()) { |
---|
425 | // case LineAttribute.MARK: |
---|
426 | // drawMark(g, xout, yout, lsize, attr); |
---|
427 | // break; |
---|
428 | // case LineAttribute.HIGHLIGHT: |
---|
429 | // stroke_.drawHighlight(g, xout, yout, lsize, attr); |
---|
430 | // break; |
---|
431 | case LineAttribute.HEAVY: |
---|
432 | stroke = new BasicStroke(attr.getWidth()); |
---|
433 | break; |
---|
434 | case LineAttribute.DASHED: |
---|
435 | float[] dashes = {4.0f, 4.0f}; |
---|
436 | stroke = new BasicStroke(1.0f, |
---|
437 | BasicStroke.CAP_SQUARE, |
---|
438 | BasicStroke.JOIN_MITER, |
---|
439 | 10.0f, |
---|
440 | dashes, |
---|
441 | 0.0f); |
---|
442 | break; |
---|
443 | case LineAttribute.STROKE: |
---|
444 | float[] arr = attr.getDashArray(); |
---|
445 | if(arr == null || (arr.length <= 1)) { |
---|
446 | stroke = new BasicStroke(attr.getWidth(), |
---|
447 | attr.getCapStyle(), |
---|
448 | attr.getMiterStyle(), |
---|
449 | attr.getMiterLimit()); |
---|
450 | } else { |
---|
451 | stroke = new BasicStroke(attr.getWidth(), |
---|
452 | attr.getCapStyle(), |
---|
453 | attr.getMiterStyle(), |
---|
454 | attr.getMiterLimit(), |
---|
455 | attr.getDashArray(), |
---|
456 | attr.getDashPhase()); |
---|
457 | } |
---|
458 | break; |
---|
459 | // case LineAttribute.MARK_LINE: |
---|
460 | // drawMark(g, xout, yout, lsize, attr); |
---|
461 | default: |
---|
462 | // case LineAttribute.SOLID: |
---|
463 | // g.drawPolyline(xout, yout, lsize); |
---|
464 | } |
---|
465 | return stroke; |
---|
466 | } |
---|
467 | } |
---|