下面列出了怎么用org.eclipse.swt.widgets.Monitor的API类实例代码及写法,或者点击链接到github查看源代码。
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 );
}
}
/**
* 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;
}
/**
* 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 static Rectangle getCurrentMonitorBounds(Display dsp) {
Monitor monitor = dsp.getPrimaryMonitor();
// Choose current Monitor
Point cursor = dsp.getCursorLocation();
Monitor[] monitors = dsp.getMonitors();
for (Monitor mon : monitors) {
Rectangle mbounds = mon.getBounds();
if (mbounds.contains(cursor)) {
monitor = mon;
break;
}
}
return monitor.getBounds();
}
/**
* 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;
}
public static Shell obtainFullScreenShell(final int id) {
final Monitor[] monitors = WorkbenchHelper.getDisplay().getMonitors();
int monitorId = id;
if (monitorId < 0) {
monitorId = 0;
}
if (monitorId > monitors.length - 1) {
monitorId = monitors.length - 1;
}
final Rectangle bounds = monitors[monitorId].getBounds();
final Shell fullScreenShell = new Shell(WorkbenchHelper.getDisplay(), SWT.NO_TRIM | SWT.ON_TOP);
fullScreenShell.setBounds(bounds);
final FillLayout fl = new FillLayout();
fl.marginHeight = 0;
fl.marginWidth = 0;
fl.spacing = 0;
// final GridLayout gl = new GridLayout(1, true);
// gl.horizontalSpacing = 0;
// gl.marginHeight = 0;
// gl.marginWidth = 0;
// gl.verticalSpacing = 0;
fullScreenShell.setLayout(fl);
return fullScreenShell;
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}
/**
* Creates a new instance.
*
* @param display
* @param monitor
*/
public MainSplash(Display display, Monitor monitor) {
this.version = ARXAnonymizer.VERSION;
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);
}
});
}
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;
}
/**
* 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;
}
/**
* 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 );
}
}
public MainView(Display mainDisplay) {
super();
Shell shell = new Shell(mainDisplay, SWT.SHELL_TRIM & ~SWT.MAX);
shell.setText("Yune: A Universal Fire Emblem Randomizer (v0.9.1)");
shell.setImage(new Image(mainDisplay, Main.class.getClassLoader().getResourceAsStream("YuneIcon.png")));
screenHeight = mainDisplay.getBounds().height;
for (Monitor monitor : mainDisplay.getMonitors()) {
screenHeight = Math.max(screenHeight, monitor.getClientArea().height);
}
screenHeight -= 20;
mainShell = shell;
setupMainShell();
resize();
/* Open shell window */
mainShell.open();
mainDisplay.addFilter(SWT.KeyDown, new Listener() {
@Override
public void handleEvent(Event event) {
if (((event.stateMask & SWT.CTRL) != 0) && ((event.stateMask & SWT.SHIFT) != 0) && (event.keyCode == 'c') && !consoleShellOpened) {
openConsole();
}
}
});
}
/**
* Center a shell on the primary monitor
*
* @param shell shell to center
*/
public static void centerShell(final Shell shell) {
final Monitor primary = shell.getDisplay().getPrimaryMonitor();
final Rectangle bounds = primary.getBounds();
final Rectangle rect = shell.getBounds();
final int x = bounds.x + (bounds.width - rect.width) / 2;
final int y = bounds.y + (bounds.height - rect.height) / 2;
shell.setLocation(x, y);
}
/**
* @param shell
* @return the bounds of the monitor on which the shell is running
*/
public static Rectangle getBoundsOfMonitorOnWhichShellIsDisplayed(final Shell shell) {
Monitor monitor = shell.getDisplay().getPrimaryMonitor();
if(shell != null && shell.getMonitor() != null) {
monitor = shell.getMonitor();
}
return monitor.getBounds();
}
/**
* Centers a dialog (Shell) on the <b>primary</b> (active) display.
*
* @param shell Shell to center on screen
* @see Shell
*/
public static void centerDialogOnScreen(final Shell shell) {
// do it by monitor to support dual-head cards and still center things correctly onto the screen people are on.
final Monitor monitor = Display.getDefault().getPrimaryMonitor();
final Rectangle bounds = monitor.getBounds();
final int screen_x = bounds.width;
final int screen_y = bounds.height;
shell.setLocation(screen_x / 2 - (shell.getBounds().width / 2), screen_y / 2 - (shell.getBounds().height / 2));
}
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);
}
}
private Rectangle calculatePosition(int scale, int offset) {
Monitor monitor = getMonitor();
offset = offset * scale;
int width = (monitor.getBounds().width + offset) / scale;
int height = (monitor.getBounds().height + offset) / scale;
int x = monitor.getBounds().x + (width - offset) / scale;
int y = monitor.getBounds().y + (height - offset) / scale;
return new Rectangle(x, y, width, height);
}
private void setShellLoc(Shell shell) {
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 void layout2Center() {
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;
}
/**
* 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 ) );
}
/**
* Centers the shell on the given monitor.
*
* @param shell
* @param monitor
*/
public static void center(Shell shell, Monitor monitor) {
Rectangle shellRect = shell.getBounds();
Rectangle displayRect = monitor.getBounds();
int x = (displayRect.width - shellRect.width) / 2;
int y = (displayRect.height - shellRect.height) / 2;
shell.setLocation(displayRect.x + x, displayRect.y + y);
}
/**
* Returns the monitor on which the application was launched.
*
* @param display
* @return
*/
private static Monitor getMonitor(Display display) {
Point mouse = display.getCursorLocation();
for (Monitor monitor : display.getMonitors()) {
if (monitor.getBounds().contains(mouse)) {
return monitor;
}
}
return display.getPrimaryMonitor();
}
/**
* 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 );
}
}