Java Code Examples for org.eclipse.swt.widgets.Button#addDisposeListener()

The following examples show how to use org.eclipse.swt.widgets.Button#addDisposeListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ButtonFactoryUtil.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Creates button with {@link org.eclipse.swt.SWT#PUSH} style. Provided parameters control other aspects of the
 * button.
 *
 * @param parent
 *            the parent used to create the button.
 * @param text
 *            the text used to set text of the button.
 * @param listener
 *            the listener added to the button.
 * @param enabled
 *            flag controls if created button is enabled.
 * @return created button.
 */
public static Button createPushButton(final Composite parent, final String text, final String tooltip,
		final SelectionListener listener, boolean enabled) {

	final Button button = new Button(parent, PUSH);
	button.setLayoutData(fillDefaults().align(FILL, CENTER).create());
	button.setText(text);
	button.setToolTipText(tooltip);
	if (null != listener) {
		button.addSelectionListener(listener);
		button.addDisposeListener(e -> {
			button.removeSelectionListener(listener);
		});
	}
	button.setEnabled(enabled);
	return button;
}
 
Example 2
Source File: StolenColorEditor.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public StolenColorEditor(final Composite parent, final SelectionListener parentListener) {
	this.listener = parentListener;
	fButton = new Button(parent, SWT.PUSH);
	fExtent = computeImageSize(parent);
	fImage = new Image(parent.getDisplay(), fExtent.x, fExtent.y);

	final GC gc = new GC(fImage);
	gc.setBackground(fButton.getBackground());
	gc.fillRectangle(0, 0, fExtent.x, fExtent.y);
	gc.dispose();

	fButton.setImage(fImage);
	fButton.addSelectionListener(new SelectionAdapter() {
		@Override
		public void widgetSelected(final SelectionEvent event) {
			final ColorDialog colorDialog = new ColorDialog(fButton.getShell());
			colorDialog.setRGB(fColorValue);
			final RGB newColor = colorDialog.open();
			if (newColor != null) {
				fColorValue = newColor;
				updateColorImage();
			}
			notifyParent(event);
		}
	});

	fButton.addDisposeListener(event -> {
		if (fImage != null) {
			fImage.dispose();
			fImage = null;
		}
		if (fColor != null) {
			fColor.dispose();
			fColor = null;
		}
	});
}