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