1 11 12 package org.eclipse.ant.internal.ui.editor.actions; 13 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.ant.internal.ui.AntUIPlugin; 18 import org.eclipse.ant.internal.ui.editor.AntEditor; 19 import org.eclipse.ant.internal.ui.editor.EditorSynchronizer; 20 import org.eclipse.ant.internal.ui.editor.OccurrencesFinder; 21 import org.eclipse.jdt.ui.actions.SelectionDispatchAction; 22 import org.eclipse.jface.text.BadLocationException; 23 import org.eclipse.jface.text.IDocument; 24 import org.eclipse.jface.text.ITextSelection; 25 import org.eclipse.jface.text.Position; 26 import org.eclipse.jface.text.link.LinkedModeModel; 27 import org.eclipse.jface.text.link.LinkedModeUI; 28 import org.eclipse.jface.text.link.LinkedPosition; 29 import org.eclipse.jface.text.link.LinkedPositionGroup; 30 import org.eclipse.jface.text.source.ISourceViewer; 31 import org.eclipse.ui.texteditor.link.EditorLinkedModeUI; 32 33 public class RenameInFileAction extends SelectionDispatchAction { 34 35 private AntEditor fEditor; 36 37 public RenameInFileAction(AntEditor antEditor) { 38 super(antEditor.getSite()); 39 fEditor= antEditor; 40 setText(AntEditorActionMessages.getString("RenameInFileAction.0")); setDescription(AntEditorActionMessages.getString("RenameInFileAction.1")); setToolTipText(AntEditorActionMessages.getString("RenameInFileAction.2")); } 44 45 48 public void run() { 49 if (fEditor == null) { 50 return; 51 } 52 53 ISourceViewer viewer= fEditor.getViewer(); 54 IDocument document= viewer.getDocument(); 55 int offset= ((ITextSelection)getSelection()).getOffset(); 56 LinkedPositionGroup group= new LinkedPositionGroup(); 57 OccurrencesFinder finder= new OccurrencesFinder(fEditor, fEditor.getAntModel(), document, offset); 58 List positions= finder.perform(); 59 if (positions == null) { 60 return; 61 } 62 63 addPositionsToGroup(offset, positions, document, group); 64 if (group.isEmpty()) { 65 return; 66 } 67 try { 68 LinkedModeModel model= new LinkedModeModel(); 69 model.addGroup(group); 70 model.forceInstall(); 71 model.addLinkingListener(new EditorSynchronizer(fEditor)); 72 LinkedModeUI ui= new EditorLinkedModeUI(model, viewer); 73 ui.setExitPosition(viewer, offset, 0, Integer.MAX_VALUE); 74 ui.enter(); 75 viewer.setSelectedRange(offset, 0); 76 } catch (BadLocationException e) { 77 AntUIPlugin.log(e); 78 } 79 } 80 81 82 private void addPositionsToGroup(int offset, List positions, IDocument document, LinkedPositionGroup group) { 83 Iterator iter= positions.iterator(); 84 int i= 0; 85 int j= 0; 86 int firstPosition= -1; 87 try { 88 while (iter.hasNext()) { 89 Position position = (Position) iter.next(); 90 if (firstPosition == -1) { 91 if (position.overlapsWith(offset, 0)) { 92 firstPosition= i; 93 group.addPosition(new LinkedPosition(document, position.getOffset(), position.getLength(), j++)); 94 } 95 } else { 96 group.addPosition(new LinkedPosition(document, position.getOffset(), position.getLength(), j++)); 97 } 98 i++; 99 } 100 101 for (i = 0; i < firstPosition; i++) { 102 Position position= (Position) positions.get(i); 103 group.addPosition(new LinkedPosition(document, position.getOffset(), position.getLength(), j++)); 104 } 105 } catch (BadLocationException be) { 106 AntUIPlugin.log(be); 107 } 108 } 109 112 public void selectionChanged(ITextSelection selection) { 113 setEnabled(fEditor != null); 114 } 115 116 120 public void setEditor(AntEditor editor) { 121 fEditor= editor; 122 } 123 } | Popular Tags |