1 11 package org.eclipse.jdt.internal.ui.text.correction; 12 13 import java.util.Arrays ; 14 import java.util.Comparator ; 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 58 public class LinkedNamesAssistProposal implements IJavaCompletionProposal, ICompletionProposalExtension2, ICommandAccess { 59 60 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 event.doit= false; 81 } 82 } else { 83 if (offset + 1 > position.getOffset() + position.getLength()) { 84 event.doit= false; 86 } 87 } 88 } 89 } 90 91 return null; } 93 } 94 95 96 public static final String ASSIST_ID= "org.eclipse.jdt.ui.correction.renameInFile.assist"; 98 private SimpleName fNode; 99 private ICompilationUnit fCompilationUnit; 100 private String fLabel; 101 private String 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 label, ICompilationUnit cu, SimpleName node, String valueSuggestion) { 112 fLabel= label; 113 fNode= node; 114 fCompilationUnit= cu; 115 fValueSuggestion= valueSuggestion; 116 fRelevance= 8; 117 } 118 119 122 public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) { 123 try { 124 Point seletion= viewer.getSelectedRange(); 125 126 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 Arrays.sort(sameNodes, new Comparator () { 141 142 public int compare(Object o1, Object o2) { 143 return rank((ASTNode) o1) - rank((ASTNode) o2); 144 } 145 146 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); 191 } catch (BadLocationException e) { 192 JavaPlugin.log(e); 193 } 194 } 195 196 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 213 public void apply(IDocument document) { 214 } 216 217 220 public Point getSelection(IDocument document) { 221 return null; 222 } 223 224 227 public String getAdditionalProposalInfo() { 228 return CorrectionMessages.LinkedNamesAssistProposal_proposalinfo; 229 } 230 231 234 public String getDisplayString() { 235 String shortCutString= CorrectionCommandHandler.getShortCutString(getCommandId()); 236 if (shortCutString != null) { 237 return Messages.format(CorrectionMessages.ChangeCorrectionProposal_name_with_shortcut, new String [] { fLabel, shortCutString }); 238 } 239 return fLabel; 240 } 241 242 245 public Image getImage() { 246 return JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LINKED_RENAME); 247 } 248 249 252 public IContextInformation getContextInformation() { 253 return null; 254 } 255 256 259 public int getRelevance() { 260 return fRelevance; 261 } 262 263 264 267 public void selected(ITextViewer textViewer, boolean smartToggle) { 268 } 269 270 273 public void unselected(ITextViewer textViewer) { 274 } 275 276 279 public boolean validate(IDocument document, int offset, DocumentEvent event) { 280 return false; 281 } 282 283 286 public String getCommandId() { 287 return ASSIST_ID; 288 } 289 290 public void setRelevance(int relevance) { 291 fRelevance= relevance; 292 } 293 294 } 295 | Popular Tags |