Java Code Examples for org.eclipse.swt.widgets.Monitor#getClientArea()
The following examples show how to use
org.eclipse.swt.widgets.Monitor#getClientArea() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseTransformDialog.java From hop with Apache License 2.0 | 6 votes |
public static void setSize( Shell shell, int prefWidth, int prefHeight ) { PropsUi props = PropsUi.getInstance(); WindowProperty winprop = props.getScreen( shell.getText() ); if ( winprop != null ) { winprop.setShell( shell, prefWidth, prefHeight ); } else { shell.layout(); winprop = new WindowProperty( shell.getText(), false, 0, 0, prefWidth, prefHeight ); winprop.setShell( shell ); // Now, as this is the first time it gets opened, try to put it in the middle of the screen... Rectangle shellBounds = shell.getBounds(); Monitor monitor = shell.getDisplay().getPrimaryMonitor(); if ( shell.getParent() != null ) { monitor = shell.getParent().getMonitor(); } Rectangle monitorClientArea = monitor.getClientArea(); int middleX = monitorClientArea.x + ( monitorClientArea.width - shellBounds.width ) / 2; int middleY = monitorClientArea.y + ( monitorClientArea.height - shellBounds.height ) / 2; shell.setLocation( middleX, middleY ); } }
Example 2
Source File: UIUtils.java From n4js with Eclipse Public License 1.0 | 6 votes |
/** * Given the desired position of the shell, this method returns an adjusted position such that the window is no * larger than its monitor, and does not extend beyond the edge of the monitor. This is used for computing the * initial window position via the parent shell, clients can use this as a utility method if they want to limit the * region in which the window may be moved. * * @param shell * the shell which shell bounds is being calculated. * @param preferredSize * the preferred position of the window. * @return a rectangle as close as possible to preferredSize that does not extend outside the monitor. */ public static Rectangle getConstrainedShellBounds(final Shell shell, final Point preferredSize) { final Point location = getInitialLocation(shell, preferredSize); final Rectangle result = new Rectangle(location.x, location.y, preferredSize.x, preferredSize.y); final Monitor monitor = getClosestMonitor(shell.getDisplay(), Geometry.centerPoint(result)); final Rectangle bounds = monitor.getClientArea(); if (result.height > bounds.height) { result.height = bounds.height; } if (result.width > bounds.width) { result.width = bounds.width; } result.x = Math.max(bounds.x, Math.min(result.x, bounds.x + bounds.width - result.width)); result.y = Math.max(bounds.y, Math.min(result.y, bounds.y + bounds.height - result.height)); return result; }
Example 3
Source File: UIUtils.java From n4js with Eclipse Public License 1.0 | 6 votes |
/** * Returns the initial location to use for the shell. The default implementation centers the shell horizontally (1/2 * of the difference to the left and 1/2 to the right) and vertically (1/3 above and 2/3 below) relative to the * active workbench windows shell, or display bounds if there is no parent shell. * * @param shell * the shell which initial location has to be calculated. * @param initialSize * the initial size of the shell, as returned by <code>getInitialSize</code>. * @return the initial location of the shell */ public static Point getInitialLocation(final Shell shell, final Point initialSize) { final Composite parent = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); Monitor monitor = shell.getDisplay().getPrimaryMonitor(); if (parent != null) { monitor = parent.getMonitor(); } final Rectangle monitorBounds = monitor.getClientArea(); Point centerPoint; if (parent != null) { centerPoint = Geometry.centerPoint(parent.getBounds()); } else { centerPoint = Geometry.centerPoint(monitorBounds); } return new Point(centerPoint.x - (initialSize.x / 2), Math.max( monitorBounds.y, Math.min(centerPoint.y - (initialSize.y * 2 / 3), monitorBounds.y + monitorBounds.height - initialSize.y))); }
Example 4
Source File: UIUtils.java From n4js with Eclipse Public License 1.0 | 6 votes |
/** * Returns the monitor whose client area contains the given point. If no monitor contains the point, returns the * monitor that is closest to the point. * * @param toSearch * point to find (display coordinates). * @param toFind * point to find (display coordinates). * @return the monitor closest to the given point. */ private static Monitor getClosestMonitor(final Display toSearch, final Point toFind) { int closest = Integer.MAX_VALUE; final Monitor[] monitors = toSearch.getMonitors(); Monitor result = monitors[0]; for (int index = 0; index < monitors.length; index++) { final Monitor current = monitors[index]; final Rectangle clientArea = current.getClientArea(); if (clientArea.contains(toFind)) { return current; } final int distance = Geometry.distanceSquared(Geometry.centerPoint(clientArea), toFind); if (distance < closest) { closest = distance; result = current; } } return result; }
Example 5
Source File: BreadcrumbItemDropDown.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 6 votes |
/** * Returns the monitor whose client area contains the given point. If no * monitor contains the point, returns the monitor that is closest to the * point. * <p> * Copied from * <code>org.eclipse.jface.window.Window.getClosestMonitor(Display, Point)</code> * </p> * * @param display the display showing the monitors * @param point point to find (display coordinates) * @return the monitor closest to the given point */ private static Monitor getClosestMonitor(Display display, Point point) { int closest = Integer.MAX_VALUE; Monitor[] monitors = display.getMonitors(); Monitor result = monitors[0]; for (int i = 0; i < monitors.length; i++) { Monitor current = monitors[i]; Rectangle clientArea = current.getClientArea(); if (clientArea.contains(point)) return current; int distance = Geometry.distanceSquared(Geometry.centerPoint(clientArea), point); if (distance < closest) { closest = distance; result = current; } } return result; }
Example 6
Source File: GamlSearchField.java From gama with GNU General Public License v3.0 | 6 votes |
/** * This method was copy/pasted from JFace. */ private static Monitor getClosestMonitor(final Display toSearch, final Point toFind) { int closest = Integer.MAX_VALUE; final Monitor[] monitors = toSearch.getMonitors(); Monitor result = monitors[0]; for (final Monitor current : monitors) { final Rectangle clientArea = current.getClientArea(); if (clientArea.contains(toFind)) { return current; } final int distance = Geometry.distanceSquared(Geometry.centerPoint(clientArea), toFind); if (distance < closest) { closest = distance; result = current; } } return result; }
Example 7
Source File: GamlSearchField.java From gama with GNU General Public License v3.0 | 6 votes |
/** * This method was copy/pasted from JFace. */ private Rectangle getConstrainedShellBounds(final Display display, final Rectangle preferredSize) { final Rectangle result = new Rectangle(preferredSize.x, preferredSize.y, preferredSize.width, preferredSize.height); final Point topLeft = new Point(preferredSize.x, preferredSize.y); final Monitor mon = getClosestMonitor(display, topLeft); final Rectangle bounds = mon.getClientArea(); if (result.height > bounds.height) { result.height = bounds.height; } if (result.width > bounds.width) { result.width = bounds.width; } result.x = Math.max(bounds.x, Math.min(result.x, bounds.x + bounds.width - result.width)); result.y = Math.max(bounds.y, Math.min(result.y, bounds.y + bounds.height - result.height)); return result; }
Example 8
Source File: BreadcrumbItemDropDown.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Returns the monitor whose client area contains the given point. If no monitor contains the * point, returns the monitor that is closest to the point. * <p> * Copied from <code>org.eclipse.jface.window.Window.getClosestMonitor(Display, Point)</code> * </p> * * @param display the display showing the monitors * @param point point to find (display coordinates) * @return the monitor closest to the given point */ private static Monitor getClosestMonitor(Display display, Point point) { int closest= Integer.MAX_VALUE; Monitor[] monitors= display.getMonitors(); Monitor result= monitors[0]; for (int i= 0; i < monitors.length; i++) { Monitor current= monitors[i]; Rectangle clientArea= current.getClientArea(); if (clientArea.contains(point)) return current; int distance= Geometry.distanceSquared(Geometry.centerPoint(clientArea), point); if (distance < closest) { closest= distance; result= current; } } return result; }
Example 9
Source File: DataOverrideHandler.java From pentaho-kettle with Apache License 2.0 | 6 votes |
private Shell getCenteredShell( Shell shell ) { Rectangle shellBounds = shell.getBounds(); Monitor monitor = shell.getDisplay().getPrimaryMonitor(); if ( shell.getParent() != null ) { monitor = shell.getParent().getMonitor(); } Rectangle monitorClientArea = monitor.getClientArea(); int middleX = monitorClientArea.x + ( monitorClientArea.width - shellBounds.width ) / 2; int middleY = monitorClientArea.y + ( monitorClientArea.height - shellBounds.height ) / 2; shell.setLocation( middleX, middleY ); return shell; }
Example 10
Source File: BaseTransformDialog.java From hop with Apache License 2.0 | 5 votes |
/** * Sets the size of this dialog with respect to the given parameters. * * @param shell the shell * @param minWidth the minimum width * @param minHeight the minimum height * @param packIt true to pack the dialog components, false otherwise */ public static void setSize( Shell shell, int minWidth, int minHeight, boolean packIt ) { PropsUi props = PropsUi.getInstance(); WindowProperty winprop = props.getScreen( shell.getText() ); if ( winprop != null ) { winprop.setShell( shell, minWidth, minHeight ); } else { if ( packIt ) { shell.pack(); } else { shell.layout(); } // OK, sometimes this produces dialogs that are waay too big. // Try to limit this a bit, m'kay? // Use the same algorithm by cheating :-) // winprop = new WindowProperty( shell ); winprop.setShell( shell, minWidth, minHeight ); // Now, as this is the first time it gets opened, try to put it in the middle of the screen... Rectangle shellBounds = shell.getBounds(); Monitor monitor = shell.getDisplay().getPrimaryMonitor(); if ( shell.getParent() != null ) { monitor = shell.getParent().getMonitor(); } Rectangle monitorClientArea = monitor.getClientArea(); int middleX = monitorClientArea.x + ( monitorClientArea.width - shellBounds.width ) / 2; int middleY = monitorClientArea.y + ( monitorClientArea.height - shellBounds.height ) / 2; shell.setLocation( middleX, middleY ); } }
Example 11
Source File: AbstractPopup.java From workspacemechanic with Eclipse Public License 1.0 | 5 votes |
private void setPosition(Shell shell) { Monitor monitor = selectMonitor(display); if (monitor != null) { Rectangle clientArea = monitor.getClientArea(); Point size = shell.getSize(); int x = (clientArea.x + clientArea.width - size.x) - 2; int y = (clientArea.y + clientArea.height - size.y) - 2; shell.setLocation(x, y); } }
Example 12
Source File: BreadcrumbItemDropDown.java From birt with Eclipse Public License 1.0 | 5 votes |
/** * Returns the monitor whose client area contains the given point. If no * monitor contains the point, returns the monitor that is closest to the * point. * <p> * Copied from * <code>org.eclipse.jface.window.Window.getClosestMonitor(Display, Point)</code> * </p> * * @param display * the display showing the monitors * @param point * point to find (display coordinates) * @return the monitor closest to the given point */ private static Monitor getClosestMonitor( Display display, Point point ) { int closest = Integer.MAX_VALUE; Monitor[] monitors = display.getMonitors( ); Monitor result = monitors[0]; for ( int i = 0; i < monitors.length; i++ ) { Monitor current = monitors[i]; Rectangle clientArea = current.getClientArea( ); if ( clientArea.contains( point ) ) return current; int distance = Geometry.distanceSquared( Geometry.centerPoint( clientArea ), point ); if ( distance < closest ) { closest = distance; result = current; } } return result; }
Example 13
Source File: UIHelper.java From birt with Eclipse Public License 1.0 | 5 votes |
/** * Center shell on specified monitor. * * @param monitor specified monitor will display shell. * @param shell the shell to be centered on monitor. */ public static void centerOnMonitor( Monitor monitor, Shell shell) { Rectangle clientArea = monitor.getClientArea(); shell.setLocation( clientArea.x + ( clientArea.width / 2 ) - ( shell.getSize( ).x / 2 ), clientArea.y + ( clientArea.height / 2 ) - ( shell.getSize( ).y / 2 ) ); }
Example 14
Source File: WindowProperty.java From hop with Apache License 2.0 | 4 votes |
/** * Performs calculations to size and position a dialog If the size passed in is too large for the primary monitor * client area, it is shrunk to fit. If the positioning leaves part of the dialog outside the client area, it is * centered instead. * * @param shell The dialog to position and size * @param onlyPosition Unused argument. If the window is outside the viewable client are, it must be resized to prevent * inaccessibility. * @param minWidth * @param minHeight */ public void setShell( Shell shell, boolean onlyPosition, int minWidth, int minHeight ) { shell.setMaximized( maximized ); shell.setBounds( new Rectangle(x, y, width, height) ); if ( minWidth > 0 || minHeight > 0 ) { Rectangle bounds = shell.getBounds(); if ( bounds.width < minWidth ) { bounds.width = minWidth; } if ( bounds.height < minHeight ) { bounds.height = minHeight; } shell.setSize( bounds.width, bounds.height ); } // Just to double check: what is the preferred size of this dialog? // This computed is a minimum. If the minimum is smaller than the // size of the current shell, we make it larger. // Point computedSize = shell.computeSize( SWT.DEFAULT, SWT.DEFAULT ); Rectangle shellSize = shell.getBounds(); if ( shellSize.width < computedSize.x ) { shellSize.width = computedSize.x; } if ( shellSize.height < computedSize.y ) { shellSize.height = computedSize.y; } shell.setBounds( shellSize ); Rectangle entireClientArea = shell.getDisplay().getClientArea(); Rectangle resizedRect = Geometry.copy( shellSize ); constrainRectangleToContainer( resizedRect, entireClientArea ); // If the persisted size/location doesn't perfectly fit // into the entire client area, the persisted settings // likely were not meant for this configuration of monitors. // Relocate the shell into either the parent monitor or if // there is no parent, the primary monitor then center it. // if ( !resizedRect.equals( shellSize ) || isClippedByUnalignedMonitors( resizedRect, shell.getDisplay() ) ) { Monitor monitor = shell.getDisplay().getPrimaryMonitor(); if ( shell.getParent() != null ) { monitor = shell.getParent().getMonitor(); } Rectangle monitorClientArea = monitor.getClientArea(); constrainRectangleToContainer( resizedRect, monitorClientArea ); resizedRect.x = monitorClientArea.x + ( monitorClientArea.width - resizedRect.width ) / 2; resizedRect.y = monitorClientArea.y + ( monitorClientArea.height - resizedRect.height ) / 2; shell.setBounds( resizedRect ); } }
Example 15
Source File: WindowProperty.java From pentaho-kettle with Apache License 2.0 | 4 votes |
/** * Performs calculations to size and position a dialog If the size passed in is too large for the primary monitor * client area, it is shrunk to fit. If the positioning leaves part of the dialog outside the client area, it is * centered instead Note that currently, many of the defaults in org.pentaho.di.ui.core/default.properties have crazy * values. This causes the failsafe code in here to fire a lot more than is really necessary. * * @param shell * The dialog to position and size * @param onlyPosition * Unused argument. If the window is outside the viewable client are, it must be resized to prevent * inaccessibility. * @param minWidth * @param minHeight */ public void setShell( Shell shell, boolean onlyPosition, int minWidth, int minHeight ) { shell.setMaximized( maximized ); shell.setBounds( rectangle ); if ( minWidth > 0 || minHeight > 0 ) { Rectangle bounds = shell.getBounds(); if ( bounds.width < minWidth ) { bounds.width = minWidth; } if ( bounds.height < minHeight ) { bounds.height = minHeight; } shell.setSize( bounds.width, bounds.height ); } // Just to double check: what is the preferred size of this dialog? // This computed is a minimum. If the minimum is smaller than the // size of the current shell, we make it larger. // Point computedSize = shell.computeSize( SWT.DEFAULT, SWT.DEFAULT ); Rectangle shellSize = shell.getBounds(); if ( shellSize.width < computedSize.x ) { shellSize.width = computedSize.x; } if ( shellSize.height < computedSize.y ) { shellSize.height = computedSize.y; } shell.setBounds( shellSize ); Rectangle entireClientArea = shell.getDisplay().getClientArea(); Rectangle resizedRect = Geometry.copy( shellSize ); constrainRectangleToContainer( resizedRect, entireClientArea ); // If the persisted size/location doesn't perfectly fit // into the entire client area, the persisted settings // likely were not meant for this configuration of monitors. // Relocate the shell into either the parent monitor or if // there is no parent, the primary monitor then center it. // if ( !resizedRect.equals( shellSize ) || isClippedByUnalignedMonitors( resizedRect, shell.getDisplay() ) ) { Monitor monitor = shell.getDisplay().getPrimaryMonitor(); if ( shell.getParent() != null ) { monitor = shell.getParent().getMonitor(); } Rectangle monitorClientArea = monitor.getClientArea(); constrainRectangleToContainer( resizedRect, monitorClientArea ); resizedRect.x = monitorClientArea.x + ( monitorClientArea.width - resizedRect.width ) / 2; resizedRect.y = monitorClientArea.y + ( monitorClientArea.height - resizedRect.height ) / 2; shell.setBounds( resizedRect ); } }