Chapter 27. Common SWT/JFace utilities - org.csstudio.ui.util

Table of Contents

Adapter utilities - org.csstudio.ui.util.AdapterUtil
Drag and drop - org.csstudio.ui.util.dnd

The plugin org.csstudio.ui.util defines common ui elements can be used in different applications.

Adapter utilities - org.csstudio.ui.util.AdapterUtil

The most important way for Eclipse RCP plug-ins to communicate is throught the use of Adapters. Unfortunately Adapters do not work well with conversions from one object to arrays of an object of different kind, so we created a few utility methods to properly handle this case. These should be used when adapting the selection from one plug-in to the other during events like drag'n'drog and context menu command/actions.

For example, this command handler convert a selection to a specific type:

public class MyCommandHandler extends AbstractHandler
{
   @Override
   public Object execute(ExecutionEvent event)
      throws ExecutionException
   {
       ISelection selection =
          HandlerUtil.getActiveMenuSelection(event);
       ProcessVariable[] pvs =
          AdapterUtil.convert(selection, ProcessVariable.class);
	   ...
}

If no selection is available, or no conversion is available, and empty array is returned. If each item in the selection is adaptable to a PV[], those arrays will be merged into a single array.

Drag and drop - org.csstudio.ui.util.dnd

This supports easier implementation for drag sources and drop targets.

To declare a source:

        Control control = ...

        // Drag PVs out of control
        new ControlSystemDragSource(control) {
            @Override
            public Object getSelection() {
                return pvs;
            }
        };

The source will take care of broadcasting the selection in all possible adaptable types.

To accept a drop:

        Control control = ...

        // Accept PVs for a drop
        new ControlSystemDropTarget(control,
            MyData.class,
            ProcessVariable[].class) {
            @Override
            public void handleDrop(Object item) {
                if (item instanceof ProcessVariable[]) {
                    control.setText(
                       Arrays.toString((ProcessVariable[]) item));
                }
                if (item instanceof MyData) {
                    control.setText(((MyData) item).getText());
                }
            }
        };

The target will take care of requesting the type request, in the order of preference given in the constructor.