source: ether_megapoli/trunk/service/implementation/gov2/noaa/pmel/sgt/PointCollectionKey.java @ 192

Last change on this file since 192 was 192, checked in by vmipsl, 13 years ago

Servlet _ Contour en cours _ package gov2

File size: 18.4 KB
Line 
1/*
2 * $Id: PointCollectionKey.java,v 1.9 2003/08/22 23:02:32 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
13package  gov.noaa.pmel.sgt;
14
15import gov.noaa.pmel.util.Point2D;
16import gov.noaa.pmel.util.Rectangle2D;
17import gov.noaa.pmel.util.Debug;
18
19import java.util.Vector;
20import java.util.Enumeration;
21import java.awt.Graphics;
22import java.awt.Rectangle;
23import java.awt.Point;
24
25import java.beans.PropertyChangeListener;
26import java.beans.PropertyChangeEvent;
27import java.beans.PropertyChangeSupport;
28
29// jdk1.2
30//import java.awt.geom.Point2D;
31
32/**
33 * <code>PointCollectionKey</code> is used to create a key for the
34 * <code>PointCartesianRenderer</code>. Multiple
35 * lines can be included in the key.
36 *
37 * @author Donald Denbo
38 * @version $Revision: 1.9 $, $Date: 2003/08/22 23:02:32 $
39 * @since 2.0
40**/
41public class PointCollectionKey implements Cloneable,
42        DataKey, Moveable, PropertyChangeListener {
43  private String ident_;
44/** @directed */
45  private Layer layer_;
46  private Vector points_;
47  private Vector label_;
48  private int columns_;
49  private int style_;
50  private int valign_;
51  private int halign_;
52  private Point2D.Double porigin_;
53  private double lineLengthP_;
54  private int maxLabelLength_;
55  private int maxLabelHeight_;
56  private boolean selected_;
57  private boolean selectable_;
58  private boolean visible_;
59  private boolean moveable_;
60  private PropertyChangeSupport changes_ = new PropertyChangeSupport(this);
61  private static final int VERTICAL_BORDER_ = 3;
62  private static final int HORIZONTAL_BORDER_ = 15;
63  private static final int COLUMN_SPACE_ = 10;
64  private static final int ROW_SPACE_ = 3;
65  private static final int LABEL_SPACE_ = 15;
66  /**
67   * Use plain line border.
68   */
69  public static final int PLAIN_LINE = 0;
70  /**
71   * Use raised border.
72   */
73  public static final int RAISED = 1;
74  /**
75   * Do not draw a border.
76   */
77  public static final int NO_BORDER = 2;
78  /**
79   * Align to top of key.
80   */
81  public static final int TOP = 0;
82  /**
83   * Align to middle of key.
84   */
85  public static final int MIDDLE = 1;
86  /**
87   * Align to bottom of key.
88   */
89  public static final int BOTTOM = 2;
90  /**
91   * Align to left of key.
92   */
93  public static final int LEFT = 0;
94  /**
95   * Align to center of key.
96   */
97  public static final int CENTER = 1;
98  /**
99   * Align to right of key.
100   */
101  public static final int RIGHT = 2;
102
103  /**
104   * @link aggregation
105   * @supplierCardinality *
106   * @label points
107   */
108  /*#PointCartesianRenderer lnkPointCartesianRenderer;*/
109
110  /** @link aggregation
111   * @label label
112   * @supplierCardinality **/
113  /*#SGLabel lnkSGLabel;*/
114
115  /**
116   * Default constructor.
117   */
118  public PointCollectionKey() {
119    this(new Point2D.Double(0.0, 0.0), BOTTOM, LEFT);
120  }
121  /**
122   * Create <code>PointCollectionKey</code>.
123   */
124  public PointCollectionKey(Point2D.Double loc,int valign,int halign) {
125    porigin_ = loc;
126    valign_ = valign;
127    halign_ = halign;
128    points_ = new Vector(2,2);
129    label_ = new Vector(2,2);
130    //
131    // set defaults
132    //
133    style_ = PLAIN_LINE;
134    columns_ = 1;
135    ident_ = "";
136    lineLengthP_ = 0.3f;
137    selected_ = false;
138    selectable_ = true;
139    visible_ = true;
140    moveable_ = true;
141  }
142  /**
143   * Create of copy of PointCollectionKey.
144   */
145  public LayerChild copy() {
146    PointCollectionKey newKey;
147    try {
148      newKey = (PointCollectionKey)clone();
149    } catch (CloneNotSupportedException e) {
150      newKey = new PointCollectionKey();
151    }
152    newKey.points_ = new Vector(2,2);
153    newKey.label_ = new Vector(2,2);
154    return newKey;
155  }
156  public void setSelected(boolean sel) {
157    selected_ = sel;
158  }
159  public boolean isSelected() {
160    return selected_;
161  }
162  public void setSelectable(boolean select) {
163    selectable_ = select;
164  }
165  public boolean isSelectable() {
166    return selectable_;
167  }
168  public boolean isMoveable() {
169    return moveable_;
170  }
171  public void setMoveable(boolean moveable) {
172    moveable_ = moveable;
173  }
174  /**
175   * Set parent layer.
176   *
177   * @param l parent layer
178   */
179  public void setLayer(Layer l) {
180    layer_ = l;
181  }
182  /**
183   * Get layer.
184   *
185   * @return layer
186   */
187  public Layer getLayer() {
188    return layer_;
189  }
190
191  public AbstractPane getPane() {
192    return layer_.getPane();
193  }
194
195  public void modified(String mess) {
196    if(layer_ != null)
197      layer_.modified(mess);
198  }
199  /**
200   * Set PointCollectionKey identifier.
201   *
202   * @param id key identifier
203   */
204  public void setId(String id) {
205    ident_ = id;
206  }
207  /**
208   * Get PointCollectionKey identifier
209   *
210   * @return identifier
211   */
212  public String getId() {
213    return ident_;
214  }
215  /**
216   * Set line length.
217   *
218   * @param len line length
219   */
220  public void setLineLengthP(double len) {
221    if(lineLengthP_ != len) {
222      lineLengthP_ = len;
223      modified("PointCollectionKey: setLineLengthP()");
224    }
225  }
226  /**
227   * Get line length
228   *
229   * @return line length
230   */
231  public double getLineLengthP() {
232    return lineLengthP_;
233  }
234  /**
235   * Set the number of columns.
236   *
237   * @param col number of columns
238   */
239  public void setColumns(int col) {
240    if(columns_ != col) {
241      columns_ = col;
242      modified("PointCollectionKey: setColumms()");
243    }
244  }
245  /**
246   * Get the number of columns.
247   *
248   * @return number of columns
249   */
250  public int getColumns() {
251    return columns_;
252  }
253  /**
254   * Set border style.
255   *
256   * @param style border style
257   * @see #PLAIN_LINE
258   * @see #RAISED
259   * @see #NO_BORDER
260   */
261  public void setBorderStyle(int style) {
262    if(style_ != style) {
263      style_ = style;
264      modified("PointCollectionKey: setBorderStyle()");
265    }
266  }
267  /**
268   * Get border style.
269   *
270   * @return border style
271   */
272  public int getBorderStyle() {
273    return style_;
274  }
275  /**
276   * Set alignment.
277   *
278   * @param vert vertical alignment
279   * @param horz horizontal alignment
280   */
281  public void setAlign(int vert,int horz) {
282    if(valign_ != vert || halign_ != horz) {
283      valign_ = vert;
284      halign_ = horz;
285      modified("PointCollectionKey: setAlign()");
286    }
287  }
288  /**
289   * Set vertical alignment
290   *
291   * @param vert vertical alignment
292   */
293  public void setVAlign(int vert) {
294    if(valign_ != vert) {
295      valign_ = vert;
296      modified("PointCollectionKey: setVAlign()");
297    }
298  }
299  /**
300   * Set horizontal alignment
301   *
302   * @param horz horizontal alignment
303   */
304  public void setHAlign(int horz) {
305    if(halign_ != horz) {
306      halign_ = horz;
307      modified("PointCollectionKey: setHAlign()");
308    }
309  }
310  /**
311   * Get vertical alignment
312   *
313   * @return vertical alignment
314   */
315  public int getVAlign() {
316    return valign_;
317  }
318  /**
319   * Get horizontal alignment
320   *
321   * @return horizontal alignment
322   */
323  public int getHAlign() {
324    return halign_;
325  }
326  /**
327   * Set location of key
328   * <BR><B>Property Change:</B> <code>location</code>.
329   *
330   * @param loc key location
331   */
332  public void setLocationP(Point2D.Double loc) {
333    if(porigin_ == null || !porigin_.equals(loc)) {
334      Point2D.Double temp = porigin_;
335      porigin_ = loc;
336      changes_.firePropertyChange("location",
337          temp,
338          porigin_);
339      modified("PointCollectionKey: setLocationP()");
340    }
341  }
342  /**
343   * Set the bounds, in physical units, of the <code>PointCollectionKey</code>
344   */
345  public void setBoundsP(Rectangle2D.Double r) {
346    setLocationP(new Point2D.Double(r.x, r.y));
347  }
348  public Rectangle2D.Double getBoundsP() {
349    throw new MethodNotImplementedError();
350  }
351  /**
352   * Get location of key.
353   *
354   * @return Key location
355   */
356  public Point2D.Double getLocationP() {
357    return porigin_;
358  }
359  /**
360   * Add a PointCartesianRenderer and label to the PointCollectionKey.
361   *
362   * @param line PointCartesianRenderer object
363   * @param label descriptive label
364   */
365  public void addPointGraph(PointCartesianRenderer points, SGLabel label) {
366    points_.addElement(points);
367    label.setLayer(layer_);
368    label.setMoveable(false);
369    label.setSelectable(false);
370    label_.addElement(label);
371    ((PointAttribute)points.getAttribute()).addPropertyChangeListener(this);
372    modified("PointCollectionKey: addPointGraph()");
373  }
374  /**
375   * Add a PointCartesianRenderer and label to the PointCollectionKey.
376   *
377   * @param rend CartesianRenderer object
378   * @param label descriptive label
379   * @since 3.0
380   */
381  public void addGraph(CartesianRenderer rend, SGLabel label)
382      throws IllegalArgumentException {
383    if(!(rend instanceof PointCartesianRenderer))
384      throw new IllegalArgumentException("Renderer is not a PointCartesianRenderer");
385    addPointGraph((PointCartesianRenderer)rend, label);
386  }
387  /**
388   * Remove a line from the PointCollectionKey.
389   *
390   */
391  public void removePointGraph(SGLabel label) {
392  }
393  /**
394   * Remove a line from the PointCollectionKey.
395   *
396   */
397  public void removePointRenderer(PointCartesianRenderer line) {
398  }
399  /**
400   * Remove a line from the PointCollectionKey.
401   *
402   */
403  public void removePointGraph(String ident) {
404  }
405  /**
406   * Remove all lines from the PointCollectionKey.
407   */
408  public void clearAll() {
409    PointAttribute attr;
410    for(Enumeration e = points_.elements(); e.hasMoreElements(); ) {
411      attr = (PointAttribute)((PointCartesianRenderer)e.nextElement()).getAttribute();
412      attr.removePropertyChangeListener(this);
413    }
414    points_.removeAllElements();
415    label_.removeAllElements();
416    modified("PointCollectionKey: clearAll()");
417  }
418  /**
419   * Remove data from key by id.
420   */
421  public void clear(String data_id) {
422    PointCartesianRenderer pcr;
423    int indx = -1;
424    for(Enumeration it = points_.elements(); it.hasMoreElements();) {
425      indx++;
426      pcr = (PointCartesianRenderer)it.nextElement();
427//        if(pcr.getLine().getId().equals(data_id)) {
428//      pcr.getAttribute().removePropertyChangeListener(this);
429//      points_.removeElement(lcr);
430//      label_.removeElementAt(indx);
431//      modified("PointCollectionKey: clear()");
432//      break;
433//        }
434    }
435  }
436  /**
437   * Return height of key row in pixels.
438   */
439  public int getRowHeight() {
440    Rectangle bounds;
441    bounds = getBounds();
442    return ROW_SPACE_ + maxLabelHeight_;
443  }
444  /**
445   * Draw the Key.
446   */
447  public void draw(Graphics g) {
448    double maxLabelLength, maxLabelHeight;
449    int numLines, numRows, i, lineLength;
450    int col, row, ytemp;
451    double xloc, labelSpace;
452    double[] xp, yp;
453    int[] xd, yd;
454    int[] xout, yout;
455    Rectangle bounds;
456    PointCartesianRenderer render = null;
457    SGLabel label;
458    PointAttribute attr = null;
459    //
460    numLines = points_.size();
461    if((numLines <= 0) || !visible_) return;
462
463    numRows = numLines/columns_;
464    if(numLines%columns_ != 0) numRows++;
465
466    xp = new double[columns_];
467    xd = new int[columns_];
468    yp = new double[numRows];
469    yd = new int[numRows];
470    xout = new int[2];
471    yout = new int[2];
472
473    g.setColor(layer_.getPane().getComponent().getForeground());
474    bounds = getBounds();
475    //
476    // compute location of rows and columns in device and physical coordinates
477    //
478    lineLength = layer_.getXPtoD(lineLengthP_) - layer_.getXPtoD(0.0f);
479    labelSpace = layer_.getXDtoP(LABEL_SPACE_) - layer_.getXDtoP(0);
480    //
481    yd[0] = bounds.y + VERTICAL_BORDER_ + maxLabelHeight_;
482    yp[0] = layer_.getYDtoP(yd[0]);
483    for(i=1; i < numRows; i++) {
484      yd[i] = yd[i-1] + ROW_SPACE_ + maxLabelHeight_;
485      yp[i] = layer_.getYDtoP(yd[i]);
486    }
487    xd[0] = bounds.x + HORIZONTAL_BORDER_;
488    xp[0] = layer_.getXDtoP(xd[0]);
489    for(i=1; i < columns_; i++) {
490      xd[i] = xd[i-1] + COLUMN_SPACE_ + lineLength + LABEL_SPACE_ + maxLabelLength_;
491      xp[i] = layer_.getXDtoP(xd[i]);
492    }
493    //
494    row = 0;
495    col = 0;
496    Object obj;
497    Enumeration labelIt = label_.elements();
498    for(Enumeration lineIt = points_.elements(); lineIt.hasMoreElements();) {
499      obj = lineIt.nextElement();
500      render = (PointCartesianRenderer)obj;
501      attr = (PointAttribute)render.getAttribute();
502      label = (SGLabel)labelIt.nextElement();
503      //
504      // draw line
505      //
506      g.setColor(attr.getColor());
507      xout[0] = xd[col];
508      xout[1] = xout[0] + lineLength;
509      yout[0] = yd[row] - maxLabelHeight_/2;
510      yout[1] = yout[0];
511
512      // hack because mark is a little too high
513      int ymark = yout[0]
514;
515      PlotMark pm = new PlotMark(attr);
516      pm.setMarkHeightP(label.getHeightP());
517      pm.paintMark(g, layer_, xout[0], ymark);
518      pm.paintMark(g, layer_, xout[1], ymark);
519      //
520      xloc = xp[col] + lineLengthP_ + labelSpace;
521      label.setLocationP(new Point2D.Double(xloc, yp[row]));
522      try {
523        label.draw(g);
524      } catch (SGException e) {}
525      //
526      col++;
527      if(col >= columns_) {
528        col = 0;
529        row++;
530      }
531    }
532    switch(style_) {
533    case PLAIN_LINE:
534      g.drawRect(bounds.x, bounds.y, bounds.width-1, bounds.height-1);
535      break;
536    case RAISED:
537      break;
538    default:
539    case NO_BORDER:
540    }
541  }
542  /**
543   * Get the bounding rectangle.
544   *
545   * @return bounding rectangle
546   */
547  public Rectangle getBounds() {
548    int lineLength;
549    int numLines, rows;
550    int x, y, height, width;
551
552    numLines = points_.size();
553    if(numLines <= 0) return new Rectangle(0, 0, 0, 0);
554    //
555    // find longest label
556    //
557    maxLabelLength_ = 0;
558    maxLabelHeight_ = 0;
559    SGLabel label;
560    for(Enumeration it = label_.elements(); it.hasMoreElements();) {
561      label = (SGLabel)it.nextElement();
562      Rectangle sz = label.getBounds();
563      maxLabelLength_ = Math.max(maxLabelLength_, sz.width);
564      maxLabelHeight_ = Math.max(maxLabelHeight_, sz.height);
565    }
566    //
567    rows = numLines/columns_;
568    if(numLines%columns_ != 0) rows++;
569    lineLength = layer_.getXPtoD(lineLengthP_) - layer_.getXPtoD(0.0f);
570    width = 2*HORIZONTAL_BORDER_ +
571      columns_*(lineLength + LABEL_SPACE_ + maxLabelLength_) +
572      (columns_ - 1)*COLUMN_SPACE_;
573    height = 2*VERTICAL_BORDER_ + rows*maxLabelHeight_ +
574      (rows-1)*ROW_SPACE_;
575    // temp fudge
576    height = height + 5;
577    //
578    x = layer_.getXPtoD(porigin_.x);
579    y = layer_.getYPtoD(porigin_.y);
580    switch(halign_) {
581    case RIGHT:
582      x = x - width;
583      break;
584    case CENTER:
585      x = x - width/2;
586    }
587    switch(valign_) {
588    case BOTTOM:
589      y = y - height;
590      break;
591    case MIDDLE:
592      y = y - height/2;
593    }
594    return new Rectangle(x, y, width, height);
595  }
596  public Point getLocation() {
597    Rectangle bnds = getBounds();
598    return new Point(bnds.x, bnds.y);
599  }
600  public void setLocation(Point loc) {
601    Rectangle bnds = getBounds();
602    setBounds(loc.x, loc.y, bnds.width, bnds.height);
603  }
604  /**
605   * Set the bounds, in pixels, of the <code>PointCollectionKey</code>
606   */
607  public void setBounds(Rectangle r) {
608    setBounds(r.x, r.y, r.width, r.height);
609  }
610  /**
611   * Set the bounds, in pixels, of the <code>PointCollectionKey</code>
612   * <BR><B>Property Change:</B> <code>location</code>.
613   */
614  public void setBounds(int x, int y, int width, int height) {
615    switch(halign_) {
616    case RIGHT:
617      x = x + width;
618      break;
619    case CENTER:
620      x = x + width/2;
621    }
622    switch(valign_) {
623    case BOTTOM:
624      y = y + height;
625      break;
626    case MIDDLE:
627      y = y + height/2;
628    }
629    double xp = layer_.getXDtoP(x);
630    double yp = layer_.getYDtoP(y);
631    if(porigin_.x != xp || porigin_.y != yp) {
632      Point2D.Double temp = porigin_;
633      porigin_.x = xp;
634      porigin_.y = yp;
635      changes_.firePropertyChange("location",
636          temp,
637          new Point2D.Double(xp, yp));
638      modified("PointCollectionKey: setBounds()");
639    }
640  }
641  Object getObjectAt(Point pt) {
642    Rectangle lbnds;
643    Rectangle bounds;
644    PointCartesianRenderer point;
645    int[] xout, yout;
646    int[] xd, yd;
647    int numLines, numRows;
648    int lineLength;
649    double labelSpace;
650    int i;
651
652    numLines = points_.size();
653    if(numLines <= 0) return null;
654
655    numRows = numLines/columns_;
656    if(numLines%columns_ != 0) numRows++;
657
658    xd = new int[columns_];
659    yd = new int[numRows];
660    xout = new int[2];
661    yout = new int[2];
662    bounds = getBounds();
663    //
664    // compute location of rows and columns in device and physical coordinates
665    //
666    lineLength = layer_.getXPtoD(lineLengthP_) - layer_.getXPtoD(0.0);
667    labelSpace = layer_.getXDtoP(LABEL_SPACE_) - layer_.getXDtoP(0);
668    //
669    yd[0] = bounds.y + VERTICAL_BORDER_ + maxLabelHeight_;
670    for(i=1; i < numRows; i++) {
671      yd[i] = yd[i-1] + ROW_SPACE_ + maxLabelHeight_;
672    }
673    xd[0] = bounds.x + HORIZONTAL_BORDER_;
674    for(i=1; i < columns_; i++) {
675      xd[i] = xd[i-1] + COLUMN_SPACE_ + lineLength + LABEL_SPACE_ + maxLabelLength_;
676    }
677    // loop over all the lines
678    int row = 0;
679    int col = 0;
680    for(Enumeration lineIt = points_.elements(); lineIt.hasMoreElements();) {
681      point = (PointCartesianRenderer)lineIt.nextElement();
682      xout[0] = xd[col];
683//        xout[1] = xout[0] + lineLength + LABEL_SPACE_ + maxLabelLength_;
684      xout[1] = xout[0] + lineLength + LABEL_SPACE_;
685      yout[0] = yd[row] - maxLabelHeight_;
686      yout[1] = yd[row];
687      lbnds = new Rectangle(xout[0], yout[0],
688                            xout[1] - xout[0],
689                            yout[1] - yout[0]);
690      if(lbnds.contains(pt)) {
691        return point;
692      }
693      //
694      col++;
695      if(col >= columns_) {
696        col = 0;
697        row++;
698      }
699    }
700    if(bounds.contains(pt)) {
701      return this;
702    }
703    return null;
704  }
705  public String toString() {
706    String name = getClass().getName();
707    return name.substring(name.lastIndexOf(".")+1) + ": " + ident_;
708  }
709  public boolean isVisible() {
710    return visible_;
711  }
712  public void setVisible(boolean visible) {
713    if(visible_ != visible) {
714      visible_ = visible;
715      modified("PointCollectionKey: setVisible()");
716    }
717  }
718  public void propertyChange(PropertyChangeEvent evt) {
719//      if(Debug.EVENT) {
720//        System.out.println("PointCollectionKey: " + evt);
721//        System.out.println("         " + evt.getPropertyName());
722//      }
723    modified("PointCollectionKey: propertyChange(" +
724       evt.getSource().toString() + "[" +
725       evt.getPropertyName() + "]" + ")");
726  }
727  public void addPropertyChangeListener(PropertyChangeListener l) {
728    changes_.addPropertyChangeListener(l);
729  }
730  public void removePropertyChangeListener(PropertyChangeListener l) {
731    changes_.removePropertyChangeListener(l);
732  }
733}
Note: See TracBrowser for help on using the repository browser.