KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > text > NbLikeEditorKit


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
20
21 package org.openide.text;
22
23 import java.beans.VetoableChangeListener JavaDoc;
24 import javax.swing.text.*;
25 import javax.swing.undo.UndoManager JavaDoc;
26
27 /**
28  * Emulates the behaviour of NetBeans editor's kit with all its special
29  * implementations.
30  *
31  * @author Jaroslav Tulach
32  */

33 class NbLikeEditorKit extends DefaultEditorKit {
34     public javax.swing.text.Document JavaDoc createDefaultDocument() {
35         return new Doc ();
36     }
37
38     class Doc extends PlainDocument
39     implements NbDocument.WriteLockable, StyledDocument {
40 // implements NbDocument.PositionBiasable, NbDocument.WriteLockable,
41
// NbDocument.Printable, NbDocument.CustomEditor, NbDocument.CustomToolbar, NbDocument.Annotatable {
42

43         public Doc() {
44             super (new StringContent ());
45             
46             // mark yourself of supporting modificationListener
47
putProperty ("supportsModificationListener", Boolean.TRUE);
48         }
49
50         public void runAtomic (Runnable JavaDoc r) {
51             try {
52                 runAtomicAsUser (r);
53             } catch (BadLocationException ex) {
54                 // too bad, no modification allowed
55
}
56         }
57
58         public void runAtomicAsUser (Runnable JavaDoc r) throws BadLocationException {
59              insOrRemoveOrRunnable (-1, null, null, -1, false, r);
60         }
61
62         public javax.swing.text.Style JavaDoc getLogicalStyle(int p) {
63             return null;
64         }
65
66         public javax.swing.text.Style JavaDoc getStyle(java.lang.String JavaDoc nm) {
67             return null;
68         }
69
70         public javax.swing.text.Style JavaDoc addStyle(java.lang.String JavaDoc nm, javax.swing.text.Style JavaDoc parent) {
71             return null;
72         }
73
74         public void setParagraphAttributes(int offset, int length, javax.swing.text.AttributeSet JavaDoc s, boolean replace) {
75         }
76
77         public void setCharacterAttributes(int offset, int length, javax.swing.text.AttributeSet JavaDoc s, boolean replace) {
78         }
79
80         public void removeStyle(java.lang.String JavaDoc nm) {
81         }
82
83         public java.awt.Font JavaDoc getFont(javax.swing.text.AttributeSet JavaDoc attr) {
84             return null;
85         }
86
87         public java.awt.Color JavaDoc getBackground(javax.swing.text.AttributeSet JavaDoc attr) {
88             return null;
89         }
90
91         public javax.swing.text.Element JavaDoc getCharacterElement(int pos) {
92             return null;
93         }
94
95         public void setLogicalStyle(int pos, javax.swing.text.Style JavaDoc s) {
96         }
97
98         public java.awt.Color JavaDoc getForeground(javax.swing.text.AttributeSet JavaDoc attr) {
99             return null;
100         }
101
102         private int changes;
103         public void insertString (int offs, String JavaDoc str, AttributeSet a) throws BadLocationException {
104             insOrRemoveOrRunnable (offs, str, a, 0, true, null);
105         }
106
107         public void remove (int offs, int len) throws BadLocationException {
108             insOrRemoveOrRunnable (offs, null, null, len, false, null);
109         }
110         
111         
112         private void insOrRemoveOrRunnable (int offset, String JavaDoc str, AttributeSet set, int len, boolean insert, Runnable JavaDoc run)
113         throws BadLocationException {
114             boolean alreadyInsideWrite = getCurrentWriter () == Thread.currentThread ();
115             if (alreadyInsideWrite) {
116                 if (run != null) {
117                     run.run ();
118                 } else {
119                     assertOffset (offset);
120                     if (insert) {
121                         super.insertString (offset, str, set);
122                     } else {
123                         super.remove(offset, len);
124                     }
125                 }
126                 return;
127             }
128             
129             Object JavaDoc o = getProperty ("modificationListener");
130             
131             if (run != null) {
132                 writeLock ();
133                 int prevChanges = changes;
134                 try {
135                     run.run ();
136                 } finally {
137                     writeUnlock ();
138                 }
139                 if (changes > prevChanges) {
140                     try {
141                         notifyModified (o, offset);
142                     } catch (BadLocationException ex) {
143                         // ok, too bad, just ignore
144
}
145                 }
146             } else {
147                 assertOffset (offset);
148                 notifyModified (o, offset);
149                 try {
150                     if (insert) {
151                         super.insertString (offset, str, set);
152                     } else {
153                         super.remove(offset, len);
154                     }
155                 } catch (BadLocationException ex) {
156                     if (o instanceof VetoableChangeListener JavaDoc) {
157                         VetoableChangeListener JavaDoc l = (VetoableChangeListener JavaDoc)o;
158                         try {
159                             l.vetoableChange (new java.beans.PropertyChangeEvent JavaDoc (this, "modified", null, Boolean.FALSE));
160                         } catch (java.beans.PropertyVetoException JavaDoc ignore) {
161                         }
162                     }
163                     throw ex;
164                 }
165             }
166         }
167         
168         private void assertOffset (int offset) throws BadLocationException {
169             if (offset < 0) throw new BadLocationException ("", offset);
170         }
171         
172         private void notifyModified (Object JavaDoc o, int offset) throws BadLocationException {
173             if (o instanceof VetoableChangeListener JavaDoc) {
174                 VetoableChangeListener JavaDoc l = (VetoableChangeListener JavaDoc)o;
175                 try {
176                     l.vetoableChange (new java.beans.PropertyChangeEvent JavaDoc (this, "modified", null, Boolean.TRUE));
177                 } catch (java.beans.PropertyVetoException JavaDoc ex) {
178                     throw new BadLocationException("Document modification vetoed", offset);
179                 }
180             }
181         }
182
183         protected void fireRemoveUpdate (javax.swing.event.DocumentEvent JavaDoc e) {
184             super.fireRemoveUpdate(e);
185             changes++;
186         }
187
188         protected void fireInsertUpdate (javax.swing.event.DocumentEvent JavaDoc e) {
189             super.fireInsertUpdate(e);
190             changes++;
191         }
192
193         protected void fireChangedUpdate (javax.swing.event.DocumentEvent JavaDoc e) {
194             super.fireChangedUpdate(e);
195             changes++;
196         }
197
198     } // end of Doc
199
}
200
Popular Tags