Search in sources :

Example 6 with AbstractDecoratedTextEditor

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

the class JavaParserTest method canIdentifyFieldWithinNestedClass.

/**
 * Given an IEditorPart and currently a field within a nested
 * class is selected, JavaParser should return a "nestedClass.fieldName"
 * construct for the current function.
 *
 * @throws Exception
 */
@Test
public void canIdentifyFieldWithinNestedClass() throws Exception {
    final String nestedClassName = "Encapsulated";
    final String currentFieldName = "variableInNestedClass";
    final String javaSourceCode = "public class JavaParserExampleClass {\n" + "private String someStrVariable = null;\n" + "static {\n" + "// empty \n" + "}\n" + "private void someMethod(String param) {\n" + "// empty \n" + "}\n" + "private class " + nestedClassName + "{\n" + "private int " + currentFieldName + " = 10;\n" + "public String getString() throws Exception {\n" + "// return a String, yay ;-)\n" + "return \"returnString\";\n" + "}\n" + "}\n" + "}\n";
    assertNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Add JavaParserExampleClass.java to project
    InputStream newFileInputStream = new ByteArrayInputStream(javaSourceCode.getBytes());
    IFile javaSourceFile = project.addFileToProject("/src/org/eclipse/changelog/tests", "JavaParserExampleClass.java", newFileInputStream);
    assertNotNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Open a source file and get the IEditorPart
    javaSourceEditorPart = openEditor(javaSourceFile);
    // make sure changelog editor content is right before merging
    assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
    // make sure we have the proper editor type
    assertTrue(javaSourceEditorPart instanceof AbstractDecoratedTextEditor);
    // Select the snippet we want
    int selectionStart = javaSourceCode.indexOf(currentFieldName);
    assertTrue(selectionStart >= 0);
    // select only a part of field name, as this should be sufficient
    int selectionLength = currentFieldName.length() - 3;
    AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
    javaEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String expectedFunctionName = nestedClassName + "." + currentFieldName;
    final String actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
    assertEquals(expectedFunctionName, actualFunctionName);
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TextSelection(org.eclipse.jface.text.TextSelection) AbstractDecoratedTextEditor(org.eclipse.ui.texteditor.AbstractDecoratedTextEditor) Test(org.junit.Test)

Example 7 with AbstractDecoratedTextEditor

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

the class JavaParserTest method canIdentifyStaticInitializerWhenInStaticInstanceInitializer.

/**
 * Given an IEditorPart and current selection is in a static instance initializer
 * block, JavaParser should be able to figure out that we were in an static
 * initializer block.
 *
 * @throws Exception
 */
@Test
public void canIdentifyStaticInitializerWhenInStaticInstanceInitializer() throws Exception {
    final String javaSourceCode = "public class JavaParserExampleClass {\n" + "private String someStrVariable = null;\n" + // create static instance initializer block
    "{\n" + "// " + OFFSET_MARKER + "\n" + "}\n" + "private void someMethod(String param) {\n" + "// empty \n" + "}\n" + "}\n";
    assertNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Add JavaParserExampleClass.java to project
    InputStream newFileInputStream = new ByteArrayInputStream(javaSourceCode.getBytes());
    IFile javaSourceFile = project.addFileToProject("/src/org/eclipse/changelog/tests", "JavaParserExampleClass.java", newFileInputStream);
    assertNotNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Open a source file and get the IEditorPart
    javaSourceEditorPart = openEditor(javaSourceFile);
    // make sure changelog editor content is right before merging
    assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
    // make sure we have the proper editor type
    assertTrue(javaSourceEditorPart instanceof AbstractDecoratedTextEditor);
    // Select the snippet we want
    int selectionStart = javaSourceCode.indexOf(OFFSET_MARKER);
    assertTrue(selectionStart >= 0);
    int selectionLength = OFFSET_MARKER.length();
    AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
    javaEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
    assertEquals("static initializer", actualFunctionName);
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TextSelection(org.eclipse.jface.text.TextSelection) AbstractDecoratedTextEditor(org.eclipse.ui.texteditor.AbstractDecoratedTextEditor) Test(org.junit.Test)

Example 8 with AbstractDecoratedTextEditor

use of org.eclipse.ui.texteditor.AbstractDecoratedTextEditor in project titan.EclipsePlug-ins by eclipse.

the class MarkerHandler method deprecateMarkers.

/**
 * Mark the provided markers as deprecated.
 * <p>
 * Those that are in an open editor, can be manipulated directly,
 *  the others will receive their value later by the AnnotationImageProvider
 *
 *  @param editor the editor being open/edited triggering the change.
 *  @param markers the markers to mark deprecated.
 */
public static void deprecateMarkers(final ISemanticTITANEditor editor, final IMarker[] markers) {
    if (editor == null || markers == null) {
        return;
    }
    try {
        AbstractDecoratedTextEditor textEditor = null;
        IFile fileInput = null;
        if (editor instanceof AbstractDecoratedTextEditor) {
            textEditor = (AbstractDecoratedTextEditor) editor;
            if (textEditor.getEditorInput() instanceof FileEditorInput) {
                final FileEditorInput input = (FileEditorInput) textEditor.getEditorInput();
                fileInput = input.getFile();
            }
        }
        for (IMarker marker : markers) {
            if (fileInput != null && textEditor != null && fileInput.equals(marker.getResource())) {
                final IEditorInput editorInput = textEditor.getEditorInput();
                final IDocumentProvider provider = textEditor.getDocumentProvider();
                if (editorInput != null && provider != null) {
                    final IAnnotationModel model = provider.getAnnotationModel(editorInput);
                    final Iterator<?> e = model.getAnnotationIterator();
                    while (e.hasNext()) {
                        final Annotation annotation = (Annotation) e.next();
                        if (annotation instanceof MarkerAnnotation && marker.equals(((MarkerAnnotation) annotation).getMarker())) {
                            annotation.markDeleted(true);
                        }
                    }
                } else {
                    if (!marker.getAttribute(AnnotationImageProvider.DEPRECATED, false)) {
                        marker.setAttribute(AnnotationImageProvider.DEPRECATED, true);
                    }
                }
            } else {
                if (!marker.getAttribute(AnnotationImageProvider.DEPRECATED, false)) {
                    marker.setAttribute(AnnotationImageProvider.DEPRECATED, true);
                }
            }
        }
    } catch (CoreException e) {
        ErrorReporter.logExceptionStackTrace(MARKER_HANDLING_ERROR, e);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) CoreException(org.eclipse.core.runtime.CoreException) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IMarker(org.eclipse.core.resources.IMarker) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEditorInput(org.eclipse.ui.IEditorInput) Annotation(org.eclipse.jface.text.source.Annotation) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) AbstractDecoratedTextEditor(org.eclipse.ui.texteditor.AbstractDecoratedTextEditor)

Example 9 with AbstractDecoratedTextEditor

use of org.eclipse.ui.texteditor.AbstractDecoratedTextEditor in project tdi-studio-se by Talend.

the class OpenExistVersionProcessWizard method openXtextEditor.

private void openXtextEditor(RepositoryNode repositoryNode, IProject fsProject, boolean readonly) {
    try {
        if (ProjectManager.getInstance().isInCurrentMainProject(repositoryNode)) {
            IFile linkedFile = null;
            IOpenJobScriptActionService openJobScriptActionService = (IOpenJobScriptActionService) GlobalServiceRegister.getDefault().getService(IOpenJobScriptActionService.class);
            if (openJobScriptActionService != null) {
                linkedFile = openJobScriptActionService.createWorkspaceLink(fsProject, repositoryNode.getObject().getProperty().getItem());
            } else {
                linkedFile = createWorkspaceLink(fsProject, fsProject.getFolder(ERepositoryObjectType.getFolderName(ERepositoryObjectType.JOB_SCRIPT)).getFolder(repositoryNode.getParent().getRepositoryPath()).getFile(repositoryNode.getObject().getProperty().getLabel()).getLocation(), repositoryNode.getObject().getProperty().getVersion());
            }
            IWorkbenchPage page = getActivePage();
            IEditorPart editor = IDE.openEditor(page, linkedFile);
            if (readonly) {
                IDocumentProvider provider = ((AbstractDecoratedTextEditor) editor).getDocumentProvider();
                Class p = provider.getClass();
                Class[] type = new Class[1];
                type[0] = Boolean.TYPE;
                Object[] para = new Object[1];
                para[0] = Boolean.TRUE;
                Method method = p.getMethod("setReadOnly", type);
                method.invoke(provider, para);
            }
        } else {
            JobScriptItem jobScriptItem = (JobScriptItem) repositoryNode.getObject().getProperty().getItem();
            IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(jobScriptItem.eResource().getURI().path()).removeFirstSegments(1).removeFileExtension());
            IFile linkedFile = createWorkspaceLink(fsProject, file.getLocation(), "");
            IWorkbenchPage page = getActivePage();
            IEditorPart editor = IDE.openEditor(page, linkedFile);
            if (readonly) {
                IDocumentProvider provider = ((AbstractDecoratedTextEditor) editor).getDocumentProvider();
                Class p = provider.getClass();
                Class[] type = new Class[1];
                type[0] = Boolean.TYPE;
                Object[] para = new Object[1];
                para[0] = Boolean.TRUE;
                Method method = p.getMethod("setReadOnly", type);
                method.invoke(provider, para);
            }
        }
    } catch (CoreException e) {
        ExceptionHandler.process(e);
    } catch (SecurityException e) {
        ExceptionHandler.process(e);
    } catch (NoSuchMethodException e) {
        ExceptionHandler.process(e);
    } catch (IllegalArgumentException e) {
        ExceptionHandler.process(e);
    } catch (IllegalAccessException e) {
        ExceptionHandler.process(e);
    } catch (InvocationTargetException e) {
        ExceptionHandler.process(e);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) IOpenJobScriptActionService(org.talend.core.ui.services.IOpenJobScriptActionService) JobScriptItem(org.talend.core.model.properties.JobScriptItem) IEditorPart(org.eclipse.ui.IEditorPart) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) AbstractDecoratedTextEditor(org.eclipse.ui.texteditor.AbstractDecoratedTextEditor) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) CoreException(org.eclipse.core.runtime.CoreException) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IRepositoryViewObject(org.talend.core.model.repository.IRepositoryViewObject)

Example 10 with AbstractDecoratedTextEditor

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

the class JavaParserTest method canDetermineThatSelectionIsJustInClass.

/**
 * Given an IEditorPart and current selection is inside a class but not within a
 * method, not selecting a field and not in a nested class (somewhere else in the
 * class) it should return an empty string for the function.
 *
 * @throws Exception
 */
@Test
public void canDetermineThatSelectionIsJustInClass() throws Exception {
    // Apparently comments don't show up in the compilation units. Makes
    // sense, right? But we can't use the OFFSET_MARKER trick in this case.
    final String javaSourceCode = "public class JavaParserExampleClass {\n" + "private String someStrVariable = null;\n" + // want to select this line indexOf(';') + 2
    "\n" + "\n" + "private void someMethod(String param) {\n" + "// empty \n" + "}\n" + "}\n";
    assertNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Add JavaParserExampleClass.java to project
    InputStream newFileInputStream = new ByteArrayInputStream(javaSourceCode.getBytes());
    IFile javaSourceFile = project.addFileToProject("/src/org/eclipse/changelog/tests", "JavaParserExampleClass.java", newFileInputStream);
    assertNotNull(project.getTestProject().findMember(new Path("/src/org/eclipse/changelog/tests/JavaParserExampleClass.java")));
    // Open a source file and get the IEditorPart
    javaSourceEditorPart = openEditor(javaSourceFile);
    // make sure changelog editor content is right before merging
    assertEquals(javaSourceCode, getContent(javaSourceEditorPart));
    // make sure we have the proper editor type
    assertTrue(javaSourceEditorPart instanceof AbstractDecoratedTextEditor);
    // Select the right point
    int selectionStart = javaSourceCode.indexOf(';') + 2;
    assertTrue(selectionStart >= 2);
    int selectionLength = 0;
    AbstractDecoratedTextEditor javaEditor = (AbstractDecoratedTextEditor) javaSourceEditorPart;
    javaEditor.getSelectionProvider().setSelection(new TextSelection(selectionStart, selectionLength));
    final String actualFunctionName = javaParser.parseCurrentFunction(javaSourceEditorPart);
    assertEquals("", /* expect empty string */
    actualFunctionName);
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TextSelection(org.eclipse.jface.text.TextSelection) AbstractDecoratedTextEditor(org.eclipse.ui.texteditor.AbstractDecoratedTextEditor) Test(org.junit.Test)

Aggregations

AbstractDecoratedTextEditor (org.eclipse.ui.texteditor.AbstractDecoratedTextEditor)11 IFile (org.eclipse.core.resources.IFile)10 Path (org.eclipse.core.runtime.Path)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 InputStream (java.io.InputStream)7 TextSelection (org.eclipse.jface.text.TextSelection)7 Test (org.junit.Test)7 CoreException (org.eclipse.core.runtime.CoreException)3 IEditorInput (org.eclipse.ui.IEditorInput)2 IEditorPart (org.eclipse.ui.IEditorPart)2 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 IMarker (org.eclipse.core.resources.IMarker)1 IPath (org.eclipse.core.runtime.IPath)1 FileRevisionEditorInput (org.eclipse.egit.ui.internal.revision.FileRevisionEditorInput)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 ITextSelection (org.eclipse.jface.text.ITextSelection)1 IRevisionRulerColumn (org.eclipse.jface.text.revisions.IRevisionRulerColumn)1