下面列出了org.eclipse.swt.widgets.Shell#addListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Add a listener that will fade the window when it get closed.
*
* @param shell
* @param duration
* @param easing
* @param runner
* : The AnimationRunner to use, or null
*
*/
public static void fadeOnClose(final Shell shell, final int duration,
final IMovement easing, AnimationRunner runner) {
final AnimationRunner useRunner;
if (runner != null) {
useRunner = runner;
} else {
useRunner = new AnimationRunner();
}
final Runnable closeListener = () -> shell.dispose();
shell.addListener(SWT.Close, e -> {
e.doit = false;
setAlpha(useRunner, shell, 0, duration, easing, closeListener,
null);
});
}
/**
* Creates an inplace tooltip.
*
* @param parent parent control.
*/
public GridToolTip(final Control parent)
{
super(parent, SWT.NONE);
shell = new Shell(parent.getShell(), SWT.NO_TRIM | SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
shell.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
shell.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
parent.addListener(SWT.Dispose, new Listener()
{
public void handleEvent(Event arg0)
{
shell.dispose();
dispose();
}
});
shell.addListener(SWT.Paint, new Listener()
{
public void handleEvent(Event e)
{
onPaint(e.gc);
}
});
}
void createPopup(String[] items, int selectionIndex) {
// create shell and list
popup = new Shell (getShell(), SWT.NO_TRIM | SWT.ON_TOP);
int style = getStyle();
int listStyle = SWT.SINGLE | SWT.V_SCROLL;
if ((style & SWT.FLAT) != 0) listStyle |= SWT.FLAT;
if ((style & SWT.RIGHT_TO_LEFT) != 0) listStyle |= SWT.RIGHT_TO_LEFT;
if ((style & SWT.LEFT_TO_RIGHT) != 0) listStyle |= SWT.LEFT_TO_RIGHT;
list = new List (popup, listStyle);
if (font != null) list.setFont(font);
if (foreground != null) list.setForeground(foreground);
if (background != null) list.setBackground(background);
int [] popupEvents = {SWT.Close, SWT.Paint, SWT.Deactivate};
for (int i=0; i<popupEvents.length; i++) popup.addListener (popupEvents [i], listener);
int [] listEvents = {SWT.MouseUp, SWT.Selection, SWT.Traverse, SWT.KeyDown, SWT.KeyUp, SWT.FocusIn, SWT.FocusOut, SWT.Dispose};
for (int i=0; i<listEvents.length; i++) list.addListener (listEvents [i], listener);
if (items != null) list.setItems(items);
if (selectionIndex != -1) list.setSelection(selectionIndex);
}
/**
* Creates an inplace tooltip.
*
* @param parent parent control.
*/
public GridToolTip(final Control parent)
{
super(parent, SWT.NONE);
shell = new Shell(parent.getShell(), SWT.NO_TRIM | SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
shell.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND));
shell.setForeground(shell.getDisplay().getSystemColor(SWT.COLOR_INFO_FOREGROUND));
parent.addListener(SWT.Dispose, new Listener()
{
public void handleEvent(Event arg0)
{
shell.dispose();
dispose();
}
});
shell.addListener(SWT.Paint, new Listener()
{
public void handleEvent(Event e)
{
onPaint(e.gc);
}
});
}
public Popup2(final IPopupProvider provider, final Widget... controls) {
super(WorkbenchHelper.getShell(), PopupDialog.HOVER_SHELLSTYLE, false, false, false, false, false, null, null);
this.provider = provider;
final Shell parent = provider.getControllingShell();
parent.addListener(SWT.Move, hide);
parent.addListener(SWT.Resize, hide);
parent.addListener(SWT.Close, hide);
parent.addListener(SWT.Deactivate, hide);
parent.addListener(SWT.Hide, hide);
parent.addListener(SWT.Dispose, event -> close());
for (final Widget c : controls) {
if (c == null) {
continue;
}
final TypedListener typedListener = new TypedListener(mtl);
c.addListener(SWT.MouseEnter, typedListener);
c.addListener(SWT.MouseExit, typedListener);
c.addListener(SWT.MouseHover, typedListener);
}
}
private void createPopupShell() {
popup = new Shell(getShell(), SWT.NO_TRIM | SWT.ON_TOP);
popup.setLayout(new GridLayout());
final int[] popupEvents = { SWT.Close, SWT.Paint, SWT.Deactivate, SWT.Dispose };
for (final int popupEvent : popupEvents) {
popup.addListener(popupEvent, listener);
}
composite = new CalculatorButtonsComposite(popup, SWT.NONE);
composite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
composite.setDisplayArea(label);
keyListener = composite.getKeyListener();
label.addKeyListener(keyListener);
popup.pack();
}
void installListeners() {
// Listeners on this popup's table and scroll bar
proposalTable.addListener(SWT.FocusOut, this);
final ScrollBar scrollbar = proposalTable.getVerticalBar();
if (scrollbar != null) {
scrollbar.addListener(SWT.Selection, this);
}
// Listeners on this popup's shell
getShell().addListener(SWT.Deactivate, this);
getShell().addListener(SWT.Close, this);
// Listeners on the target control
control.addListener(SWT.MouseDoubleClick, this);
control.addListener(SWT.MouseDown, this);
control.addListener(SWT.Dispose, this);
control.addListener(SWT.FocusOut, this);
// Listeners on the target control's shell
final Shell controlShell = control.getShell();
controlShell.addListener(SWT.Move, this);
controlShell.addListener(SWT.Resize, this);
}
/**
* Create contents of the dialog.
*/
private void createContents() {
shell = new Shell(getParent(), getStyle());
shell.addListener(SWT.Close, new Listener() {
public void handleEvent(Event event) {
result = "";
event.doit = true;
}
});
shell.setSize(272, 138);
shell.setText(getText());
shell.setLayout(new FormLayout());
}
void createPopup( Object[] items, int selectionIndex )
{
// create shell and list
popup = new Shell( getShell( ), SWT.NO_TRIM | SWT.ON_TOP );
table = new Table( popup, SWT.SINGLE
| SWT.V_SCROLL
| SWT.FULL_SELECTION );
new TableColumn( table, SWT.LEFT );
if ( font != null )
table.setFont( font );
if ( foreground != null )
table.setForeground( foreground );
if ( background != null )
table.setBackground( background );
label.setBackground( table.getBackground( ) );
label.setForeground( table.getForeground( ) );
label.setFont( table.getFont( ) );
int[] popupEvents = {
SWT.Close, SWT.Paint, SWT.Deactivate
};
for ( int i = 0; i < popupEvents.length; i++ )
popup.addListener( popupEvents[i], listener );
int[] tableEvents = {
SWT.MouseUp,
SWT.Selection,
SWT.Traverse,
SWT.KeyDown,
SWT.KeyUp,
SWT.FocusIn,
SWT.FocusOut,
SWT.Dispose
};
for ( int i = 0; i < tableEvents.length; i++ )
table.addListener( tableEvents[i], listener );
}
/**
* creates the popup shell.
*
* @param selectionIndex
*/
void createPopup(final int selectionIndex) {
// create shell and table
popup = new Shell(getShell(), SWT.NO_TRIM | SWT.ON_TOP);
// create table
table = new Table(popup, SWT.SINGLE | SWT.FULL_SELECTION);
if (font != null) {
table.setFont(font);
}
if (foreground != null) {
table.setForeground(foreground);
}
if (background != null) {
table.setBackground(background);
}
// Add popup listeners
final int[] popupEvents = { SWT.Close, SWT.Paint, SWT.Deactivate, SWT.Help };
for (final int popupEvent : popupEvents) {
popup.addListener(popupEvent, listener);
}
// add table listeners
final int[] tableEvents = { SWT.MouseUp, SWT.Selection, SWT.Traverse, SWT.KeyDown, SWT.KeyUp, SWT.FocusIn,
SWT.Dispose };
for (final int tableEvent : tableEvents) {
table.addListener(tableEvent, listener);
}
// set the selection
if (selectionIndex != -1) {
table.setSelection(selectionIndex);
}
}
/**
* Open the window.
*/
public void open() {
if (isOpen == true)
return;
isOpen = true;
display = Display.getDefault();
shell = new Shell(parent);//
shell.setLayout(new GridLayout());
shell.open();
shell.setSize(273, 400);
shell.setText("查找/替换");
createForm();
shell.addListener(SWT.Close, new Listener() {
public void handleEvent(Event event) {
switch (event.type) {
case SWT.Close:
isOpen = false;
break;
}
}
});
shell.layout();
while (shell != null && !shell.isDisposed()) {
if (display != null && !display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* creates the popup shell.
* @param selectionIndex
*/
void createPopup(int selectionIndex) {
// create shell and table
popup = new Shell(getShell(), SWT.NO_TRIM | SWT.ON_TOP);
// create table
table = new Table(popup, SWT.SINGLE | SWT.FULL_SELECTION);
if (font != null)
table.setFont(font);
if (foreground != null)
table.setForeground(foreground);
if (background != null)
table.setBackground(background);
// Add popup listeners
int[] popupEvents = { SWT.Close, SWT.Paint, SWT.Deactivate };
for (int i = 0; i < popupEvents.length; i++) {
popup.addListener(popupEvents[i], listener);
}
// add table listeners
int[] tableEvents = { SWT.MouseUp, SWT.Selection, SWT.Traverse, SWT.KeyDown, SWT.KeyUp, SWT.FocusIn,
SWT.Dispose };
for (int i = 0; i < tableEvents.length; i++) {
table.addListener(tableEvents[i], listener);
}
// set the selection
if (selectionIndex != -1) {
table.setSelection(selectionIndex);
}
}
public MessageModal(Shell parent, String title, String message) {
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);
dialogShell.addListener(SWT.CLOSE, new Listener() {
@Override
public void handleEvent(Event event) {
event.doit = false;
}
});
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);
imageLabel.setLayoutData(imageData);
contentGroup = new Composite(dialogShell, SWT.NONE);
FormLayout contentLayout = new FormLayout();
contentLayout.marginTop = 5;
contentLayout.marginLeft = 5;
contentLayout.marginBottom = 5;
contentLayout.marginRight = 5;
contentGroup.setLayout(contentLayout);
titleLabel = new Label(contentGroup, SWT.LEFT);
titleLabel.setText(title);
FontData normalFont = titleLabel.getFont().getFontData()[0];
Font boldFont = new Font(display, normalFont.getName(), normalFont.getHeight(), SWT.BOLD);
titleLabel.setFont(boldFont);
FormData titleData = new FormData();
titleData.top = new FormAttachment(0, 0);
titleData.left = new FormAttachment(0, 0);
titleData.right = new FormAttachment(100, 0);
titleLabel.setLayoutData(titleData);
descriptionLabel = new Label(contentGroup, SWT.LEFT | SWT.WRAP);
descriptionLabel.setText(message);
FormData descriptionData = new FormData();
descriptionData.left = new FormAttachment(titleLabel, 0, SWT.LEFT);
descriptionData.right = new FormAttachment(titleLabel, 0, SWT.RIGHT);
descriptionData.top = new FormAttachment(titleLabel, 10);
descriptionData.bottom = new FormAttachment(100, -5);
Point expectedSize = descriptionLabel.computeSize(SWT.DEFAULT, SWT.DEFAULT);
descriptionData.width = Math.max(200, expectedSize.x);
descriptionData.height = Math.max(60, expectedSize.y);
descriptionLabel.setLayoutData(descriptionData);
FormData groupData = new FormData();
groupData.left = new FormAttachment(imageLabel, 10);
groupData.top = new FormAttachment(imageLabel, 0, SWT.TOP);
groupData.right = new FormAttachment(100, -10);
contentGroup.setLayoutData(groupData);
layoutSize();
Rectangle parentBounds = parent.getBounds();
Rectangle dialogBounds = dialogShell.getBounds();
dialogShell.setLocation(parentBounds.x + (parentBounds.width - dialogBounds.width) / 2, parentBounds.y + (parentBounds.height - dialogBounds.height) / 2);
}
void createPopup(Collection<CTreeComboItem> items, CTreeComboItem selectedItem) {
// create shell and list
popup = new Shell(getShell(), SWT.NO_TRIM | SWT.ON_TOP);
final int style = getStyle();
int listStyle = SWT.H_SCROLL | SWT.V_SCROLL | SWT.SINGLE;
if ((style & SWT.FLAT) != 0) {
listStyle |= SWT.FLAT;
}
if ((style & SWT.RIGHT_TO_LEFT) != 0) {
listStyle |= SWT.RIGHT_TO_LEFT;
}
if ((style & SWT.LEFT_TO_RIGHT) != 0) {
listStyle |= SWT.LEFT_TO_RIGHT;
}
tree = new Tree(popup, listStyle);
tree.addTreeListener(hookListener);
if (font != null) {
tree.setFont(font);
}
if (foreground != null) {
tree.setForeground(foreground);
}
if (background != null) {
tree.setBackground(background);
}
final int[] popupEvents = { SWT.Close, SWT.Paint, SWT.Deactivate };
for (int i = 0; i < popupEvents.length; i++) {
popup.addListener(popupEvents[i], listener);
}
final int[] listEvents = { SWT.MouseUp, SWT.Selection, SWT.Traverse, SWT.KeyDown, SWT.KeyUp, SWT.FocusIn, SWT.Dispose, SWT.Collapse, SWT.Expand };
for (int i = 0; i < listEvents.length; i++) {
tree.addListener(listEvents[i], listener);
}
for (final CTreeComboColumn c : columns) {
final TreeColumn col = new TreeColumn(tree, SWT.NONE);
c.setRealTreeColumn(col);
}
if (items != null) {
createTreeItems(items.toArray(new CTreeComboItem[0]));
}
if (selectedItem != null) {
tree.setSelection(selectedItem.getRealTreeItem());
}
}
private void createContentShell() {
contentShell = new Shell(getShell(), SWT.NO_TRIM | SWT.ON_TOP);
contentShell.addListener(SWT.Close, shellListener);
contentShell.addListener(SWT.Deactivate, shellListener);
}
void createPopup( String[] items, int selectionIndex )
{
// create shell and list
popup = new Shell( getShell( ), SWT.NO_TRIM | SWT.ON_TOP );
int style = getStyle( );
int listStyle = SWT.SINGLE | SWT.V_SCROLL;
if ( ( style & SWT.FLAT ) != 0 )
listStyle |= SWT.FLAT;
if ( ( style & SWT.RIGHT_TO_LEFT ) != 0 )
listStyle |= SWT.RIGHT_TO_LEFT;
if ( ( style & SWT.LEFT_TO_RIGHT ) != 0 )
listStyle |= SWT.LEFT_TO_RIGHT;
list = new List( popup, listStyle );
if ( font != null )
list.setFont( font );
if ( foreground != null )
list.setForeground( foreground );
if ( background != null )
list.setBackground( background );
int[] popupEvents = {
SWT.Close, SWT.Paint, SWT.Deactivate
};
for ( int i = 0; i < popupEvents.length; i++ )
popup.addListener( popupEvents[i], listener );
int[] listEvents = {
SWT.MouseUp,
SWT.Selection,
SWT.Traverse,
SWT.KeyDown,
SWT.KeyUp,
SWT.FocusIn,
SWT.Dispose
};
for ( int i = 0; i < listEvents.length; i++ )
list.addListener( listEvents[i], listener );
if ( items != null )
list.setItems( items );
if ( selectionIndex != -1 )
list.setSelection( selectionIndex );
}
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
final Composite composite = new Composite(shell, SWT.NONE);
composite.setEnabled(false);
composite.setLayout(new FillLayout());
Button button = new Button(composite, SWT.PUSH);
button.setText("Button");
composite.pack();
composite.setLocation(10, 10);
final Point[] offset = new Point[1];
Listener listener = new Listener() {
public void handleEvent(Event event) {
switch (event.type) {
case SWT.MouseDown:
Rectangle rect = composite.getBounds();
if (rect.contains(event.x, event.y)) {
Point pt1 = composite.toDisplay(0, 0);
Point pt2 = shell.toDisplay(event.x, event.y);
offset[0] = new Point(pt2.x - pt1.x, pt2.y - pt1.y);
}
break;
case SWT.MouseMove:
if (offset[0] != null) {
Point pt = offset[0];
composite.setLocation(event.x - pt.x, event.y - pt.y);
}
break;
case SWT.MouseUp:
offset[0] = null;
break;
}
}
};
shell.addListener(SWT.MouseDown, listener);
shell.addListener(SWT.MouseUp, listener);
shell.addListener(SWT.MouseMove, listener);
shell.setSize(300, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* The Hide button has been pressed.
*/
public void hidePressed()
{
if (hideOnFinish)
{
final Shell activeShell = getShell();
toast = new GenericInfoPopupDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
infoTitle, infoMessage, new Runnable()
{
public void run()
{
activeShell.setVisible(true);
}
});
toast.open();
activeShell.setVisible(false);
activeShell.addListener(SWT.Show, new Listener()
{
public void handleEvent(Event event)
{
if (toast != null)
{
// Incase if the shell is opened through other source, close the toast
toast.close();
}
}
});
activeShell.addShellListener(new ShellAdapter()
{
@Override
public void shellClosed(ShellEvent e)
{
if (toast != null)
{
// In case the shell gets closed programatically, close the toast.
toast.close();
}
}
});
}
}
/**
* creates the popup shell.
* @param selectionIndex
*/
void createPopup(int selectionIndex) {
// create shell and table
popup = new Shell(getShell(), SWT.NO_TRIM | SWT.ON_TOP);
// set style
int style = getStyle();
int tableStyle = SWT.SINGLE | SWT.V_SCROLL;
if ((style & SWT.FLAT) != 0)
tableStyle |= SWT.FLAT;
if ((style & SWT.RIGHT_TO_LEFT) != 0)
tableStyle |= SWT.RIGHT_TO_LEFT;
if ((style & SWT.LEFT_TO_RIGHT) != 0)
tableStyle |= SWT.LEFT_TO_RIGHT;
// create table
table = new Table(popup, SWT.SINGLE | SWT.FULL_SELECTION);
if (font != null)
table.setFont(font);
if (foreground != null)
table.setForeground(foreground);
if (background != null)
table.setBackground(background);
// Add popup listeners
int[] popupEvents = { SWT.Close, SWT.Paint, SWT.Deactivate, SWT.Help };
for (int i = 0; i < popupEvents.length; i++) {
popup.addListener(popupEvents[i], listener);
}
// add table listeners
int[] tableEvents = { SWT.MouseMove, SWT.MouseUp, SWT.Selection, SWT.Traverse, SWT.KeyDown, SWT.KeyUp,
SWT.FocusIn, SWT.Dispose };
for (int i = 0; i < tableEvents.length; i++) {
table.addListener(tableEvents[i], listener);
}
// set the selection
if (selectionIndex != -1) {
table.setSelection(selectionIndex);
}
}
/**
* creates the popup shell.
* @param selectionIndex
*/
void createPopup(int selectionIndex) {
// create shell and table
popup = new Shell(getShell(), SWT.NO_TRIM | SWT.ON_TOP);
// set style
int style = getStyle();
int tableStyle = SWT.SINGLE | SWT.V_SCROLL;
if ((style & SWT.FLAT) != 0)
tableStyle |= SWT.FLAT;
if ((style & SWT.RIGHT_TO_LEFT) != 0)
tableStyle |= SWT.RIGHT_TO_LEFT;
if ((style & SWT.LEFT_TO_RIGHT) != 0)
tableStyle |= SWT.LEFT_TO_RIGHT;
// create table
table = new Table(popup, SWT.SINGLE | SWT.FULL_SELECTION);
if (font != null)
table.setFont(font);
if (foreground != null)
table.setForeground(foreground);
if (background != null)
table.setBackground(background);
// Add popup listeners
int[] popupEvents = { SWT.Close, SWT.Paint, SWT.Deactivate, SWT.Help };
for (int i = 0; i < popupEvents.length; i++) {
popup.addListener(popupEvents[i], listener);
}
// add table listeners
int[] tableEvents = { SWT.MouseMove, SWT.MouseUp, SWT.Selection, SWT.Traverse, SWT.KeyDown, SWT.KeyUp,
SWT.FocusIn, SWT.Dispose };
for (int i = 0; i < tableEvents.length; i++) {
table.addListener(tableEvents[i], listener);
}
// set the selection
if (selectionIndex != -1) {
table.setSelection(selectionIndex);
}
}