KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.Arrays JavaDoc;
14 import java.util.Comparator JavaDoc;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.VerifyEvent;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.graphics.Point;
20
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.DocumentEvent;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.text.ITextViewer;
26 import org.eclipse.jface.text.contentassist.ICompletionProposalExtension2;
27 import org.eclipse.jface.text.contentassist.IContextInformation;
28 import org.eclipse.jface.text.link.LinkedModeModel;
29 import org.eclipse.jface.text.link.LinkedModeUI;
30 import org.eclipse.jface.text.link.LinkedPosition;
31 import org.eclipse.jface.text.link.LinkedPositionGroup;
32 import org.eclipse.jface.text.link.LinkedModeUI.ExitFlags;
33 import org.eclipse.jface.text.link.LinkedModeUI.IExitPolicy;
34
35 import org.eclipse.ui.IEditorPart;
36 import org.eclipse.ui.texteditor.link.EditorLinkedModeUI;
37
38 import org.eclipse.jdt.core.ICompilationUnit;
39 import org.eclipse.jdt.core.dom.ASTNode;
40 import org.eclipse.jdt.core.dom.CompilationUnit;
41 import org.eclipse.jdt.core.dom.SimpleName;
42
43 import org.eclipse.jdt.internal.corext.dom.LinkedNodeFinder;
44 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
45 import org.eclipse.jdt.internal.corext.util.Messages;
46
47 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
48
49 import org.eclipse.jdt.internal.ui.JavaPlugin;
50 import org.eclipse.jdt.internal.ui.JavaPluginImages;
51 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
52 import org.eclipse.jdt.internal.ui.javaeditor.EditorHighlightingSynchronizer;
53 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
54
55 /**
56  * A template proposal.
57  */

58 public class LinkedNamesAssistProposal implements IJavaCompletionProposal, ICompletionProposalExtension2, ICommandAccess {
59
60     /**
61      * An exit policy that skips Backspace and Delete at the beginning and at the end
62      * of a linked position, respectively.
63      *
64      * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=183925 .
65      */

66     public static class DeleteBlockingExitPolicy implements IExitPolicy {
67         private IDocument fDocument;
68
69         public DeleteBlockingExitPolicy(IDocument document) {
70             fDocument= document;
71         }
72
73         public ExitFlags doExit(LinkedModeModel model, VerifyEvent event, int offset, int length) {
74             if (length == 0 && (event.character == SWT.BS || event.character == SWT.DEL)) {
75                 LinkedPosition position= model.findPosition(new LinkedPosition(fDocument, offset, 0, LinkedPositionGroup.NO_STOP));
76                 if (position != null) {
77                     if (event.character == SWT.BS) {
78                         if (offset - 1 < position.getOffset()) {
79                             //skip backspace at beginning of linked position
80
event.doit= false;
81                         }
82                     } else /* event.character == SWT.DEL */ {
83                         if (offset + 1 > position.getOffset() + position.getLength()) {
84                             //skip delete at end of linked position
85
event.doit= false;
86                         }
87                     }
88                 }
89             }
90             
91             return null; // don't change behavior
92
}
93     }
94
95     
96     public static final String JavaDoc ASSIST_ID= "org.eclipse.jdt.ui.correction.renameInFile.assist"; //$NON-NLS-1$
97

98     private SimpleName fNode;
99     private ICompilationUnit fCompilationUnit;
100     private String JavaDoc fLabel;
101     private String JavaDoc fValueSuggestion;
102     private int fRelevance;
103
104     public LinkedNamesAssistProposal(ICompilationUnit cu, SimpleName node) {
105         this(CorrectionMessages.LinkedNamesAssistProposal_description, cu, node, null);
106         fNode= node;
107         fCompilationUnit= cu;
108         fRelevance= 8;
109     }
110
111     public LinkedNamesAssistProposal(String JavaDoc label, ICompilationUnit cu, SimpleName node, String JavaDoc valueSuggestion) {
112         fLabel= label;
113         fNode= node;
114         fCompilationUnit= cu;
115         fValueSuggestion= valueSuggestion;
116         fRelevance= 8;
117     }
118
119     /* (non-Javadoc)
120      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
121      */

122     public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
123         try {
124             Point seletion= viewer.getSelectedRange();
125
126             // get full ast
127
CompilationUnit root= JavaPlugin.getDefault().getASTProvider().getAST(fCompilationUnit, ASTProvider.WAIT_YES, null);
128
129             ASTNode nameNode= NodeFinder.perform(root, fNode.getStartPosition(), fNode.getLength());
130             final int pos= fNode.getStartPosition();
131
132             ASTNode[] sameNodes;
133             if (nameNode instanceof SimpleName) {
134                 sameNodes= LinkedNodeFinder.findByNode(root, (SimpleName) nameNode);
135             } else {
136                 sameNodes= new ASTNode[] { nameNode };
137             }
138
139             // sort for iteration order, starting with the node @ offset
140
Arrays.sort(sameNodes, new Comparator JavaDoc() {
141
142                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
143                     return rank((ASTNode) o1) - rank((ASTNode) o2);
144                 }
145
146                 /**
147                  * Returns the absolute rank of an <code>ASTNode</code>. Nodes
148                  * preceding <code>offset</code> are ranked last.
149                  *
150                  * @param node the node to compute the rank for
151                  * @return the rank of the node with respect to the invocation offset
152                  */

153                 private int rank(ASTNode node) {
154                     int relativeRank= node.getStartPosition() + node.getLength() - pos;
155                     if (relativeRank < 0)
156                         return Integer.MAX_VALUE + relativeRank;
157                     else
158                         return relativeRank;
159                 }
160
161             });
162
163             IDocument document= viewer.getDocument();
164             LinkedPositionGroup group= new LinkedPositionGroup();
165             for (int i= 0; i < sameNodes.length; i++) {
166                 ASTNode elem= sameNodes[i];
167                 group.addPosition(new LinkedPosition(document, elem.getStartPosition(), elem.getLength(), i));
168             }
169
170             LinkedModeModel model= new LinkedModeModel();
171             model.addGroup(group);
172             model.forceInstall();
173             JavaEditor editor= getJavaEditor();
174             if (editor != null) {
175                 model.addLinkingListener(new EditorHighlightingSynchronizer(editor));
176             }
177
178             LinkedModeUI ui= new EditorLinkedModeUI(model, viewer);
179             ui.setExitPolicy(new DeleteBlockingExitPolicy(document));
180             ui.setExitPosition(viewer, offset, 0, LinkedPositionGroup.NO_STOP);
181             ui.enter();
182
183             if (fValueSuggestion != null) {
184                 document.replace(nameNode.getStartPosition(), nameNode.getLength(), fValueSuggestion);
185                 IRegion selectedRegion= ui.getSelectedRegion();
186                 seletion= new Point(selectedRegion.getOffset(), fValueSuggestion.length());
187             }
188             
189             viewer.setSelectedRange(seletion.x, seletion.y); // by default full word is selected, restore original selection
190

191         } catch (BadLocationException e) {
192             JavaPlugin.log(e);
193         }
194     }
195
196     /**
197      * Returns the currently active java editor, or <code>null</code> if it
198      * cannot be determined.
199      *
200      * @return the currently active java editor, or <code>null</code>
201      */

202     private JavaEditor getJavaEditor() {
203         IEditorPart part= JavaPlugin.getActivePage().getActiveEditor();
204         if (part instanceof JavaEditor)
205             return (JavaEditor) part;
206         else
207             return null;
208     }
209
210     /*
211      * @see ICompletionProposal#apply(IDocument)
212      */

213     public void apply(IDocument document) {
214         // can't do anything
215
}
216
217     /*
218      * @see ICompletionProposal#getSelection(IDocument)
219      */

220     public Point getSelection(IDocument document) {
221         return null;
222     }
223
224     /*
225      * @see ICompletionProposal#getAdditionalProposalInfo()
226      */

227     public String JavaDoc getAdditionalProposalInfo() {
228         return CorrectionMessages.LinkedNamesAssistProposal_proposalinfo;
229     }
230
231     /*
232      * @see ICompletionProposal#getDisplayString()
233      */

234     public String JavaDoc getDisplayString() {
235         String JavaDoc shortCutString= CorrectionCommandHandler.getShortCutString(getCommandId());
236         if (shortCutString != null) {
237             return Messages.format(CorrectionMessages.ChangeCorrectionProposal_name_with_shortcut, new String JavaDoc[] { fLabel, shortCutString });
238         }
239         return fLabel;
240     }
241
242     /*
243      * @see ICompletionProposal#getImage()
244      */

245     public Image getImage() {
246         return JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LINKED_RENAME);
247     }
248
249     /*
250      * @see ICompletionProposal#getContextInformation()
251      */

252     public IContextInformation getContextInformation() {
253         return null;
254     }
255
256     /*
257      * @see IJavaCompletionProposal#getRelevance()
258      */

259     public int getRelevance() {
260         return fRelevance;
261     }
262
263
264     /* (non-Javadoc)
265      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#selected(org.eclipse.jface.text.ITextViewer, boolean)
266      */

267     public void selected(ITextViewer textViewer, boolean smartToggle) {
268     }
269
270     /* (non-Javadoc)
271      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#unselected(org.eclipse.jface.text.ITextViewer)
272      */

273     public void unselected(ITextViewer textViewer) {
274     }
275
276     /* (non-Javadoc)
277      * @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#validate(org.eclipse.jface.text.IDocument, int, org.eclipse.jface.text.DocumentEvent)
278      */

279     public boolean validate(IDocument document, int offset, DocumentEvent event) {
280         return false;
281     }
282
283     /* (non-Javadoc)
284      * @see org.eclipse.jdt.internal.ui.text.correction.IShortcutProposal#getProposalId()
285      */

286     public String JavaDoc getCommandId() {
287         return ASSIST_ID;
288     }
289
290     public void setRelevance(int relevance) {
291         fRelevance= relevance;
292     }
293
294 }
295
Popular Tags