KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > retouche > InstantRenameAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.editor.retouche;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.swing.text.BadLocationException JavaDoc;
29 import javax.swing.text.Document JavaDoc;
30 import javax.swing.text.JTextComponent JavaDoc;
31
32 import org.netbeans.api.gsf.InstantRenamer;
33 import org.netbeans.api.gsf.OffsetRange;
34 import org.netbeans.api.gsf.CancellableTask;
35 import org.netbeans.api.gsf.ColoringAttributes;
36 import org.netbeans.api.retouche.source.CompilationController;
37 import org.netbeans.api.retouche.source.Phase;
38 import org.netbeans.api.retouche.source.Source;
39 import org.netbeans.editor.BaseAction;
40 import org.netbeans.editor.Utilities;
41 import org.netbeans.modules.editor.highlights.spi.Highlight;
42 import org.netbeans.modules.gsf.Language;
43 import org.netbeans.modules.gsf.LanguageRegistry;
44 import org.openide.DialogDisplayer;
45 import org.openide.ErrorManager;
46 import org.openide.NotifyDescriptor;
47 import org.openide.loaders.DataObject;
48 import org.openide.util.RequestProcessor;
49
50
51 /**
52  * This file is originally from Retouche, the Java Support
53  * infrastructure in NetBeans. I have modified the file as little
54  * as possible to make merging Retouche fixes back as simple as
55  * possible.
56  *
57  *
58  * @author Jan Lahoda
59  * @author Tor Norbye
60  */

61 public class InstantRenameAction extends BaseAction {
62     /** Creates a new instance of InstantRenameAction */
63     public InstantRenameAction() {
64         super("in-place-refactoring",
65             ABBREV_RESET | MAGIC_POSITION_RESET | UNDO_MERGE_RESET | SAVE_POSITION);
66     }
67
68     public void actionPerformed(ActionEvent JavaDoc evt, final JTextComponent JavaDoc target) {
69         try {
70             final int caret = target.getCaretPosition();
71             String JavaDoc ident = Utilities.getIdentifier(Utilities.getDocument(target), caret);
72
73             if (ident == null) {
74                 Utilities.setStatusBoldText(target, "Cannot perform instant rename here.");
75
76                 return;
77             }
78
79             DataObject od =
80                 (DataObject)target.getDocument().getProperty(Document.StreamDescriptionProperty);
81             Source js = Source.forFileObject(od.getPrimaryFile());
82             final boolean[] wasResolved = new boolean[1];
83             final Set JavaDoc<Highlight>[] changePoints = new Set JavaDoc[1];
84
85             js.runUserActionTask(new CancellableTask<CompilationController>() {
86                     public void cancel() {
87                     }
88
89                     public void run(CompilationController controller)
90                         throws Exception JavaDoc {
91                         if (controller.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) {
92                             return;
93                         }
94
95                         Document JavaDoc doc = target.getDocument();
96                         Language language =
97                             LanguageRegistry.getInstance()
98                                             .getLanguageByMimeType(controller.getFileObject()
99                                                                              .getMIMEType());
100
101                         if (language != null) {
102                             InstantRenamer renamer = language.getInstantRenamer();
103
104                             if ((renamer == null) || !renamer.isRenameAllowed(controller, caret)) {
105                                 wasResolved[0] = false;
106
107                                 return;
108                             }
109
110                             wasResolved[0] = true;
111
112                             Set JavaDoc<OffsetRange> regions = renamer.getRenameRegions(controller, caret);
113
114                             if ((regions != null) && (regions.size() > 0)) {
115                                 Set JavaDoc<Highlight> highlights = new HashSet JavaDoc(regions.size() * 2);
116
117                                 for (OffsetRange range : regions) {
118                                     //ColoringAttributes colors = highlights.get(range);
119
Collection JavaDoc<ColoringAttributes> c =
120                                         Collections.singletonList(ColoringAttributes.LOCAL_VARIABLE);
121                                     Highlight h =
122                                         org.netbeans.modules.retouche.editor.semantic.Utilities.createHighlight(doc,
123                                             range.getStart(), range.getEnd(), c, null);
124
125                                     if (h != null) {
126                                         highlights.add(h);
127                                     }
128                                 }
129
130                                 changePoints[0] = highlights;
131                             }
132                         }
133                     }
134                 }, true);
135
136             if (wasResolved[0]) {
137                 if (changePoints[0] != null) {
138                     doInstantRename(changePoints[0], target, caret, ident);
139                 } else {
140                     doFullRename();
141                 }
142             } else {
143                 Utilities.setStatusBoldText(target, "Cannot perform instant rename here.");
144             }
145         } catch (BadLocationException JavaDoc e) {
146             ErrorManager.getDefault().notify(e);
147         } catch (IOException JavaDoc ioe) {
148             ErrorManager.getDefault().notify(ioe);
149         }
150     }
151
152     @Override JavaDoc
153     protected Class JavaDoc getShortDescriptionBundleClass() {
154         return InstantRenameAction.class;
155     }
156     
157     private void doInstantRename(Set JavaDoc<Highlight> changePoints, JTextComponent JavaDoc target, int caret,
158         String JavaDoc ident) throws BadLocationException JavaDoc {
159         InstantRenamePerformer.performInstantRename(target, changePoints, caret);
160     }
161
162     private void doFullRename() {
163         final NotifyDescriptor nd =
164             new NotifyDescriptor.Message(
165                 "Placeholder: Cannot perform instant rename, starting full rename instead");
166
167         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
168                 public void run() {
169                     DialogDisplayer.getDefault().notify(nd);
170                 }
171             });
172     }
173 }
174
Popular Tags