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.awt.Point;
044: import java.awt.Component;
045:
046: import javax.swing.JEditorPane;
047: import javax.swing.JViewport;
048: import javax.swing.text.StyledDocument;
049:
050: import org.openide.*;
051: import org.openide.text.*;
052: import org.openide.nodes.*;
053: import org.openide.cookies.*;
054:
055: import org.netbeans.modules.xml.*;
056: import org.netbeans.modules.xml.sync.*;
057:
058: /**
059: * Takes care about specifics of XML test representations.
060: * It covers update() method.
061: *
062: * @author Petr Kuzel
063: * @version
064: */
065: public class XMLTextRepresentation extends TextRepresentation {
066:
067: /** Creates new XMLTextRepresentation */
068: public XMLTextRepresentation(TextEditorSupport editor,
069: Synchronizator sync) {
070: super (editor, sync);
071: }
072:
073: /*
074: * Retrives editor cookie and perform text replacing holding caret and view at original
075: * position if possible.
076: *
077: */
078: public void updateText(Object input) {
079: if (Util.THIS.isLoggable()) /* then */
080: Util.THIS.debug("XMLTextRepresentation::updateText");//, new RuntimeException ("Updating text...........")); // NOI18N
081:
082: final String in = (String) input;
083:
084: try {
085:
086: EditorCookie es = editor;
087: if (es != null) {
088:
089: StyledDocument tmpdoc = es.getDocument();
090: if (tmpdoc == null)
091: tmpdoc = es.openDocument();
092:
093: //sample editor position
094:
095: JEditorPane[] eps = es.getOpenedPanes();
096: JEditorPane pane = null;
097: JViewport port = null;
098: int caretPosition = 0;
099: Point viewPosition = null;
100: if (eps != null) {
101: pane = eps[0];
102: caretPosition = pane.getCaretPosition();
103: port = getParentViewport(pane);
104: if (port != null)
105: viewPosition = port.getViewPosition();
106: }
107:
108: // prepare modification task
109:
110: final Exception[] taskEx = new Exception[] { null };
111: final StyledDocument sdoc = tmpdoc;
112:
113: Runnable task = new Runnable() {
114: public void run() {
115: try {
116: sdoc.remove(0, sdoc.getLength()); // right alternative
117:
118: // we are at Unicode level
119: sdoc.insertString(0, in, null);
120: } catch (Exception iex) {
121: taskEx[0] = iex;
122: }
123: }
124: };
125:
126: // perform document modification
127:
128: org.openide.text.NbDocument.runAtomicAsUser(sdoc, task);
129:
130: //??? setModified (true);
131:
132: //restore editor position
133:
134: if (eps != null) {
135: try {
136: pane.setCaretPosition(caretPosition);
137: } catch (IllegalArgumentException e) {
138: }
139: port.setViewPosition(viewPosition);
140: }
141:
142: if (taskEx[0] != null) {
143: throw taskEx[0];
144: }
145:
146: } //es!=null
147:
148: } catch (Exception e) {
149: ErrorManager.getDefault().notify(e);
150: }
151: }
152:
153: /**
154: * Update the representation without marking it as modified.
155: */
156: public void update(Object change) {
157: if (change instanceof String) {
158: String update = (String) change;
159: updateText(update);
160: }
161: }
162:
163: /**
164: * Is this representation modified since last sync?
165: */
166: public boolean isModified() {
167: return false; //!!! es.isModified();
168: }
169:
170: private JViewport getParentViewport(JEditorPane component) {
171: Component pc = component.getParent();
172: return (pc instanceof JViewport) ? (JViewport) pc : null;
173: }
174:
175: }
|