KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > rename > InstantRenamePerformer


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.java.editor.rename;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.event.KeyEvent JavaDoc;
23 import java.awt.event.KeyListener JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.swing.event.CaretEvent JavaDoc;
29 import javax.swing.event.DocumentEvent JavaDoc;
30 import javax.swing.event.DocumentListener JavaDoc;
31 import javax.swing.text.BadLocationException JavaDoc;
32 import javax.swing.text.Document JavaDoc;
33 import javax.swing.text.JTextComponent JavaDoc;
34 import javax.swing.text.Position JavaDoc;
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 /**
49  *
50  * @author Jan Lahoda
51  */

52 public class InstantRenamePerformer implements DocumentListener JavaDoc, KeyListener JavaDoc {
53     
54     private SyncDocumentRegion region;
55     private Document JavaDoc doc;
56     private JTextComponent JavaDoc target;
57     private MutablePositionRegion mainRegion;
58     
59     /** Creates a new instance of InstantRenamePerformer */
60     private InstantRenamePerformer(JTextComponent JavaDoc target, Set JavaDoc<Highlight> highlights, int caretOffset) throws BadLocationException JavaDoc {
61     this.target = target;
62     doc = target.getDocument();
63     
64     MutablePositionRegion mainRegion = null;
65     List JavaDoc<MutablePositionRegion> regions = new ArrayList JavaDoc<MutablePositionRegion>();
66     List JavaDoc<Highlight> newHighlights = new ArrayList JavaDoc<Highlight>();
67     
68     for (Highlight h : highlights) {
69         Position JavaDoc start = NbDocument.createPosition(doc, h.getStart(), Bias.Backward);
70         Position JavaDoc 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 JavaDoc("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 JavaDoc target, Set JavaDoc<Highlight> highlights, int caretOffset) throws BadLocationException JavaDoc {
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 JavaDoc 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 JavaDoc e) {
133     if (inSync)
134         return ;
135     
136         //#89997: do not sync the regions for the "remove" part of replace selection,
137
//as the consequent insert may use incorrect offset, and the regions will be synced
138
//after the insert anyway.
139
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 JavaDoc e) {
150     }
151
152     public void caretUpdate(CaretEvent JavaDoc e) {
153     }
154
155     public void keyTyped(KeyEvent JavaDoc e) {
156     }
157
158     public void keyPressed(KeyEvent JavaDoc 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 JavaDoc 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 JavaDoc(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