Search in sources :

Example 26 with DropTargetEvent

use of org.eclipse.swt.dnd.DropTargetEvent in project eclipse.platform.text by eclipse.

the class AbstractTextEditor method installTextDragAndDrop.

/**
 * Installs text drag and drop on the given source viewer.
 *
 * @param viewer the viewer
 * @since 3.3
 */
protected void installTextDragAndDrop(final ISourceViewer viewer) {
    if (viewer == null || fIsTextDragAndDropInstalled)
        return;
    final IDragAndDropService dndService = getSite().getService(IDragAndDropService.class);
    if (dndService == null)
        return;
    final StyledText st = viewer.getTextWidget();
    // Install drag source
    final ISelectionProvider selectionProvider = viewer.getSelectionProvider();
    final DragSource source = new DragSource(st, DND.DROP_COPY | DND.DROP_MOVE);
    source.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    source.addDragListener(new DragSourceAdapter() {

        String fSelectedText;

        Point fSelection;

        @Override
        public void dragStart(DragSourceEvent event) {
            fTextDragAndDropToken = null;
            try {
                fSelection = st.getSelection();
                event.doit = isLocationSelected(new Point(event.x, event.y));
                ISelection selection = selectionProvider.getSelection();
                if (selection instanceof ITextSelection)
                    fSelectedText = ((ITextSelection) selection).getText();
                else
                    // fallback to widget
                    fSelectedText = st.getSelectionText();
            } catch (IllegalArgumentException ex) {
                event.doit = false;
            }
        }

        private boolean isLocationSelected(Point point) {
            // FIXME: https://bugs.eclipse.org/bugs/show_bug.cgi?id=260922
            if (isBlockSelectionModeEnabled())
                return false;
            int offset = st.getOffsetAtPoint(point);
            Point p = st.getLocationAtOffset(offset);
            if (p.x > point.x)
                offset--;
            return offset >= fSelection.x && offset < fSelection.y;
        }

        @Override
        public void dragSetData(DragSourceEvent event) {
            event.data = fSelectedText;
            // Can be any non-null object
            fTextDragAndDropToken = this;
        }

        @Override
        public void dragFinished(DragSourceEvent event) {
            try {
                if (event.detail == DND.DROP_MOVE && validateEditorInputState()) {
                    Point newSelection = st.getSelection();
                    int length = fSelection.y - fSelection.x;
                    int delta = 0;
                    if (newSelection.x < fSelection.x)
                        delta = length;
                    // $NON-NLS-1$
                    st.replaceTextRange(fSelection.x + delta, length, "");
                    if (fTextDragAndDropToken == null) {
                        // Move in same editor - end compound change
                        IRewriteTarget target = getAdapter(IRewriteTarget.class);
                        if (target != null)
                            target.endCompoundChange();
                    }
                }
            } finally {
                fTextDragAndDropToken = null;
            }
        }
    });
    // Install drag target
    DropTargetListener dropTargetListener = new DropTargetAdapter() {

        private Point fSelection;

        @Override
        public void dragEnter(DropTargetEvent event) {
            fTextDragAndDropToken = null;
            fSelection = st.getSelection();
            if (event.detail == DND.DROP_DEFAULT) {
                if ((event.operations & DND.DROP_MOVE) != 0) {
                    event.detail = DND.DROP_MOVE;
                } else if ((event.operations & DND.DROP_COPY) != 0) {
                    event.detail = DND.DROP_COPY;
                } else {
                    event.detail = DND.DROP_NONE;
                }
            }
        }

        @Override
        public void dragOperationChanged(DropTargetEvent event) {
            if (event.detail == DND.DROP_DEFAULT) {
                if ((event.operations & DND.DROP_MOVE) != 0) {
                    event.detail = DND.DROP_MOVE;
                } else if ((event.operations & DND.DROP_COPY) != 0) {
                    event.detail = DND.DROP_COPY;
                } else {
                    event.detail = DND.DROP_NONE;
                }
            }
        }

        @Override
        public void dragOver(DropTargetEvent event) {
            event.feedback |= DND.FEEDBACK_SCROLL;
        }

        @Override
        public void drop(DropTargetEvent event) {
            try {
                if (fTextDragAndDropToken != null && event.detail == DND.DROP_MOVE) {
                    // Move in same editor
                    int caretOffset = st.getCaretOffset();
                    if (fSelection.x <= caretOffset && caretOffset <= fSelection.y) {
                        event.detail = DND.DROP_NONE;
                        return;
                    }
                    // Start compound change
                    IRewriteTarget target = getAdapter(IRewriteTarget.class);
                    if (target != null)
                        target.beginCompoundChange();
                }
                if (!validateEditorInputState()) {
                    event.detail = DND.DROP_NONE;
                    return;
                }
                String text = (String) event.data;
                if (isBlockSelectionModeEnabled()) {
                // FIXME fix block selection and DND
                // if (fTextDNDColumnSelection != null && fTextDragAndDropToken != null && event.detail == DND.DROP_MOVE) {
                // // DND_MOVE within same editor - remove origin before inserting
                // Rectangle newSelection= st.getColumnSelection();
                // st.replaceColumnSelection(fTextDNDColumnSelection, ""); //$NON-NLS-1$
                // st.replaceColumnSelection(newSelection, text);
                // st.setColumnSelection(newSelection.x, newSelection.y, newSelection.x + fTextDNDColumnSelection.width - fTextDNDColumnSelection.x, newSelection.y + fTextDNDColumnSelection.height - fTextDNDColumnSelection.y);
                // } else {
                // Point newSelection= st.getSelection();
                // st.insert(text);
                // IDocument document= getDocumentProvider().getDocument(getEditorInput());
                // int startLine= st.getLineAtOffset(newSelection.x);
                // int startColumn= newSelection.x - st.getOffsetAtLine(startLine);
                // int endLine= startLine + document.computeNumberOfLines(text);
                // int endColumn= startColumn + TextUtilities.indexOf(document.getLegalLineDelimiters(), text, 0)[0];
                // st.setColumnSelection(startColumn, startLine, endColumn, endLine);
                // }
                } else {
                    Point newSelection = st.getSelection();
                    try {
                        int modelOffset = widgetOffset2ModelOffset(viewer, newSelection.x);
                        viewer.getDocument().replace(modelOffset, 0, text);
                    } catch (BadLocationException e) {
                        return;
                    }
                    st.setSelectionRange(newSelection.x, text.length());
                }
            } finally {
                fTextDragAndDropToken = null;
            }
        }
    };
    dndService.addMergedDropTarget(st, DND.DROP_MOVE | DND.DROP_COPY, new Transfer[] { TextTransfer.getInstance() }, dropTargetListener);
    fIsTextDragAndDropInstalled = true;
}
Also used : DragSourceAdapter(org.eclipse.swt.dnd.DragSourceAdapter) StyledText(org.eclipse.swt.custom.StyledText) IDragAndDropService(org.eclipse.ui.dnd.IDragAndDropService) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) DragSource(org.eclipse.swt.dnd.DragSource) Point(org.eclipse.swt.graphics.Point) ITextSelection(org.eclipse.jface.text.ITextSelection) Point(org.eclipse.swt.graphics.Point) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) DropTargetListener(org.eclipse.swt.dnd.DropTargetListener) IRewriteTarget(org.eclipse.jface.text.IRewriteTarget) ISelection(org.eclipse.jface.viewers.ISelection) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 27 with DropTargetEvent

use of org.eclipse.swt.dnd.DropTargetEvent in project eclipse.platform.text by eclipse.

the class AbstractTemplatesPage method initializeDND.

/**
 * Initializes drag and drop the template items
 */
private void initializeDND() {
    DragSourceAdapter dragListener = new DragSourceAdapter() {

        @Override
        public void dragStart(DragSourceEvent event) {
            if (getSelectedTemplates().length == 0) {
                event.doit = false;
            }
        }

        @Override
        public void dragSetData(DragSourceEvent event) {
            if (TemplatesTransfer.getInstance().isSupportedType(event.dataType)) {
                event.data = getSelectedTemplates();
            }
        }
    };
    fTreeViewer.addDragSupport(DND.DROP_COPY | DND.DROP_MOVE, new Transfer[] { TemplatesTransfer.getInstance() }, dragListener);
    DropTargetAdapter dropListener = new DropTargetAdapter() {

        Transfer textTransfer = TextTransfer.getInstance();

        Transfer templateTransfer = TemplatesTransfer.getInstance();

        @Override
        public void dragEnter(DropTargetEvent event) {
            if (event.detail == DND.DROP_DEFAULT)
                event.detail = DND.DROP_COPY;
        }

        @Override
        public void dragOperationChanged(DropTargetEvent event) {
            if (event.detail == DND.DROP_DEFAULT)
                event.detail = DND.DROP_COPY;
        }

        @Override
        public void dragOver(DropTargetEvent event) {
            event.feedback |= DND.FEEDBACK_SCROLL;
            if (event.item == null) {
                event.detail = DND.DROP_NONE;
                return;
            }
            int index = 0;
            boolean isTemplateTransfer = false;
            while (index < event.dataTypes.length) {
                if (textTransfer.isSupportedType(event.dataTypes[index])) {
                    break;
                }
                if (templateTransfer.isSupportedType(event.dataTypes[index])) {
                    isTemplateTransfer = true;
                    break;
                }
                index++;
            }
            if (index < event.dataTypes.length) {
                event.currentDataType = event.dataTypes[index];
                if (event.detail == DND.DROP_DEFAULT || !isTemplateTransfer)
                    event.detail = DND.DROP_COPY;
                return;
            }
        }

        @Override
        public void drop(DropTargetEvent event) {
            if (event.item == null)
                return;
            Object object = ((TreeItem) event.item).getData();
            final String contextId;
            if (object instanceof TemplateContextType)
                contextId = ((TemplateContextType) object).getId();
            else
                contextId = ((TemplatePersistenceData) object).getTemplate().getContextTypeId();
            if (textTransfer.isSupportedType(event.currentDataType)) {
                // $NON-NLS-1$ //$NON-NLS-2$
                String text = ((String) event.data).replaceAll("\\$", "\\$\\$");
                final Template template = new Template(createTemplateName(), TemplatesMessages.TemplatesPage_paste_description, contextId, text, true);
                getShell().getDisplay().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        addTemplate(template);
                    }
                });
                return;
            }
            if (templateTransfer.isSupportedType(event.currentDataType)) {
                final TemplatePersistenceData[] templates = (TemplatePersistenceData[]) event.data;
                final int dropType = event.detail;
                getShell().getDisplay().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        if (dropType == DND.DROP_COPY)
                            copyTemplates(templates, contextId);
                        else
                            moveTemplates(templates, contextId);
                    }
                });
            }
        }
    };
    Transfer[] transfers = new Transfer[] { TextTransfer.getInstance(), TemplatesTransfer.getInstance() };
    fTreeViewer.addDropSupport(DND.DROP_COPY | DND.DROP_MOVE, transfers, dropListener);
}
Also used : DragSourceAdapter(org.eclipse.swt.dnd.DragSourceAdapter) TemplatePersistenceData(org.eclipse.jface.text.templates.persistence.TemplatePersistenceData) TreeItem(org.eclipse.swt.widgets.TreeItem) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) Point(org.eclipse.swt.graphics.Point) Template(org.eclipse.jface.text.templates.Template) DragSourceEvent(org.eclipse.swt.dnd.DragSourceEvent) DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) Transfer(org.eclipse.swt.dnd.Transfer) TextTransfer(org.eclipse.swt.dnd.TextTransfer) TemplateContextType(org.eclipse.jface.text.templates.TemplateContextType)

Example 28 with DropTargetEvent

use of org.eclipse.swt.dnd.DropTargetEvent in project yamcs-studio by yamcs.

the class PVTupleTableEditor method createPVTupleListTableViewer.

/**
 * Creates and configures a {@link TableViewer}.
 *
 * @param parent
 *            The parent for the table
 * @return The {@link TableViewer}
 */
private TableViewer createPVTupleListTableViewer(final Composite parent) {
    final TableViewer viewer = new TableViewer(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
    viewer.getTable().setLinesVisible(true);
    viewer.getTable().setHeaderVisible(true);
    final TableViewerColumn numColumn = new TableViewerColumn(viewer, SWT.NONE);
    numColumn.getColumn().setText("#");
    numColumn.getColumn().setMoveable(false);
    numColumn.getColumn().setWidth(50);
    final TableViewerColumn pvColumn = new TableViewerColumn(viewer, SWT.NONE);
    pvColumn.getColumn().setText("PV Name");
    pvColumn.getColumn().setMoveable(false);
    pvColumn.getColumn().setWidth(220);
    pvColumn.setEditingSupport(new PVColumnEditingSupport(viewer, viewer.getTable()));
    final TableViewerColumn TrigColumn = new TableViewerColumn(viewer, SWT.NONE);
    TrigColumn.getColumn().setText("Trigger");
    TrigColumn.getColumn().setMoveable(false);
    TrigColumn.getColumn().pack();
    TrigColumn.setEditingSupport(new TriggerColumnEditingSupport(viewer, viewer.getTable()));
    viewer.setContentProvider(new ArrayContentProvider());
    viewer.setLabelProvider(new PVTupleLabelProvider(pvTupleList));
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        public void selectionChanged(final SelectionChangedEvent event) {
            refreshToolbarOnSelection();
        }
    });
    viewer.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    DropTarget target = new DropTarget(viewer.getControl(), DND.DROP_MOVE | DND.DROP_COPY);
    target.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    target.addDropListener(new DropTargetListener() {

        @Override
        public void dragEnter(DropTargetEvent event) {
        }

        @Override
        public void dragLeave(DropTargetEvent event) {
        }

        @Override
        public void dragOperationChanged(DropTargetEvent event) {
        }

        @Override
        public void dragOver(DropTargetEvent event) {
        }

        @Override
        public void drop(DropTargetEvent event) {
            if (event == null || !(event.data instanceof String))
                return;
            String txt = (String) event.data;
            String[] names = txt.split("[\r\n]+");
            PVTuple[] tuples = new PVTuple[names.length];
            int i = 0;
            for (String name : names) {
                tuples[i] = new PVTuple(name, true);
                pvTupleList.add(tuples[i]);
                i++;
            }
            refreshTableViewerFromAction(tuples);
        }

        @Override
        public void dropAccept(DropTargetEvent event) {
        }
    });
    return viewer;
}
Also used : ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) PVTuple(org.csstudio.opibuilder.script.PVTuple) DropTargetListener(org.eclipse.swt.dnd.DropTargetListener) ArrayContentProvider(org.eclipse.jface.viewers.ArrayContentProvider) GridData(org.eclipse.swt.layout.GridData) DropTarget(org.eclipse.swt.dnd.DropTarget) TableViewer(org.eclipse.jface.viewers.TableViewer) TableViewerColumn(org.eclipse.jface.viewers.TableViewerColumn)

Aggregations

DropTargetEvent (org.eclipse.swt.dnd.DropTargetEvent)28 DropTarget (org.eclipse.swt.dnd.DropTarget)20 DropTargetAdapter (org.eclipse.swt.dnd.DropTargetAdapter)13 Point (org.eclipse.swt.graphics.Point)9 DropTargetListener (org.eclipse.swt.dnd.DropTargetListener)8 Transfer (org.eclipse.swt.dnd.Transfer)7 TextTransfer (org.eclipse.swt.dnd.TextTransfer)6 DragSource (org.eclipse.swt.dnd.DragSource)5 DragSourceEvent (org.eclipse.swt.dnd.DragSourceEvent)5 TreeItem (org.eclipse.swt.widgets.TreeItem)5 File (java.io.File)4 ArrayList (java.util.ArrayList)4 ISelectionChangedListener (org.eclipse.jface.viewers.ISelectionChangedListener)4 SelectionChangedEvent (org.eclipse.jface.viewers.SelectionChangedEvent)4 GridData (org.eclipse.swt.layout.GridData)4 GridLayout (org.eclipse.swt.layout.GridLayout)3 RepositoryBundle (bndtools.model.repo.RepositoryBundle)2 RepositoryBundleVersion (bndtools.model.repo.RepositoryBundleVersion)2 URI (java.net.URI)2 List (java.util.List)2