SWTImageJ implementation - eclipse-swtimagej/SWTImageJ GitHub Wiki

General SWT implementation

In general the SWTImageJ implementation follows the general ImageJ layout of packages, classes and folders familiar to ImageJ developers.

For some parts (selection API) the SWTGraphics2D library is used to convert Java2D calls to SWT graphic calls, see: See: https://github.com/jfree/swtgraphics2d

For a fast image display the processor classes of ImageJ have been translated to SWT. For this I tried out several known described implementations but found myself a more effective method with a direct pixel transfer similar to the creation of a BufferedImage (see the different processor classes).

In ImageJ the user interface or the image display (ImageWindow) are AWT Frames or Dialogs. In SWT the shells are created within the classes with some custom methods. The reasons are noted in the API description (see API description 1, 2) and on StackOverflow:

Link API description 1 , Link API description 2 , Stack Overflow

To identify them in the WindowManager (responsible to address open images, etc.) an interface has been created WindowSwt implemented by dialogs, etc., which were former Frame and Dialog instances. Some typical Frame events have been integrated in the interface as default methods.

The main image display will not resize the shell when zooming in or out occurs (embedded concept). When the visible zoomed display of the image is larger then the shell (parent canvas fills the shell) the zoom indicator will be displayed.

For efficiency the painting area is restricted to the visible shell or editor area.

In the original ImageJ implementation the AWTEvent is used as an argument with the ij.gui.DialogListener interface method dialogItemChanged. This was ported to a TypedEvent argument for SWT.

List of some typical changes for SWT

Layout differences

Instead of the BorderLayout most of the times the SWT GridLayout is used which can be applied very flexible to components. Note that the ImageCanvas is filled inside of a centered parent composite (centered with the GridLayout - the original ImageLayout is not used here). The parent composite can be added to a shell (this is default when you start SWTImageJ as a Desktop application) or different composites (embeddable). Listeners:

AWTEvent -> TypedEvent (used in ij.gui.DialogListener)

actionPerformed -> widgetSelected

For the different SWT widgets the 'widgetSelected' action has to be differentiated. Here an example with different former AWT actions in combination:

public void widgetSelected(SelectionEvent e) {
	if (e.widget instanceof org.eclipse.swt.widgets.Slider) {
		adjustmentValueChanged(e);
	} else if (e.widget instanceof org.eclipse.swt.widgets.Combo) {
		 itemStateChanged(e);
	}

	else if (e.widget instanceof org.eclipse.swt.widgets.Button) {
		org.eclipse.swt.widgets.Button bu = (org.eclipse.swt.widgets.Button) e.widget;
		int style = bu.getStyle();
		if ((style & SWT.CHECK) != 0) {
			itemStateChanged(e);
		} else {
			actionPerformed(e);
		}
	}
}

SWT Widgets

Checkbox -> Button (SWT.CHECK)

Choice -> List

addRadioButtonGroup -> Group with Button(SWT.RADIO)

Dialogs

The HTML dialog uses the SWT Browser.

Some dialogs are based on JFace.

Mouse Listeners

Some mouse events are not available in SWT. You have to change the following listeners:

mousePressed = mouseDown

mouseReleased = mouseUp

ImageJ GenericDialog

In AWT the GenericDialog widgets are not explicitly disposed when closed. Some widget are still accessed after the close event in AWT. This is not possible in SWT. To mimic this behaviour the SWT dialog is hidden when closed and stored in a list. It will be disposed when a new GenericDialog is opened.

How to convert existing ImageJ Plugins

ImageJ PluginTool

You have to import SWT Listeners:

import org.eclipse.swt.events.*;

See the Plugin Tool template which was ported to SWT.

Examples

Some ImageJ examples are loaded from the Internet (e.g., Java source and images) in the menu "Help->Examples". The URL for the scripting and Java source was changed to a raw Github path (this SWTImageJ repository) to load the correct SWT examples.