1 19 package org.netbeans.modules.editor.retouche; 20 21 import java.awt.event.ActionEvent ; 22 import java.io.IOException ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.Set ; 27 28 import javax.swing.text.BadLocationException ; 29 import javax.swing.text.Document ; 30 import javax.swing.text.JTextComponent ; 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 61 public class InstantRenameAction extends BaseAction { 62 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 evt, final JTextComponent target) { 69 try { 70 final int caret = target.getCaretPosition(); 71 String 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 <Highlight>[] changePoints = new Set [1]; 84 85 js.runUserActionTask(new CancellableTask<CompilationController>() { 86 public void cancel() { 87 } 88 89 public void run(CompilationController controller) 90 throws Exception { 91 if (controller.toPhase(Phase.RESOLVED).compareTo(Phase.RESOLVED) < 0) { 92 return; 93 } 94 95 Document 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 <OffsetRange> regions = renamer.getRenameRegions(controller, caret); 113 114 if ((regions != null) && (regions.size() > 0)) { 115 Set <Highlight> highlights = new HashSet (regions.size() * 2); 116 117 for (OffsetRange range : regions) { 118 Collection <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 e) { 146 ErrorManager.getDefault().notify(e); 147 } catch (IOException ioe) { 148 ErrorManager.getDefault().notify(ioe); 149 } 150 } 151 152 @Override 153 protected Class getShortDescriptionBundleClass() { 154 return InstantRenameAction.class; 155 } 156 157 private void doInstantRename(Set <Highlight> changePoints, JTextComponent target, int caret, 158 String ident) throws BadLocationException { 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 () { 168 public void run() { 169 DialogDisplayer.getDefault().notify(nd); 170 } 171 }); 172 } 173 } 174 | Popular Tags |