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

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

Nouveau projet

File size: 10.5 KB
Line 
1/*
2 * $Id: ValueIcon.java,v 1.19 2003/08/22 23:02:39 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.swing;
13
14import gov.noaa.pmel.sgt.LayerChild;
15import gov.noaa.pmel.sgt.Layer;
16import gov.noaa.pmel.sgt.CartesianGraph;
17import gov.noaa.pmel.sgt.AbstractPane;
18
19import gov.noaa.pmel.util.Point2D;
20import gov.noaa.pmel.util.SoTPoint;
21import gov.noaa.pmel.util.Debug;
22
23import javax.swing.ImageIcon;
24import java.awt.Rectangle;
25import java.awt.Point;
26import java.awt.Image;
27import java.net.URL;
28import java.awt.Graphics;
29import java.awt.Component;
30import java.awt.Font;
31import java.awt.Color;
32import java.awt.FontMetrics;
33import java.beans.PropertyChangeSupport;
34import java.beans.PropertyChangeListener;
35import java.beans.PropertyVetoException;
36import java.beans.VetoableChangeSupport;
37import java.beans.VetoableChangeListener;
38/**
39 * <code>ValueIcon</code> extends <code>ImageIcon</code> to create a
40 * icon than can be dragged on a <code>sgt</code> plot displaying the
41 * local coordinates along with the image.  Typically a cross-hairs
42 * image is used, but others can be substituted.
43 *
44 * @author Donald Denbo
45 * @version $Revision: 1.19 $, $Date: 2003/08/22 23:02:39 $
46 * @since 2.0
47 * @see UserIcon
48 * @see ValueIconFormat
49 */
50public class ValueIcon extends ImageIcon implements LayerChild, Draggable {
51  private int iWidth_ = 0;
52  private int iHeight_ = 0;
53  private boolean selected_ = false;
54  private boolean selectable_ = true;
55  private boolean moved_ = false;
56  private Layer layer_ = null;
57  private String id_ = null;
58  private boolean visible_ = true;
59  private Rectangle bounds_ = new Rectangle();
60  private Point2D.Double loc_ = new Point2D.Double();
61  private SoTPoint uLoc_ = new SoTPoint(0.0, 0.0);
62  private Font font_ = new Font("Dialog", Font.PLAIN, 12);
63  private Color textColor_ = Color.black;
64  private PropertyChangeSupport changes_ = new PropertyChangeSupport(this);
65  private VetoableChangeSupport vetos_ = new VetoableChangeSupport(this);
66  private ValueIconFormat frmt_ = new ValueIconFormat("#####.##", "#.#");
67  private final static String defTFrmt = "yyyy-MM-dd";
68  /**
69   * Construct a <code>ValueIcon</code> using an image from a
70   * specified file.
71   *
72   * @param filename name of image file
73   * @param description brief textual description of the image
74   */
75  public ValueIcon(String filename, String description) {
76    super(filename, description);
77    getImageSize();
78    frmt_.setTimeFormat(defTFrmt);
79  }
80  /**
81   * Construct a <code>ValueIcon</code> using an image from a
82   * specified <code>URL</code>.
83   *
84   * @param location URL of image file
85   * @param description brief textual description of the image
86   */
87  public ValueIcon(URL location, String description) {
88    super(location, description);
89    getImageSize();
90    frmt_.setTimeFormat(defTFrmt);
91  }
92  /**
93   * Construct a <code>ValueIcon</code> using an <code>Image</code>.
94   *
95   * @param image the image
96   * @param description brief textual description of the image
97   */
98  public ValueIcon(Image image, String description) {
99    super(image, description);
100    getImageSize();
101    frmt_.setTimeFormat(defTFrmt);
102  }
103  /**
104   * Set format to be used to create the value string.  Default format
105   * is <code>new ValueIconFormat("#####.##", "#")</code>.
106   *
107   * @param the value format
108   */
109  public void setValueFormat(ValueIconFormat vf) {
110    frmt_ = vf;
111  }
112  /**
113   * Paint the icon at the specified location.
114   */
115  public void paintIcon(Component c, Graphics g, int x, int y) {
116    String text;
117    double xu, yu;
118    double xt, yt;
119    //
120    // compute bounds
121    //
122    text = frmt_.format(uLoc_);
123    FontMetrics fmet = g.getFontMetrics();
124    int wid = fmet.stringWidth(text);
125    int hgt = fmet.getMaxAscent() + fmet.getMaxDescent();
126    bounds_.x = x;
127    bounds_.y = y;
128    bounds_.width = iWidth_ + wid;
129    bounds_.height = iHeight_ + fmet.getMaxDescent();
130    //
131    if(visible_) {
132      g.drawImage(getImage(), bounds_.x, bounds_.y, layer_.getPane().getComponent());
133      int xl = bounds_.x + iWidth_;
134      int yl = bounds_.y + iHeight_;
135      g.setFont(font_);
136      g.setColor(layer_.getPane().getComponent().getBackground());
137      g.fillRect(xl,yl-fmet.getMaxAscent(),wid,hgt);
138      g.setColor(textColor_);
139      g.drawString(text, xl, yl);
140    }
141  }
142  /**
143   * Set the font for the value label.
144   *
145   * @param font the font
146   */
147  public void setFont(Font font) {
148    font_ = font;
149  }
150  /**
151   * Get the value label font
152   */
153  public Font getFont() {
154    return font_;
155  }
156  private void getImageSize() {
157    iWidth_ = super.getIconWidth();
158    iHeight_ = super.getIconHeight();
159  }
160  /**
161   * Get the total width, icon + label.
162   */
163  public int getIconWidth() {
164    return bounds_.width;
165  }
166  /**
167   * Get the total heigth.
168   */
169  public int getIconHeight() {
170    return bounds_.height;
171  }
172  public LayerChild copy() {
173    return null;
174  }
175  public void setVisible(boolean vis) {
176    if(visible_ != vis) {
177      visible_ = vis;
178    }
179  }
180  public boolean isVisible() {
181    return visible_;
182  }
183
184  public void draw(Graphics g) {
185    int x = ((CartesianGraph)layer_.getGraph()).getXUtoD(uLoc_.getX()) -
186      iWidth_/2;
187    int y = ((CartesianGraph)layer_.getGraph()).getYUtoD(uLoc_.getY()) -
188      iHeight_/2;
189    paintIcon(layer_.getPane().getComponent(), g, x, y);
190  }
191  public String getId() {
192    return id_;
193  }
194  public Layer getLayer() {
195    return layer_;
196  }
197  public AbstractPane getPane() {
198    return layer_.getPane();
199  }
200  public void modified(String mess) {
201    if(layer_ != null)
202      layer_.modified(mess);
203  }
204  public void setId(String id) {
205    id_ = id;
206  }
207  public void setLayer(Layer l) {
208    layer_ = l;
209  }
210  public String toString() {
211    return "ValueIcon: " + id_;
212  }
213  public Rectangle getBounds() {
214    return bounds_;
215  }
216  public boolean isSelected() {
217    return selected_;
218  }
219  public void setSelected(boolean sel) {
220    selected_ = sel;
221  }
222  public boolean isSelectable() {
223    return selectable_;
224  }
225  public void setSelectable(boolean select) {
226    selectable_ = select;
227  }
228  /**
229   * Get the icon location in physical units.
230   */
231  public Point2D.Double getLocationP() {
232    return loc_;
233  }
234  /**
235   * Set the icon location in physical units.
236   * <BR><B>Property Change:</B> <code>location</code>.
237   */
238  public void setLocationP(Point2D.Double loc) {
239    SoTPoint pt;
240    loc_ = loc;
241    bounds_.x = layer_.getXPtoD(loc_.x) - iWidth_/2;
242    bounds_.y = layer_.getYPtoD(loc_.y) - iHeight_/2;
243    pt = ((CartesianGraph)layer_.getGraph()).getPtoU(loc_);
244    if(!pt.equals(uLoc_) || moved_) {
245      SoTPoint temp = new SoTPoint(pt);
246      changes_.firePropertyChange("location",
247          uLoc_,
248          temp);
249      uLoc_ = temp;
250      moved_ = false;
251    }
252  }
253  /**
254   * Get the icon location in user units.
255   *
256   * @since 3.0
257   */
258  public SoTPoint getLocationU() {
259    return uLoc_;
260  }
261  /**
262   * Set the icon location in user units.  Location change can't be
263   * vetoed.
264   *
265   * @since 3.0
266   */
267  public void setLocationUNoVeto(SoTPoint loc) {
268    moved_ = moved_ || !loc.equals(uLoc_);
269    uLoc_ = loc;
270    loc_.x = ((CartesianGraph)layer_.getGraph()).getXUtoP(uLoc_.getX());
271    loc_.y = ((CartesianGraph)layer_.getGraph()).getYUtoP(uLoc_.getY());
272    bounds_.x = layer_.getXPtoD(loc_.x);
273    bounds_.y = layer_.getYPtoD(loc_.y);
274  }
275  /**
276   * Set the icon location in user units.  Location change can be
277   * vetoed.
278   * <BR><B>Property Change:</B> <code>location</code>.
279   * @since 3.0
280   */
281  public void setLocationU(SoTPoint loc) throws PropertyVetoException {
282    if(!loc.equals(uLoc_) || moved_) {
283      vetos_.fireVetoableChange("location", uLoc_, loc);
284
285      changes_.firePropertyChange("location",
286          uLoc_,
287          loc);
288      uLoc_ = loc;
289      moved_ = false;
290      loc_.x = ((CartesianGraph)layer_.getGraph()).getXUtoP(uLoc_.getX());
291      loc_.y = ((CartesianGraph)layer_.getGraph()).getYUtoP(uLoc_.getY());
292      bounds_.x = layer_.getXPtoD(loc_.x) - iWidth_/2;
293      bounds_.y = layer_.getYPtoD(loc_.y) - iHeight_/2;
294    }
295  }
296  /**
297   * Set icon location in device coordinates. Locatoin change can't be
298   * vetoed.
299   */
300  public void setLocationNoVeto(int x, int y) {
301    SoTPoint pt;
302    bounds_.x = x;
303    bounds_.y = y;
304    loc_.x = layer_.getXDtoP(x);
305    loc_.y = layer_.getYDtoP(y);
306    pt = ((CartesianGraph)layer_.getGraph()).getPtoU(loc_);
307    moved_ = moved_ || !pt.equals(uLoc_);
308    uLoc_ = new SoTPoint(pt);
309  }
310  /**
311   * Set icon location in device units
312   */
313  public void setLocation(Point loc) {
314    setLocation(loc, true);
315  }
316  /**
317   * Set icon location in device units and optionally fire a
318   * <code>PropertyChangeEvent</code>.
319   *
320   * @since 3.0
321   */
322  public void setLocation(Point loc, boolean fireEvent) {
323    setBounds(loc.x, loc.y, 0, 0, fireEvent);
324  }
325  /**
326   * Set icon bounds.
327   * <BR><B>Property Change:</B> <code>location</code>.
328   */
329  public void setBounds(int x, int y, int width, int height) {
330    setBounds(x, y, width, height, true);
331  }
332
333  private void setBounds(int x, int y, int width, int height, boolean fireEvent) {
334    SoTPoint pt;
335    bounds_.x = x;
336    bounds_.y = y;
337    loc_.x = layer_.getXDtoP(x + iWidth_/2);
338    loc_.y = layer_.getYDtoP(y + iHeight_/2);
339    pt = ((CartesianGraph)layer_.getGraph()).getPtoU(loc_);
340    moved_ = moved_ || !pt.equals(uLoc_);
341    if(moved_) {
342      SoTPoint temp = new SoTPoint(pt);
343      if(fireEvent) {
344        changes_.firePropertyChange("location",
345                                    uLoc_,
346                                    temp);
347        moved_ = false;
348      }
349      uLoc_ = temp;
350    }
351
352  }
353  /**
354   * Set icon bounds.
355   */
356  public void setBounds(Rectangle bounds) {
357    setBounds(bounds.x, bounds.y, bounds.width, bounds.height);
358  }
359  public void addVetoableChangeListener(VetoableChangeListener l) {
360    vetos_.addVetoableChangeListener(l);
361  }
362  public void removeVetoableChangeListener(VetoableChangeListener l) {
363    vetos_.removeVetoableChangeListener(l);
364  }
365  public void addPropertyChangeListener(PropertyChangeListener l) {
366    changes_.addPropertyChangeListener(l);
367  }
368  public void removePropertyChangeListener(PropertyChangeListener l) {
369    changes_.removePropertyChangeListener(l);
370  }
371
372}
373
Note: See TracBrowser for help on using the repository browser.