[jbosstools-commits] JBoss Tools SVN: r42625 - trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker.
jbosstools-commits at lists.jboss.org
jbosstools-commits at lists.jboss.org
Tue Jul 17 18:09:16 EDT 2012
Author: dazarov
Date: 2012-07-17 18:09:15 -0400 (Tue, 17 Jul 2012)
New Revision: 42625
Modified:
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/CDIProblemMarkerResolutionGenerator.java
trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/CreateCDIElementMarkerResolution.java
Log:
Make CDI Quick Fixes work for As-You-Type validator annotations https://issues.jboss.org/browse/JBIDE-12328
Modified: trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/CDIProblemMarkerResolutionGenerator.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/CDIProblemMarkerResolutionGenerator.java 2012-07-17 18:19:12 UTC (rev 42624)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/CDIProblemMarkerResolutionGenerator.java 2012-07-17 22:09:15 UTC (rev 42625)
@@ -35,8 +35,14 @@
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.source.Annotation;
+import org.eclipse.ui.IEditorInput;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IMarkerResolution;
import org.eclipse.ui.IMarkerResolutionGenerator2;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.DocumentProviderRegistry;
import org.eclipse.ui.texteditor.IDocumentProvider;
@@ -97,7 +103,84 @@
return -1;
}
- private IQuickFix[] findResolutions(ICompilationUnit compilationUnit, int problemId, int offset, ICDIMarkerResolutionGeneratorExtension[] extensions) throws JavaModelException{
+ private int getMessageID(TemporaryAnnotation annotation){
+ Integer attribute = ((Integer) annotation.getAttributes().get(CDIValidationErrorManager.MESSAGE_ID_ATTRIBUTE_NAME));
+ if (attribute != null)
+ return attribute.intValue();
+
+ return -1;
+ }
+
+ private IQuickFix[] findXMLResolutions(IFile file, int messageId, int start, int end, boolean asYouType) throws JavaModelException{
+ IJavaProject javaProject = EclipseUtil.getJavaProject(file.getProject());
+
+ FileEditorInput input = new FileEditorInput(file);
+ IDocumentProvider provider = DocumentProviderRegistry.getDefault().getDocumentProvider(input);
+ try {
+ provider.connect(input);
+ } catch (CoreException e) {
+ CDIUIPlugin.getDefault().logError(e);
+ }
+
+ IDocument document = provider.getDocument(input);
+
+ String text="";
+ try {
+ text = document.get(start, end-start);
+ } catch (BadLocationException e) {
+ CDIUIPlugin.getDefault().logError(e);
+ } finally {
+ provider.disconnect(input);
+ }
+ boolean correctTypeName = JavaConventionsUtil.validatePackageName(text, javaProject).isOK();
+
+ if(messageId == CDIValidationErrorManager.UNKNOWN_ALTERNATIVE_BEAN_CLASS_NAME_ID){
+ IJavaElement element = findJavaElementByQualifiedName(javaProject, text);
+ if(element == null && correctTypeName){
+ return new IQuickFix[] {
+ new CreateCDIElementMarkerResolution(file.getProject(), text, CreateCDIElementMarkerResolution.CREATE_BEAN_CLASS)
+ };
+ }
+ }else if(messageId == CDIValidationErrorManager.UNKNOWN_ALTERNATIVE_ANNOTATION_NAME_ID){
+ IJavaElement element = findJavaElementByQualifiedName(javaProject, text);
+ if(element == null && correctTypeName){
+ return new IQuickFix[] {
+ new CreateCDIElementMarkerResolution(file.getProject(), text, CreateCDIElementMarkerResolution.CREATE_STEREOTYPE)
+ };
+ }
+ }else if(messageId == CDIValidationErrorManager.ILLEGAL_ALTERNATIVE_BEAN_CLASS_ID){
+ IJavaElement element = getTypeToAddAlternativeToBean(javaProject, text, asYouType);
+ if(element != null){
+ return new IQuickFix[] {
+ new AddAnnotationMarkerResolution(element, CDIConstants.ALTERNATIVE_ANNOTATION_TYPE_NAME)
+ };
+ }
+ }else if(messageId == CDIValidationErrorManager.ILLEGAL_ALTERNATIVE_ANNOTATION_ID){
+ IJavaElement element = getTypeToAddAlternativeToStereotype(javaProject, text, asYouType);
+ if(element != null){
+ return new IQuickFix[] {
+ new AddAnnotationMarkerResolution(element, CDIConstants.ALTERNATIVE_ANNOTATION_TYPE_NAME)
+ };
+ }
+ }else if(messageId == CDIValidationErrorManager.UNKNOWN_DECORATOR_BEAN_CLASS_NAME_ID){
+ IJavaElement element = findJavaElementByQualifiedName(javaProject, text);
+ if(element == null && correctTypeName){
+ return new IQuickFix[] {
+ new CreateCDIElementMarkerResolution(file.getProject(), text, CreateCDIElementMarkerResolution.CREATE_DECORATOR)
+ };
+ }
+ }else if(messageId == CDIValidationErrorManager.UNKNOWN_INTERCEPTOR_CLASS_NAME_ID){
+ IJavaElement element = findJavaElementByQualifiedName(javaProject, text);
+ if(element == null && correctTypeName){
+ return new IQuickFix[] {
+ new CreateCDIElementMarkerResolution(file.getProject(), text, CreateCDIElementMarkerResolution.CREATE_INTERCEPTOR)
+ };
+ }
+ }
+ return new IQuickFix[]{};
+ }
+
+ private IQuickFix[] findJavaResolutions(ICompilationUnit compilationUnit, int problemId, int offset, ICDIMarkerResolutionGeneratorExtension[] extensions, boolean asYouType) throws JavaModelException{
if (problemId == CDIValidationErrorManager.ILLEGAL_PRODUCER_FIELD_IN_SESSION_BEAN_ID) {
IField field = findNonStaticField(compilationUnit, offset);
if(field != null){
@@ -143,7 +226,7 @@
List<IMarkerResolution> resolutions = new ArrayList<IMarkerResolution>();
- IInjectionPoint injectionPoint = findInjectionPoint(compilationUnit, offset);
+ IInjectionPoint injectionPoint = findInjectionPoint(compilationUnit, offset, asYouType);
if(injectionPoint != null){
List<IBean> beans;
if(problemId == CDIValidationErrorManager.AMBIGUOUS_INJECTION_POINTS_ID){
@@ -185,7 +268,7 @@
IField field = findPublicField(compilationUnit, offset);
CDICoreNature cdiNature = CDIUtil.getCDINatureWithProgress(field.getUnderlyingResource().getProject());
if(cdiNature != null){
- ICDIProject cdiProject = cdiNature.getDelegate();
+ ICDIProject cdiProject = CDIUtil.getCDIProject((IFile)compilationUnit.getUnderlyingResource(), cdiNature, asYouType);
if(cdiProject != null){
Set<IBean> beans = cdiProject.getBeans(field.getUnderlyingResource().getFullPath());
@@ -366,7 +449,7 @@
if(ta != null && ta.type != null){
CDICoreNature cdiNature = CDIUtil.getCDINatureWithProgress(ta.type.getUnderlyingResource().getProject());
if(cdiNature != null){
- ICDIProject cdiProject = cdiNature.getDelegate();
+ ICDIProject cdiProject = CDIUtil.getCDIProject((IFile)compilationUnit.getUnderlyingResource(), cdiNature, asYouType);;
IType declarationType = findNamedDeclarationType(cdiProject, ta.type, problemId == CDIValidationErrorManager.DECORATOR_HAS_NAME_ID);
ArrayList<IMarkerResolution> resolutions = new ArrayList<IMarkerResolution>();
if(declarationType != null){
@@ -470,7 +553,7 @@
}
}
}else if(problemId == CDIValidationErrorManager.PARAM_INJECTION_DECLARES_EMPTY_NAME_ID){
- List<IMarkerResolution> resolutions = getAddNameResolutions(compilationUnit, offset);
+ List<IMarkerResolution> resolutions = getAddNameResolutions(compilationUnit, offset, asYouType);
return resolutions.toArray(new IQuickFix[]{});
}else if(problemId == CDIValidationErrorManager.MULTIPLE_DISPOSING_PARAMETERS_ID){
ILocalVariable parameter = findParameter(compilationUnit, offset);
@@ -499,7 +582,7 @@
final IFile file = (IFile) marker.getResource();
- IJavaProject javaProject = EclipseUtil.getJavaProject(file.getProject());
+
Integer attribute = ((Integer) marker.getAttribute(IMarker.CHAR_START));
if (attribute == null)
@@ -516,78 +599,15 @@
if (JAVA_EXTENSION.equals(file.getFileExtension())) {
ICompilationUnit compilationUnit = EclipseUtil.getCompilationUnit(file);
- return findResolutions(compilationUnit, messageId, start, extensions);
+ return findJavaResolutions(compilationUnit, messageId, start, extensions, false);
}else if (XML_EXTENSION.equals(file.getFileExtension())){
- FileEditorInput input = new FileEditorInput(file);
- IDocumentProvider provider = DocumentProviderRegistry.getDefault().getDocumentProvider(input);
- try {
- provider.connect(input);
- } catch (CoreException e) {
- CDIUIPlugin.getDefault().logError(e);
- }
-
- IDocument document = provider.getDocument(input);
-
- String text="";
- try {
- text = document.get(start, end-start);
- } catch (BadLocationException e) {
- CDIUIPlugin.getDefault().logError(e);
- } finally {
- provider.disconnect(input);
- }
-
- boolean correctTypeName = JavaConventionsUtil.validatePackageName(text, javaProject).isOK();
-
- if(messageId == CDIValidationErrorManager.UNKNOWN_ALTERNATIVE_BEAN_CLASS_NAME_ID){
- IJavaElement element = findJavaElementByQualifiedName(javaProject, text);
- if(element == null && correctTypeName){
- return new IMarkerResolution[] {
- new CreateCDIElementMarkerResolution(file.getProject(), text, CreateCDIElementMarkerResolution.CREATE_BEAN_CLASS)
- };
- }
- }else if(messageId == CDIValidationErrorManager.UNKNOWN_ALTERNATIVE_ANNOTATION_NAME_ID){
- IJavaElement element = findJavaElementByQualifiedName(javaProject, text);
- if(element == null && correctTypeName){
- return new IMarkerResolution[] {
- new CreateCDIElementMarkerResolution(file.getProject(), text, CreateCDIElementMarkerResolution.CREATE_STEREOTYPE)
- };
- }
- }else if(messageId == CDIValidationErrorManager.ILLEGAL_ALTERNATIVE_BEAN_CLASS_ID){
- IJavaElement element = getTypeToAddAlternativeToBean(javaProject, text);
- if(element != null){
- return new IMarkerResolution[] {
- new AddAnnotationMarkerResolution(element, CDIConstants.ALTERNATIVE_ANNOTATION_TYPE_NAME)
- };
- }
- }else if(messageId == CDIValidationErrorManager.ILLEGAL_ALTERNATIVE_ANNOTATION_ID){
- IJavaElement element = getTypeToAddAlternativeToStereotype(javaProject, text);
- if(element != null){
- return new IMarkerResolution[] {
- new AddAnnotationMarkerResolution(element, CDIConstants.ALTERNATIVE_ANNOTATION_TYPE_NAME)
- };
- }
- }else if(messageId == CDIValidationErrorManager.UNKNOWN_DECORATOR_BEAN_CLASS_NAME_ID){
- IJavaElement element = findJavaElementByQualifiedName(javaProject, text);
- if(element == null && correctTypeName){
- return new IMarkerResolution[] {
- new CreateCDIElementMarkerResolution(file.getProject(), text, CreateCDIElementMarkerResolution.CREATE_DECORATOR)
- };
- }
- }else if(messageId == CDIValidationErrorManager.UNKNOWN_INTERCEPTOR_CLASS_NAME_ID){
- IJavaElement element = findJavaElementByQualifiedName(javaProject, text);
- if(element == null && correctTypeName){
- return new IMarkerResolution[] {
- new CreateCDIElementMarkerResolution(file.getProject(), text, CreateCDIElementMarkerResolution.CREATE_INTERCEPTOR)
- };
- }
- }
+ return findXMLResolutions(file, messageId, start, end, false);
}
return new IMarkerResolution[] {};
}
- private List<IMarkerResolution> getAddNameResolutions(ICompilationUnit compilationUnit, int start) throws JavaModelException{
+ private List<IMarkerResolution> getAddNameResolutions(ICompilationUnit compilationUnit, int start, boolean asYouType) throws JavaModelException{
List<IMarkerResolution> resolutions = new ArrayList<IMarkerResolution>();
ILocalVariable parameter = findParameter(compilationUnit, start);
if(parameter != null){
@@ -597,7 +617,7 @@
if(cdiNature == null)
return resolutions;
- ICDIProject cdiProject = cdiNature.getDelegate();
+ ICDIProject cdiProject = CDIUtil.getCDIProject((IFile)compilationUnit.getUnderlyingResource(), cdiNature, asYouType);
if(cdiProject == null){
return resolutions;
@@ -628,7 +648,7 @@
return resolutions;
}
- private IType getTypeToAddAlternativeToBean(IJavaProject javaProject, String qualifiedName){
+ private IType getTypeToAddAlternativeToBean(IJavaProject javaProject, String qualifiedName, boolean asYouType) throws JavaModelException{
IType type = null;
try {
type = javaProject.findType(qualifiedName);
@@ -639,7 +659,7 @@
if(type != null){
CDICoreNature cdiNature = CDIUtil.getCDINatureWithProgress(javaProject.getProject());
if(cdiNature != null){
- ICDIProject cdiProject = cdiNature.getDelegate();
+ ICDIProject cdiProject = CDIUtil.getCDIProject((IFile)type.getUnderlyingResource(), cdiNature, asYouType);
if(cdiProject != null){
IClassBean classBean = cdiProject.getBeanClass(type);
@@ -652,7 +672,7 @@
return null;
}
- private IType getTypeToAddAlternativeToStereotype(IJavaProject javaProject, String qualifiedName){
+ private IType getTypeToAddAlternativeToStereotype(IJavaProject javaProject, String qualifiedName, boolean asYouType) throws JavaModelException{
IType type = null;
try {
type = javaProject.findType(qualifiedName);
@@ -663,7 +683,7 @@
if(type != null){
CDICoreNature cdiNature = CDIUtil.getCDINatureWithProgress(javaProject.getProject());
if(cdiNature != null){
- ICDIProject cdiProject = cdiNature.getDelegate();
+ ICDIProject cdiProject = CDIUtil.getCDIProject((IFile)type.getUnderlyingResource(), cdiNature, asYouType);
if(cdiProject != null){
IStereotype stereotype = cdiProject.getStereotype(qualifiedName);
@@ -771,7 +791,7 @@
}
- private IInjectionPoint findInjectionPoint(ICompilationUnit compilationUnit, int start) throws JavaModelException{
+ private IInjectionPoint findInjectionPoint(ICompilationUnit compilationUnit, int start, boolean asYouType) throws JavaModelException{
IJavaElement element = findJavaElement(compilationUnit, start);
if(element == null)
return null;
@@ -780,7 +800,7 @@
if(cdiNature == null)
return null;
- ICDIProject cdiProject = cdiNature.getDelegate();
+ ICDIProject cdiProject = CDIUtil.getCDIProject((IFile)compilationUnit.getUnderlyingResource(), cdiNature, asYouType);
if(cdiProject == null){
return null;
@@ -1034,14 +1054,31 @@
@Override
public IJavaCompletionProposal[] getProposals(Annotation annotation) {
if(annotation instanceof TempJavaProblemAnnotation){
- int messageId = ((TempJavaProblemAnnotation) annotation).getId() - TempJavaProblem.TEMP_PROBLEM_ID;
- ICompilationUnit compilationUnit = ((TempJavaProblemAnnotation) annotation).getCompilationUnit();
- int start = ((TempJavaProblemAnnotation) annotation).getPosition();
+ TempJavaProblemAnnotation javaAnnotation = (TempJavaProblemAnnotation) annotation;
- ICDIMarkerResolutionGeneratorExtension[] extensions = CDIQuickFixExtensionManager.getInstances();
+ int messageId = javaAnnotation.getId() - TempJavaProblem.TEMP_PROBLEM_ID;
+ ICompilationUnit compilationUnit = javaAnnotation.getCompilationUnit();
+ if(compilationUnit != null){
+ int start = javaAnnotation.getPosition();
+
+ ICDIMarkerResolutionGeneratorExtension[] extensions = CDIQuickFixExtensionManager.getInstances();
+
+ try {
+ return findJavaResolutions(compilationUnit, messageId, start, extensions, true);
+ } catch (JavaModelException e) {
+ CDIUIPlugin.getDefault().logError(e);
+ }
+ }
+ }else if(annotation instanceof TemporaryAnnotation){
+ TemporaryAnnotation tempAnnotation = (TemporaryAnnotation)annotation;
+ IFile file = getFile();
+ int messageId = getMessageID(tempAnnotation);
+ int start = tempAnnotation.getPosition().getOffset();
+ int end = start + tempAnnotation.getPosition().getLength();
+
try {
- return findResolutions(compilationUnit, messageId, start, extensions);
+ return findXMLResolutions(file, messageId, start, end, true);
} catch (JavaModelException e) {
CDIUIPlugin.getDefault().logError(e);
}
@@ -1049,4 +1086,20 @@
return null;
}
+ private static IFile getFile(){
+ IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ if(window != null){
+ IWorkbenchPage page = window.getActivePage();
+ if(page != null){
+ IEditorPart editor = page.getActiveEditor();
+ if(editor != null){
+ IEditorInput input = editor.getEditorInput();
+ if(input instanceof IFileEditorInput){
+ return ((IFileEditorInput) input).getFile();
+ }
+ }
+ }
+ }
+ return null;
+ }
}
Modified: trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/CreateCDIElementMarkerResolution.java
===================================================================
--- trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/CreateCDIElementMarkerResolution.java 2012-07-17 18:19:12 UTC (rev 42624)
+++ trunk/cdi/plugins/org.jboss.tools.cdi.ui/src/org/jboss/tools/cdi/ui/marker/CreateCDIElementMarkerResolution.java 2012-07-17 22:09:15 UTC (rev 42625)
@@ -12,12 +12,14 @@
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Shell;
-import org.eclipse.ui.IMarkerResolution2;
import org.eclipse.ui.PlatformUI;
import org.jboss.tools.cdi.core.CDIImages;
import org.jboss.tools.cdi.internal.core.refactoring.CDIMarkerResolutionUtils;
@@ -28,9 +30,10 @@
import org.jboss.tools.cdi.ui.wizard.NewInterceptorCreationWizard;
import org.jboss.tools.cdi.ui.wizard.NewStereotypeCreationWizard;
import org.jboss.tools.common.model.ui.wizards.NewTypeWizardAdapter;
+import org.jboss.tools.common.quickfix.IQuickFix;
import org.jboss.tools.common.refactoring.TestableResolutionWithDialog;
-public class CreateCDIElementMarkerResolution implements IMarkerResolution2, TestableResolutionWithDialog{
+public class CreateCDIElementMarkerResolution implements IQuickFix, TestableResolutionWithDialog{
private static final String OBJECT = "java.lang.Object";
public static final int CREATE_BEAN_CLASS = 1;
@@ -65,15 +68,15 @@
@Override
public void run(IMarker marker){
- internal_run(marker, false);
+ internal_run(false);
}
@Override
public void runForTest(IMarker marker) {
- internal_run(marker, true);
+ internal_run(true);
}
- private void internal_run(IMarker marker, boolean test) {
+ private void internal_run(boolean test) {
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
NewCDIElementWizard wizard = null;
switch(id){
@@ -123,5 +126,35 @@
return CDIImages.QUICKFIX_EDIT;
}
+ @Override
+ public int getRelevance() {
+ return 100;
+ }
+ @Override
+ public void apply(IDocument document) {
+ internal_run(false);
+ }
+
+ @Override
+ public Point getSelection(IDocument document) {
+ return null;
+ }
+
+ @Override
+ public String getAdditionalProposalInfo() {
+ return getLabel();
+ }
+
+ @Override
+ public String getDisplayString() {
+ return getLabel();
+ }
+
+ @Override
+ public IContextInformation getContextInformation() {
+ return null;
+ }
+
+
}
More information about the jbosstools-commits
mailing list