Search in sources :

Example 76 with TextSelection

use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.

the class XSLTemplatesPage method insertTemplate.

// This code based on the JavaTemplatesPage code. Modified to work with
// SSE
@Override
protected void insertTemplate(Template template, IDocument document) {
    ISourceViewer contextViewer = fTextEditor.getTextViewer();
    ITextSelection textSelection = (ITextSelection) contextViewer.getSelectionProvider().getSelection();
    if (!isValidTemplate(document, template, textSelection.getOffset(), textSelection.getLength()))
        return;
    beginCompoundChange(contextViewer);
    /*
		 * The Editor checks whether a completion for a word exists before it
		 * allows for the template to be applied. We pickup the current text at
		 * the selection position and replace it with the first char of the
		 * template name for this to succeed. Another advantage by this method
		 * is that the template replaces the selected text provided the
		 * selection by itself is not used in the template pattern.
		 */
    String savedText;
    try {
        savedText = document.get(textSelection.getOffset(), textSelection.getLength());
        if (savedText.length() == 0) {
            String prefix = getIdentifierPart(document, template, textSelection.getOffset(), textSelection.getLength());
            if (prefix.length() > 0 && !template.getName().startsWith(prefix)) {
                return;
            }
            if (prefix.length() > 0) {
                contextViewer.setSelectedRange(textSelection.getOffset() - prefix.length(), prefix.length());
                textSelection = (ITextSelection) contextViewer.getSelectionProvider().getSelection();
            }
        }
        document.replace(textSelection.getOffset(), textSelection.getLength(), template.getName().substring(0, 1));
    } catch (BadLocationException e) {
        endCompoundChange(contextViewer);
        return;
    }
    Region region = new Region(textSelection.getOffset() + 1, 0);
    contextViewer.getSelectionProvider().setSelection(new TextSelection(textSelection.getOffset(), 1));
    DocumentTemplateContext context = getContext(document, template, textSelection.getOffset(), textSelection.getLength());
    // $NON-NLS-1$
    context.setVariable("selection", savedText);
    if (context.getKey().length() == 0) {
        try {
            document.replace(textSelection.getOffset(), 1, savedText);
        } catch (BadLocationException e) {
            endCompoundChange(contextViewer);
            return;
        }
    }
    ITextViewer viewer = fTextEditor.getTextViewer();
    int offset = viewer.getTextWidget().getCaretOffset();
    int startLength = offset - region.getOffset();
    // $NON-NLS-1$ //$NON-NLS-2$
    String pattern = template.getPattern().replace("${cursor}", "");
    CustomCompletionProposal proposal = new CustomCompletionProposal(pattern, offset, 0, startLength + pattern.length(), getImage(template), template.getName(), null, null, 0);
    fTextEditor.getSite().getPage().activate(fTextEditor);
    proposal.apply(fTextEditor.getTextViewer(), ' ', 0, offset);
    viewer.getTextWidget().setCaretOffset(offset + pattern.length() - 1);
    endCompoundChange(contextViewer);
}
Also used : DocumentTemplateContext(org.eclipse.jface.text.templates.DocumentTemplateContext) ITextSelection(org.eclipse.jface.text.ITextSelection) TextSelection(org.eclipse.jface.text.TextSelection) CustomCompletionProposal(org.eclipse.wst.xsl.ui.provisional.contentassist.CustomCompletionProposal) Region(org.eclipse.jface.text.Region) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException) ITextViewer(org.eclipse.jface.text.ITextViewer)

Example 77 with TextSelection

use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.

the class URLFileRegionHyperlink method open.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.text.hyperlink.IHyperlink#open()
	 */
public void open() {
    if (fURL != null) {
        IEditorInput input = new StorageEditorInput(new URLStorage(fURL));
        IEditorDescriptor descriptor;
        try {
            descriptor = IDE.getEditorDescriptor(input.getName(), true);
            if (descriptor != null) {
                IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                IEditorPart openedEditor = IDE.openEditor(page, input, descriptor.getId(), true);
                IRegion definitionRegion = findDefinitionRegion(new URLStorage(fURL));
                if (definitionRegion != null) {
                    openedEditor.getSite().getSelectionProvider().setSelection(new TextSelection(definitionRegion.getOffset(), definitionRegion.getLength()));
                }
            }
        } catch (PartInitException e) {
            Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
        }
    }
}
Also used : IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) TextSelection(org.eclipse.jface.text.TextSelection) IEditorDescriptor(org.eclipse.ui.IEditorDescriptor) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) IEditorInput(org.eclipse.ui.IEditorInput) IRegion(org.eclipse.jface.text.IRegion)

Example 78 with TextSelection

use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.

the class AbstractDropAction method insert.

/*
	 * Replaces targetEditor's current selection by "text"
	 */
protected boolean insert(String text, IEditorPart targetEditor) {
    if (text == null || text.length() == 0) {
        return true;
    }
    ITextSelection textSelection = null;
    IDocument doc = null;
    ISelection selection = null;
    ISourceEditingTextTools tools = targetEditor.getAdapter(ISourceEditingTextTools.class);
    if (tools != null) {
        doc = tools.getDocument();
        selection = tools.getSelection();
    }
    ITextEditor textEditor = null;
    if (targetEditor instanceof ITextEditor) {
        textEditor = (ITextEditor) targetEditor;
    }
    if (textEditor == null) {
        textEditor = ((IAdaptable) targetEditor).getAdapter(ITextEditor.class);
    }
    if (textEditor == null && tools != null && tools.getEditorPart() instanceof ITextEditor) {
        textEditor = (ITextEditor) tools.getEditorPart();
    }
    if (textEditor == null && tools != null && tools.getEditorPart() != null) {
        textEditor = tools.getEditorPart().getAdapter(ITextEditor.class);
    }
    if (selection == null && textEditor != null) {
        selection = textEditor.getSelectionProvider().getSelection();
    }
    if (doc == null && textEditor != null) {
        doc = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    }
    if (selection instanceof ITextSelection) {
        textSelection = (ITextSelection) selection;
        try {
            doc.replace(textSelection.getOffset(), textSelection.getLength(), text);
        } catch (BadLocationException e) {
            return false;
        }
    }
    if (textEditor != null && textSelection != null) {
        ISelectionProvider sp = textEditor.getSelectionProvider();
        ITextSelection sel = new TextSelection(textSelection.getOffset(), text.length());
        sp.setSelection(sel);
        textEditor.selectAndReveal(sel.getOffset(), sel.getLength());
    }
    return true;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) ISelectionProvider(org.eclipse.jface.viewers.ISelectionProvider) TextSelection(org.eclipse.jface.text.TextSelection) ITextSelection(org.eclipse.jface.text.ITextSelection) ISelection(org.eclipse.jface.viewers.ISelection) ISourceEditingTextTools(org.eclipse.wst.sse.ui.internal.provisional.extensions.ISourceEditingTextTools) ITextSelection(org.eclipse.jface.text.ITextSelection) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 79 with TextSelection

use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.

the class TestPropertySheetConfiguration method testPropertySourceRemoval.

public void testPropertySourceRemoval() throws BadLocationException {
    IDocument document = (IDocument) fEditor.getAdapter(IDocument.class);
    // set up the editor document
    document.replace(0, 0, "<test><myproperty props=\"yes\" /></test>");
    // set current selection in editor
    ISelection setSelection = new TextSelection(9, 0);
    fEditor.getSelectionProvider().setSelection(setSelection);
    // get current selection in editor
    Object item = null;
    ISelection selection = fEditor.getSelectionProvider().getSelection();
    if (selection instanceof IStructuredSelection) {
        item = ((IStructuredSelection) selection).getFirstElement();
        IPropertySheetPage propertySheet = (IPropertySheetPage) fEditor.getAdapter(IPropertySheetPage.class);
        assertTrue("No ConfigurablePropertySheetPage found", propertySheet instanceof ConfigurablePropertySheetPage);
        if (propertySheet instanceof ConfigurablePropertySheetPage) {
            ConfigurablePropertySheetPage cps = (ConfigurablePropertySheetPage) propertySheet;
            PropertySheetConfiguration config = cps.getConfiguration();
            assertNotNull("No property sheet configuration found", config);
            IPropertySourceProvider provider = config.getPropertySourceProvider(cps);
            assertNotNull("No property sheet provider found", provider);
            IPropertySource source = provider.getPropertySource(item);
            if (source instanceof IPropertySourceExtension) {
                boolean canRemove = ((IPropertySourceExtension) source).isPropertyRemovable("props");
                assertTrue("Current property cannot be removed", canRemove);
                if (canRemove) {
                    ((IPropertySourceExtension) source).removeProperty("props");
                    assertTrue("Current property cannot be removed", true);
                    // failing assert
                    return;
                }
            }
        }
    }
    // if we get to here, always fail, since something went wrong.
    assertTrue("testPropertySourceRemoval test did not take expected path", false);
}
Also used : ConfigurablePropertySheetPage(org.eclipse.wst.sse.ui.internal.properties.ConfigurablePropertySheetPage) IPropertySourceProvider(org.eclipse.ui.views.properties.IPropertySourceProvider) IPropertySheetPage(org.eclipse.ui.views.properties.IPropertySheetPage) TextSelection(org.eclipse.jface.text.TextSelection) ISelection(org.eclipse.jface.viewers.ISelection) IPropertySourceExtension(org.eclipse.wst.sse.ui.views.properties.IPropertySourceExtension) IPropertySource(org.eclipse.ui.views.properties.IPropertySource) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) PropertySheetConfiguration(org.eclipse.wst.sse.ui.views.properties.PropertySheetConfiguration) IDocument(org.eclipse.jface.text.IDocument)

Example 80 with TextSelection

use of org.eclipse.jface.text.TextSelection in project webtools.sourceediting by eclipse.

the class OccurrencesSearchViewPage method showMatch.

/**
 * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#showMatch(org.eclipse.search.ui.text.Match,
 *      int, int)
 */
protected void showMatch(Match match, int currentOffset, int currentLength, boolean activate) throws PartInitException {
    BasicSearchMatchElement element = (BasicSearchMatchElement) match.getElement();
    IWorkbenchPage activePage = getActivePage();
    try {
        if (activePage != null) {
            // open editor if needed
            IDE.openEditor(getActivePage(), element.getFile());
            // set the selection in the open editor
            IEditorPart editor = activePage.getActiveEditor();
            if (activate)
                editor.getSite().getPage().activate(editor);
            editor.getEditorSite().getSelectionProvider().setSelection(new TextSelection(currentOffset, currentLength));
        }
    } catch (PartInitException e) {
        // possible exception trying to open editor
        Logger.logException(e);
    }
}
Also used : TextSelection(org.eclipse.jface.text.TextSelection) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException)

Aggregations

TextSelection (org.eclipse.jface.text.TextSelection)105 IFile (org.eclipse.core.resources.IFile)35 ITextSelection (org.eclipse.jface.text.ITextSelection)24 ISelection (org.eclipse.jface.viewers.ISelection)24 Test (org.junit.Test)23 IDocument (org.eclipse.jface.text.IDocument)21 IEditorPart (org.eclipse.ui.IEditorPart)19 InputStream (java.io.InputStream)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 Path (org.eclipse.core.runtime.Path)13 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)11 BadLocationException (org.eclipse.jface.text.BadLocationException)10 IRegion (org.eclipse.jface.text.IRegion)10 XtextResource (org.eclipse.xtext.resource.XtextResource)9 IPreferencesService (org.eclipse.core.runtime.preferences.IPreferencesService)8 Module (org.eclipse.titan.designer.AST.Module)8 ProjectSourceParser (org.eclipse.titan.designer.parsers.ProjectSourceParser)8 XExpression (org.eclipse.xtext.xbase.XExpression)8 TTCN3Editor (org.eclipse.titan.designer.editors.ttcn3editor.TTCN3Editor)7 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)7