001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.text;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.Iterator;
015:
016: import org.eclipse.core.resources.IMarker;
017: import org.eclipse.jface.text.BadLocationException;
018: import org.eclipse.jface.text.IDocument;
019: import org.eclipse.jface.text.Position;
020: import org.eclipse.jface.text.contentassist.ICompletionProposal;
021: import org.eclipse.jface.text.contentassist.IContextInformation;
022: import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext;
023: import org.eclipse.jface.text.quickassist.IQuickAssistProcessor;
024: import org.eclipse.jface.text.quickassist.QuickAssistAssistant;
025: import org.eclipse.jface.text.source.Annotation;
026: import org.eclipse.jface.text.source.IAnnotationModel;
027: import org.eclipse.pde.internal.ui.PDEPluginImages;
028: import org.eclipse.pde.internal.ui.correction.AbstractPDEMarkerResolution;
029: import org.eclipse.pde.internal.ui.correction.ResolutionGenerator;
030: import org.eclipse.swt.graphics.Image;
031: import org.eclipse.swt.graphics.Point;
032: import org.eclipse.ui.IMarkerResolution;
033: import org.eclipse.ui.texteditor.MarkerAnnotation;
034:
035: public class PDEQuickAssistAssistant extends QuickAssistAssistant {
036:
037: private Image fCreateImage;
038: private Image fRenameImage;
039: private Image fRemoveImage;
040:
041: class PDECompletionProposal implements ICompletionProposal {
042:
043: Position fPosition;
044: IMarkerResolution fResolution;
045: IMarker fMarker;
046:
047: public PDECompletionProposal(IMarkerResolution resolution,
048: Position pos, IMarker marker) {
049: fPosition = pos;
050: fResolution = resolution;
051: fMarker = marker;
052: }
053:
054: public void apply(IDocument document) {
055: fResolution.run(fMarker);
056: }
057:
058: public Point getSelection(IDocument document) {
059: return new Point(fPosition.offset, 0);
060: }
061:
062: public String getAdditionalProposalInfo() {
063: if (fResolution instanceof AbstractPDEMarkerResolution)
064: return ((AbstractPDEMarkerResolution) fResolution)
065: .getDescription();
066: return null;
067: }
068:
069: public String getDisplayString() {
070: return fResolution.getLabel();
071: }
072:
073: public Image getImage() {
074: if (fResolution instanceof AbstractPDEMarkerResolution) {
075: switch (((AbstractPDEMarkerResolution) fResolution)
076: .getType()) {
077: case AbstractPDEMarkerResolution.CREATE_TYPE:
078: return fCreateImage;
079: case AbstractPDEMarkerResolution.REMOVE_TYPE:
080: return fRemoveImage;
081: case AbstractPDEMarkerResolution.RENAME_TYPE:
082: return fRenameImage;
083: }
084: }
085: return null;
086: }
087:
088: public IContextInformation getContextInformation() {
089: return null;
090: }
091:
092: }
093:
094: class PDEQuickAssistProcessor implements IQuickAssistProcessor {
095:
096: ResolutionGenerator fGenerator = new ResolutionGenerator();
097: HashMap fResMap = new HashMap();
098:
099: public String getErrorMessage() {
100: return null;
101: }
102:
103: public boolean canFix(Annotation annotation) {
104: if (!(annotation instanceof MarkerAnnotation))
105: return false;
106: IMarker marker = ((MarkerAnnotation) annotation)
107: .getMarker();
108: IMarkerResolution[] resolutions = fGenerator
109: .getResolutions(marker);
110: boolean canFix = resolutions.length > 0;
111: if (canFix)
112: if (!fResMap.containsKey(marker))
113: fResMap.put(marker, resolutions);
114: return canFix;
115: }
116:
117: public boolean canAssist(
118: IQuickAssistInvocationContext invocationContext) {
119: return false;
120: }
121:
122: public ICompletionProposal[] computeQuickAssistProposals(
123: IQuickAssistInvocationContext invocationContext) {
124: IAnnotationModel amodel = invocationContext
125: .getSourceViewer().getAnnotationModel();
126: IDocument doc = invocationContext.getSourceViewer()
127: .getDocument();
128:
129: int offset = invocationContext.getOffset();
130: Iterator it = amodel.getAnnotationIterator();
131: ArrayList list = new ArrayList();
132: while (it.hasNext()) {
133: Object key = it.next();
134: if (!(key instanceof MarkerAnnotation))
135: continue;
136:
137: MarkerAnnotation annotation = (MarkerAnnotation) key;
138: IMarker marker = annotation.getMarker();
139:
140: IMarkerResolution[] mapping = (IMarkerResolution[]) fResMap
141: .get(marker);
142: if (mapping != null) {
143: Position pos = amodel.getPosition(annotation);
144: try {
145: int line = doc.getLineOfOffset(pos.getOffset());
146: int start = pos.getOffset();
147: String delim = doc.getLineDelimiter(line);
148: int delimLength = delim != null ? delim
149: .length() : 0;
150: int end = doc.getLineLength(line) + start
151: - delimLength;
152: if (offset >= start && offset <= end)
153: for (int i = 0; i < mapping.length; i++)
154: list.add(new PDECompletionProposal(
155: mapping[i], pos, marker));
156: } catch (BadLocationException e) {
157: }
158:
159: }
160: }
161: return (ICompletionProposal[]) list
162: .toArray(new ICompletionProposal[list.size()]);
163: }
164: }
165:
166: public PDEQuickAssistAssistant() {
167: setQuickAssistProcessor(new PDEQuickAssistProcessor());
168: fCreateImage = PDEPluginImages.DESC_ADD_ATT.createImage();
169: fRemoveImage = PDEPluginImages.DESC_DELETE.createImage();
170: fRenameImage = PDEPluginImages.DESC_REFRESH.createImage();
171: }
172:
173: public void dispose() {
174: fCreateImage.dispose();
175: fRemoveImage.dispose();
176: fRenameImage.dispose();
177: }
178: }
|