1 11 package org.eclipse.jdt.internal.ui.text.correction; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IPath; 20 21 import org.eclipse.core.filebuffers.FileBuffers; 22 import org.eclipse.core.filebuffers.ITextFileBufferManager; 23 24 import org.eclipse.core.resources.IMarker; 25 26 import org.eclipse.jface.text.ITextViewer; 27 import org.eclipse.jface.text.Position; 28 import org.eclipse.jface.text.contentassist.ICompletionProposal; 29 import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext; 30 import org.eclipse.jface.text.quickassist.IQuickAssistProcessor; 31 import org.eclipse.jface.text.source.Annotation; 32 import org.eclipse.jface.text.source.IAnnotationModel; 33 34 import org.eclipse.ui.IEditorInput; 35 import org.eclipse.ui.IEditorPart; 36 import org.eclipse.ui.IMarkerHelpRegistry; 37 import org.eclipse.ui.IMarkerResolution; 38 import org.eclipse.ui.IStorageEditorInput; 39 import org.eclipse.ui.texteditor.SimpleMarkerAnnotation; 40 41 import org.eclipse.ui.ide.IDE; 42 43 import org.eclipse.jdt.ui.text.java.CompletionProposalComparator; 44 import org.eclipse.jdt.ui.text.java.IInvocationContext; 45 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; 46 import org.eclipse.jdt.ui.text.java.IProblemLocation; 47 48 import org.eclipse.jdt.internal.ui.JavaPlugin; 49 import org.eclipse.jdt.internal.ui.javaeditor.IJavaAnnotation; 50 import org.eclipse.jdt.internal.ui.text.spelling.WordQuickFixProcessor; 51 52 53 58 public class PropertiesFileCorrectionProcessor implements IQuickAssistProcessor { 59 60 private static String fgErrorMessage; 61 private static WordQuickFixProcessor fgWordQuickFixProcessor= new WordQuickFixProcessor(); 62 63 public static boolean isQuickFixableType(Annotation annotation) { 64 return (annotation instanceof IJavaAnnotation || annotation instanceof SimpleMarkerAnnotation) && !annotation.isMarkedDeleted(); 65 } 66 67 public static boolean hasCorrections(Annotation annotation) { 68 if (annotation instanceof IJavaAnnotation) { 69 IJavaAnnotation javaAnnotation= (IJavaAnnotation) annotation; 70 int problemId= javaAnnotation.getId(); 71 if (problemId != -1) 72 return fgWordQuickFixProcessor.hasCorrections(null, problemId); 73 } 74 if (annotation instanceof SimpleMarkerAnnotation) { 75 return hasCorrections(((SimpleMarkerAnnotation) annotation).getMarker()); 76 } 77 return false; 78 } 79 80 private static boolean hasCorrections(IMarker marker) { 81 if (marker == null || !marker.exists()) 82 return false; 83 84 IMarkerHelpRegistry registry= IDE.getMarkerHelpRegistry(); 85 return registry != null && registry.hasResolutions(marker); 86 } 87 88 public static boolean hasAssists(IInvocationContext context) { 89 return false; 90 } 91 92 private PropertiesFileCorrectionAssistant fAssistant; 93 94 95 98 public PropertiesFileCorrectionProcessor(PropertiesFileCorrectionAssistant assistant) { 99 fAssistant= assistant; 100 } 101 102 105 public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext quickAssistContext) { 106 ITextViewer viewer= quickAssistContext.getSourceViewer(); 107 int documentOffset= quickAssistContext.getOffset(); 108 109 IEditorPart part= fAssistant.getEditor(); 110 111 int length= viewer != null ? viewer.getSelectedRange().y : 0; 112 AssistContext context= new AssistContext(null, documentOffset, length); 113 114 fgErrorMessage= null; 115 ArrayList proposals= new ArrayList (); 116 117 IAnnotationModel model= null; 118 IEditorInput input= part.getEditorInput(); 119 if (!(input instanceof IStorageEditorInput)) 120 return null; 121 122 ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); 123 IPath path= null; 124 try { 125 path= ((IStorageEditorInput)input).getStorage().getFullPath(); 126 if (path == null) 127 return null; 128 manager.connect(path, null); 129 } catch (CoreException e) { 130 JavaPlugin.log(e.getStatus()); 131 return null; 132 } 133 134 try { 135 model= manager.getTextFileBuffer(path).getAnnotationModel(); 136 if (model != null) { 137 processAnnotations(context, model, proposals); 138 } 139 if (proposals.isEmpty()) { 140 proposals.add(new ChangeCorrectionProposal(CorrectionMessages.NoCorrectionProposal_description, null, 0, null)); 141 } 142 143 ICompletionProposal[] res= (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]); 144 Arrays.sort(res, new CompletionProposalComparator()); 145 return res; 146 } finally { 147 try { 148 manager.disconnect(path, null); 149 } catch (CoreException e) { 150 JavaPlugin.log(e.getStatus()); 151 } 152 } 153 } 154 155 private boolean isAtPosition(int offset, Position pos) { 156 return (pos != null) && (offset >= pos.getOffset() && offset <= (pos.getOffset() + pos.getLength())); 157 } 158 159 private void processAnnotations(IInvocationContext context, IAnnotationModel model, ArrayList proposals) { 160 int offset= context.getSelectionOffset(); 161 162 ArrayList problems= new ArrayList (); 163 Iterator iter= model.getAnnotationIterator(); 164 while (iter.hasNext()) { 165 Annotation annotation= (Annotation) iter.next(); 166 if (isQuickFixableType(annotation)) { 167 Position pos= model.getPosition(annotation); 168 if (isAtPosition(offset, pos)) { 169 processAnnotation(annotation, pos, problems, proposals); 170 } 171 } 172 } 173 IProblemLocation[] problemLocations= (IProblemLocation[]) problems.toArray(new IProblemLocation[problems.size()]); 174 collectCorrections(context, problemLocations, proposals); 175 } 176 177 private void processAnnotation(Annotation curr, Position pos, List problems, List proposals) { 178 if (curr instanceof IJavaAnnotation) { 179 IJavaAnnotation javaAnnotation= (IJavaAnnotation) curr; 180 int problemId= javaAnnotation.getId(); 181 if (problemId != -1) { 182 problems.add(new ProblemLocation(pos.getOffset(), pos.getLength(), javaAnnotation)); 183 return; } 185 } 186 if (curr instanceof SimpleMarkerAnnotation) { 187 IMarker marker= ((SimpleMarkerAnnotation) curr).getMarker(); 188 IMarkerResolution[] res= IDE.getMarkerHelpRegistry().getResolutions(marker); 189 if (res.length > 0) { 190 for (int i= 0; i < res.length; i++) { 191 proposals.add(new MarkerResolutionProposal(res[i], marker)); 192 } 193 } 194 } 195 } 196 197 public static void collectCorrections(IInvocationContext context, IProblemLocation[] locations, ArrayList proposals) { 198 try { 199 IJavaCompletionProposal[] res= fgWordQuickFixProcessor.getCorrections(context, locations); 200 if (res != null) { 201 for (int k= 0; k < res.length; k++) { 202 proposals.add(res[k]); 203 } 204 } 205 } catch (Exception e) { 206 fgErrorMessage= CorrectionMessages.JavaCorrectionProcessor_error_quickfix_message; 207 JavaPlugin.log(e); 208 } 209 } 210 211 public static void collectAssists(IInvocationContext context, IProblemLocation[] locations, ArrayList proposals) { 212 } 214 215 218 public String getErrorMessage() { 219 return fgErrorMessage; 220 } 221 222 226 public boolean canFix(Annotation annotation) { 227 return annotation != null && "org.eclipse.ui.workbench.texteditor.spelling".equals(annotation.getType()); } 229 230 234 public boolean canAssist(IQuickAssistInvocationContext invocationContext) { 235 return false; 236 } 237 238 } 239 | Popular Tags |