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

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

Nouveau projet

File size: 15.2 KB
Line 
1/*
2 * $Id: SGLabel.java,v 1.20 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.awt.image.PixelGrabber;
20import  java.awt.image.MemoryImageSource;
21import  java.awt.image.ColorModel;
22import  java.awt.image.ImageObserver;
23import  java.awt.*;
24import  java.beans.*;
25
26import java.beans.PropertyChangeListener;
27import java.beans.PropertyChangeSupport;
28import java.io.Serializable;
29// jdk1.2
30//import  java.awt.geom.Rectangle2D;
31//import  java.awt.geom.Point2D;
32
33/**
34 * Draws text on a layer object. SGLabel uses the drawString() method
35 * of the Graphics class.  SGLabel allows the user to align the text
36 * both vertically (TOP, MIDDLE, and BOTTOM) and horizontally
37 * (LEFT, MIDDLE, and RIGHT).  The font, color, and height (in user
38 * coordinates) can also be specified.  The SGLabel can also be drawn
39 * either HORIZONTAL or VERTICAL.
40 *
41 * @author Donald Denbo
42 * @version $Revision: 1.20 $, $Date: 2003/08/22 23:02:32 $
43 * @since 1.0
44 * @see java.awt.Graphics
45 */
46public class SGLabel implements Cloneable, LayerChild, Moveable, Serializable {
47  private String ident_;
48  private LabelDrawer proxy_;
49  private boolean selected_;
50  private boolean selectable_;
51  private boolean moveable_;
52  private transient PropertyChangeSupport changes_ = new PropertyChangeSupport(this);
53  /**
54   * Align top of label
55   **/
56  public static final int TOP = 0;
57  /**
58   * Align middle of label
59   **/
60  public static final int MIDDLE = 1;
61  /**
62   * Align bottom of label
63   **/
64  public static final int BOTTOM = 2;
65  /**
66   * Align left of label
67   **/
68  public static final int LEFT = 0;
69  /**
70   * Align center of label
71   **/
72  public static final int CENTER = 1;
73  /**
74   * Align right of label
75   **/
76  public static final int RIGHT = 2;
77  /**
78   * Orient label horizontal
79   */
80  public static final int HORIZONTAL = 0;
81  /**
82   * Orient label vertical
83   */
84  public static final int VERTICAL = 1;
85  /**
86   * Orient label at an angle
87   * @since 2.0
88   */
89  public static final int ANGLE = 2;
90
91  static {
92    try {
93      BeanInfo info = Introspector.getBeanInfo(SGLabel.class);
94      PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
95      for(int i=0; i < descriptors.length; i++) {
96        PropertyDescriptor pd = descriptors[i];
97        if(pd.getName().equals("layer")) {
98          pd.setValue("transient", Boolean.TRUE);
99        }
100      }
101    } catch (IntrospectionException ie) {
102      ie.printStackTrace();
103    }
104  }
105  /**
106   * Quick SGLabel constructor. Default alignment of BOTTOM and LEFT are
107   * used.  Default height is 0.12.
108   *
109   * @param id Label identifier
110   * @param lbl String to be plotted.
111   */
112  public SGLabel(String id,String lbl,Point2D.Double loc) {
113    this(id, lbl, 0.12, loc, BOTTOM, LEFT);
114  }
115  /**
116   * Long SGLabel constructor.
117   *
118   * @param id Label identifier
119   * @param lbl String to be plotted
120   * @param hgt String height in physical units
121   * @param loc Location to plot label in physical units
122   * @param valign Vertical alignment
123   * @param halign Horizontal alignment
124   **/
125  public SGLabel(String id, String lbl, double hgt,
126                 Point2D.Double loc, int valign, int halign) {
127    if(PaneProxy.Java2D) {
128      proxy_ = new LabelDrawer2(lbl, hgt, loc, valign, halign);
129    } else {
130      proxy_ = new LabelDrawer1(lbl, hgt, loc, valign, halign);
131    }
132    ident_ = id;
133    proxy_.setOrientation(HORIZONTAL);
134    proxy_.setAngle(0.0);
135    proxy_.setColor(null);
136    proxy_.setFont(new Font("Helvetica", Font.PLAIN, 14));
137    selected_ = false;
138    selectable_ = true;
139    proxy_.setVisible(true);
140    moveable_ = true;
141  }
142//    private void bindStringDrawer() {
143//      Class cl;
144//      boolean java2d = true;
145//      try {
146//        cl = Class.forName("java.awt.Graphics2D");
147//      } catch (ClassNotFoundException e) {
148//        java2d = false;
149//      }
150//      if(java2d) {
151//        stringDraw_ = new StringDrawer2();
152//      } else {
153//        stringDraw_ = new StringDrawer1();
154//      }
155//    }
156
157  public LayerChild copy() {
158    SGLabel newLabel;
159    try {
160      newLabel = (SGLabel)clone();
161    } catch (CloneNotSupportedException e) {
162      newLabel = new SGLabel(ident_,
163                             proxy_.getText(),
164                             proxy_.getHeightP(),
165                             proxy_.getLocationP(),
166                             proxy_.getVAlign(),
167                             proxy_.getHAlign());
168      newLabel.setColor(proxy_.getColor());
169      newLabel.setFont(proxy_.getFont());
170      if(proxy_.getOrientation() == ANGLE) {
171        newLabel.setAngle(proxy_.getAngle());
172      } else {
173        newLabel.setOrientation(proxy_.getOrientation());
174      }
175    }
176    return newLabel;
177  }
178  /**
179   * @since 3.0
180   */
181  public boolean equals(Object obj) {
182    if(obj == null || !(obj instanceof SGLabel)) return false;
183    SGLabel sg = (SGLabel)obj;
184/*    boolean t1 = !ident_.equals(sg.getId());
185    boolean t2 = !proxy_.getText().equals(sg.getText());
186    boolean t3 = proxy_.getHeightP() != sg.getHeightP();
187    boolean t4 = !proxy_.getLocationP().equals(sg.getLocationP());
188    boolean t5 = proxy_.getVAlign() != sg.getVAlign();
189    boolean t6 = proxy_.getHAlign() != sg.getHAlign();
190    boolean t7 = (proxy_.getColor() != null) && !proxy_.getColor().equals(sg.getColor());
191    boolean t8 = (proxy_.getFont() != null) && !proxy_.getFont().equals(sg.getFont());
192    boolean t9 = proxy_.getOrientation() != sg.getOrientation();
193    if(t1 || t2 || t3 || t4 || t5 || t6 || t7 || t8 || t9) return false; */
194    if((!ident_.equals(sg.getId())) ||
195       (!proxy_.getText().equals(sg.getText())) ||
196       (proxy_.getHeightP() != sg.getHeightP()) ||
197       (!proxy_.getLocationP().equals(sg.getLocationP())) ||
198       (proxy_.getVAlign() != sg.getVAlign()) ||
199       (proxy_.getHAlign() != sg.getHAlign()) ||
200       (proxy_.getColor() != null) && (!proxy_.getColor().equals(sg.getColor())) ||
201       (proxy_.getFont() != null) && (!proxy_.getFont().equals(sg.getFont())) ||
202       (proxy_.getOrientation() != sg.getOrientation())) return false;
203    if(proxy_.getOrientation() == ANGLE) {
204      if(proxy_.getAngle() != sg.getAngle()) return false;
205    }
206    return true;
207  }
208
209  public void draw(Graphics g) throws LayerNotFoundException  {
210    proxy_.draw(g);
211  }
212  public void setSelected(boolean sel) {
213    selected_ = sel;
214  }
215  public boolean isSelected() {
216    return selected_;
217  }
218  public void setSelectable(boolean select) {
219    selectable_ = select;
220  }
221  public boolean isSelectable() {
222    return selectable_;
223  }
224  /**
225   * Set the color.
226   *
227   * @param color The color of the label.
228   * @see java.awt.Color
229   **/
230  public void setColor(Color color) {
231    Color clr = proxy_.getColor();
232    if(clr == null || !clr.equals(color)) {
233      proxy_.setColor(color);
234      modified("SGLabel: setColor()");
235    }
236  }
237  /**
238   * Get the color.
239   *
240   * @return The current color of the label.
241   **/
242  public Color getColor() {
243    return proxy_.getColor();
244  }
245  /**
246   * Set the font.
247   *
248   * @param fnt The Font to use to draw the label.
249   * @see java.awt.Font
250   **/
251  public void setFont(Font fnt) {
252    Font font = proxy_.getFont();
253    if(font == null || !font.equals(fnt)) {
254      proxy_.setFont(fnt);
255      modified("SGLabel: setFont()");
256    }
257  }
258  /**
259   * Get the font.
260   *
261   * @return The current font for the label.
262   **/
263  public Font getFont() {
264    return proxy_.getFont();
265  }
266  /**
267   * Set the height of the label in physical coordinates.
268   *
269   * @param hgt The label height.
270   **/
271  public void setHeightP(double hgt) {
272    double height = proxy_.getHeightP();
273    if(height != hgt) {
274      proxy_.setHeightP(hgt);
275      modified("SGLabel: setHeightP()");
276    }
277  }
278  /**
279   * Get the label height in physical coordinates.
280   *
281   * @return The label height.
282   **/
283  public double getHeightP() {
284    return proxy_.getHeightP();
285  }
286  /**
287   * Set the vertical and horizontal alignment.  The vertical alignment
288   * can be TOP, MIDDLE, or BOTTOM, and the horizontal alignment
289   * LEFT, CENTER, or RIGHT.
290   *
291   * @param vert The vertical alignment.
292   * @param horz The horizontal alignment.
293   **/
294  public void setAlign(int vert,int horz) {
295    int valign = proxy_.getVAlign();
296    int halign = proxy_.getHAlign();
297    if(valign != vert || halign != horz) {
298      proxy_.setVAlign(vert);
299      proxy_.setHAlign(horz);
300      modified("SGLabel: setAlign()");
301    }
302  }
303  /**
304   * Set the horizontal alignment. The alignment can be LEFT, CENTER,
305   * or RIGHT.
306   *
307   * @param horz The horizontal alignment.
308   **/
309  public void setHAlign(int horz) {
310    int halign = proxy_.getHAlign();
311    if(halign != horz) {
312      proxy_.setHAlign(horz);
313      modified("SGLabeo: setHAlign()");
314    }
315  }
316  /**
317   * Get the horizontal alignment.
318   *
319   * @return the horizontal alignment.
320   **/
321  public int getHAlign() {
322    return  proxy_.getHAlign();
323  }
324  /**
325   * Set the vertical alignment. The alignment can be TOP, MIDDLE,
326   * or BOTTOM.
327   *
328   * @param vert The vertical alignment.
329   **/
330  public void setVAlign(int vert) {
331    int valign = proxy_.getVAlign();
332    if(valign != vert) {
333      proxy_.setVAlign(vert);
334      modified("SGLabel: setVAlign()");
335    }
336  }
337  /**
338   * Get the vertical alignment.
339   *
340   * @return the vertical alignment.
341   **/
342  public int getVAlign() {
343    return proxy_.getVAlign();
344  }
345  /**
346   * Set the label reference location in physcial coordinates.
347   * <BR><B>Property Change:</B> <code>location</code>.
348   *
349   * @param loc physical location of label
350   **/
351  public void setLocationP(Point2D.Double loc) {
352    Point2D.Double porigin = proxy_.getLocationP();
353    if(porigin == null || !porigin.equals(loc)) {
354      Point2D.Double temp = porigin;
355      porigin = loc;
356      proxy_.setLocationP(loc);
357      if(changes_ == null) changes_ = new PropertyChangeSupport(this);
358      changes_.firePropertyChange("location",
359                                  temp,
360                                  porigin);
361      modified("SGLabel: setLocationP()");
362    }
363  }
364  /**
365   * Get the label reference location in physcial coordinates.
366   *
367   * @return the labels position.
368   **/
369  public Point2D.Double getLocationP() {
370    return proxy_.getLocationP();
371  }
372  /**
373   * Set the orientation. The orientation can be HORIZONTAL or
374   * VERTICAL.
375   *
376   * @param orient The orientation.
377   **/
378  public void setOrientation(int orient) {
379    int or = proxy_.getOrientation();
380    if(or != orient) {
381      proxy_.setOrientation(orient);
382      modified("SGLabel: setOrientation()");
383    }
384  }
385  /**
386   * Get the origentation.
387   *
388   * @return the orientation
389   **/
390  public int getOrientation() {
391    return proxy_.getOrientation();
392  }
393  /**
394   * Draw label at arbitrary rotation.  Warning: Rotated labels are
395   * not drawn very well when using JDK1.1. For best results use
396   * JDK1.2 or newer.
397   * @since 2.0
398   */
399  public void setAngle(double angle) {
400    proxy_.setAngle(angle);
401  }
402  /**
403   * Get label drawing angle.
404   * @since 2.0
405   */
406  public double getAngle() {
407    return proxy_.getAngle();
408  }
409  /**
410   *
411   **/
412  public void setLayer(Layer l) {
413    proxy_.setLayer(l);
414  }
415  /**
416   * Get the layer.
417   *
418   * @return the layer object.
419   **/
420  public Layer getLayer() {
421    return proxy_.getLayer();
422  }
423
424  public AbstractPane getPane() {
425    return proxy_.getLayer().getPane();
426  }
427
428  public void modified(String text) {
429    Layer layer = proxy_.getLayer();
430    if(layer != null) {
431      layer.modified(text);
432    }
433  }
434
435  /**
436   * Get the label text.
437   *
438   * @return the label text
439   **/
440  public String getText() {
441    return proxy_.getText();
442  }
443  /**
444   * Set the label text.
445   *
446   * @param lbl the label text
447   **/
448  public void setText(String lbl) {
449    String label = proxy_.getText();
450    if(label == null || !label.equals(lbl)) {
451      proxy_.setText(lbl);
452      modified("SGLabel: setText()");
453    }
454  }
455  /**
456   * Get the label identifier.
457   *
458   * @return the identifier
459   **/
460  public String getId() {
461    return ident_;
462  }
463  /**
464   * Set the label identifier.
465   *
466   * @param  id the label identifier
467   */
468  public void setId(String id) {
469    ident_ = id;
470  }
471  /**
472   * Get the label height in device coordinates.
473   *
474   * @return the label height
475   **/
476  public int getHeight() {
477    return 0;
478  }
479  /**
480   * Get the label position in device coordinates.
481   *
482   * @return the label position
483   **/
484  public Point getLocation() {
485    return proxy_.getLocation();
486  }
487  /**
488   * Set the label reference location in pixel coordinates.
489   * <BR><B>Property Change:</B> <code>location</code>.
490   *
491   * @param loc physical location of label
492   **/
493  public void setLocation(Point loc) {
494    Point dloc = proxy_.getLocation();
495    if(dloc.x != loc.x || dloc.y != loc.y) {
496      Point temp = new Point(dloc.x, dloc.y);
497      proxy_.setLocation(loc);
498      if(changes_ == null) changes_ = new PropertyChangeSupport(this);
499      changes_.firePropertyChange("location",
500                                  temp,
501                                  loc);
502    }
503  }
504  /**
505   * Get the label bounds in physical units.
506   *
507   * @return the label bounds
508   **/
509  public Rectangle2D.Double getBoundsP() {
510    return proxy_.getBoundsP();
511  }
512  /**
513   * Get the label bounds in device units.
514   *
515   * @return the label bounds
516   **/
517  public Rectangle getBounds() {
518    return proxy_.getBounds();
519  }
520  /**
521   * Set the label bounds in device units.
522   */
523  public void setBounds(Rectangle r) {
524    setBounds(r.x, r.y, r.width, r.height);
525  }
526  /**
527   * Set the label bounds in device units.
528   */
529  public void setBounds(int x, int y, int width, int height) {
530    proxy_.setBounds(x, y, width, height);
531  }
532  public String toString() {
533    String name = getClass().getName();
534    return name.substring(name.lastIndexOf(".")+1) + ": " + ident_;
535  }
536  public boolean isVisible() {
537    return proxy_.isVisible();
538  }
539  public void setVisible(boolean visible) {
540    boolean vis = proxy_.isVisible();
541    if(vis != visible) {
542      proxy_.setVisible(visible);
543      modified("SGLabel: setVisible()");
544    }
545  }
546  public boolean isMoveable() {
547    return moveable_;
548  }
549  public void setMoveable(boolean moveable) {
550    if(moveable_ != moveable) {
551      moveable_ = moveable;
552      modified("SGLabel: setMoveable()");
553    }
554  }
555  /**
556   * Get the string width in device units.
557   * @since 2.0
558   */
559  public float getStringWidth(Graphics g) {
560    return proxy_.getStringWidth(g);
561  }
562  /**
563   * Get the string height in device units.
564   * @since 2.0
565   */
566  public float getStringHeight(Graphics g) {
567    return proxy_.getStringHeight(g);
568  }
569
570  public void addPropertyChangeListener(PropertyChangeListener l) {
571    if(changes_ == null) changes_ = new PropertyChangeSupport(this);
572    changes_.addPropertyChangeListener(l);
573  }
574  public void removePropertyChangeListener(PropertyChangeListener l) {
575    changes_.removePropertyChangeListener(l);
576  }
577}
Note: See TracBrowser for help on using the repository browser.