KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > PDEQuickAssistAssistant


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.pde.internal.ui.editor.text;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 import org.eclipse.core.resources.IMarker;
18 import org.eclipse.jface.text.BadLocationException;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.Position;
21 import org.eclipse.jface.text.contentassist.ICompletionProposal;
22 import org.eclipse.jface.text.contentassist.IContextInformation;
23 import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext;
24 import org.eclipse.jface.text.quickassist.IQuickAssistProcessor;
25 import org.eclipse.jface.text.quickassist.QuickAssistAssistant;
26 import org.eclipse.jface.text.source.Annotation;
27 import org.eclipse.jface.text.source.IAnnotationModel;
28 import org.eclipse.pde.internal.ui.PDEPluginImages;
29 import org.eclipse.pde.internal.ui.correction.AbstractPDEMarkerResolution;
30 import org.eclipse.pde.internal.ui.correction.ResolutionGenerator;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.ui.IMarkerResolution;
34 import org.eclipse.ui.texteditor.MarkerAnnotation;
35
36 public class PDEQuickAssistAssistant extends QuickAssistAssistant {
37
38     private Image fCreateImage;
39     private Image fRenameImage;
40     private Image fRemoveImage;
41     
42     class PDECompletionProposal implements ICompletionProposal {
43
44         Position fPosition;
45         IMarkerResolution fResolution;
46         IMarker fMarker;
47         
48         public PDECompletionProposal(IMarkerResolution resolution, Position pos, IMarker marker) {
49             fPosition = pos;
50             fResolution = resolution;
51             fMarker = marker;
52         }
53         
54         public void apply(IDocument document) {
55             fResolution.run(fMarker);
56         }
57
58         public Point getSelection(IDocument document) {
59             return new Point(fPosition.offset, 0);
60         }
61
62         public String JavaDoc getAdditionalProposalInfo() {
63             if (fResolution instanceof AbstractPDEMarkerResolution)
64                 return ((AbstractPDEMarkerResolution)fResolution).getDescription();
65             return null;
66         }
67
68         public String JavaDoc getDisplayString() {
69             return fResolution.getLabel();
70         }
71
72         public Image getImage() {
73             if (fResolution instanceof AbstractPDEMarkerResolution) {
74                 switch (((AbstractPDEMarkerResolution)fResolution).getType()) {
75                 case AbstractPDEMarkerResolution.CREATE_TYPE:
76                     return fCreateImage;
77                 case AbstractPDEMarkerResolution.REMOVE_TYPE:
78                     return fRemoveImage;
79                 case AbstractPDEMarkerResolution.RENAME_TYPE:
80                     return fRenameImage;
81                 }
82             }
83             return null;
84         }
85
86         public IContextInformation getContextInformation() {
87             return null;
88         }
89         
90     }
91     
92     class PDEQuickAssistProcessor implements IQuickAssistProcessor {
93
94         ResolutionGenerator fGenerator = new ResolutionGenerator();
95         HashMap JavaDoc fResMap = new HashMap JavaDoc();
96         
97         public String JavaDoc getErrorMessage() {
98             return null;
99         }
100
101         public boolean canFix(Annotation annotation) {
102             if (!(annotation instanceof MarkerAnnotation))
103                 return false;
104             IMarker marker = ((MarkerAnnotation)annotation).getMarker();
105             IMarkerResolution[] resolutions = fGenerator.getResolutions(marker);
106             boolean canFix = resolutions.length > 0;
107             if (canFix)
108                 if (!fResMap.containsKey(marker))
109                     fResMap.put(marker, resolutions);
110             return canFix;
111         }
112
113         public boolean canAssist(IQuickAssistInvocationContext invocationContext) {
114             return false;
115         }
116
117         public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
118             IAnnotationModel amodel = invocationContext.getSourceViewer().getAnnotationModel();
119             IDocument doc = invocationContext.getSourceViewer().getDocument();
120             
121             int offset = invocationContext.getOffset();
122             Iterator JavaDoc it = amodel.getAnnotationIterator();
123             ArrayList JavaDoc list = new ArrayList JavaDoc();
124             while (it.hasNext()) {
125                 Object JavaDoc key = it.next();
126                 if (!(key instanceof MarkerAnnotation))
127                     continue;
128                 
129                 MarkerAnnotation annotation = (MarkerAnnotation)key;
130                 IMarker marker = annotation.getMarker();
131                 
132                 IMarkerResolution[] mapping = (IMarkerResolution[])fResMap.get(marker);
133                 if (mapping != null) {
134                     Position pos = amodel.getPosition(annotation);
135                     try {
136                         int line = doc.getLineOfOffset(pos.getOffset());
137                         int start = pos.getOffset();
138                         String JavaDoc delim = doc.getLineDelimiter(line);
139                         int delimLength = delim != null ? delim.length() : 0;
140                         int end = doc.getLineLength(line) + start - delimLength;
141                         if (offset >= start && offset <= end)
142                             for (int i = 0; i < mapping.length; i++)
143                                 list.add(new PDECompletionProposal(mapping[i], pos, marker));
144                     } catch (BadLocationException e) {
145                     }
146                     
147                 }
148             }
149             return (ICompletionProposal[]) list.toArray(new ICompletionProposal[list.size()]);
150         }
151     }
152
153
154     
155     public PDEQuickAssistAssistant() {
156         setQuickAssistProcessor(new PDEQuickAssistProcessor());
157         fCreateImage = PDEPluginImages.DESC_ADD_ATT.createImage();
158         fRemoveImage = PDEPluginImages.DESC_DELETE.createImage();
159         fRenameImage = PDEPluginImages.DESC_REFRESH.createImage();
160     }
161     
162     public void dispose() {
163         fCreateImage.dispose();
164         fRemoveImage.dispose();
165         fRenameImage.dispose();
166     }
167 }
168
Popular Tags