Index: openide/src/org/openide/util/Utilities.java =================================================================== RCS file: /cvs/openide/src/org/openide/util/Utilities.java,v retrieving revision 1.84 diff -c -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 27 Feb 2002 15:10:38 -0000 *************** *** 41,46 **** --- 41,49 ---- 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,1342 **** return m; } /** @return size of the screen. The size is modified for Windows OS * - some pointes are subtracted to reflect a presence of the taskbar */ public static final Dimension getScreenSize() { ! Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); if (isWindows() && !Boolean.getBoolean ("netbeans.no.taskbar")) { screenSize.height -= TYPICAL_WINDOWS_TASKBAR_HEIGHT; } else if ((getOperatingSystem() & OS_MAC) != 0) --- 1335,1473 ---- 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 + */ + private 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(); + } + + /** + * Helps client code place components on the center of the screen. It + * handles multiple monitor configuration correctly, placing the + * rectangle on the current monitor. + * + * @param componentSize the size of the component + * @return bounds of the centered component + * + * @since 2.5 + */ + public static Rectangle getCenterBounds(Dimension componentSize) { + GraphicsConfiguration gconf = getCurrentGraphicsConfiguration (); + return getCenterBounds (gconf, componentSize); + } + + + /** + * Helps client code place components on the center of the screen. It + * handles multiple monitor configuration correctly, placing the + * rectangle on the current monitor. + * + * @param componentSize the size of the component + * @return bounds of the centered component + */ + private static Rectangle getCenterBounds (GraphicsConfiguration gconf, Dimension componentSize) { + 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); + } + + /** + * 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. Works for the current monitor. + * + * @return the rectangle of the screen where one can place windows + */ + public static Rectangle getScreenBounds() { + GraphicsConfiguration gconf = getCurrentGraphicsConfiguration (); + 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; + } + /** @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, + * instead getScreenBounds should be used + * + * @see #getScreenBounds */ public static final Dimension 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,1388 **** contentPane.add(chooser, BorderLayout.CENTER); dialog.pack(); ! dialog.setLocationRelativeTo(parent); chooser.rescanCurrentDirectory(); final int[] retValue = new int[] { javax.swing.JFileChooser.CANCEL_OPTION }; --- 1513,1520 ---- contentPane.add(chooser, BorderLayout.CENTER); dialog.pack(); ! dialog.setBounds(getCenterBounds(parent.getGraphicsConfiguration(), ! dialog.getSize())); chooser.rescanCurrentDirectory(); final int[] retValue = new int[] { javax.swing.JFileChooser.CANCEL_OPTION };