1 11 12 package org.jivesoftware.messenger.launcher; 13 14 import java.awt.*; 15 import java.awt.event.MouseEvent ; 16 import java.net.URL ; 17 import java.util.Hashtable ; 18 import javax.swing.*; 19 20 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 (166, 202, 240); 27 public static final Color TOOLTIP_COLOR = new java.awt.Color (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 imageCache = new Hashtable (); 34 35 private GraphicUtils() { 36 } 37 38 39 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 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 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 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 132 public static Point getPopupMenuShowPoint(JPopupMenu popup, MouseEvent 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 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; 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); } 179 180 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 int x = parentLocation.x + (parentSize.width - size.width) / 2; 197 int y = parentLocation.y + (parentSize.height - size.height) / 2; 198 199 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 201 202 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 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 247 public static Component getFocusableComponentOrChild(Component c) { 248 return getFocusableComponentOrChild(c, false); 249 } 250 251 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) { 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 306 public static Component focusComponentOrChild(Component c) { 307 return focusComponentOrChild(c, false); 308 } 309 310 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 340 public static Image loadFromResource(String imageName, Class cls) { 341 try { 342 final URL 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 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 399 public static final String toHTMLColor(Color c) { 400 int color = c.getRGB(); 401 color |= 0xff000000; 402 String s = Integer.toHexString(color); 403 return s.substring(2); 404 } 405 406 public static final String createToolTip(String text, int width) { 407 final String htmlColor = toHTMLColor(TOOLTIP_COLOR); 408 final String 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 createToolTip(String text) { 413 final String htmlColor = toHTMLColor(TOOLTIP_COLOR); 414 final String toolTip = "<html><table bgColor=" + htmlColor + "><tr><td><b>" + text + "</b></td></tr></table></table>"; 415 return toolTip; 416 } 417 } 418 419 | Popular Tags |