下面列出了org.eclipse.swt.widgets.Shell#computeSize ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Overridden to auto-size the shell according to the selected width.
*/
@Override
protected void constrainShellSize() {
super.constrainShellSize();
try {
// the shell property within the Windows class is private - need to access it via reflection
final Field shellField = Window.class.getDeclaredField( "shell" );
shellField.setAccessible( true );
final Shell thisShell = (Shell) shellField.get( this );
thisShell.pack();
final int height = thisShell.computeSize( width, SWT.DEFAULT ).y;
thisShell.setBounds( thisShell.getBounds().x, thisShell.getBounds().y, width + 4, height + 2 );
} catch ( final Exception e ) {
// nothing to do
}
}
/**
* @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
*/
@Override
public void mouseDown(MouseEvent e) {
Control theControl = (Control) e.widget;
Display display = theControl.getDisplay();
Shell tip = new Shell(theControl.getShell(), SWT.ON_TOP | SWT.TOOL);
tip.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
FillLayout layout = new FillLayout();
layout.marginHeight = 1;
layout.marginWidth = 2;
tip.setLayout(layout);
Label label = new Label(tip, SWT.NONE);
label.setForeground(display.getSystemColor(SWT.COLOR_INFO_FOREGROUND));
label.setBackground(display.getSystemColor(SWT.COLOR_INFO_BACKGROUND));
label.setText(theControl.getToolTipText());
label.addMouseTrackListener(this);
Point size = tip.computeSize(SWT.DEFAULT, SWT.DEFAULT);
Rectangle rect = theControl.getBounds();
Point pt = theControl.getParent().toDisplay(rect.x, rect.y);
tip.setBounds(pt.x, pt.y, size.x, size.y);
tip.setVisible(true);
}
/**
* Overridden to auto-size the shell according to the selected width.
*/
@Override
protected void constrainShellSize() {
super.constrainShellSize();
try {
// the shell property within the Windows class is private - need to access it via reflection
final Field shellField = Window.class.getDeclaredField( "shell" );
shellField.setAccessible( true );
final Shell thisShell = (Shell) shellField.get( this );
thisShell.pack();
final int height = thisShell.computeSize( width, SWT.DEFAULT ).y;
thisShell.setBounds( thisShell.getBounds().x, thisShell.getBounds().y, width + 4, height + 2 );
} catch ( final Exception e ) {
// nothing to do
}
}
/**
* Set the size of the given shell such that more content can be shown. The
* shell size does not exceed {@link #DROP_DOWN_HIGHT} and
* {@link #DROP_DOWN_WIDTH}.
*
* @param shell the shell to resize
*/
private void resizeShell(final Shell shell) {
Point size = shell.getSize();
int currentWidth = size.x;
int currentHeight = size.y;
if (currentHeight >= DROP_DOWN_HIGHT && currentWidth >= DROP_DOWN_WIDTH)
return;
Point preferedSize = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
int newWidth;
if (currentWidth >= DROP_DOWN_WIDTH) {
newWidth = currentWidth;
} else {
newWidth = Math.min(Math.max(preferedSize.x, currentWidth),
DROP_DOWN_WIDTH);
}
int newHeight;
if (currentHeight >= DROP_DOWN_HIGHT) {
newHeight = currentHeight;
} else {
newHeight = Math.min(Math.max(preferedSize.y, currentHeight),
DROP_DOWN_HIGHT);
}
if (newHeight != currentHeight || newWidth != currentWidth) {
shell.setRedraw(false);
try {
shell.setSize(newWidth, newHeight);
if (!isLTR()) {
Point location = shell.getLocation();
shell.setLocation(location.x - (newWidth - currentWidth), location.y);
}
} finally {
shell.setRedraw(true);
}
}
}
@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);
}
}
}
/**
* format error message,separator line according to the Shell width
*
* @param shell
* @param errorMessage
* @return
*/
private static String addLineSeparator( Shell shell, String errorMessage )
{
if ( errorMessage == null )
{
return null;
}
int width = shell.computeSize( SWT.DEFAULT, SWT.DEFAULT ).x
- WIDTH_OFFSET;
GC gc = new GC( shell );
int maxChar = width / gc.getFontMetrics( ).getAverageCharWidth( );
StringBuffer sb = new StringBuffer( );
int currentLineLength = 0;
char[] chars = errorMessage.toCharArray( );
for ( char tempChar : chars )
{
sb.append( tempChar );
currentLineLength++;
if ( LINE_SEPARATOR.contains( new String( new char[]{
tempChar
} ) ) )
{
currentLineLength = 0;
}
if ( currentLineLength == maxChar )
{
// in windows, use '\n' instead of '\r\n'
sb.append( LINE_SEPARATOR.length( ) == 2 ? LINE_SEPARATOR.charAt( 1 )
: LINE_SEPARATOR );
currentLineLength = 0;
}
}
return sb.toString( );
}
void showTip(String txt, ItemPkg tp, Table table) {
tip = new Shell((Shell) null, SWT.ON_TOP | SWT.TOOL);
tip.setLayout(new FillLayout());
tip.setBackground(table.getBackground());
createCommandTip(tip, (Command) getSelectables().get(txt));
Point size = tip.computeSize(SWT.DEFAULT, SWT.DEFAULT);
Rectangle rect = tp.getBounds();
Point pt = table.toDisplay(rect.x + getSizeAdjustment(), rect.y
- size.y);
tip.setBounds(pt.x, pt.y, size.x, size.y);
tip.setVisible(true);
}
void showTip(String txt, ItemPkg tp, Table table) {
tip = new Shell((Shell) null, SWT.ON_TOP | SWT.TOOL);
tip.setLayout(new FillLayout());
tip.setBackground(table.getBackground());
createBufferTip(tip, (IEditorReference)getSelectables().get(txt));
Point size = tip.computeSize(SWT.DEFAULT, SWT.DEFAULT);
Rectangle rect = tp.getBounds();
Point pt = table.toDisplay(rect.x + getSizeAdjustment(), rect.y
- size.y);
tip.setBounds(pt.x, pt.y, size.x, size.y);
tip.setVisible(true);
}
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);
}
/**
* 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 );
}
}
public ProgressModal(Shell parent, String title) {
display = Display.getDefault();
yuneImage = new Image(display, Main.class.getClassLoader().getResourceAsStream("YuneIcon_100x100.png"));
dialogShell = new Shell(parent, SWT.PRIMARY_MODAL | SWT.DIALOG_TRIM);
dialogShell.setText(title);
dialogShell.setImage(yuneImage);
progressBar = new ProgressBar(dialogShell, SWT.SMOOTH);
FormLayout mainLayout = new FormLayout();
mainLayout.marginWidth = 5;
mainLayout.marginHeight = 5;
dialogShell.setLayout(mainLayout);
imageLabel = new Label(dialogShell, SWT.NONE);
imageLabel.setImage(yuneImage);
FormData imageData = new FormData(100, 100);
imageData.left = new FormAttachment(0, 10);
imageData.top = new FormAttachment(0, 10);
imageData.bottom = new FormAttachment(100, -10);
imageLabel.setLayoutData(imageData);
FormData progressData = new FormData(300, 20);
progressData.left = new FormAttachment(imageLabel, 10);
progressData.bottom = new FormAttachment(imageLabel, -50, SWT.CENTER);
progressData.right = new FormAttachment(100, -10);
progressBar.setLayoutData(progressData);
statusLabel = new Label(dialogShell, SWT.NONE);
FormData statusData = new FormData();
statusData.left = new FormAttachment(progressBar, 0, SWT.LEFT);
statusData.top = new FormAttachment(progressBar, 5);
statusData.right = new FormAttachment(100, -10);
statusLabel.setLayoutData(statusData);
dialogShell.layout();
final Point newSize = dialogShell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
dialogShell.setSize(newSize);
Rectangle parentBounds = parent.getBounds();
Rectangle dialogBounds = dialogShell.getBounds();
dialogShell.setLocation(parentBounds.x + (parentBounds.width - dialogBounds.width) / 2, parentBounds.y + (parentBounds.height - dialogBounds.height) / 2);
}
/**
* Set the size of the given shell such that more content can be shown. The shell size does not
* exceed a user-configurable maximum.
*
* @param shell the shell to resize
*/
private void resizeShell(final Shell shell) {
Point size= shell.getSize();
int currentWidth= size.x;
int currentHeight= size.y;
int maxHeight= getMaxHeight();
if (currentHeight >= maxHeight && currentWidth >= DROP_DOWN_MAX_WIDTH)
return;
Point preferedSize= shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
int newWidth;
if (currentWidth >= DROP_DOWN_MAX_WIDTH) {
newWidth= currentWidth;
} else {
newWidth= Math.min(Math.max(preferedSize.x, currentWidth), DROP_DOWN_MAX_WIDTH);
}
int newHeight;
if (currentHeight >= maxHeight) {
newHeight= currentHeight;
} else {
newHeight= Math.min(Math.max(preferedSize.y, currentHeight), maxHeight);
}
if (newHeight != currentHeight || newWidth != currentWidth) {
shell.setRedraw(false);
try {
isResizingProgrammatically= true;
shell.setSize(newWidth, newHeight);
if (!isLTR()) {
Point location= shell.getLocation();
shell.setLocation(location.x - (newWidth - currentWidth), location.y);
}
} finally {
isResizingProgrammatically= false;
shell.setRedraw(true);
}
}
}
/**
* Set the size of the given shell such that more content can be shown. The
* shell size does not exceed {@link #DROP_DOWN_HIGHT} and
* {@link #DROP_DOWN_WIDTH}.
*
* @param shell
* the shell to resize
*/
private void resizeShell( final Shell shell )
{
Point size = shell.getSize( );
int currentWidth = size.x;
int currentHeight = size.y;
if ( currentHeight >= DROP_DOWN_HIGHT
&& currentWidth >= DROP_DOWN_WIDTH )
return;
Point preferedSize = shell.computeSize( SWT.DEFAULT, SWT.DEFAULT, true );
int newWidth;
if ( currentWidth >= DROP_DOWN_WIDTH )
{
newWidth = currentWidth;
}
else
{
newWidth = Math.min( Math.max( preferedSize.x, currentWidth ),
DROP_DOWN_WIDTH );
}
int newHeight;
if ( currentHeight >= DROP_DOWN_HIGHT )
{
newHeight = currentHeight;
}
else
{
newHeight = Math.min( Math.max( preferedSize.y, currentHeight ),
DROP_DOWN_HIGHT );
}
if ( newHeight != currentHeight || newWidth != currentWidth )
{
shell.setRedraw( false );
try
{
shell.setSize( newWidth, newHeight );
if ( !isLTR( ) )
{
Point location = shell.getLocation( );
shell.setLocation( location.x - ( newWidth - currentWidth ),
location.y );
}
}
finally
{
shell.setRedraw( true );
}
}
}
public void open() {
Shell parent = getParent();
Display display = parent.getDisplay();
shell = new Shell( parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.SHEET | SWT.RESIZE );
shell.setImage( GUIResource.getInstance().getImageSpoon() );
shell.setLayout( new FormLayout() );
shell.setText( title );
props.setLook( shell );
Label wlImage = new Label( shell, SWT.NONE );
wlImage.setImage( GUIResource.getInstance().getImageWarning32() );
FormData fdWarnImage = new FormData();
fdWarnImage.left = new FormAttachment( 0, 15 );
fdWarnImage.top = new FormAttachment( 0, 15 );
fdWarnImage.height = 32;
fdWarnImage.width = 32;
wlImage.setLayoutData( fdWarnImage );
props.setLook( wlImage );
Label wlMessage = new Label( shell, SWT.FLAT | SWT.WRAP );
wlMessage.setText( message );
FormData fdMessage = new FormData();
fdMessage.left = new FormAttachment( wlImage, 15, SWT.RIGHT );
fdMessage.right = new FormAttachment( 100, -15 );
fdMessage.top = new FormAttachment( wlImage, 0, SWT.TOP );
wlMessage.setLayoutData( fdMessage );
props.setLook( wlMessage );
Button spacer = new Button( shell, SWT.NONE );
FormData fdSpacer = new FormData();
fdSpacer.right = new FormAttachment( 100, 0 );
fdSpacer.bottom = new FormAttachment( 100, -15 );
fdSpacer.left = new FormAttachment( 100, -11 );
fdSpacer.top = new FormAttachment( wlMessage, 15, SWT.BOTTOM );
spacer.setLayoutData( fdSpacer );
spacer.setVisible( false );
props.setLook( spacer );
Control attachTo = spacer;
for ( String label : listenerMap.keySet() ) {
Button wButton = new Button( shell, SWT.PUSH );
wButton.setText( label );
FormData fdButton = new FormData();
fdButton.right = new FormAttachment( attachTo, -Const.MARGIN, SWT.LEFT );
fdButton.bottom = new FormAttachment( attachTo, 0, SWT.BOTTOM );
wButton.setLayoutData( fdButton );
wButton.addListener( SWT.Selection, listenAndDispose( listenerMap.get( label ) ) );
props.setLook( wButton );
attachTo = wButton;
}
Point point = shell.computeSize( 436, SWT.DEFAULT );
shell.setSize( point );
shell.open();
while ( !shell.isDisposed() ) {
if ( !display.readAndDispatch() ) {
display.sleep();
}
}
}
/**
* 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 );
}
}
/**
* Shows/hides the advanced option widgets.
*/
protected void handleAdvancedButtonSelect() {
Shell shell = getShell();
Point shellSize = shell.getSize();
Composite composite = (Composite) getDialogArea();
if (linkedResourceComposite != null) {
linkedResourceComposite.dispose();
linkedResourceComposite = null;
composite.layout();
shell.setSize(shellSize.x, basicShellHeight);
advancedButton.setText(IDEWorkbenchMessages.showAdvanced);
} else {
if (basicShellHeight == -1) {
basicShellHeight = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT,
true).y;
}
linkedResourceComposite = linkedResourceGroup
.createContents(linkedResourceParent);
shellSize = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
shell.setSize(shellSize);
composite.layout();
advancedButton.setText(IDEWorkbenchMessages.hideAdvanced);
}
}