KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > text > TextEditorComponent


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.xml.core.text;
20
21 import java.io.ObjectInput JavaDoc;
22 import java.io.ObjectOutput JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.awt.event.FocusEvent JavaDoc;
25 import java.awt.event.FocusListener JavaDoc;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import javax.swing.JEditorPane JavaDoc;
29 import javax.swing.event.CaretEvent JavaDoc;
30 import javax.swing.event.CaretListener JavaDoc;
31
32
33 import org.openide.loaders.DataObject;
34 import org.openide.windows.CloneableTopComponent;
35 import org.openide.windows.Workspace;
36 import org.openide.windows.Mode;
37 import org.openide.windows.WindowManager;
38 import org.openide.text.CloneableEditor;
39 import org.openide.text.CloneableEditorSupport;
40
41 /**
42  * CloneableEditor subclass taking care about listening and delegating these event to
43  * support.
44  * <p>
45  * Listens at: DataObject, caret
46  *
47  * @author Libor Kramolis
48  * @version 0.1
49  */

50 public class TextEditorComponent extends CloneableEditor {
51
52     /** Serial Version UID */
53     private static final long serialVersionUID =5983822115073046891L;
54
55     /** The support, subclass of EditorSupport */
56     private TextEditorSupport support;
57     
58     /** Listener on caret movements */
59     private CaretListener JavaDoc caretListener;
60
61
62     //
63
// init
64
//
65

66     /** Only for externalization */
67     public TextEditorComponent () {
68         super();
69     }
70
71     /** Creates new editor */
72     public TextEditorComponent (TextEditorSupport editor) {
73         super (editor);
74         initialize();
75         dockIntoEditorMode();
76     }
77
78     
79     //
80
// itself
81
//
82

83 // // called from inner class -- to be 1.2 compiler happy
84
// protected void updateName () {
85
// super.updateName();
86
// }
87

88     /** Obtain a support for this component */
89     private void initialize () {
90         
91         //#25368 there is memory leak in window system for default persistence type
92
// anyway remember position only for opened components
93
putClientProperty("PersistenceType", "OnlyOpened"); // NOI18N
94

95         support = (TextEditorSupport)cloneableEditorSupport(); //(TextEditorSupport)obj.getCookie (TextEditorSupport.class);
96
caretListener = new CaretListener JavaDoc() {
97                 public void caretUpdate (CaretEvent JavaDoc e) {
98                     support.restartTimer (true);
99                 }
100             };
101         if (pane != null) { //??? what if does not exist
102
pane.addFocusListener (new FocusListener JavaDoc () {
103                     public void focusGained (FocusEvent JavaDoc e) {
104                     }
105                     public void focusLost (FocusEvent JavaDoc e) {
106                         support.syncDocument (true);
107                     }
108                 });
109         }
110 // support.getDataObject().addPropertyChangeListener (new PropertyChangeListener () {
111
// public void propertyChange (PropertyChangeEvent ev) {
112
// if (DataObject.PROP_NAME.equals (ev.getPropertyName())) {
113
// TextEditorComponent.this.updateName();
114
// }
115
// }
116
// });
117
}
118
119     /**
120      */

121     private void dockIntoEditorMode () {
122     // dock into editor mode if possible
123
Workspace current = WindowManager.getDefault().getCurrentWorkspace();
124         Mode editorMode = current.findMode (CloneableEditorSupport.EDITOR_MODE);
125         if ( editorMode != null ) {
126             editorMode.dockInto (this);
127         }
128     }
129
130 // /** Returns Editor pane for private use.
131
// * @return Editor pane for private use.
132
// */
133
// private JEditorPane getEditorPane () {
134
// return pane;
135
// }
136

137 // /* Is called from the clone method to create new component from this one.
138
// * This implementation only clones the object by calling super.clone method.
139
// * @return the copy of this object
140
// */
141
// protected CloneableTopComponent createClonedObject () {
142
// return support.createTextEditorComponent();
143
// }
144

145
146     /* This method is called when parent window of this component has focus,
147      * and this component is preferred one in it. This implementation adds
148      * performer to the ToggleBreakpointAction.
149      */

150     protected void componentActivated () {
151         pane.addCaretListener (caretListener);
152         super.componentActivated();
153     }
154
155     /*
156      * This method is called when parent window of this component losts focus,
157      * or when this component losts preferrence in the parent window. This
158      * implementation removes performer from the ToggleBreakpointAction.
159      */

160     protected void componentDeactivated () {
161         pane.removeCaretListener (caretListener);
162         super.componentDeactivated();
163     }
164
165     /** Deserialize this top component.
166      * @param in the stream to deserialize from
167      */

168     public void readExternal (ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
169         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TextEditorComponent.readExternal()"); // NOI18N
170

171         super.readExternal (in);
172
173         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug (" .readExternal(): support = " + cloneableEditorSupport()); // NOI18N
174

175         initialize();
176
177         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("\tdone."); // NOI18N
178
}
179     
180     /** Serialize this top component.
181      * Subclasses wishing to store state must call the super method, then write to the stream.
182      * @param out the stream to serialize to
183      */

184     public void writeExternal (ObjectOutput JavaDoc out) throws IOException JavaDoc {
185         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("TextEditorComponent.writeExternal(): support = " + cloneableEditorSupport()); // NOI18N
186

187         super.writeExternal (out);
188
189         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("\tdone."); // NOI18N
190
}
191     
192 }
193
Popular Tags