001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.xml.text;
042:
043: import java.io.ObjectInput;
044: import java.io.ObjectOutput;
045: import java.io.IOException;
046: import java.awt.event.FocusEvent;
047: import java.awt.event.FocusListener;
048: import javax.swing.event.CaretEvent;
049: import javax.swing.event.CaretListener;
050:
051: import org.openide.windows.Workspace;
052: import org.openide.windows.Mode;
053: import org.openide.windows.WindowManager;
054: import org.openide.text.CloneableEditor;
055: import org.openide.text.CloneableEditorSupport;
056:
057: /**
058: * CloneableEditor subclass taking care about listening and delegating these event to
059: * support.
060: * <p>
061: * Listens at: DataObject, caret
062: *
063: * @author Libor Kramolis
064: * @version 0.1
065: */
066: public class TextEditorComponent extends CloneableEditor {
067:
068: /** Serial Version UID */
069: private static final long serialVersionUID = 5983822115073046891L;
070:
071: /** The support, subclass of EditorSupport */
072: private TextEditorSupport support;
073:
074: /** Listener on caret movements */
075: private CaretListener caretListener;
076:
077: //
078: // init
079: //
080:
081: /** Only for externalization */
082: public TextEditorComponent() {
083: super ();
084: }
085:
086: /** Creates new editor */
087: public TextEditorComponent(TextEditorSupport editor) {
088: super (editor);
089: initialize();
090: dockIntoEditorMode();
091: }
092:
093: //
094: // itself
095: //
096:
097: // // called from inner class -- to be 1.2 compiler happy
098: // protected void updateName () {
099: // super.updateName();
100: // }
101:
102: /** Obtain a support for this component */
103: private void initialize() {
104:
105: //#25368 there is memory leak in window system for default persistence type
106: // anyway remember position only for opened components
107: putClientProperty("PersistenceType", "OnlyOpened"); // NOI18N
108:
109: support = (TextEditorSupport) cloneableEditorSupport(); //(TextEditorSupport)obj.getCookie (TextEditorSupport.class);
110: caretListener = new CaretListener() {
111: public void caretUpdate(CaretEvent e) {
112: support.restartTimer(true);
113: }
114: };
115: if (pane != null) { //??? what if does not exist
116: pane.addFocusListener(new FocusListener() {
117: public void focusGained(FocusEvent e) {
118: }
119:
120: public void focusLost(FocusEvent e) {
121: support.syncDocument(true);
122: }
123: });
124: }
125: // support.getDataObject().addPropertyChangeListener (new PropertyChangeListener () {
126: // public void propertyChange (PropertyChangeEvent ev) {
127: // if (DataObject.PROP_NAME.equals (ev.getPropertyName())) {
128: // TextEditorComponent.this.updateName();
129: // }
130: // }
131: // });
132: }
133:
134: /**
135: */
136: private void dockIntoEditorMode() {
137: // dock into editor mode if possible
138: Workspace current = WindowManager.getDefault()
139: .getCurrentWorkspace();
140: Mode editorMode = current
141: .findMode(CloneableEditorSupport.EDITOR_MODE);
142: if (editorMode != null) {
143: editorMode.dockInto(this );
144: }
145: }
146:
147: // /** Returns Editor pane for private use.
148: // * @return Editor pane for private use.
149: // */
150: // private JEditorPane getEditorPane () {
151: // return pane;
152: // }
153:
154: // /* Is called from the clone method to create new component from this one.
155: // * This implementation only clones the object by calling super.clone method.
156: // * @return the copy of this object
157: // */
158: // protected CloneableTopComponent createClonedObject () {
159: // return support.createTextEditorComponent();
160: // }
161:
162: /* This method is called when parent window of this component has focus,
163: * and this component is preferred one in it. This implementation adds
164: * performer to the ToggleBreakpointAction.
165: */
166: protected void componentActivated() {
167: pane.addCaretListener(caretListener);
168: super .componentActivated();
169: }
170:
171: /*
172: * This method is called when parent window of this component losts focus,
173: * or when this component losts preferrence in the parent window. This
174: * implementation removes performer from the ToggleBreakpointAction.
175: */
176: protected void componentDeactivated() {
177: pane.removeCaretListener(caretListener);
178: super .componentDeactivated();
179: }
180:
181: /** Deserialize this top component.
182: * @param in the stream to deserialize from
183: */
184: public void readExternal(ObjectInput in) throws IOException,
185: ClassNotFoundException {
186: if (Util.THIS.isLoggable()) /* then */
187: Util.THIS.debug("TextEditorComponent.readExternal()"); // NOI18N
188:
189: super .readExternal(in);
190:
191: if (Util.THIS.isLoggable()) /* then */
192: Util.THIS
193: .debug(" .readExternal(): support = "
194: + cloneableEditorSupport()); // NOI18N
195:
196: initialize();
197:
198: if (Util.THIS.isLoggable()) /* then */
199: Util.THIS.debug("\tdone."); // NOI18N
200: }
201:
202: /** Serialize this top component.
203: * Subclasses wishing to store state must call the super method, then write to the stream.
204: * @param out the stream to serialize to
205: */
206: public void writeExternal(ObjectOutput out) throws IOException {
207: if (Util.THIS.isLoggable()) /* then */
208: Util.THIS
209: .debug("TextEditorComponent.writeExternal(): support = "
210: + cloneableEditorSupport()); // NOI18N
211:
212: super .writeExternal(out);
213:
214: if (Util.THIS.isLoggable()) /* then */
215: Util.THIS.debug("\tdone."); // NOI18N
216: }
217:
218: }
|