Index: openide/src/org/openide/util/Utilities.java =================================================================== RCS file: /cvs/openide/src/org/openide/util/Utilities.java,v retrieving revision 1.84 diff -u -r1.84 Utilities.java --- openide/src/org/openide/util/Utilities.java 20 Feb 2002 09:15:42 -0000 1.84 +++ openide/src/org/openide/util/Utilities.java 26 Feb 2002 16:41:01 -0000 @@ -41,6 +41,9 @@ import java.util.HashSet; import org.openide.util.enum.SingletonEnumeration; +import org.openide.modules.Dependency; +import org.openide.modules.SpecificationVersion; + /** Otherwise uncategorized useful static methods. * * @author Jan Palka, Ian Formanek, Jaroslav Tulach @@ -1332,11 +1335,152 @@ return m; } + /** + * Finds out the monitor where the user currently has the input focus. + * This method is usually used to help the client code to figure out on + * which monitor it should place newly created windows/frames/dialogs. + * + * @return the GraphicsConfiguration of the monitor which currently has the + * input focus + * + * @since 2.5 + */ + public static GraphicsConfiguration getCurrentGraphicsConfiguration() { + Frame[] frames = Frame.getFrames(); + + for (int i = 0; i < frames.length; i++) { + if (javax.swing.SwingUtilities.findFocusOwner(frames[i]) != null) { + return frames[i].getGraphicsConfiguration(); + } + } + return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); + } + + /** + * Returns the usable area of the screen where applications can place its + * windows. The method subtracts from the screen the area of taskbars, + * system menus and the like. + * + * @param gconf the GraphicsConfiguration of the monitor + * @return the rectangle of the screen where one can place windows + * + * @since 2.5 + */ + public static Rectangle getUsableScreenBounds(GraphicsConfiguration gconf) { + if (gconf == null) + gconf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); + + Rectangle bounds = new Rectangle(gconf.getBounds()); + + String str; + + str = System.getProperty("netbeans.screen.insets"); // NOI18N + if (str != null) { + StringTokenizer st = new StringTokenizer(str, ", "); // NOI18N + if (st.countTokens() == 4) { + try { + bounds.y = Integer.parseInt(st.nextToken()); + bounds.x = Integer.parseInt(st.nextToken()); + bounds.height -= bounds.y + Integer.parseInt(st.nextToken()); + bounds.width -= bounds.x + Integer.parseInt(st.nextToken()); + } + catch (NumberFormatException ex) { + ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); + } + } + return bounds; + } + + str = System.getProperty("netbeans.taskbar.height"); // NOI18N + if (str != null) { + bounds.height -= Integer.getInteger(str, 0).intValue(); + return bounds; + } + + // if JDK 1.4 or later + + if (Dependency.JAVA_SPEC.compareTo(new SpecificationVersion("1.4")) >= 0) { // NOI18N + try { + Toolkit toolkit = Toolkit.getDefaultToolkit(); + Method m = Toolkit.class.getMethod("getScreenInsets", // NOI18N + new Class[] { GraphicsConfiguration.class }); + if (m == null) + return bounds; + + Insets insets = (Insets) m.invoke(toolkit, new Object[] { gconf }); + bounds.y += insets.top; + bounds.x += insets.left; + bounds.height -= insets.top + insets.bottom; + bounds.width -= insets.left + insets.right; + } + catch (Exception ex) { + ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); + } + return bounds; + } + + if (Utilities.isWindows ()) { + bounds.height -= Utilities.TYPICAL_WINDOWS_TASKBAR_HEIGHT; + return bounds; + } + + if ((getOperatingSystem() & OS_MAC) != 0) { + bounds.height -= TYPICAL_MACOSX_MENU_HEIGHT; + return bounds; + } + + return bounds; + } + + /** + * Helps client code place components on the center of the screen. It + * handles multiple monitor configuration correctly + * + * @param componentSize the size of the component + * @return bounds of the centered component + * + * @since 2.5 + */ + public static Rectangle getCenterBounds(Dimension componentSize) { + return getCenterBounds(getCurrentGraphicsConfiguration(), + componentSize); + } + + /** + * Helps client code place components on the center of the screen. It + * handles multiple monitor configuration correctly + * + * @param gconf the GraphicsConfiguration of the monitor + * @param componentSize the size of the component + * @return bounds of the centered component + * + * @since 2.5 + */ + public static Rectangle getCenterBounds(GraphicsConfiguration gconf, + Dimension componentSize) { + if (gconf == null) + gconf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); + + Rectangle bounds = gconf.getBounds(); + return new Rectangle(bounds.x + (bounds.width - componentSize.width) / 2, + bounds.y + (bounds.height - componentSize.height) / 2, + componentSize.width, + componentSize.height); + } + /** @return size of the screen. The size is modified for Windows OS * - some pointes are subtracted to reflect a presence of the taskbar + * + * @deprecated this method is almost useless in multiple monitor configuration + * + * @see #getCurrentGraphicsConfiguration + * @see #getUsableScreenBounds(GraphicsConfiguration) + * @see #getCenterBounds(Dimension) + * @see #getCenterBounds(GraphicsConfiguration, Dimension) */ public static final Dimension getScreenSize() { - Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); + Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); + if (isWindows() && !Boolean.getBoolean ("netbeans.no.taskbar")) { screenSize.height -= TYPICAL_WINDOWS_TASKBAR_HEIGHT; } else if ((getOperatingSystem() & OS_MAC) != 0) @@ -1382,7 +1526,8 @@ contentPane.add(chooser, BorderLayout.CENTER); dialog.pack(); - dialog.setLocationRelativeTo(parent); + dialog.setBounds(getCenterBounds(parent.getGraphicsConfiguration(), + dialog.getSize())); chooser.rescanCurrentDirectory(); final int[] retValue = new int[] { javax.swing.JFileChooser.CANCEL_OPTION };