1 7 package org.jdesktop.swing.utils; 8 9 import java.awt.Component ; 10 import java.awt.GraphicsDevice ; 11 import java.awt.GraphicsEnvironment ; 12 import java.awt.GridBagConstraints ; 13 import java.awt.Insets ; 14 import java.awt.MouseInfo ; 15 import java.awt.Point ; 16 import java.awt.Rectangle ; 17 import java.awt.Window ; 18 19 import javax.swing.JComponent ; 20 import javax.swing.JDesktopPane ; 21 import javax.swing.JDialog ; 22 import javax.swing.JFrame ; 23 import javax.swing.JInternalFrame ; 24 import javax.swing.RootPaneContainer ; 25 import org.jdesktop.swing.utils.Spatial; 26 27 32 public final class WindowUtils { 33 34 37 private WindowUtils() { 38 } 39 40 55 public static Point getPointForCentering(Window window) { 56 Point mousePoint = MouseInfo.getPointerInfo().getLocation(); 58 GraphicsDevice [] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(); 59 for (GraphicsDevice device : devices) { 60 Rectangle bounds = device.getDefaultConfiguration().getBounds(); 61 if (mousePoint.x >= bounds.x && mousePoint.y >= bounds.y 63 && mousePoint.x <= (bounds.x + bounds.width) 64 && mousePoint.y <= (bounds.y + bounds.height)) { 65 int screenWidth = bounds.width; 67 int screenHeight = bounds.height; 68 int width = window.getWidth(); 69 int height = window.getHeight(); 70 Point p = new Point (((screenWidth - width) / 2) + bounds.x, ((screenHeight - height) / 2) + bounds.y); 71 return p; 72 } 73 } 74 return new Point (0,0); 75 } 76 77 93 public static Point getPointForCentering(JInternalFrame window, JDesktopPane desktop) { 94 Point mousePoint = MouseInfo.getPointerInfo().getLocation(); 96 GraphicsDevice [] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(); 97 for (GraphicsDevice device : devices) { 98 Rectangle bounds = device.getDefaultConfiguration().getBounds(); 99 if (mousePoint.x >= bounds.x && mousePoint.y >= bounds.y 101 && mousePoint.x <= (bounds.x + bounds.width) 102 && mousePoint.y <= (bounds.y + bounds.height)) { 103 int screenWidth = bounds.width; 105 int screenHeight = bounds.height; 106 int width = window.getWidth(); 107 int height = window.getHeight(); 108 Point p = new Point (((screenWidth - width) / 2) + bounds.x, ((screenHeight - height) / 2) + bounds.y); 109 return p; 110 } 111 } 112 return new Point (0,0); 113 } 114 115 122 public static void setConstraints(GridBagConstraints gbc, int gridx, int gridy, int gridwidth, int gridheight, 123 double weightx, double weighty, int anchor, int fill, int top, int left, int bottom, int right) { 124 gbc.gridx = gridx; 125 gbc.gridy = gridy; 126 gbc.gridwidth = gridwidth; 127 gbc.gridheight = gridheight; 128 gbc.weightx = weightx; 129 gbc.weighty = weighty; 130 gbc.anchor = anchor; 131 gbc.fill = fill; 132 gbc.insets = new Insets (top, left, bottom, right); 133 } 134 135 141 public static Spatial getSpatial(Window win) { 142 Spatial spatial = new Spatial(win.getY(), win.getX(), win.getWidth(), win.getHeight()); 143 return spatial; 144 } 145 146 152 public static Spatial getSpatial(JComponent comp) { 153 Spatial spatial = new Spatial(comp.getY(), comp.getX(), comp.getWidth(), comp.getHeight()); 154 return spatial; 155 } 156 157 162 public static RootPaneContainer findRootPaneContainer(Component c) { 163 if (c == null) { 164 return null; 165 } else if (c instanceof RootPaneContainer ) { 166 return (RootPaneContainer )c; 167 } else { 168 return findRootPaneContainer(c.getParent()); 169 } 170 } 171 172 177 public static JFrame findJFrame(Component c) { 178 if (c == null) { 179 return null; 180 } else if (c instanceof RootPaneContainer ) { 181 return (JFrame )c; 182 } else { 183 return findJFrame(c.getParent()); 184 } 185 } 186 187 192 public static JDialog findJDialog(Component c) { 193 if (c == null) { 194 return null; 195 } else if (c instanceof JDialog ) { 196 return (JDialog )c; 197 } else { 198 return findJDialog(c.getParent()); 199 } 200 } 201 } 202 | Popular Tags |