1 19 package org.netbeans.modules.java.editor.rename; 20 21 import java.awt.Color ; 22 import java.awt.event.KeyEvent ; 23 import java.awt.event.KeyListener ; 24 import java.util.ArrayList ; 25 import java.util.Collections ; 26 import java.util.List ; 27 import java.util.Set ; 28 import javax.swing.event.CaretEvent ; 29 import javax.swing.event.DocumentEvent ; 30 import javax.swing.event.DocumentListener ; 31 import javax.swing.text.BadLocationException ; 32 import javax.swing.text.Document ; 33 import javax.swing.text.JTextComponent ; 34 import javax.swing.text.Position ; 35 import javax.swing.text.Position.Bias; 36 import org.netbeans.editor.BaseDocument; 37 import org.netbeans.editor.BaseKit; 38 import org.netbeans.editor.Coloring; 39 import org.netbeans.lib.editor.codetemplates.SyncDocumentRegion; 40 import org.netbeans.lib.editor.util.swing.MutablePositionRegion; 41 import org.netbeans.lib.editor.util.swing.PositionRegion; 42 import org.netbeans.modules.editor.highlights.spi.Highlight; 43 import org.netbeans.modules.editor.highlights.spi.Highlighter; 44 import org.openide.filesystems.FileObject; 45 import org.openide.loaders.DataObject; 46 import org.openide.text.NbDocument; 47 48 52 public class InstantRenamePerformer implements DocumentListener , KeyListener { 53 54 private SyncDocumentRegion region; 55 private Document doc; 56 private JTextComponent target; 57 private MutablePositionRegion mainRegion; 58 59 60 private InstantRenamePerformer(JTextComponent target, Set <Highlight> highlights, int caretOffset) throws BadLocationException { 61 this.target = target; 62 doc = target.getDocument(); 63 64 MutablePositionRegion mainRegion = null; 65 List <MutablePositionRegion> regions = new ArrayList <MutablePositionRegion>(); 66 List <Highlight> newHighlights = new ArrayList <Highlight>(); 67 68 for (Highlight h : highlights) { 69 Position start = NbDocument.createPosition(doc, h.getStart(), Bias.Backward); 70 Position end = NbDocument.createPosition(doc, h.getEnd(), Bias.Forward); 71 MutablePositionRegion current = new MutablePositionRegion(start, end); 72 73 if (isIn(current, caretOffset)) { 74 mainRegion = current; 75 } else { 76 regions.add(current); 77 } 78 79 newHighlights.add(new RenameHighlight(current)); 80 } 81 82 if (mainRegion == null) { 83 throw new IllegalArgumentException ("No highlight contains the caret."); 84 } 85 86 regions.add(0, mainRegion); 87 88 region = new SyncDocumentRegion(doc, regions); 89 90 if (doc instanceof BaseDocument) { 91 ((BaseDocument) doc).setPostModificationDocumentListener(this); 92 } 93 94 target.addKeyListener(this); 95 96 target.putClientProperty(InstantRenamePerformer.class, this); 97 98 Highlighter.getDefault().setHighlights(getFileObject(), "instant-rename", newHighlights); 99 100 target.select(mainRegion.getStartOffset(), mainRegion.getEndOffset()); 101 } 102 103 private FileObject getFileObject() { 104 DataObject od = (DataObject) doc.getProperty(Document.StreamDescriptionProperty); 105 106 if (od == null) 107 return null; 108 109 return od.getPrimaryFile(); 110 } 111 112 public static void performInstantRename(JTextComponent target, Set <Highlight> highlights, int caretOffset) throws BadLocationException { 113 new InstantRenamePerformer(target, highlights, caretOffset); 114 } 115 116 private boolean isIn(MutablePositionRegion region, int caretOffset) { 117 return region.getStartOffset() <= caretOffset && caretOffset <= region.getEndOffset(); 118 } 119 120 private boolean inSync; 121 122 public synchronized void insertUpdate(DocumentEvent e) { 123 if (inSync) 124 return ; 125 126 inSync = true; 127 region.sync(0); 128 inSync = false; 129 target.repaint(); 130 } 131 132 public synchronized void removeUpdate(DocumentEvent e) { 133 if (inSync) 134 return ; 135 136 if (doc.getProperty(BaseKit.DOC_REPLACE_SELECTION_PROPERTY) != null) { 140 return ; 141 } 142 143 inSync = true; 144 region.sync(0); 145 inSync = false; 146 target.repaint(); 147 } 148 149 public void changedUpdate(DocumentEvent e) { 150 } 151 152 public void caretUpdate(CaretEvent e) { 153 } 154 155 public void keyTyped(KeyEvent e) { 156 } 157 158 public void keyPressed(KeyEvent e) { 159 if ( (e.getKeyCode() == KeyEvent.VK_ESCAPE && e.getModifiers() == 0) 160 || (e.getKeyCode() == KeyEvent.VK_ENTER && e.getModifiers() == 0)) { 161 release(); 162 e.consume(); 163 } 164 } 165 166 public void keyReleased(KeyEvent e) { 167 } 168 169 private void release() { 170 target.putClientProperty(InstantRenamePerformer.class, null); 171 if (doc instanceof BaseDocument) { 172 ((BaseDocument) doc).setPostModificationDocumentListener(null); 173 } 174 target.removeKeyListener(this); 175 Highlighter.getDefault().setHighlights(getFileObject(), "instant-rename", Collections.emptyList()); 176 177 region = null; 178 doc = null; 179 target = null; 180 mainRegion = null; 181 } 182 183 private static final class RenameHighlight implements Highlight { 184 185 private PositionRegion region; 186 private static final Coloring COLORING = new Coloring(null, null, new Color (138, 191, 236)); 187 188 public RenameHighlight(PositionRegion region) { 189 this.region = region; 190 } 191 192 public int getStart() { 193 return region.getStartOffset(); 194 } 195 196 public int getEnd() { 197 return region.getEndOffset(); 198 } 199 200 public Coloring getColoring() { 201 return COLORING; 202 } 203 } 204 205 } 206 | Popular Tags |