KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > actions > RenameInFileAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
12 package org.eclipse.ant.internal.ui.editor.actions;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
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")); //$NON-NLS-1$
41
setDescription(AntEditorActionMessages.getString("RenameInFileAction.1")); //$NON-NLS-1$
42
setToolTipText(AntEditorActionMessages.getString("RenameInFileAction.2")); //$NON-NLS-1$
43
}
44     
45     /* (non-Javadoc)
46      * @see org.eclipse.jface.action.IAction#run()
47      */

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 JavaDoc 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 JavaDoc positions, IDocument document, LinkedPositionGroup group) {
83         Iterator JavaDoc 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     /* (non-Javadoc)
110      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.text.ITextSelection)
111      */

112     public void selectionChanged(ITextSelection selection) {
113         setEnabled(fEditor != null);
114     }
115
116     /**
117      * Set the Ant editor associated with the action
118      * @param editor the ant editor to do the renames
119      */

120     public void setEditor(AntEditor editor) {
121         fEditor= editor;
122     }
123 }
Popular Tags