/** * 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; }
/** * 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))); }
/** * 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; }
private Point calculateCenterOnCurrentDisplay(Point initialSize) { if (shell==null || shell.isDisposed()){ return new Point(0,0); } /* fall back implemenrtation for unpersisted location */ Composite parent = getParent(); Monitor monitor = null; if (parent == null) { monitor = shell.getDisplay().getPrimaryMonitor(); } else { monitor = parent.getMonitor(); } 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))); }
protected FXCanvasScrollApp() { shell = new Shell(); shell.setText(this.getClass().getSimpleName()); shell.setLayout(new FillLayout()); ScrolledComposite scrollPane = new ScrolledComposite(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); FXCanvas fxCanvas = new FXCanvas(scrollPane, SWT.BORDER); fxCanvas.setScene(createScene(SCROLL_CONTAINER_ID)); scrollPane.setContent(fxCanvas); scrollPane.setExpandHorizontal(true); scrollPane.setExpandVertical(true); fxCanvas.pack(); scrollPane.setMinSize(fxCanvas.getSize()); shell.pack(); Monitor monitor = shell.getMonitor(); Rectangle monitorRect = monitor.getClientArea(); Rectangle shellRect = shell.getBounds(); shellRect.x = Math.max(0, (monitorRect.width - shellRect.width) / 2); shellRect.y = Math.max(0, (monitorRect.height - shellRect.height) / 2); shell.setBounds(shellRect); shell.open(); }
protected FXCanvasBrowserApp() { shell = new Shell(); shell.setText(this.getClass().getSimpleName()); shell.setLayout(new FillLayout()); FXCanvas fxCanvas = new FXCanvas(shell, SWT.BORDER); browser = new WebView(); browser.getEngine().getLoadWorker().stateProperty().addListener( new ChangeListener<State>() { public void changed(ObservableValue ov, State oldState, State newState) { if (newState == State.SUCCEEDED) { successLabel.setText(SUCCESS_MESSAGE); } } }); fxCanvas.setScene(createScene()); shell.pack(); Monitor monitor = shell.getMonitor(); Rectangle monitorRect = monitor.getClientArea(); Rectangle shellRect = shell.getBounds(); shellRect.x = Math.max(0, (monitorRect.width - shellRect.width) / 2); shellRect.y = Math.max(0, (monitorRect.height - shellRect.height) / 2); shell.setBounds(shellRect); shell.open(); }
@Override protected Control createDialogArea(Composite parent) { parent.setBackgroundMode(SWT.INHERIT_DEFAULT); parent.setBackground(BTSUIConstants.VIEW_BACKGROUND_DESELECTED_COLOR); Composite control = createContentArea(parent); control.setData("org.eclipse.e4.ui.css.id", "LoginDialog"); Rectangle controlRect = control.getBounds(); // looks strange in multi monitor environments // Rectangle displayBounds = shell.getDisplay().getBounds(); shell.getDisplay(); Monitor primary = Display.getDefault().getPrimaryMonitor(); Rectangle displayBounds = primary.getBounds(); int x = (displayBounds.width - controlRect.width) / 2; int y = (displayBounds.height - controlRect.height) / 2; shell.setBounds(x, y, controlRect.width, controlRect.height); return control; }
public StandardWidgetToolkit(String... commandLineArgs) { this.swtRunnableFactory = RUNNABLE_FACTORY_OVERRIDE != null ? RUNNABLE_FACTORY_OVERRIDE : this; this.commandLineArgs = commandLineArgs; display = new Display(); shell = new Shell(display); shell.setText("OAuth 2.0 Authorization Request"); shell.setLayout(new FillLayout()); Monitor monitor = display.getPrimaryMonitor(); Rectangle bounds = monitor.getBounds(); Dimension size = new Dimension((int) (bounds.width * 0.25), (int) (bounds.height * 0.55)); shell.setSize(size.width, size.height); shell.setLocation((bounds.width - size.width) / 2, (bounds.height - size.height) / 2); Browser browser = new org.eclipse.swt.browser.Browser(shell, SWT.ON_TOP); swtInterceptingBrowser = new SwtInterceptingBrowser(browser, display, shell); }
private void initShell() { shell = new Shell(Display.getCurrent(), SWT.APPLICATION_MODAL | SWT.SHEET); shell.setText("Feature-Expression Management Dialog"); shell.setImage(VariantSyncPlugin.getDefault() .getImageDescriptor("icons/featureExpression.png") .createImage()); shell.setSize(500, 485); GridLayout shellLayout = new GridLayout(); shellLayout.marginWidth = 0; shellLayout.marginHeight = 0; shell.setLayout(shellLayout); Monitor primary = shell.getDisplay().getPrimaryMonitor(); Rectangle bounds = primary.getBounds(); Rectangle rect = shell.getBounds(); int x = bounds.x + (bounds.width - rect.width) / 2; int y = bounds.y + (bounds.height - rect.height) / 2; shell.setLocation(x, y); }
/** * 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; }
/** * 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 (int idx = 0; idx < monitors.length; idx++) { final Monitor current = monitors[idx]; 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; }
/** * 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; }
private Rectangle getDisplayBounds(final Point location) { Rectangle displayBounds; final Monitor[] allMonitors = _ownerControl.getDisplay().getMonitors(); if (allMonitors.length > 1) { // By default present in the monitor of the control displayBounds = _ownerControl.getMonitor().getBounds(); final Point p = new Point(location.x, location.y); // Search on which monitor the event occurred Rectangle tmp; for (final Monitor element : allMonitors) { tmp = element.getBounds(); if (tmp.contains(p)) { displayBounds = tmp; break; } } } else { displayBounds = _ownerControl.getDisplay().getBounds(); } return displayBounds; }
public static Rectangle getDisplayBounds(final Control composite, final Point location) { Rectangle displayBounds; final Monitor[] allMonitors = composite.getDisplay().getMonitors(); if (allMonitors.length > 1) { // By default present in the monitor of the control displayBounds = composite.getMonitor().getBounds(); final Point p = new Point(location.x, location.y); // Search on which monitor the event occurred Rectangle tmp; for (final Monitor element : allMonitors) { tmp = element.getBounds(); if (tmp.contains(p)) { displayBounds = tmp; break; } } } else { displayBounds = composite.getDisplay().getBounds(); } return displayBounds; }
/** * 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; }
/** * 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 parent shell, or display bounds if there is no parent * shell. * * @param initialSize * the initial size of the shell, as returned by * <code>getInitialSize</code>. * @return the initial location of the shell */ protected Point getInitialLocation(Point initialSize) { Composite parent = shell.getParent(); Monitor monitor = shell.getDisplay().getPrimaryMonitor(); if (parent != null) { monitor = parent.getMonitor(); } 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))); }
/** * 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. If this is ever made public, it should be moved into a separate * utility class. * * @param toSearch * point to find (display coordinates) * @param toFind * point to find (display coordinates) * @return the montor closest to the given point */ private static Monitor getClosestMonitor(Display toSearch, Point toFind) { int closest = Integer.MAX_VALUE; Monitor[] monitors = toSearch.getMonitors(); Monitor result = monitors[0]; for (int idx = 0; idx < monitors.length; idx++) { Monitor current = monitors[idx]; Rectangle clientArea = current.getClientArea(); if (clientArea.contains(toFind)) { return current; } int distance = Geometry.distanceSquared(Geometry .centerPoint(clientArea), toFind); if (distance < closest) { closest = distance; result = current; } } return result; }
/** * Given the desired position of the window, 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, and subclasses can use this as a utility method * if they want to limit the region in which the window may be moved. * * @param preferredSize * the preferred position of the window * @return a rectangle as close as possible to preferredSize that does not * extend outside the monitor * * @since 3.0 */ protected Rectangle getConstrainedShellBounds(Rectangle preferredSize) { Rectangle result = new Rectangle(preferredSize.x, preferredSize.y, preferredSize.width, preferredSize.height); Monitor mon = getClosestMonitor(getShell().getDisplay(), Geometry .centerPoint(result)); 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; }
private Point getDefaultLocation(Point initialSize, int offsetRight, int offsetTop) { Composite parent = getShell().getParent(); Monitor monitor = getShell().getDisplay().getPrimaryMonitor(); if (parent != null) { monitor = parent.getMonitor(); } Rectangle monitorBounds = monitor.getClientArea(); Point topRight; if (parent != null) { Rectangle parentBounds = parent.getBounds(); topRight = new Point(parentBounds.x + parentBounds.width, parentBounds.y); } else { topRight = new Point(monitorBounds.x + monitorBounds.width, monitorBounds.y); } return new Point(topRight.x - (initialSize.x + offsetRight), // Math.max(monitorBounds.y, Math.min(topRight.y + offsetTop, monitorBounds.y + monitorBounds.height - initialSize.y))); }
public static void main(String[] args) { final long endTime = args != null && args.length >= 1 ? Long .parseLong(args[0]) : Long.MAX_VALUE; final Display d = new Display(); @Nullable Monitor sec = null; for (final Monitor m : d.getMonitors()) { if (d.getPrimaryMonitor() != m) { sec = m; break; } } System.out.println(sec); run(endTime, d, sec, null); }
/** * This method is needed in the case where the display has multiple monitors, but * they do not form a uniform rectangle. In this case, it is possible for Geometry.moveInside() * to not detect that the window is partially or completely clipped. * We check to make sure at least the upper left portion of the rectangle is visible to give the * user the ability to reposition the dialog in this rare case. * @param constrainee * @param display * @return */ private boolean isClippedByUnalignedMonitors(Rectangle constrainee, Display display) { boolean isClipped; Monitor[] monitors = display.getMonitors(); if (monitors.length > 0) { // Loop searches for a monitor proving false isClipped = true; for (Monitor monitor : monitors) { if (monitor.getClientArea().contains(constrainee.x + 10, constrainee.y + 10)) { isClipped = false; break; } } } else { isClipped = false; } return isClipped; }
@Override protected void configureShell(Shell shell) { super.configureShell(shell); shell.setText("JIsoCreator"); shell.setSize(800, 600); shell.setImage(ImageUtils.getInstance().loadImage("iso.png")); Monitor primary = Display.getCurrent().getPrimaryMonitor(); Rectangle bounds = primary.getBounds(); Rectangle rect = shell.getBounds(); int x = bounds.x + ((bounds.width - rect.width) / 2); int y = bounds.y + ((bounds.height - rect.height) / 2); shell.setLocation(x, y); }
public static Rectangle getPrimaryScreenClientArea(Display display) { Monitor primaryMonitorBySwt = display.getPrimaryMonitor(); Rectangle primaryMonitorClientAreaBySwt = primaryMonitorBySwt.getClientArea(); GraphicsDevice[]screens = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices(); for (GraphicsDevice screen : screens) { if (isPrimaryMonitor(screen)) { // Cut off any excess area such as OS task-bars. Rectangle primaryScreenBoundsByJava = new Rectangle( screen.getDefaultConfiguration().getBounds().x, screen.getDefaultConfiguration().getBounds().y, screen.getDefaultConfiguration().getBounds().width, screen.getDefaultConfiguration().getBounds().height); return primaryMonitorClientAreaBySwt.intersection(primaryScreenBoundsByJava); } } // No primary screen has been found by java, use SWT to get clientArea of PrimaryScreen as fallback. return primaryMonitorClientAreaBySwt; }
/** * Creates a new instance. * * @param display * @param monitor */ public MainSplash(Display display, Monitor monitor) { this.version = Resources.getVersion(); this.splash = Resources.getSplash(display); this.shell = new Shell(SWT.ON_TOP | (isMac() ? 0 : SWT.NO_TRIM)); this.shell.setImages(Resources.getIconSet(display)); this.shell.setSize(splash.getBounds().width, splash.getBounds().height); // Center SWTUtil.center(shell, monitor); // Paint shell.addPaintListener(new PaintListener(){ public void paintControl(PaintEvent arg0) { paint(arg0.gc); } }); }
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, new Rectangle( 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 ); } }
/** * This method is needed in the case where the display has multiple monitors, but they do not form a uniform * rectangle. In this case, it is possible for Geometry.moveInside() to not detect that the window is partially or * completely clipped. We check to make sure at least the upper left portion of the rectangle is visible to give the * user the ability to reposition the dialog in this rare case. * * @param constrainee * @param display * @return */ private boolean isClippedByUnalignedMonitors( Rectangle constrainee, Display display ) { boolean isClipped; Monitor[] monitors = display.getMonitors(); if ( monitors.length > 0 ) { // Loop searches for a monitor proving false isClipped = true; for ( Monitor monitor : monitors ) { if ( monitor.getClientArea().contains( constrainee.x + 10, constrainee.y + 10 ) ) { isClipped = false; break; } } } else { isClipped = false; } return isClipped; }
@Override protected Point getInitialLocation(Point initialSize) { Monitor activeMonitor = Stream.of(display.getMonitors()) .filter(m -> m.getBounds().intersects(getShell().getBounds())) .findFirst() .orElse(display.getPrimaryMonitor()); Rectangle visualBounds = activeMonitor.getClientArea(); int x = visualBounds.x + (visualBounds.width - initialSize.x) / 2; int y = visualBounds.y + (visualBounds.height - initialSize.y) / 2; return new Point(x, y); }
@Override public void configureShell(Shell newShell) { super.configureShell(newShell); newShell.setText("Limit chars logs"); newShell.setSize(nWidth, nHeight); int nLeft = 0; int nTop = 0; Display display = newShell.getDisplay(); Point pt = display.getCursorLocation(); Monitor[] monitors = display.getMonitors(); for (int i = 0; i < monitors.length; i++) { if (monitors[i].getBounds().contains(pt)) { Rectangle rect = monitors[i].getClientArea(); if (rect.x < 0) nLeft = ((rect.width - nWidth) / 2) + rect.x; else nLeft = (rect.width - nWidth) / 2; if (rect.y < 0) nTop = ((rect.height - nHeight) / 2) + rect.y; else nTop = (rect.height - nHeight) / 2; break; } } newShell.setBounds(nLeft, nTop, nWidth, nHeight); }
@Override public void configureShell(Shell newShell) { super.configureShell(newShell); newShell.setText("Parameters availables"); newShell.setSize(nWidth, nHeight); int nLeft = 0; int nTop = 0; Display display = newShell.getDisplay(); Point pt = display.getCursorLocation(); Monitor[] monitors = display.getMonitors(); for (int i = 0; i < monitors.length; i++) { if (monitors[i].getBounds().contains(pt)) { Rectangle rect = monitors[i].getClientArea(); if (rect.x < 0) nLeft = ((rect.width - nWidth) / 2) + rect.x; else nLeft = (rect.width - nWidth) / 2; if (rect.y < 0) nTop = ((rect.height - nHeight) / 2) + rect.y; else nTop = (rect.height - nHeight) / 2; break; } } newShell.setBounds(nLeft, nTop, nWidth, nHeight); }
@Override public void configureShell(Shell newShell) { super.configureShell(newShell); newShell.setText(dialogTitle); int nLeft = 0; int nTop = 0; Display display = newShell.getDisplay(); // // mods jmc 22/10/2013 // nWidth = newShell.getSize().x; // nHeight = newShell.getSize().y; Point pt = display.getCursorLocation(); Monitor [] monitors = display.getMonitors(); for (int i= 0; i<monitors.length; i++) { if (monitors[i].getBounds().contains(pt)) { Rectangle rect = monitors[i].getClientArea(); if (rect.x < 0) nLeft = ((rect.width - nWidth) / 2) + rect.x; else nLeft = (rect.width - nWidth) / 2; if (rect.y < 0) nTop = ((rect.height - nHeight) / 2) + rect.y; else nTop = (rect.height - nHeight) / 2; break; } } newShell.setBounds(nLeft, nTop, nWidth, nHeight); }
@Override public void configureShell(Shell newShell) { super.configureShell(newShell); newShell.setText("Engine Log settings"); newShell.setSize(nWidth, nHeight); int nLeft = 0; int nTop = 0; Display display = newShell.getDisplay(); Point pt = display.getCursorLocation(); Monitor[] monitors = display.getMonitors(); for (int i = 0; i < monitors.length; i++) { if (monitors[i].getBounds().contains(pt)) { Rectangle rect = monitors[i].getClientArea(); if (rect.x < 0) nLeft = ((rect.width - nWidth) / 2) + rect.x; else nLeft = (rect.width - nWidth) / 2; if (rect.y < 0) nTop = ((rect.height - nHeight) / 2) + rect.y; else nTop = (rect.height - nHeight) / 2; break; } } newShell.setBounds(nLeft, nTop, nWidth, nHeight); }
private static Point center(Display display, Shell shell) { Monitor primary = display.getPrimaryMonitor(); Rectangle bounds = primary.getBounds(); Rectangle rect = shell.getBounds(); int x = bounds.x + (bounds.width - rect.width) / 2; int y = bounds.y + (bounds.height - rect.height) / 2; Point location = new Point(x, y); return location; }
private Monitor selectMonitor(Display display) { Shell activeShell = display.getActiveShell(); if (activeShell != null && activeShell.getMonitor() != null) { return activeShell.getMonitor(); } // Fall-back Monitor[] monitors = display.getMonitors(); if (monitors.length > 0) { return monitors[0]; } // Worst case. What? Can this happen? return null; }
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); } }