Search in sources :

Example 71 with ISelectionProvider

use of org.eclipse.jface.viewers.ISelectionProvider 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 72 with ISelectionProvider

use of org.eclipse.jface.viewers.ISelectionProvider in project eclipse.platform.text by eclipse.

the class GotoLastEditPositionAction method run.

@Override
public void run() {
    EditPosition editPosition = TextEditorPlugin.getDefault().getLastEditPosition();
    if (editPosition == null)
        return;
    final Position pos = editPosition.getPosition();
    if (pos == null || pos.isDeleted)
        return;
    IWorkbenchWindow window = getWindow();
    if (window == null)
        return;
    IWorkbenchPage page = window.getActivePage();
    IEditorPart editor;
    try {
        editor = page.openEditor(editPosition.getEditorInput(), editPosition.getEditorId());
    } catch (PartInitException ex) {
        // $NON-NLS-1$
        IStatus status = new Status(IStatus.ERROR, TextEditorPlugin.PLUGIN_ID, IStatus.OK, "Go to Last Edit Location failed", ex);
        TextEditorPlugin.getDefault().getLog().log(status);
        return;
    }
    // Optimization - could also use else branch
    if (editor instanceof ITextEditor) {
        ITextEditor textEditor = (ITextEditor) editor;
        textEditor.selectAndReveal(pos.offset, pos.length);
        return;
    }
    /*
		 * Workaround: send out a text selection
		 * XXX: Needs to be improved, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=32214
		 */
    if (editor != null) {
        IEditorSite site = editor.getEditorSite();
        if (site == null)
            return;
        ISelectionProvider provider = editor.getEditorSite().getSelectionProvider();
        if (provider == null)
            return;
        provider.setSelection(new TextSelection(pos.offset, pos.length));
    }
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IStatus(org.eclipse.core.runtime.IStatus) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) EditPosition(org.eclipse.ui.internal.texteditor.EditPosition) EditPosition(org.eclipse.ui.internal.texteditor.EditPosition) Position(org.eclipse.jface.text.Position) TextSelection(org.eclipse.jface.text.TextSelection) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) IEditorSite(org.eclipse.ui.IEditorSite)

Example 73 with ISelectionProvider

use of org.eclipse.jface.viewers.ISelectionProvider in project eclipse.platform.text by eclipse.

the class IncrementalFindTarget method uninstall.

/**
 * Uninstalls itself. I.e. removes all listeners installed in <code>install</code>.
 */
private void uninstall() {
    fTextViewer.removeTextListener(this);
    ISelectionProvider selectionProvider = fTextViewer.getSelectionProvider();
    if (selectionProvider != null)
        selectionProvider.removeSelectionChangedListener(this);
    StyledText text = fTextViewer.getTextWidget();
    if (text != null) {
        text.removeMouseListener(this);
        text.removeFocusListener(this);
    }
    if (fTextViewer instanceof ITextViewerExtension) {
        ((ITextViewerExtension) fTextViewer).removeVerifyKeyListener(this);
    } else {
        if (text != null)
            text.removeVerifyKeyListener(this);
    }
    ICommandService commandService = PlatformUI.getWorkbench().getAdapter(ICommandService.class);
    if (commandService != null)
        commandService.removeExecutionListener(this);
    fInstalled = false;
}
Also used : ITextViewerExtension(org.eclipse.jface.text.ITextViewerExtension) StyledText(org.eclipse.swt.custom.StyledText) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ICommandService(org.eclipse.ui.commands.ICommandService)

Example 74 with ISelectionProvider

use of org.eclipse.jface.viewers.ISelectionProvider in project eclipse.platform.text by eclipse.

the class TextSelectionNavigationLocation method equalsLocationOf.

/**
 * Tells whether this location is equal to the current
 * location in the given text editor.
 *
 * @param part the text editor
 * @return <code>true</code> if the locations are equal
 */
private boolean equalsLocationOf(ITextEditor part) {
    if (fPosition == null)
        return true;
    if (fPosition.isDeleted)
        return false;
    ISelectionProvider provider = part.getSite().getSelectionProvider();
    ISelection selection = provider.getSelection();
    if (selection instanceof ITextSelection) {
        ITextSelection textSelection = (ITextSelection) selection;
        if (textSelection.getOffset() == fPosition.offset && textSelection.getLength() == fPosition.length) {
            String text = textSelection.getText();
            if (text != null) {
                try {
                    return text.equals(fDocument.get(fPosition.offset, fPosition.length));
                } catch (BadLocationException e) {
                }
            }
        }
    }
    return false;
}
Also used : ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) ISelection(org.eclipse.jface.viewers.ISelection) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 75 with ISelectionProvider

use of org.eclipse.jface.viewers.ISelectionProvider in project eclipse.platform.text by eclipse.

the class AbstractTemplatesPage method dispose.

@Override
public void dispose() {
    ISelectionProvider selectionProvider = fViewer.getSelectionProvider();
    if (selectionProvider instanceof IPostSelectionProvider)
        ((IPostSelectionProvider) selectionProvider).removePostSelectionChangedListener(fSelectionChangedListener);
    else
        selectionProvider.removeSelectionChangedListener(fSelectionChangedListener);
    fTextEditor.setAction(ITextEditorActionConstants.PASTE, fEditorOldPasteAction);
    if (fContextMenu != null && !fContextMenu.isDisposed())
        fContextMenu.dispose();
    if (fTemplateChangeListener != null)
        getTemplatePreferenceStore().removePropertyChangeListener(fTemplateChangeListener);
    super.dispose();
}
Also used : IPostSelectionProvider(org.eclipse.jface.viewers.IPostSelectionProvider) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider)

Aggregations

ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)83 TreeViewer (org.eclipse.jface.viewers.TreeViewer)40 ICubridNode (com.cubrid.common.ui.spi.model.ICubridNode)25 ISelection (org.eclipse.jface.viewers.ISelection)24 ISchemaNode (com.cubrid.common.ui.spi.model.ISchemaNode)21 CubridDatabase (com.cubrid.common.ui.spi.model.CubridDatabase)18 CubridNodeChangedEvent (com.cubrid.common.ui.spi.event.CubridNodeChangedEvent)17 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)15 ExecTaskWithProgress (com.cubrid.common.ui.spi.progress.ExecTaskWithProgress)13 TaskExecutor (com.cubrid.common.ui.spi.progress.TaskExecutor)13 ITextSelection (org.eclipse.jface.text.ITextSelection)12 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)12 ArrayList (java.util.ArrayList)10 Shell (org.eclipse.swt.widgets.Shell)10 CommonTaskExec (com.cubrid.common.ui.spi.progress.CommonTaskExec)8 IEditorPart (org.eclipse.ui.IEditorPart)8 HashSet (java.util.HashSet)6 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 StyledText (org.eclipse.swt.custom.StyledText)6 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)6