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);
}
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);
}
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);
}
}
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);
}
}
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);
}
Aggregations