Search in sources :

Example 41 with IDocumentProvider

use of org.eclipse.ui.texteditor.IDocumentProvider in project linuxtools by eclipse.

the class ProfileUIUtils method openEditorAndSelect.

/**
 * Open a file in the Editor at the specified offset, highlighting the given length
 *
 * @param path : Absolute path pointing to the file which will be opened.
 * @param line - line number to select, 0 to not select a line
 * @param project - current project object
 * @throws BadLocationException - Line number not valid in file
 * @throws PartInitException if the editor could not be initialized
 * @throws CoreException if the proxy cannot be initialized
 * @since 3.1
 */
public static void openEditorAndSelect(String path, int line, IProject project) throws PartInitException, BadLocationException, CoreException {
    IWorkbenchPage activePage = ProfileUIPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
    IRemoteFileProxy proxy = null;
    proxy = RemoteProxyManager.getInstance().getFileProxy(project);
    IFileStore file = proxy.getResource(path);
    if (file.fetchInfo().exists()) {
        IEditorPart editor = IDE.openEditorOnFileStore(activePage, file);
        if (editor instanceof ITextEditor) {
            ITextEditor textEditor = (ITextEditor) editor;
            if (line > 0) {
                IDocumentProvider provider = textEditor.getDocumentProvider();
                IDocument document = provider.getDocument(textEditor.getEditorInput());
                // zero-indexed
                int start = document.getLineOffset(line - 1);
                textEditor.selectAndReveal(start, 0);
            }
        }
    }
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IRemoteFileProxy(org.eclipse.linuxtools.profiling.launch.IRemoteFileProxy) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IFileStore(org.eclipse.core.filesystem.IFileStore) IEditorPart(org.eclipse.ui.IEditorPart) IDocument(org.eclipse.jface.text.IDocument)

Example 42 with IDocumentProvider

use of org.eclipse.ui.texteditor.IDocumentProvider in project linuxtools by eclipse.

the class SpecfileEditorToggleCommentActionDelegate method execute.

@Override
public Object execute(ExecutionEvent event) {
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    ITextEditor specfileEditor = editor.getAdapter(ITextEditor.class);
    IDocumentProvider dp = specfileEditor.getDocumentProvider();
    IDocument document = dp.getDocument(specfileEditor.getEditorInput());
    ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
    if (currentSelection instanceof ITextSelection) {
        ITextSelection selection = (ITextSelection) currentSelection;
        // $NON-NLS-1$
        String selectedContent = "";
        try {
            int begin = document.getLineOffset(selection.getStartLine());
            StringBuilder sb = new StringBuilder(document.get().substring(0, begin));
            String content = document.get().substring(begin, selection.getOffset() + selection.getLength());
            if (linesContentCommentChar(content)) {
                if (selection.getStartLine() == selection.getEndLine()) {
                    selectedContent = ISpecfileSpecialSymbols.COMMENT_START + content;
                } else {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    selectedContent = ISpecfileSpecialSymbols.COMMENT_START + content.replaceAll("\n", "\n#");
                }
            } else {
                selectedContent = // $NON-NLS-1$
                content.replaceFirst(ISpecfileSpecialSymbols.COMMENT_START, "").replaceAll("\n#", // $NON-NLS-1$ //$NON-NLS-2$
                "\n");
            }
            sb.append(selectedContent);
            sb.append(document.get().substring(selection.getOffset() + selection.getLength(), document.get().length()));
            document.set(sb.toString());
            specfileEditor.setHighlightRange(selection.getOffset(), selection.getLength(), true);
        } catch (BadLocationException e) {
            SpecfileLog.logError(e);
        }
    }
    return null;
}
Also used : ITextEditor(org.eclipse.ui.texteditor.ITextEditor) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) ISelection(org.eclipse.jface.viewers.ISelection) IEditorPart(org.eclipse.ui.IEditorPart) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)42 IDocument (org.eclipse.jface.text.IDocument)20 IEditorInput (org.eclipse.ui.IEditorInput)14 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)12 IEditorPart (org.eclipse.ui.IEditorPart)10 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)9 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)9 IFile (org.eclipse.core.resources.IFile)6 CoreException (org.eclipse.core.runtime.CoreException)6 BadLocationException (org.eclipse.jface.text.BadLocationException)6 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)6 PartInitException (org.eclipse.ui.PartInitException)6 AbstractTextEditor (org.eclipse.ui.texteditor.AbstractTextEditor)6 IOException (java.io.IOException)4 IPath (org.eclipse.core.runtime.IPath)4 IDocumentWrapper (bndtools.editor.model.IDocumentWrapper)3 IResource (org.eclipse.core.resources.IResource)3 IProxyRepositoryFactory (org.talend.repository.model.IProxyRepositoryFactory)3 Workspace (aQute.bnd.build.Workspace)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2