source: ether_statistics/service/implementation/gov/noaa/pmel/sgt/dm/Annote.java @ 569

Last change on this file since 569 was 569, checked in by vmipsl, 12 years ago

Nouveau projet

File size: 19.7 KB
Line 
1/*
2 *  $Id: Annote.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 */
12package gov.noaa.pmel.sgt.dm;
13
14import gov.noaa.pmel.sgt.Graph;
15import gov.noaa.pmel.sgt.CartesianGraph;
16import gov.noaa.pmel.sgt.LineAttribute;
17import gov.noaa.pmel.sgt.PointAttribute;
18import gov.noaa.pmel.sgt.SGLabel;
19
20import gov.noaa.pmel.util.SoTPoint;
21import gov.noaa.pmel.util.SoTDomain;
22import gov.noaa.pmel.util.SoTRange;
23import gov.noaa.pmel.util.SoTValue;
24import gov.noaa.pmel.util.Rectangle2D;
25import gov.noaa.pmel.util.GeoDate;
26import gov.noaa.pmel.util.GeoDateArray;
27
28import java.io.Serializable;
29import java.awt.Color;
30import java.awt.Rectangle;
31
32import java.beans.PropertyChangeSupport;
33import java.beans.PropertyChangeListener;
34import java.beans.PropertyChangeEvent;
35/**
36 * Abstract class for annotations.
37 * @author Donald Denbo
38 * @version $Revision: 1.8 $
39 * @since 3.0
40 */
41public abstract class Annote implements SGTData, Serializable, PropertyChangeListener {
42  protected transient PropertyChangeSupport changes_;
43  // serial version ref 1.7.2.5
44  private static final long serialVersionUID = 7305616377566581275L;
45  protected String id_ = null;
46
47/**
48 * Class for line annotations.
49 * @author Donald Denbo
50 * @version $Revision: 1.8 $
51 * @since 3.0
52 */
53  public static class Line extends Annote {
54    SGTLine line;
55    LineAttribute attr;
56
57    public Line(String id, SGTLine line, LineAttribute attr) {
58      id_ = id;
59      this.line = line;
60      this.attr = attr;
61      init();
62    }
63    public void init() {
64      if(attr != null) attr.addPropertyChangeListener(this);
65      changes_ = new PropertyChangeSupport(this);
66    }
67    public SGTLine getLine() {
68      return line;
69    }
70    public LineAttribute getAttribute() {
71      return attr;
72    }
73    public SoTDomain getDomain() {
74      return new SoTDomain(line.getXRange(), line.getYRange());
75    }
76    public Rectangle getBounds(Graph graph) {
77      return computeBounds(graph, this.getDomain());
78    }
79    public void moveBy(SoTPoint point) {
80      double[] xu = null;
81      double[] yu = null;
82      long[] tu = null;
83      double delta;
84      long tdelta;
85      if(!(line instanceof SimpleLine)) return;
86      if(line.isXTime()) {
87        tu = line.getGeoDateArray().getTime();
88        tdelta = point.getX().getLongTime();
89        for(int i=0; i < tu.length; i++) {
90          tu[i] += tdelta;
91        }
92        ((SimpleLine)line).setTimeArray(new GeoDateArray(tu));
93      } else {
94        xu = line.getXArray();
95        delta = ((Number)point.getX().getObjectValue()).doubleValue();
96        for(int i=0; i < xu.length; i++) {
97          xu[i] += delta;
98        }
99        ((SimpleLine)line).setXArray(xu);
100      }
101      if(line.isYTime()) {
102        tu = line.getGeoDateArray().getTime();
103        tdelta = point.getY().getLongTime();
104        for(int i=0; i < tu.length; i++) {
105          tu[i] += tdelta;
106        }
107        ((SimpleLine)line).setTimeArray(new GeoDateArray(tu));
108      } else {
109        yu = line.getYArray();
110        delta = ((Number)point.getY().getObjectValue()).doubleValue();
111        for(int i=0; i < yu.length; i++) {
112          yu[i] += delta;
113        }
114        ((SimpleLine)line).setYArray(yu);
115      }
116      changes_.firePropertyChange("lineMoved", true, false);
117    }
118    public void setXArray(double[] xa) {
119    }
120    public void setYArray(double[] ya) {
121    }
122    public void setTimeArray(GeoDateArray gda) {
123    }
124    public void propertyChange(PropertyChangeEvent evt) {
125      changes_.firePropertyChange(evt);
126    }
127    /* SGTData required methods */
128    public SGTData copy() {
129      SGTData copy = null;
130      try {
131        copy = (SGTData)this.clone();
132      } catch (CloneNotSupportedException ex) {
133        ex.printStackTrace();
134      }
135      return copy;
136    }
137    public String getId() {
138      return line.getId();
139    }
140    public SGLabel getKeyTitle() {
141      return line.getKeyTitle();
142    }
143    public String getTitle() {
144      return line.getTitle();
145    }
146    public SGTMetaData getXMetaData() {
147      return line.getXMetaData();
148    }
149    public SoTRange getXRange() {
150      return line.getXRange();
151    }
152    public SGTMetaData getYMetaData() {
153      return line.getYMetaData();
154    }
155    public SoTRange getYRange() {
156      return line.getYRange();
157    }
158    public boolean isXTime() {
159      return line.isXTime();
160    }
161    public boolean isYTime() {
162      return line.isYTime();
163    }
164  }
165
166/**
167 * Class for point annotations.
168 * @author Donald Denbo
169 * @version $Revision: 1.8 $
170 * @since 3.0
171 */
172  public static class Point extends Annote {
173    SGTPoint point;
174    PointAttribute attr;
175
176    public Point(String id, SGTPoint point, PointAttribute attr) {
177      id_ = id;
178      this.point = point;
179      this.attr = attr;
180      init();
181    }
182    public void init() {
183      if(attr != null) attr.addPropertyChangeListener(this);
184      changes_ = new PropertyChangeSupport(this);
185    }
186    public SGTPoint getPoint() {
187      return point;
188    }
189    public PointAttribute getAttribute() {
190      return attr;
191    }
192
193    public SoTDomain getDomain() {
194      return new SoTDomain(point.getXRange(), point.getYRange());
195    }
196
197    public Rectangle getBounds(Graph graph) {
198      /** @todo add text bounds */
199      Rectangle rect = computeBounds(graph, this.getDomain());
200      double hgt = attr.getMarkHeightP();
201      int ihgt = graph.getLayer().getXPtoD(hgt);
202      rect.setBounds(rect.x-ihgt/2,
203                     rect.y-ihgt/2,
204                     ihgt, ihgt);
205      return rect;
206    }
207
208    public void moveBy(SoTPoint pnt) {
209      double xu = 0.0;
210      double yu = 0.0;
211      long tu = 0;
212      double delta;
213      long tdelta;
214      if(!(point instanceof SimplePoint)) return;
215      if(point.isXTime()) {
216        tu = point.getLongTime();
217        tdelta = pnt.getX().getLongTime();
218        tu += tdelta;
219        ((SimplePoint)point).setTime(tu);
220      } else {
221        xu = point.getX();
222        delta = ((Number)pnt.getX().getObjectValue()).doubleValue();
223        xu += delta;
224        ((SimplePoint)point).setX(xu);
225      }
226      if(point.isYTime()) {
227        tu = point.getLongTime();
228        tdelta = pnt.getY().getLongTime();
229        tu += tdelta;
230        ((SimplePoint)point).setTime(tu);
231      } else {
232        yu = point.getY();
233        delta = ((Number)pnt.getY().getObjectValue()).doubleValue();
234        yu += delta;
235        ((SimplePoint)point).setY(yu);
236      }
237      changes_.firePropertyChange("pointMoved", true, false);
238    }
239    public void propertyChange(PropertyChangeEvent evt) {
240      changes_.firePropertyChange(evt);
241    }
242    /* SGTData required methods */
243    public SGTData copy() {
244      SGTData copy = null;
245      try {
246        copy = (SGTData)this.clone();
247      } catch (CloneNotSupportedException ex) {
248        ex.printStackTrace();
249      }
250      return copy;
251    }
252    public String getId() {
253      return point.getId();
254    }
255    public SGLabel getKeyTitle() {
256      return point.getKeyTitle();
257    }
258    public String getTitle() {
259      return point.getTitle();
260    }
261    public SGTMetaData getXMetaData() {
262      return point.getXMetaData();
263    }
264    public SoTRange getXRange() {
265      return point.getXRange();
266    }
267    public SGTMetaData getYMetaData() {
268      return point.getYMetaData();
269    }
270    public SoTRange getYRange() {
271      return point.getYRange();
272    }
273    public boolean isXTime() {
274      return point.isXTime();
275    }
276    public boolean isYTime() {
277      return point.isYTime();
278    }
279  }
280
281/**
282 * Class for text annotations.
283 * @author Donald Denbo
284 * @version $Revision: 1.8 $
285 * @since 3.0
286 */
287  public static class Text extends Annote {
288    SoTPoint location;
289    SGLabel text;
290
291    public Text(String id, SoTPoint location, SGLabel text) {
292      id_ = id;
293      this.location = location;
294      this.text = text;
295      init();
296    }
297    public void init() {
298      if(text != null) text.addPropertyChangeListener(this);
299      changes_ = new PropertyChangeSupport(this);
300    }
301    public SoTPoint getLocation() {
302      return location;
303    }
304    public SGLabel getText() {
305      return text;
306    }
307    public SoTDomain getDomain() {
308
309      return null;
310    }
311    private SoTDomain getDomain(Graph graph) {
312      CartesianGraph cg = (CartesianGraph)graph;
313      SoTRange xRange = null;
314      SoTRange yRange = null;
315      Rectangle2D.Double bnds = text.getBoundsP();
316/** @todo rewrite to go directly to device units */
317      double xloc = cg.getXUtoP(location.getX());
318      double yloc = cg.getYUtoP(location.getY());
319      double width = bnds.width;
320      double height = bnds.height;
321      if(location.isXTime()) {
322        long min = cg.getXPtoLongTime(xloc);
323        long max = cg.getXPtoLongTime(xloc + width);
324        xRange = new SoTRange.Time(min, max);
325      } else {
326        double min = cg.getXPtoU(xloc);
327        double max = cg.getXPtoU(xloc + width);
328        xRange = new SoTRange.Double(min, max);
329      }
330      if(location.isYTime()) {
331        long min = cg.getYPtoLongTime(yloc);
332        long max = cg.getYPtoLongTime(yloc + height);
333        yRange = new SoTRange.Time(min, max);
334      } else {
335        double min = cg.getYPtoU(yloc);
336        double max = cg.getYPtoU(yloc + height);
337        yRange = new SoTRange.Double(min, max);
338      }
339      return new SoTDomain(xRange, yRange);
340    }
341    public Rectangle getBounds(Graph graph) {
342      return computeBounds(graph, this.getDomain(graph));
343    }
344    public void moveBy(SoTPoint point) {
345      location.add(point);
346      changes_.firePropertyChange("textMoved", true, false);
347    }
348    public void propertyChange(PropertyChangeEvent evt) {
349      changes_.firePropertyChange(evt);
350    }
351    /* SGTData required methods */
352    public SGTData copy() {
353      SGTData copy = null;
354      try {
355        copy = (SGTData)this.clone();
356      } catch (CloneNotSupportedException ex) {
357        ex.printStackTrace();
358      }
359      return copy;
360    }
361    public String getId() {
362      return text.getId();
363    }
364    public SGLabel getKeyTitle() {
365      return null;
366    }
367    public String getTitle() {
368      return null;
369    }
370    public SGTMetaData getXMetaData() {
371      return null;
372    }
373    public SoTRange getXRange() {
374      return null;
375    }
376    public SGTMetaData getYMetaData() {
377      return null;
378    }
379    public SoTRange getYRange() {
380      return null;
381    }
382    public boolean isXTime() {
383      return location.isXTime();
384    }
385    public boolean isYTime() {
386      return location.isYTime();
387    }
388  }
389
390/**
391 * Class for oval annotations.
392 * @author Donald Denbo
393 * @version $Revision: 1.8 $
394 * @since 3.0
395 */
396  public static class Oval extends Annote {
397    SoTPoint upperLeft;
398    SoTPoint lowerRight;
399    LineAttribute attr;
400    Color color;
401    SoTRange xRange_ = null;
402    SoTRange yRange_ = null;
403
404    public Oval(String id, SoTPoint upperLeft, SoTPoint lowerRight,
405                LineAttribute attr, Color color) {
406      id_ = id;
407      this.upperLeft = upperLeft;
408      this.lowerRight = lowerRight;
409      this.attr = attr;
410      this.color = color;
411      init();
412      computeRange();
413    }
414    public void init() {
415      if(attr != null) attr.addPropertyChangeListener(this);
416      changes_ = new PropertyChangeSupport(this);
417    }
418    public SoTPoint getUpperLeft() {
419      return upperLeft;
420    }
421    public void setUpperLeft(SoTPoint ul) {
422      upperLeft = ul;
423      computeRange();
424      changes_.firePropertyChange("ovalMoved", true, false);
425    }
426    public SoTPoint getLowerRight() {
427      return lowerRight;
428    }
429    public void setLowerRight(SoTPoint lr) {
430      lowerRight = lr;
431      computeRange();
432      changes_.firePropertyChange("ovalMoved", true, false);
433    }
434    public void setLocation(SoTPoint ul, SoTPoint lr) {
435      upperLeft = ul;
436      lowerRight = lr;
437      computeRange();
438      changes_.firePropertyChange("ovalMoved", true, false);
439    }
440    public LineAttribute getLineAttribute() {
441      return attr;
442    }
443    public Color getFillColor() {
444      return color;
445    }
446    public void setFillColor(Color color) {
447      this.color = color;
448      changes_.firePropertyChange("colorChanged", true, false);
449    }
450    public SoTDomain getDomain() {
451      return new SoTDomain(xRange_, yRange_);
452    }
453    public Rectangle getBounds(Graph graph) {
454      return computeBounds(graph, this.getDomain());
455    }
456    public void moveBy(SoTPoint point) {
457      upperLeft.add(point);
458      lowerRight.add(point);
459      computeRange();
460      changes_.firePropertyChange("ovalMoved", true, false);
461    }
462    private void computeRange() {
463      if(upperLeft.getX().isTime()) {
464        xRange_ = new SoTRange.Time(upperLeft.getX().getLongTime(),
465                                    lowerRight.getX().getLongTime());
466      } else {
467        double xmin = ((Number)upperLeft.getX().getObjectValue()).doubleValue();
468        double xmax = ((Number)lowerRight.getX().getObjectValue()).doubleValue();
469        xRange_ = new SoTRange.Double(xmin, xmax);
470      }
471      if(upperLeft.getY().isTime()) {
472        yRange_ = new SoTRange.Time(lowerRight.getY().getLongTime(),
473                                    upperLeft.getY().getLongTime());
474      } else {
475        double ymin = ((Number)lowerRight.getY().getObjectValue()).doubleValue();
476        double ymax = ((Number)upperLeft.getY().getObjectValue()).doubleValue();
477        yRange_ = new SoTRange.Double(ymin, ymax);
478      }
479    }
480    public void propertyChange(PropertyChangeEvent evt) {
481      changes_.firePropertyChange(evt);
482    }
483    /* SGTData required methods */
484    public SGTData copy() {
485      SGTData copy = null;
486      try {
487        copy = (SGTData)this.clone();
488      } catch (CloneNotSupportedException ex) {
489        ex.printStackTrace();
490      }
491      return copy;
492    }
493    public String getId() {
494      return null;
495    }
496    public SGLabel getKeyTitle() {
497      return null;
498    }
499    public String getTitle() {
500      return null;
501    }
502    public SGTMetaData getXMetaData() {
503      return null;
504    }
505    public SoTRange getXRange() {
506      return xRange_;
507    }
508    public SGTMetaData getYMetaData() {
509      return null;
510    }
511    public SoTRange getYRange() {
512      return yRange_;
513    }
514    public boolean isXTime() {
515      return upperLeft.isXTime();
516    }
517    public boolean isYTime() {
518      return upperLeft.isYTime();
519    }
520  }
521
522/**
523 * Class for rectangle annotations.
524 * @author Donald Denbo
525 * @version $Revision: 1.8 $
526 * @since 3.0
527 */
528  public static class Rect extends Annote {
529    SoTPoint upperLeft;
530    SoTPoint lowerRight;
531    LineAttribute attr;
532    Color color;
533    SoTRange xRange_ = null;
534    SoTRange yRange_ = null;
535
536    public Rect(String id, SoTPoint upperLeft, SoTPoint lowerRight,
537                LineAttribute attr, Color color) {
538      id_ = id;
539      this.upperLeft = upperLeft;
540      this.lowerRight = lowerRight;
541      this.attr = attr;
542      this.color = color;
543      init();
544      computeRange();
545    }
546    public void init() {
547      if(attr != null) attr.addPropertyChangeListener(this);
548      changes_ = new PropertyChangeSupport(this);
549    }
550    public SoTPoint getUpperLeft() {
551      return upperLeft;
552    }
553    public void setUpperLeft(SoTPoint ul) {
554      upperLeft = ul;
555      computeRange();
556      changes_.firePropertyChange("rectMoved", true, false);
557    }
558    public SoTPoint getLowerRight() {
559      return lowerRight;
560    }
561    public void setLowerRight(SoTPoint lr) {
562      lowerRight = lr;
563      computeRange();
564      changes_.firePropertyChange("rectMoved", true, false);
565    }
566    public void setLocation(SoTPoint ul, SoTPoint lr) {
567      upperLeft = ul;
568      lowerRight = lr;
569      computeRange();
570      changes_.firePropertyChange("rectMoved", true, false);
571    }
572    public LineAttribute getLineAttribute() {
573      return attr;
574    }
575    public Color getFillColor() {
576      return color;
577    }
578    public void setFillColor(Color color) {
579      this.color = color;
580      changes_.firePropertyChange("colorChanged", true, false);
581    }
582    public SoTDomain getDomain() {
583      return new SoTDomain(xRange_, yRange_);
584    }
585    public Rectangle getBounds(Graph graph) {
586      return computeBounds(graph, this.getDomain());
587    }
588    public void moveBy(SoTPoint point) {
589      upperLeft.add(point);
590      lowerRight.add(point);
591      computeRange();
592      changes_.firePropertyChange("rectMoved", true, false);
593    }
594    private void computeRange() {
595      if(upperLeft.getX().isTime()) {
596        xRange_ = new SoTRange.Time(upperLeft.getX().getLongTime(),
597                                    lowerRight.getX().getLongTime());
598      } else {
599        double xmin = ((Number)upperLeft.getX().getObjectValue()).doubleValue();
600        double xmax = ((Number)lowerRight.getX().getObjectValue()).doubleValue();
601        xRange_ = new SoTRange.Double(xmin, xmax);
602      }
603      if(upperLeft.getY().isTime()) {
604        yRange_ = new SoTRange.Time(lowerRight.getY().getLongTime(),
605                                    upperLeft.getY().getLongTime());
606      } else {
607        double ymin = ((Number)lowerRight.getY().getObjectValue()).doubleValue();
608        double ymax = ((Number)upperLeft.getY().getObjectValue()).doubleValue();
609        yRange_ = new SoTRange.Double(ymin, ymax);
610      }
611    }
612    public void propertyChange(PropertyChangeEvent evt) {
613      changes_.firePropertyChange(evt);
614    }
615    /* SGTData required methods */
616    public SGTData copy() {
617      SGTData copy = null;
618      try {
619        copy = (SGTData)this.clone();
620      } catch (CloneNotSupportedException ex) {
621        ex.printStackTrace();
622      }
623      return copy;
624    }
625    public String getId() {
626      return null;
627    }
628    public SGLabel getKeyTitle() {
629      return null;
630    }
631    public String getTitle() {
632      return null;
633    }
634    public SGTMetaData getXMetaData() {
635      return null;
636    }
637    public SoTRange getXRange() {
638      return xRange_;
639    }
640    public SGTMetaData getYMetaData() {
641      return null;
642    }
643    public SoTRange getYRange() {
644      return yRange_;
645    }
646    public boolean isXTime() {
647      return upperLeft.isXTime();
648    }
649    public boolean isYTime() {
650      return upperLeft.isYTime();
651    }
652  }
653
654  protected Annote() {
655  }
656
657  protected Rectangle computeBounds(Graph graph, SoTDomain domain) {
658    Rectangle rect = null;
659    if(domain == null || !(graph instanceof CartesianGraph)) return rect;
660    CartesianGraph cg = (CartesianGraph)graph;
661    int xd, yd;
662    int width, height;
663
664    int xd1 = cg.getXUtoD(domain.getXRange().getStart());
665    int yd1 = cg.getYUtoD(domain.getYRange().getStart());
666    int xd2 = cg.getXUtoD(domain.getXRange().getEnd());
667    int yd2 = cg.getYUtoD(domain.getYRange().getEnd());
668    if(xd1 < xd2) {
669      xd = xd1;
670      width = xd2 - xd1;
671    } else {
672      xd = xd2;
673      width = xd1 - xd2;
674    }
675    if(yd1 < yd2) {
676      yd = yd1;
677      height = yd2 - yd1;
678    } else {
679      yd = yd2;
680      height = yd1 - yd2;
681    }
682    rect = new Rectangle(xd, yd, width, height);
683    return rect;
684  }
685
686  public abstract Rectangle getBounds(Graph graph);
687  public abstract SoTDomain getDomain();
688  public abstract void moveBy(SoTPoint point);
689  public abstract void propertyChange(PropertyChangeEvent evt);
690
691  public String getAnnoteId() {
692    return id_;
693  }
694  /**
695   * Init method used to setup serialized object.
696   */
697  public abstract void init();
698
699  /* SGTData required methods */
700  public void addPropertyChangeListener(PropertyChangeListener l) {
701    if(changes_ == null) changes_ = new PropertyChangeSupport(this);
702    changes_.addPropertyChangeListener(l);
703  }
704  public void removePropertyChangeListener(PropertyChangeListener l) {
705    if(changes_ == null) changes_ = new PropertyChangeSupport(this);
706    changes_.removePropertyChangeListener(l);
707  }
708}
Note: See TracBrowser for help on using the repository browser.