Search in sources :

Example 91 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class TraceEditor method createPageContainer.

@Override
protected Composite createPageContainer(final Composite parent) {
    Composite tree = new Composite(parent, SWT.NONE);
    final Sash sash = new Sash(parent, SWT.HORIZONTAL);
    text = new StyledText(parent, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL);
    text.setFont(JFaceResources.getTextFont());
    final FormLayout form = new FormLayout();
    parent.setLayout(form);
    FormData treeData = new FormData();
    treeData.left = new FormAttachment(0, 0);
    treeData.right = new FormAttachment(100, 0);
    treeData.top = new FormAttachment(0, 0);
    treeData.bottom = new FormAttachment(sash, 0);
    tree.setLayoutData(treeData);
    final int limit = 20, percent = 50;
    final FormData sashData = new FormData();
    sashData.left = new FormAttachment(0, 0);
    sashData.top = new FormAttachment(percent, 0);
    sashData.right = new FormAttachment(100, 0);
    sash.setLayoutData(sashData);
    sash.addListener(SWT.Selection, new Listener() {

        @Override
        public void handleEvent(Event e) {
            Rectangle sashRect = sash.getBounds();
            Rectangle parentRect = parent.getClientArea();
            int bottom = parentRect.height - sashRect.height - limit;
            e.y = Math.max(Math.min(e.y, bottom), limit);
            if (e.y != sashRect.y) {
                sashData.top = new FormAttachment(0, e.y);
                parent.layout();
            }
        }
    });
    FormData textData = new FormData();
    textData.left = new FormAttachment(0, 0);
    textData.right = new FormAttachment(100, 0);
    textData.top = new FormAttachment(sash, 0);
    textData.bottom = new FormAttachment(100, 0);
    text.setLayoutData(textData);
    addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            ISelection selection = event.getSelection();
            if (selection instanceof IStructuredSelection) {
                try {
                    Object x = ((IStructuredSelection) selection).getFirstElement();
                    if (x instanceof EObject)
                        updateText((EObject) x);
                    if (x instanceof Resource)
                        updateText(((Resource) x).getContents().get(0));
                } catch (Exception e) {
                    text.setText(Throwables.getStackTraceAsString(e));
                }
            }
        }
    });
    Menu contextMenu = new Menu(text);
    MenuItem copyItem = new MenuItem(contextMenu, SWT.PUSH);
    copyItem.setText("&Copy");
    copyItem.setAccelerator(SWT.MOD1 | 'C');
    copyItem.setEnabled(false);
    final Clipboard cb = new Clipboard(Display.getDefault());
    copyItem.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            String textData = text.getSelectionText();
            TextTransfer textTransfer = TextTransfer.getInstance();
            cb.setContents(new Object[] { textData }, new Transfer[] { textTransfer });
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });
    text.setMenu(contextMenu);
    text.addSelectionListener(new SelectionListener() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            final String selectionText = text.getSelectionText();
            copyItem.setEnabled(!"".equals(selectionText));
            final Point range = text.getSelectionRange();
            TraceEditor.super.setSelection(new ITextSelection() {

                @Override
                public boolean isEmpty() {
                    return "".equals(selectionText);
                }

                @Override
                public String getText() {
                    return selectionText;
                }

                @Override
                public int getStartLine() {
                    return -1;
                }

                @Override
                public int getOffset() {
                    return range.x;
                }

                @Override
                public int getLength() {
                    return range.y;
                }

                @Override
                public int getEndLine() {
                    return -1;
                }
            });
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {
        }
    });
    return tree;
}
Also used : ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) Listener(org.eclipse.swt.widgets.Listener) SelectionListener(org.eclipse.swt.events.SelectionListener) Rectangle(org.eclipse.swt.graphics.Rectangle) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) EObject(org.eclipse.emf.ecore.EObject) ISelection(org.eclipse.jface.viewers.ISelection) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu) FormAttachment(org.eclipse.swt.layout.FormAttachment) FormLayout(org.eclipse.swt.layout.FormLayout) FormData(org.eclipse.swt.layout.FormData) StyledText(org.eclipse.swt.custom.StyledText) Composite(org.eclipse.swt.widgets.Composite) Sash(org.eclipse.swt.widgets.Sash) ISelectionChangedListener(org.eclipse.jface.viewers.ISelectionChangedListener) Resource(org.eclipse.emf.ecore.resource.Resource) MenuItem(org.eclipse.swt.widgets.MenuItem) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point) TraceNotFoundException(org.eclipse.xtext.generator.trace.TraceNotFoundException) ITextSelection(org.eclipse.jface.text.ITextSelection) TextTransfer(org.eclipse.swt.dnd.TextTransfer) Transfer(org.eclipse.swt.dnd.Transfer) SelectionChangedEvent(org.eclipse.jface.viewers.SelectionChangedEvent) Event(org.eclipse.swt.widgets.Event) SelectionEvent(org.eclipse.swt.events.SelectionEvent) EObject(org.eclipse.emf.ecore.EObject) Clipboard(org.eclipse.swt.dnd.Clipboard) SelectionListener(org.eclipse.swt.events.SelectionListener) TextTransfer(org.eclipse.swt.dnd.TextTransfer)

Example 92 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class AbstractCursorHandlingTest method assertState.

protected void assertState(String string, XtextEditor editor) {
    int cursor = string.indexOf('|');
    assertEquals(string.replace("|", ""), editor.getDocument().get());
    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
    assertEquals("unexpected cursor position:", cursor, selection.getOffset());
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 93 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class AbstractAutoEditTest method selectText.

protected void selectText(XtextEditor editor, int relativeToCurrentOffset, int length) throws Exception {
    ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
    editor.getInternalSourceViewer().setSelectedRange(selection.getOffset() + relativeToCurrentOffset, length);
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 94 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class ExternalEditorTest method assertSelectionIsAtOffset.

private void assertSelectionIsAtOffset(XtextEditor editor, int expectedOffset) {
    ITextSelection newSelection = (ITextSelection) editor.getSelectionProvider().getSelection();
    assertEquals(expectedOffset, newSelection.getOffset());
}
Also used : ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 95 with ITextSelection

use of org.eclipse.jface.text.ITextSelection in project xtext-eclipse by eclipse.

the class AbstractOpenHierarchyHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    XtextEditor editor = EditorUtils.getActiveXtextEditor(event);
    if (editor != null) {
        ISelection selection = editor.getSelectionProvider().getSelection();
        if (selection instanceof ITextSelection) {
            IWorkbenchWindow workbenchWindow = editor.getEditorSite().getWorkbenchWindow();
            editor.getDocument().priorityReadOnly(resource -> {
                openHierarchy(eObjectAtOffsetHelper.resolveElementAt(resource, ((ITextSelection) selection).getOffset()), workbenchWindow);
                return null;
            });
        }
    }
    return null;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) ISelection(org.eclipse.jface.viewers.ISelection) ITextSelection(org.eclipse.jface.text.ITextSelection)

Aggregations

ITextSelection (org.eclipse.jface.text.ITextSelection)205 ISelection (org.eclipse.jface.viewers.ISelection)65 IDocument (org.eclipse.jface.text.IDocument)52 BadLocationException (org.eclipse.jface.text.BadLocationException)46 IRegion (org.eclipse.jface.text.IRegion)34 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)34 Region (org.eclipse.jface.text.Region)31 IEditorPart (org.eclipse.ui.IEditorPart)29 ISelectionProvider (org.eclipse.jface.viewers.ISelectionProvider)25 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)21 ArrayList (java.util.ArrayList)18 TextSelection (org.eclipse.jface.text.TextSelection)17 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)14 Point (org.eclipse.swt.graphics.Point)13 IResource (org.eclipse.core.resources.IResource)12 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)12 IFile (org.eclipse.core.resources.IFile)11 StyledText (org.eclipse.swt.custom.StyledText)11 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)11 Template (org.eclipse.jface.text.templates.Template)10