KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > PropertiesFileCorrectionProcessor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.correction;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
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 /**
54  * PropertiesFileCorrectionProcessor.
55  *
56  * @since 3.1
57  */

58 public class PropertiesFileCorrectionProcessor implements IQuickAssistProcessor {
59
60     private static String JavaDoc 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     /*
96      * Constructor for JavaCorrectionProcessor.
97      */

98     public PropertiesFileCorrectionProcessor(PropertiesFileCorrectionAssistant assistant) {
99         fAssistant= assistant;
100     }
101
102     /*
103      * @see IContentAssistProcessor#computeCompletionProposals(ITextViewer, int)
104      */

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 JavaDoc proposals= new ArrayList JavaDoc();
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 JavaDoc proposals) {
160         int offset= context.getSelectionOffset();
161
162         ArrayList JavaDoc problems= new ArrayList JavaDoc();
163         Iterator JavaDoc 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 JavaDoc problems, List JavaDoc 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; // java problems all handled by the quick assist processors
184
}
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 JavaDoc 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 JavaDoc 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 JavaDoc proposals) {
212         // no quick assists
213
}
214
215     /*
216      * @see IContentAssistProcessor#getErrorMessage()
217      */

218     public String JavaDoc getErrorMessage() {
219         return fgErrorMessage;
220     }
221
222     /*
223      * @see org.eclipse.jface.text.quickassist.IQuickAssistProcessor#canFix(org.eclipse.jface.text.source.Annotation)
224      * @since 3.2
225      */

226     public boolean canFix(Annotation annotation) {
227         return annotation != null && "org.eclipse.ui.workbench.texteditor.spelling".equals(annotation.getType()); //$NON-NLS-1$
228
}
229
230     /*
231      * @see org.eclipse.jface.text.quickassist.IQuickAssistProcessor#canAssist(org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext)
232      * @since 3.2
233      */

234     public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
235         return false;
236     }
237
238 }
239
Popular Tags