Search in sources :

Example 1 with ControlSystemDropTarget

use of org.csstudio.ui.util.dnd.ControlSystemDropTarget in project org.csstudio.display.builder by kasemir.

the class DataBrowserEditor method hookDragAndDrop.

/**
 * Allow 'drop' of PV name, archive, ..
 *  @param canvas FXCanvas
 */
private void hookDragAndDrop(final Control canvas) {
    // CS-Studio uses the ControlSystemDragSource
    // and ControlSystemDropTarget to transfer its own
    // data types, using the SWT drag-and-drop API
    // plus some class loader hacking to de-serialize
    // types from other plugins.
    // 
    // The JavaFX plot is inside an FXCanvas.
    // The FXCanvas establishes an SWT DropTarget
    // to forward drops from SWT to the hosted JavaFX scene.
    // 
    // Issues:
    // The FXCanvas only reliably forwards standard transfers
    // (string, ..), and not custom data types.
    // SWT generally doesn't allow multiple drop targets.
    // 
    // Option 1)
    // Use the JavaFX drop API on the plot, and parse the received strings.
    // -> Don't want to parse possibly arbitrary strings.
    // 
    // Option 2)
    // Change the ControlSystemDropTarget to use
    // getSite().getService(IDragAndDropService.class).addMergedDropTarget(..)
    // https://bugs.eclipse.org/bugs/show_bug.cgi?id=162192
    // This would allow 'adding' another DropTarget,
    // but requires changes to the ControlSystemDropTarget,
    // would only work in RCP, not plain SWT.
    // Who then receives the drop events would depend on the order
    // in which drop targets are added and which transfers they use.
    // 
    // Option 3)
    // Hack around the SWT limitation by fetching
    // the FXCanva's DropTarget via
    // control.getData(DND.DROP_TARGET_KEY);
    // and disposing it.
    // Checking the source code for the SWT DropTarget
    // on Windows, Mac and Linux for SWT 3.104,
    // this detaches the DropTarget and allows us
    // to add our own.
    final DropTarget old = (DropTarget) canvas.getData(DND.DROP_TARGET_KEY);
    if (old != null)
        old.dispose();
    if (canvas.getData(DND.DROP_TARGET_KEY) != null)
        throw new IllegalStateException("Cannot remove FXCanvas's DropTarget");
    // Allow dropped arrays
    new ControlSystemDropTarget(canvas, ChannelInfo[].class, ProcessVariable[].class, ArchiveDataSource[].class, File.class, String.class) {

        @Override
        public void handleDrop(final Object item) {
            final PlotListener lst = plot.getListener();
            if (lst == null)
                return;
            if (item instanceof ChannelInfo[]) {
                final ChannelInfo[] channels = (ChannelInfo[]) item;
                final int N = channels.length;
                final ProcessVariable[] pvs = new ProcessVariable[N];
                final ArchiveDataSource[] archives = new ArchiveDataSource[N];
                for (int i = 0; i < N; ++i) {
                    pvs[i] = channels[i].getProcessVariable();
                    archives[i] = channels[i].getArchiveDataSource();
                }
                lst.droppedPVNames(pvs, archives);
            } else if (item instanceof ProcessVariable[]) {
                final ProcessVariable[] pvs = (ProcessVariable[]) item;
                lst.droppedPVNames(pvs, null);
            } else if (item instanceof ArchiveDataSource[]) {
                final ArchiveDataSource[] archives = (ArchiveDataSource[]) item;
                lst.droppedPVNames(null, archives);
            } else if (item instanceof String) {
                final List<String> pvs = new ArrayList<>();
                // Allow passing in many names, assuming that white space separates them
                // $NON-NLS-1$
                final String[] names = ((String) item).split("[\\r\\n\\t ]+");
                for (String one_name : names) {
                    // Might also have received "[pv1, pv2, pv2]", turn that into "pv1", "pv2", "pv3"
                    String suggestion = one_name;
                    if (suggestion.startsWith("["))
                        suggestion = suggestion.substring(1);
                    if (suggestion.endsWith("]") && !suggestion.contains("["))
                        suggestion = suggestion.substring(0, suggestion.length() - 1);
                    if (suggestion.endsWith(","))
                        suggestion = suggestion.substring(0, suggestion.length() - 1);
                    pvs.add(suggestion);
                }
                if (pvs.size() > 0)
                    lst.droppedNames(pvs.toArray(new String[pvs.size()]));
            } else if (item instanceof String[]) {
                // File names arrive as String[]...
                final String[] files = (String[]) item;
                try {
                    for (String filename : files) lst.droppedFilename(filename);
                } catch (Exception ex) {
                    ExceptionDetailsErrorDialog.openError(canvas.getShell(), Messages.Error, ex);
                }
            }
        }
    };
}
Also used : PlotListener(org.csstudio.trends.databrowser3.ui.PlotListener) ProcessVariable(org.csstudio.csdata.ProcessVariable) ArchiveDataSource(org.csstudio.trends.databrowser3.model.ArchiveDataSource) ChannelInfo(org.csstudio.trends.databrowser3.model.ChannelInfo) ControlSystemDropTarget(org.csstudio.ui.util.dnd.ControlSystemDropTarget) PartInitException(org.eclipse.ui.PartInitException) List(java.util.List) ArrayList(java.util.ArrayList) ControlSystemDropTarget(org.csstudio.ui.util.dnd.ControlSystemDropTarget) DropTarget(org.eclipse.swt.dnd.DropTarget)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 ProcessVariable (org.csstudio.csdata.ProcessVariable)1 ArchiveDataSource (org.csstudio.trends.databrowser3.model.ArchiveDataSource)1 ChannelInfo (org.csstudio.trends.databrowser3.model.ChannelInfo)1 PlotListener (org.csstudio.trends.databrowser3.ui.PlotListener)1 ControlSystemDropTarget (org.csstudio.ui.util.dnd.ControlSystemDropTarget)1 DropTarget (org.eclipse.swt.dnd.DropTarget)1 PartInitException (org.eclipse.ui.PartInitException)1