1 | /* |
---|
2 | * $Id: Logo.java,v 1.15 2001/12/13 19:07:04 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; |
---|
14 | |
---|
15 | import gov.noaa.pmel.util.Point2D; |
---|
16 | import gov.noaa.pmel.util.Dimension2D; |
---|
17 | import gov.noaa.pmel.util.Debug; |
---|
18 | |
---|
19 | import java.awt.Panel; |
---|
20 | import java.awt.Graphics; |
---|
21 | import java.awt.Rectangle; |
---|
22 | import java.awt.Point; |
---|
23 | import java.net.URL; |
---|
24 | import java.awt.Image; |
---|
25 | import java.awt.MediaTracker; |
---|
26 | |
---|
27 | import java.beans.PropertyChangeListener; |
---|
28 | import java.beans.PropertyChangeSupport; |
---|
29 | // jdk1.2 |
---|
30 | //import java.awt.geom.Point2D; |
---|
31 | |
---|
32 | /** |
---|
33 | * Logo displays an Image on its parent Layer. Logo implements the |
---|
34 | * LayerChild interface. |
---|
35 | * |
---|
36 | * @author Donald Denbo |
---|
37 | * @version $Revision: 1.15 $, $Date: 2001/12/13 19:07:04 $ |
---|
38 | * @since 1.0 |
---|
39 | * @see LayerChild |
---|
40 | **/ |
---|
41 | public class Logo implements Cloneable, LayerChild, Moveable { |
---|
42 | private String ident_; |
---|
43 | /** @directed */ |
---|
44 | private Layer layer_ = null; |
---|
45 | private Point2D.Double porigin_; |
---|
46 | private int valign_; |
---|
47 | private int halign_; |
---|
48 | private Image image_ = null; |
---|
49 | private URL imageURL_; |
---|
50 | private boolean selected_; |
---|
51 | private boolean selectable_; |
---|
52 | private boolean visible_; |
---|
53 | private boolean moveable_; |
---|
54 | private PropertyChangeSupport changes_ = new PropertyChangeSupport(this); |
---|
55 | /** |
---|
56 | * Align to top of Logo. |
---|
57 | */ |
---|
58 | public static final int TOP = 0; |
---|
59 | /** |
---|
60 | * Align to middle of Logo. |
---|
61 | */ |
---|
62 | public static final int MIDDLE = 1; |
---|
63 | /** |
---|
64 | * Align to bottom of Logo. |
---|
65 | */ |
---|
66 | public static final int BOTTOM = 2; |
---|
67 | /** |
---|
68 | * Align to left of Logo. |
---|
69 | */ |
---|
70 | public static final int LEFT = 0; |
---|
71 | /** |
---|
72 | * Align to center of Logo. |
---|
73 | */ |
---|
74 | public static final int CENTER = 1; |
---|
75 | /** |
---|
76 | * Align to right of Logo. |
---|
77 | */ |
---|
78 | public static final int RIGHT = 2; |
---|
79 | /** |
---|
80 | * Default constructor. The default location for the Logo is at |
---|
81 | * (0.0, 0.0) and aligned BOTTOM and LEFT. |
---|
82 | */ |
---|
83 | public Logo() { |
---|
84 | this(new Point2D.Double(0.0, 0.0), BOTTOM, LEFT); |
---|
85 | } |
---|
86 | /** |
---|
87 | * Create a Logo object. The initial object will not have |
---|
88 | * an associated Image until setImage or setImageURL methods |
---|
89 | * have been invoked. Vertical alignments include TOP, MIDDLE, and |
---|
90 | * BOTTOM. Horizontal alignments include LEFT, CENTER, and RIGHT. |
---|
91 | * |
---|
92 | * @param loc Location of Logo |
---|
93 | * @param valign vertical alignment |
---|
94 | * @param halign horizontal alignment |
---|
95 | * |
---|
96 | * @see #setImageURL |
---|
97 | * @see #setImage |
---|
98 | * @see java.awt.Image |
---|
99 | */ |
---|
100 | public Logo(Point2D.Double loc,int valign,int halign) { |
---|
101 | porigin_ = loc; |
---|
102 | valign_ = valign; |
---|
103 | halign_ = halign; |
---|
104 | ident_ = ""; |
---|
105 | selected_ = false; |
---|
106 | selectable_ = true; |
---|
107 | visible_ = true; |
---|
108 | moveable_ = true; |
---|
109 | } |
---|
110 | /** |
---|
111 | * Create of copy of Logo. |
---|
112 | */ |
---|
113 | public LayerChild copy() { |
---|
114 | Logo newLogo; |
---|
115 | try { |
---|
116 | newLogo = (Logo)clone(); |
---|
117 | } catch (CloneNotSupportedException e) { |
---|
118 | newLogo = new Logo(); |
---|
119 | } |
---|
120 | return newLogo; |
---|
121 | } |
---|
122 | public void setSelected(boolean sel) { |
---|
123 | selected_ = sel; |
---|
124 | } |
---|
125 | public boolean isSelected() { |
---|
126 | return selected_; |
---|
127 | } |
---|
128 | public void setSelectable(boolean select) { |
---|
129 | selectable_ = select; |
---|
130 | } |
---|
131 | public boolean isSelectable() { |
---|
132 | return selectable_; |
---|
133 | } |
---|
134 | public boolean isMoveable() { |
---|
135 | return moveable_; |
---|
136 | } |
---|
137 | public void setMoveable(boolean moveable) { |
---|
138 | moveable_ = moveable; |
---|
139 | } |
---|
140 | /** |
---|
141 | * Set the URL for the Logo image. The URL or the image |
---|
142 | * must be set. |
---|
143 | * |
---|
144 | * @param url image URL |
---|
145 | * |
---|
146 | * @see #setImage |
---|
147 | */ |
---|
148 | public void setImageURL(URL url) { |
---|
149 | if(imageURL_ == null || !imageURL_.equals(url)) { |
---|
150 | imageURL_ = url; |
---|
151 | modified("Logo: setImageURL()"); |
---|
152 | } |
---|
153 | } |
---|
154 | /** |
---|
155 | * Get the image URL. The URL will be null if no imageURL was |
---|
156 | * specified. |
---|
157 | * |
---|
158 | * @return the imageURL |
---|
159 | */ |
---|
160 | public URL getImageURL() { |
---|
161 | return imageURL_; |
---|
162 | } |
---|
163 | /** |
---|
164 | * Set the Logo image. The image or the imageURL |
---|
165 | * must be set. |
---|
166 | * |
---|
167 | * @param img Logo image |
---|
168 | * |
---|
169 | * @see #setImageURL |
---|
170 | */ |
---|
171 | public void setImage(Image img) { |
---|
172 | if(image_ == null || !image_.equals(img)) { |
---|
173 | image_ = img; |
---|
174 | modified("Logo: setImage()"); |
---|
175 | } |
---|
176 | } |
---|
177 | /** |
---|
178 | * Set parent layer. |
---|
179 | * |
---|
180 | * @param l parent layer |
---|
181 | */ |
---|
182 | public void setLayer(Layer l) { |
---|
183 | layer_ = l; |
---|
184 | } |
---|
185 | /** |
---|
186 | * Get layer. |
---|
187 | * |
---|
188 | * @return layer |
---|
189 | */ |
---|
190 | public Layer getLayer() { |
---|
191 | return layer_; |
---|
192 | } |
---|
193 | |
---|
194 | public AbstractPane getPane() { |
---|
195 | return layer_.getPane(); |
---|
196 | } |
---|
197 | |
---|
198 | public void modified(String mess) { |
---|
199 | // if(Debug.EVENT) System.out.println("Logo: modified()"); |
---|
200 | if(layer_ != null) |
---|
201 | layer_.modified(mess); |
---|
202 | } |
---|
203 | /** |
---|
204 | * Set Logo identifier. |
---|
205 | * |
---|
206 | * @param id logo identifier |
---|
207 | */ |
---|
208 | public void setId(String id) { |
---|
209 | ident_ = id; |
---|
210 | } |
---|
211 | /** |
---|
212 | * Get Logo identifier |
---|
213 | * |
---|
214 | * @return identifier |
---|
215 | */ |
---|
216 | public String getId() { |
---|
217 | return ident_; |
---|
218 | } |
---|
219 | /** |
---|
220 | * Set alignment. |
---|
221 | * |
---|
222 | * @param vert vertical alignment |
---|
223 | * @param horz horizontal alignment |
---|
224 | */ |
---|
225 | public void setAlign(int vert,int horz) { |
---|
226 | if(valign_ != vert || halign_ != horz) { |
---|
227 | valign_ = vert; |
---|
228 | halign_ = horz; |
---|
229 | modified("Logo: setAlign()"); |
---|
230 | } |
---|
231 | } |
---|
232 | /** |
---|
233 | * Set vertical alignment |
---|
234 | * |
---|
235 | * @param vert vertical alignment |
---|
236 | */ |
---|
237 | public void setVAlign(int vert) { |
---|
238 | if(valign_ != vert) { |
---|
239 | valign_ = vert; |
---|
240 | modified("Logo: setVAlign()"); |
---|
241 | } |
---|
242 | } |
---|
243 | /** |
---|
244 | * Set horizontal alignment |
---|
245 | * |
---|
246 | * @param horz horizontal alignment |
---|
247 | */ |
---|
248 | public void setHAlign(int horz) { |
---|
249 | if(halign_ != horz) { |
---|
250 | halign_ = horz; |
---|
251 | modified("Logo: setHAlign()"); |
---|
252 | } |
---|
253 | } |
---|
254 | /** |
---|
255 | * Get vertical alignment |
---|
256 | * |
---|
257 | * @return vertical alignment |
---|
258 | */ |
---|
259 | public int getVAlign() { |
---|
260 | return valign_; |
---|
261 | } |
---|
262 | /** |
---|
263 | * Get horizontal alignment |
---|
264 | * |
---|
265 | * @return horizontal alignment |
---|
266 | */ |
---|
267 | public int getHAlign() { |
---|
268 | return halign_; |
---|
269 | } |
---|
270 | /** |
---|
271 | * Set location of logo |
---|
272 | * <BR><B>Property Change:</B> <code>location</code>. |
---|
273 | * |
---|
274 | * @param loc logo location |
---|
275 | */ |
---|
276 | public void setLocationP(Point2D.Double loc) { |
---|
277 | if(porigin_ == null || !porigin_.equals(loc)) { |
---|
278 | Point2D.Double temp = porigin_; |
---|
279 | porigin_ = loc; |
---|
280 | changes_.firePropertyChange("location", |
---|
281 | temp, |
---|
282 | porigin_); |
---|
283 | modified("Logo: setLocationP()"); |
---|
284 | } |
---|
285 | } |
---|
286 | /** |
---|
287 | * Get location of logo. |
---|
288 | * |
---|
289 | * @return Logo location |
---|
290 | */ |
---|
291 | public Point2D.Double getLocationP() { |
---|
292 | return porigin_; |
---|
293 | } |
---|
294 | /** |
---|
295 | * Draw the Logo. If the imageURL was specified the image |
---|
296 | * is retrieved using the URL and MediaTracker. The image |
---|
297 | * is displayed after it is loaded. |
---|
298 | */ |
---|
299 | public void draw(Graphics g) { |
---|
300 | Rectangle bnds; |
---|
301 | if(!visible_) return; |
---|
302 | if (imageURL_ != null && layer_ != null && image_ == null) { |
---|
303 | image_ = layer_.getPane().getComponent().getToolkit().getImage(imageURL_); |
---|
304 | if (image_ != null) { |
---|
305 | MediaTracker mt = new MediaTracker(layer_.getPane().getComponent()); |
---|
306 | try { |
---|
307 | mt.addImage(image_, 0); |
---|
308 | mt.waitForAll(); |
---|
309 | if(mt.isErrorAny()) |
---|
310 | System.err.println("Logo: Error loading image"); |
---|
311 | } |
---|
312 | catch (InterruptedException ie) { } |
---|
313 | System.out.println("MediaTracker: " + mt.checkAll()); |
---|
314 | } |
---|
315 | } |
---|
316 | layer_.getPane().getComponent().invalidate(); |
---|
317 | if(image_ != null) { |
---|
318 | bnds = getBounds(); |
---|
319 | g.drawImage(image_, bnds.x, bnds.y, |
---|
320 | layer_.getPane().getComponent()); |
---|
321 | } |
---|
322 | } |
---|
323 | /** |
---|
324 | * Get the bounding rectangle. |
---|
325 | * |
---|
326 | * @return bounding rectangle |
---|
327 | */ |
---|
328 | public Rectangle getBounds() { |
---|
329 | Rectangle bounds; |
---|
330 | int x, y; |
---|
331 | int width, height; |
---|
332 | if(image_ != null) { |
---|
333 | width = image_.getWidth(layer_.getPane().getComponent()); |
---|
334 | height = image_.getHeight(layer_.getPane().getComponent()); |
---|
335 | x = layer_.getXPtoD(porigin_.x); |
---|
336 | y = layer_.getYPtoD(porigin_.y); |
---|
337 | switch(halign_) { |
---|
338 | case RIGHT: |
---|
339 | x = x - width; |
---|
340 | break; |
---|
341 | case CENTER: |
---|
342 | x = x - width/2; |
---|
343 | } |
---|
344 | switch(valign_) { |
---|
345 | case BOTTOM: |
---|
346 | y = y - height; |
---|
347 | break; |
---|
348 | case MIDDLE: |
---|
349 | y = y - height/2; |
---|
350 | } |
---|
351 | return new Rectangle(x, y, width, height); |
---|
352 | } |
---|
353 | return null; |
---|
354 | } |
---|
355 | public Point getLocation() { |
---|
356 | Rectangle bnds = getBounds(); |
---|
357 | return new Point(bnds.x, bnds.y); |
---|
358 | } |
---|
359 | |
---|
360 | public void setLocation(Point loc) { |
---|
361 | Rectangle bnds = getBounds(); |
---|
362 | setBounds(loc.x, loc.y, bnds.width, bnds.height); |
---|
363 | } |
---|
364 | /** |
---|
365 | * Set the bounds of the <code>Logo</code> |
---|
366 | */ |
---|
367 | public void setBounds(Rectangle r) { |
---|
368 | setBounds(r.x, r.y, r.width, r.height); |
---|
369 | } |
---|
370 | /** |
---|
371 | * Set the bounds of the <code>Logo</code> |
---|
372 | * <BR><B>Property Change:</B> <code>location</code>. |
---|
373 | */ |
---|
374 | public void setBounds(int x, int y, int width, int height) { |
---|
375 | switch(halign_) { |
---|
376 | case RIGHT: |
---|
377 | x = x + width; |
---|
378 | break; |
---|
379 | case CENTER: |
---|
380 | x = x + width/2; |
---|
381 | } |
---|
382 | switch(valign_) { |
---|
383 | case BOTTOM: |
---|
384 | y = y + height; |
---|
385 | break; |
---|
386 | case MIDDLE: |
---|
387 | y = y + height/2; |
---|
388 | } |
---|
389 | double xp = layer_.getXDtoP(x); |
---|
390 | double yp = layer_.getYDtoP(y); |
---|
391 | if(porigin_.x != xp || porigin_.y != yp) { |
---|
392 | Point2D.Double temp = porigin_; |
---|
393 | porigin_.x = xp; |
---|
394 | porigin_.y = yp; |
---|
395 | changes_.firePropertyChange("location", |
---|
396 | temp, |
---|
397 | new Point2D.Double(xp, yp)); |
---|
398 | modified("Logo: setBounds()"); |
---|
399 | } |
---|
400 | } |
---|
401 | public String toString() { |
---|
402 | String name = getClass().getName(); |
---|
403 | return name.substring(name.lastIndexOf(".")+1) + ": " + ident_; |
---|
404 | } |
---|
405 | public boolean isVisible() { |
---|
406 | return visible_; |
---|
407 | } |
---|
408 | public void setVisible(boolean visible) { |
---|
409 | if(visible_ != visible) { |
---|
410 | visible_ = visible; |
---|
411 | modified("Logo: setVisible()"); |
---|
412 | } |
---|
413 | } |
---|
414 | public void addPropertyChangeListener(PropertyChangeListener l) { |
---|
415 | changes_.addPropertyChangeListener(l); |
---|
416 | } |
---|
417 | public void removePropertyChangeListener(PropertyChangeListener l) { |
---|
418 | changes_.removePropertyChangeListener(l); |
---|
419 | } |
---|
420 | } |
---|