下面列出了org.eclipse.swt.widgets.Shell#getSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* @param args
*/
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, true));
createText(new Group(shell, SWT.NONE));
Point p = shell.getSize();
p.y = (int) (shell.getMonitor().getBounds().height * 75) / 100;
p.x = (int) (shell.getMonitor().getBounds().width * 50) / 100;
shell.setSize(p);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* Sets the size for the dialog based on its previous size. The width of the
* dialog is its previous width, if it exists. Otherwise, it is simply the
* packed width of the dialog. The maximum width is 40% of the workbench
* window's width. The dialog's height is the packed height of the dialog to
* a maximum of half the height of the workbench window.
*
* @return The size of the dialog
*/
private final Point configureSize() {
final Shell shell = getShell();
// Get the packed size of the shell.
shell.pack();
final Point size = shell.getSize();
// Enforce maximum sizing.
final Shell workbenchWindowShell = EditorUtils.getShell();
if (workbenchWindowShell != null) {
final Point workbenchWindowSize = workbenchWindowShell.getSize();
final int maxWidth = workbenchWindowSize.x * 2 / 5;
final int maxHeight = workbenchWindowSize.y / 2;
if (size.x > maxWidth) {
size.x = maxWidth;
}
if (size.y > maxHeight) {
size.y = maxHeight;
}
}
// Set the size for the shell.
shell.setSize(size);
return size;
}
/**
* @param args
*/
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("CDateTime");
shell.setLayout(new GridLayout(2, false));
CDateTime cdt = new CDateTime(shell, CDT.BORDER | CDT.SPINNER);
cdt.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
shell.pack();
Point size = shell.getSize();
Rectangle screen = display.getMonitors()[0].getBounds();
shell.setBounds(
(screen.width-size.x)/2,
(screen.height-size.y)/2,
size.x,
size.y
);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
@Override
protected void updateStatus(IStatus status) {
super.updateStatus(status);
Shell shell= getShell();
if (shell != null && ! shell.isDisposed()) {
Point size= shell.getSize();
Point minSize= shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
if (minSize.x > size.x || minSize.y > size.y) {
shell.setSize(minSize);
}
}
}
/**
* Shellのウインドウ位置とサイズを保存します
*
* @param clazz ウインドウクラス
* @param shell Shell
*/
public static void saveWindowLocation(Class<? extends Dialog> clazz, Shell shell) {
Map<String, WindowLocationBean> map = AppConfig.get().getWindowLocationMap();
Point location = shell.getLocation();
Point size = shell.getSize();
WindowLocationBean wlocation = new WindowLocationBean();
wlocation.setX(location.x);
wlocation.setY(location.y);
wlocation.setWidth(size.x);
wlocation.setHeight(size.y);
synchronized (map) {
map.put(clazz.getName(), wlocation);
}
}
/**
* Computes the correct dialog size for the given page and resizes its shell if necessary.
*
* @param page the wizard page
*/
private void updateSizeForPage(IWizardPage page) {
// ensure the page container is large enough
Point delta = calculatePageSizeDelta(page);
if (delta.x > 0 || delta.y > 0) {
// increase the size of the shell
Shell shell = getShell();
Point shellSize = shell.getSize();
setShellSize(shellSize.x + delta.x, shellSize.y + delta.y);
constrainShellSize();
}
}
/**
* @param args
*/
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("CDC");
shell.setLayout(new GridLayout());
GridLayout layout = new GridLayout(2, true);
shell.setLayout(layout);
final CDateTime cdc1 = new CDateTime(shell, CDT.BORDER);
cdc1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
final CDateTime cdc2 = new CDateTime(shell, CDT.BORDER | CDT.DROP_DOWN);
cdc2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
shell.pack();
Point size = shell.getSize();
Rectangle screen = display.getMonitors()[0].getBounds();
shell.setBounds(
(screen.width-size.x)/2,
(screen.height-size.y)/2,
size.x,
size.y
);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* @param shell shell that will appear
*/
protected static void makeShellAppears(final Shell shell) {
if (shell == null || shell.isDisposed()) {
return;
}
final Rectangle clientArea = Display.getDefault().getPrimaryMonitor().getClientArea();
final int startX = clientArea.x + clientArea.width - shell.getSize().x;
final int stepForPosition = MAX_DURATION_FOR_OPENING / shell.getSize().y * STEP;
final int stepForAlpha = STEP * 255 / shell.getSize().y;
final int lastPosition = clientArea.y + clientArea.height - shell.getSize().y;
shell.setAlpha(0);
shell.setLocation(startX, clientArea.y + clientArea.height);
shell.open();
shell.getDisplay().timerExec(stepForPosition, new Runnable() {
@Override
public void run() {
if (shell == null || shell.isDisposed()) {
return;
}
shell.setLocation(startX, shell.getLocation().y - STEP);
shell.setAlpha(shell.getAlpha() + stepForAlpha);
if (shell.getLocation().y >= lastPosition) {
shell.getDisplay().timerExec(stepForPosition, this);
} else {
shell.setAlpha(255);
Display.getDefault().timerExec(DISPLAY_TIME, fadeOut(shell, false));
}
}
});
}
public static void center(Shell parent, Shell child) {
Rectangle shellBounds = parent.getBounds();
Point size = child.getSize();
int diffX = (shellBounds.width - size.x) / 2;
int diffY = (shellBounds.height - size.y) / 2;
child.setLocation(shellBounds.x + diffX, shellBounds.y + diffY);
}
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("CDateTime");
shell.setLayout(new GridLayout(2, false));
CDateTime cdt = new CDateTime(shell, CDT.BORDER);
String pattern = "dd.MM.yyyy HH:mm";
cdt.setPattern(pattern);
cdt.setSelection(new Date());
cdt.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Label output = new Label(shell, SWT.NONE);
output.setText("<press enter to see output value>");
cdt.addSelectionListener(new SelectionAdapter() {
SimpleDateFormat format = new SimpleDateFormat(pattern);
@Override
public void widgetSelected(SelectionEvent e) {
String result = format.format(cdt.getSelection());
output.setText(result);
System.out.println(result);
}
});
shell.pack();
Point size = shell.getSize();
Rectangle screen = display.getMonitors()[0].getBounds();
shell.setBounds( (screen.width - size.x) / 2, (screen.height - size.y) / 2, size.x, size.y);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* Computes the correct dialog size for the given page and resizes its shell if necessary.
*
* @param page the wizard page
*/
private void updateSizeForPage(IWizardPage page) {
// ensure the page container is large enough
Point delta = calculatePageSizeDelta(page);
if (delta.x > 0 || delta.y > 0) {
// increase the size of the shell
Shell shell = getShell();
Point shellSize = shell.getSize();
setShellSize(shellSize.x + delta.x, shellSize.y + delta.y);
constrainShellSize();
}
}
/**
* @param args
*/
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("CDateTime");
shell.setLayout(new GridLayout());
final CDateTime cdt = new CDateTime(shell, CDT.BORDER | CDT.SIMPLE);
cdt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
cdt.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
cdt.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
cdt.setPickerBackgroundColor(display.getSystemColor(SWT.COLOR_BLUE));
cdt.setPickerForegroundColor(display.getSystemColor(SWT.COLOR_WHITE));
cdt.setPickerTodayColor(display.getSystemColor(SWT.COLOR_YELLOW));
shell.pack();
final Point size = shell.getSize();
final Rectangle screen = display.getMonitors()[0].getBounds();
shell.setBounds((screen.width - size.x) / 2, (screen.height - size.y) / 2, size.x, size.y);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* Try to make the shell fully visible in the display. If the shell cannot
* fit the display, it will be positioned so that top-left corner is at
* <code>(0, 0)</code> in display-relative coordinates.
*
* @param shell
* the shell to make fully visible
*/
private static void makeShellFullyVisible(Shell shell) {
Rectangle displayBounds = shell.getDisplay().getBounds();
Point absCoord = shell.toDisplay(0, 0);
Point shellSize = shell.getSize();
Point newLocation = new Point(absCoord.x, absCoord.y);
newLocation.x = Math.max(0, Math.min(absCoord.x, displayBounds.width - shellSize.x));
newLocation.y = Math.max(0, Math.min(absCoord.y, displayBounds.height - shellSize.y));
if (!newLocation.equals(absCoord)) {
shell.setLocation(newLocation);
}
}
/**
* @param args
*/
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("CDateTime");
shell.setLayout(new GridLayout());
final CDateTime cdt = new CDateTime(shell, CDT.BORDER | CDT.COMPACT | CDT.SIMPLE);
cdt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
cdt.addListener(SWT.DefaultSelection, event -> {
System.out.println(cdt.getSelection());
});
shell.pack();
Point size = shell.getSize();
Rectangle screen = display.getMonitors()[0].getBounds();
shell.setBounds( //
(screen.width - size.x) / 2, //
(screen.height - size.y) / 2, //
size.x, //
size.y);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* @param args
*/
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("CDC");
shell.setLayout(new GridLayout());
GridLayout layout = new GridLayout(2, true);
shell.setLayout(layout);
final BaseCombo cdc1 = new CDateTime(shell, CDT.BORDER);
cdc1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
final BaseCombo cdc2 = new CDateTime(shell, CDT.BORDER | CDT.DROP_DOWN);
cdc2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
shell.pack();
Point size = shell.getSize();
Rectangle screen = display.getMonitors()[0].getBounds();
shell.setBounds(
(screen.width-size.x)/2,
(screen.height-size.y)/2,
size.x,
size.y
);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private void relayout() {
final Shell shell = section.getShell();
final Point defaultSize = shell.getSize();
final Point size = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
shell.setSize(defaultSize.x, size.y);
shell.layout(true, true);
}
/**
* @param args
*/
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("CDateTime");
shell.setLayout(new GridLayout());
final CDateTime cdt1 = new CDateTime(shell, CDT.BORDER | CDT.DROP_DOWN);
cdt1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
CDateTimeBuilder builder = CDateTimeBuilder.getStandard();
builder.setFooter(Footer.Today().align(SWT.RIGHT), Footer.Clear().align(SWT.LEFT));
final CDateTime cdt2 = new CDateTime(shell, CDT.BORDER | CDT.SIMPLE);
cdt2.setBuilder(builder);
cdt2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
shell.pack();
Point size = shell.getSize();
Rectangle screen = display.getMonitors()[0].getBounds();
shell.setBounds(
(screen.width-size.x)/2,
(screen.height-size.y)/2,
size.x,
size.y
);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* Places the vertex window on the 3D editor
*/
public static void placeVertexWindow() {
final Composite3D lastHoveredC3d = DatFile.getLastHoveredComposite();
if (lastHoveredC3d == null || Display.getDefault().getActiveShell() == null) return;
final VertexWindow vertexWindow = Editor3DWindow.getWindow().getVertexWindow();
final DatFile df = lastHoveredC3d.getLockableDatFileReference();
final Set<Vertex> selectedVertices = df.getVertexManager().getSelectedVertices();
final boolean singleVertexSelected = !df.isReadOnly() && selectedVertices.size() == 1;
final boolean addingSomething = Editor3DWindow.getWindow().isAddingSomething();
final boolean windowShouldBeDisplayed = singleVertexSelected && !addingSomething;
Vertex newSelectedVertex = new Vertex(0,0,0);
if (singleVertexSelected) {
try {
newSelectedVertex = selectedVertices.iterator().next();
} catch (NoSuchElementException consumed) {}
}
if (windowShouldBeDisplayed && vertexWindow.getShell() == null) {
vertexWindow.run();
lastHoveredC3d.setFocus();
Editor3DWindow.getWindow().getShell().setActive();
} else if (!windowShouldBeDisplayed && vertexWindow.getShell() != null) {
vertexWindow.close();
}
final Shell vertexWindowShell = vertexWindow.getShell();
if (vertexWindowShell == null || vertexWindowShell.isDisposed()) {
return;
}
if (singleVertexSelected) {
vertexWindow.updateVertex(newSelectedVertex);
}
final Point old = vertexWindowShell.getLocation();
final Point a = ShellHelper.absolutePositionOnShell(lastHoveredC3d);
final Point s = vertexWindowShell.getSize();
final int xPos = a.x - s.x + lastHoveredC3d.getSize().x;
final int yPos = a.y;
if (old.x != xPos || old.y != yPos) {
vertexWindowShell.setLocation(xPos, yPos);
}
}
/**
* @param args
*/
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("CDateTime");
shell.setLayout(new GridLayout());
CDateTimeBuilder builder = CDateTimeBuilder.getStandard();
builder.setMinDate(Calendar.getInstance());
Calendar max = Calendar.getInstance();
max.add(Calendar.DAY_OF_MONTH, 5);
builder.setMaxDate(max);
final CDateTime cdt1 = new CDateTime(shell, CDT.BORDER | CDT.DROP_DOWN);
cdt1.setBuilder(builder);
cdt1.setSelection(new Date());
cdt1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
final CDateTime cdt2 = new CDateTime(shell, CDT.BORDER | CDT.DROP_DOWN);
cdt2.setBuilder(builder);
cdt2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
cdt2.setSelection(new Date());
cdt2.setEditable(false);
final CDateTime cdt3 = new CDateTime(shell, CDT.BORDER | CDT.SIMPLE);
cdt3.setBuilder(builder);
cdt3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
cdt3.setEditable(false);
shell.pack();
Point size = shell.getSize();
Rectangle screen = display.getMonitors()[0].getBounds();
shell.setBounds(
(screen.width-size.x)/2,
(screen.height-size.y)/2,
size.x,
size.y
);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* @param args
*/
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell( display );
shell.setText( "CDateTime" );
shell.setLayout( new GridLayout() );
final String pattern = "EE, dd.MM.yyyy";
final Label lbl1 = new Label( shell, SWT.NONE );
lbl1.setText( "Date is rolling:" );
lbl1.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
final CDateTime cdt1 = new CDateTime( shell, CDT.BORDER );
cdt1.setPattern( pattern );
cdt1.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
// CDateTimeBuilder builder = CDateTimeBuilder.getStandard();
// builder.setFooter( Footer.Today().align( SWT.RIGHT ), Footer.Clear().align( SWT.LEFT ) );
final Label lbl2 = new Label( shell, SWT.NONE );
lbl2.setText( "Date is adding:" );
lbl2.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
final CDateTime cdt2 = new CDateTime( shell, CDT.BORDER | CDT.ADD_ON_ROLL );
// cdt2.setBuilder( builder );
cdt2.setPattern( pattern );
cdt2.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
final Label lbl3 = new Label( shell, SWT.NONE );
lbl3.setText( "Select each 'DAY' and press 'UP_ARROW'!" );
lbl3.setLayoutData( new GridData( SWT.FILL, SWT.CENTER, true, false ) );
Calendar cal = new GregorianCalendar();
cal.set( 2017, 0, 31 );
cdt1.setSelection( cal.getTime() );
cdt2.setSelection( cal.getTime() );
shell.pack();
Point size = shell.getSize();
Rectangle screen = display.getMonitors()[0].getBounds();
shell.setBounds(
( screen.width - size.x ) / 2,
( screen.height - size.y ) / 2,
size.x,
size.y );
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}