KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > launcher > GraphicUtils


1 /**
2  * $RCSfile: GraphicUtils.java,v $
3  * $Revision: 1.2 $
4  * $Date: 2004/10/25 23:41:59 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.launcher;
13
14 import java.awt.*;
15 import java.awt.event.MouseEvent JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import javax.swing.*;
19
20 /**
21  * <code>GraphicsUtils</code> class defines common user-interface related utility
22  * functions.
23  */

24 public final class GraphicUtils {
25     private static final Insets HIGHLIGHT_INSETS = new Insets(1, 1, 1, 1);
26     public static final Color SELECTION_COLOR = new java.awt.Color JavaDoc(166, 202, 240);
27     public static final Color TOOLTIP_COLOR = new java.awt.Color JavaDoc(166, 202, 240);
28
29     protected final static Component component = new Component() {
30     };
31     protected final static MediaTracker tracker = new MediaTracker(component);
32
33     private static Hashtable JavaDoc imageCache = new Hashtable JavaDoc();
34
35     private GraphicUtils() {
36     }
37
38
39     /**
40      * Sets the location of the specified window so that it is centered on screen.
41      *
42      * @param window The window to be centered.
43      */

44     public static void centerWindowOnScreen(Window window) {
45         final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
46         final Dimension size = window.getSize();
47
48         if (size.height > screenSize.height) {
49             size.height = screenSize.height;
50         }
51
52         if (size.width > screenSize.width) {
53             size.width = screenSize.width;
54         }
55
56         window.setLocation((screenSize.width - size.width) / 2,
57                 (screenSize.height - size.height) / 2);
58     }
59
60     /**
61      * Draws a single-line highlight border rectangle.
62      *
63      * @param g The graphics context to use for drawing.
64      * @param x The left edge of the border.
65      * @param y The top edge of the border.
66      * @param width The width of the border.
67      * @param height The height of the border.
68      * @param raised <code>true</code> if the border is to be drawn raised,
69      * <code>false</code> if lowered.
70      * @param shadow The shadow color for the border.
71      * @param highlight The highlight color for the border.
72      * @see javax.swing.border.EtchedBorder
73      * @see javax.swing.plaf.basic.BasicGraphicsUtils#drawEtchedRect
74      */

75     public static void drawHighlightBorder(Graphics g, int x, int y,
76                                            int width, int height, boolean raised,
77                                            Color shadow, Color highlight) {
78         final Color oldColor = g.getColor();
79         g.translate(x, y);
80
81         g.setColor(raised ? highlight : shadow);
82         g.drawLine(0, 0, width - 2, 0);
83         g.drawLine(0, 1, 0, height - 2);
84
85         g.setColor(raised ? shadow : highlight);
86         g.drawLine(width - 1, 0, width - 1, height - 1);
87         g.drawLine(0, height - 1, width - 2, height - 1);
88
89         g.translate(-x, -y);
90         g.setColor(oldColor);
91     }
92
93     /**
94      * Return the amount of space taken up by a highlight border drawn by
95      * <code>drawHighlightBorder()</code>.
96      *
97      * @return The <code>Insets</code> needed for the highlight border.
98      * @see #drawHighlightBorder
99      */

100     public static Insets getHighlightBorderInsets() {
101         return HIGHLIGHT_INSETS;
102     }
103
104     public static ImageIcon createImageIcon(Image image) {
105         if (image == null) {
106             return null;
107         }
108
109         synchronized (tracker) {
110             tracker.addImage(image, 0);
111             try {
112                 tracker.waitForID(0, 0);
113             } catch (InterruptedException JavaDoc e) {
114                 System.out.println("INTERRUPTED while loading Image");
115             }
116             tracker.removeImage(image, 0);
117         }
118
119         return new ImageIcon(image);
120     }
121
122     /**
123      * Returns a point where the given popup menu should be shown. The
124      * point is calculated by adjusting the X and Y coordinates from the
125      * given mouse event so that the popup menu will not be clipped by
126      * the screen boundaries.
127      *
128      * @param popup the popup menu
129      * @param event the mouse event
130      * @return the point where the popup menu should be shown
131      */

132     public static Point getPopupMenuShowPoint(JPopupMenu popup, MouseEvent JavaDoc event) {
133         Component source = (Component) event.getSource();
134         Point topLeftSource = source.getLocationOnScreen();
135         Point ptRet = getPopupMenuShowPoint(popup,
136                 topLeftSource.x + event.getX(),
137                 topLeftSource.y + event.getY());
138         ptRet.translate(-topLeftSource.x, -topLeftSource.y);
139         return ptRet;
140     }
141
142     /**
143      * Returns a point where the given popup menu should be shown. The
144      * point is calculated by adjusting the X and Y coordinates so that
145      * the popup menu will not be clipped by the screen boundaries.
146      *
147      * @param popup the popup menu
148      * @param x the x position in screen coordinate
149      * @param y the y position in screen coordinates
150      * @return the point where the popup menu should be shown in screen
151      * coordinates
152      */

153     public static Point getPopupMenuShowPoint(JPopupMenu popup, int x, int y) {
154         Dimension sizeMenu = popup.getPreferredSize();
155         Point bottomRightMenu = new Point(x + sizeMenu.width, y + sizeMenu.height);
156
157         Rectangle[] screensBounds = getScreenBounds();
158         int n = screensBounds.length;
159         for (int i = 0; i < n; i++) {
160             Rectangle screenBounds = screensBounds[i];
161             if (screenBounds.x <= x && x <= (screenBounds.x + screenBounds.width)) {
162                 Dimension sizeScreen = screenBounds.getSize();
163                 sizeScreen.height -= 32; // Hack to help prevent menu being clipped by Windows/Linux/Solaris Taskbar.
164

165                 int xOffset = 0;
166                 if (bottomRightMenu.x > (screenBounds.x + sizeScreen.width))
167                     xOffset = -sizeMenu.width;
168
169                 int yOffset = 0;
170                 if (bottomRightMenu.y > (screenBounds.y + sizeScreen.height))
171                     yOffset = sizeScreen.height - bottomRightMenu.y;
172
173                 return new Point(x + xOffset, y + yOffset);
174             }
175         }
176
177         return new Point(x, y); // ? that would mean that the top left point was not on any screen.
178
}
179
180     /**
181      * Centers the window over a component (usually another window).
182      * The window must already have been sized.
183      */

184     public static void centerWindowOnComponent(Window window, Component over) {
185         if ((over == null) || !over.isShowing()) {
186             centerWindowOnScreen(window);
187             return;
188         }
189
190
191         Point parentLocation = over.getLocationOnScreen();
192         Dimension parentSize = over.getSize();
193         Dimension size = window.getSize();
194
195         // Center it.
196
int x = parentLocation.x + (parentSize.width - size.width) / 2;
197         int y = parentLocation.y + (parentSize.height - size.height) / 2;
198
199         // Now, make sure it's onscreen
200
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
201
202         // This doesn't actually work on the Mac, where the screen
203
// doesn't necessarily start at 0,0
204
if (x + size.width > screenSize.width)
205             x = screenSize.width - size.width;
206
207         if (x < 0)
208             x = 0;
209
210         if (y + size.height > screenSize.height)
211             y = screenSize.height - size.height;
212
213         if (y < 0)
214             y = 0;
215
216         window.setLocation(x, y);
217     }
218
219     /**
220      * @return returns true if the component of one of its child has the focus
221      */

222     public static boolean isAncestorOfFocusedComponent(Component c) {
223         if (c.hasFocus()) {
224             return true;
225         } else {
226             if (c instanceof Container) {
227                 Container cont = (Container) c;
228                 int n = cont.getComponentCount();
229                 for (int i = 0; i < n; i++) {
230                     Component child = cont.getComponent(i);
231                     if (isAncestorOfFocusedComponent(child))
232                         return true;
233                 }
234             }
235         }
236         return false;
237     }
238
239     /**
240      * Returns the first component in the tree of <code>c</code> that can accept
241      * the focus.
242      *
243      * @param c the root of the component hierarchy to search
244      * @see #focusComponentOrChild
245      * @deprecated replaced by {@link #getFocusableComponentOrChild(Component, boolean)}
246      */

247     public static Component getFocusableComponentOrChild(Component c) {
248         return getFocusableComponentOrChild(c, false);
249     }
250
251     /**
252      * Returns the first component in the tree of <code>c</code> that can accept
253      * the focus.
254      *
255      * @param c the root of the component hierarchy to search
256      * @param deepest if <code>deepest</code> is true the method will return the first and deepest component that can accept the
257      * focus. For example, if both a child and its parent are focusable and <code>deepest</code> is true, the child is
258      * returned.
259      * @see #focusComponentOrChild
260      */

261     public static Component getFocusableComponentOrChild(Component c, boolean deepest) {
262         if (c != null && c.isEnabled() && c.isVisible()) {
263             if (c instanceof Container) {
264                 Container cont = (Container) c;
265
266                 if (deepest == false) { // first one is a good one
267
if (c instanceof JComponent) {
268                         JComponent jc = (JComponent) c;
269                         if (jc.isRequestFocusEnabled()) {
270                             return jc;
271                         }
272                     }
273                 }
274
275                 int n = cont.getComponentCount();
276                 for (int i = 0; i < n; i++) {
277                     Component child = cont.getComponent(i);
278                     Component focused = getFocusableComponentOrChild(child, deepest);
279                     if (focused != null) {
280                         return focused;
281                     }
282                 }
283
284                 if (c instanceof JComponent) {
285                     if (deepest == true) {
286                         JComponent jc = (JComponent) c;
287                         if (jc.isRequestFocusEnabled()) {
288                             return jc;
289                         }
290                     }
291                 } else {
292                     return c;
293                 }
294             }
295         }
296
297         return null;
298     }
299
300     /**
301      * Puts the focus on the first component in the tree of <code>c</code> that
302      * can accept the focus.
303      *
304      * @see #getFocusableComponentOrChild
305      */

306     public static Component focusComponentOrChild(Component c) {
307         return focusComponentOrChild(c, false);
308     }
309
310     /**
311      * Puts the focus on the first component in the tree of <code>c</code> that
312      * can accept the focus.
313      *
314      * @param c the root of the component hierarchy to search
315      * @param deepest if <code>deepest</code> is true the method will focus the first and deepest component that can
316      * accept the focus.
317      * For example, if both a child and its parent are focusable and <code>deepest</code> is true, the child is focused.
318      * @see #getFocusableComponentOrChild
319      */

320     public static Component focusComponentOrChild(Component c, boolean deepest) {
321         final Component focusable = getFocusableComponentOrChild(c, deepest);
322         if (focusable != null) {
323             focusable.requestFocus();
324         }
325         return focusable;
326     }
327
328     /**
329      * Loads an {@link Image} named <code>imageName</code> as a resource
330      * relative to the Class <code>cls</code>. If the <code>Image</code> can
331      * not be loaded, then <code>null</code> is returned. Images loaded here
332      * will be added to an internal cache based upon the full {@link URL} to
333      * their location.
334      * <p/>
335      * <em>This method replaces legacy code from JDeveloper 3.x and earlier.</em>
336      *
337      * @see Class#getResource(String)
338      * @see Toolkit#createImage(URL)
339      */

340     public static Image loadFromResource(String JavaDoc imageName, Class JavaDoc cls) {
341         try {
342             final URL JavaDoc url = cls.getResource(imageName);
343
344             if (url == null) {
345                 return null;
346             }
347
348             Image image = (Image) imageCache.get(url.toString());
349
350             if (image == null) {
351                 image = Toolkit.getDefaultToolkit().createImage(url);
352                 imageCache.put(url.toString(), image);
353             }
354
355             return image;
356         } catch (Exception JavaDoc e) {
357             e.printStackTrace();
358         }
359
360         return null;
361     }
362
363     public static Rectangle[] getScreenBounds() {
364         GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
365         final GraphicsDevice[] screenDevices = graphicsEnvironment.getScreenDevices();
366         Rectangle[] screenBounds = new Rectangle[screenDevices.length];
367         for (int i = 0; i < screenDevices.length; i++) {
368             GraphicsDevice screenDevice = screenDevices[i];
369             final GraphicsConfiguration defaultConfiguration = screenDevice.getDefaultConfiguration();
370             screenBounds[i] = defaultConfiguration.getBounds();
371         }
372
373         return screenBounds;
374     }
375
376     public static final void makeSameSize(JComponent[] comps) {
377         if (comps.length == 0) {
378             return;
379         }
380
381         int max = 0;
382         for (int i = 0; i < comps.length; i++) {
383             int w = comps[i].getPreferredSize().width;
384             max = w > max ? w : max;
385         }
386
387         Dimension dim = new Dimension(max, comps[0].getPreferredSize().height);
388         for (int i = 0; i < comps.length; i++) {
389             comps[i].setPreferredSize(dim);
390         }
391     }
392
393     /**
394      * Return the hexidecimal color from a java.awt.Color
395      *
396      * @param c
397      * @return hexadecimal string
398      */

399     public static final String JavaDoc toHTMLColor(Color c) {
400         int color = c.getRGB();
401         color |= 0xff000000;
402         String JavaDoc s = Integer.toHexString(color);
403         return s.substring(2);
404     }
405
406     public static final String JavaDoc createToolTip(String JavaDoc text, int width) {
407         final String JavaDoc htmlColor = toHTMLColor(TOOLTIP_COLOR);
408         final String JavaDoc toolTip = "<html><table width=" + width + " bgColor=" + htmlColor + "><tr><td><b>" + text + "</b></td></tr></table></table>";
409         return toolTip;
410     }
411
412     public static final String JavaDoc createToolTip(String JavaDoc text) {
413         final String JavaDoc htmlColor = toHTMLColor(TOOLTIP_COLOR);
414         final String JavaDoc toolTip = "<html><table bgColor=" + htmlColor + "><tr><td><b>" + text + "</b></td></tr></table></table>";
415         return toolTip;
416     }
417 }
418
419
Popular Tags