KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Point JavaDoc;
22 import java.awt.Component JavaDoc;
23
24 import javax.swing.JEditorPane JavaDoc;
25 import javax.swing.JViewport JavaDoc;
26 import javax.swing.text.StyledDocument JavaDoc;
27
28 import org.openide.*;
29 import org.openide.text.*;
30 import org.openide.nodes.*;
31 import org.openide.cookies.*;
32
33 import org.netbeans.modules.xml.core.*;
34 import org.netbeans.modules.xml.core.sync.*;
35
36 /**
37  * Takes care about specifics of XML test representations.
38  * It covers update() method.
39  *
40  * @author Petr Kuzel
41  * @version
42  */

43 public class XMLTextRepresentation extends TextRepresentation {
44
45     /** Creates new XMLTextRepresentation */
46     public XMLTextRepresentation(TextEditorSupport editor, Synchronizator sync) {
47         super(editor, sync);
48     }
49
50
51     /*
52      * Retrives editor cookie and perform text replacing holding caret and view at original
53      * position if possible.
54      *
55      */

56     public void updateText (Object JavaDoc input) {
57         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("XMLTextRepresentation::updateText");//, new RuntimeException ("Updating text...........")); // NOI18N
58

59         final String JavaDoc in = (String JavaDoc) input;
60         
61         try {
62
63             EditorCookie es = editor;
64             if (es != null) {
65
66                 StyledDocument JavaDoc tmpdoc = es.getDocument();
67                 if (tmpdoc == null)
68                     tmpdoc = es.openDocument();
69
70                 //sample editor position
71

72                 JEditorPane JavaDoc[] eps = es.getOpenedPanes();
73                 JEditorPane JavaDoc pane = null;
74                 JViewport JavaDoc port = null;
75                 int caretPosition = 0;
76                 Point JavaDoc viewPosition = null;
77                 if (eps != null) {
78                     pane = eps[0];
79                     caretPosition = pane.getCaretPosition();
80                     port = getParentViewport (pane);
81                     if (port != null)
82                         viewPosition = port.getViewPosition();
83                 }
84
85                 // prepare modification task
86

87                 final Exception JavaDoc[] taskEx = new Exception JavaDoc[] {null};
88                 final StyledDocument JavaDoc sdoc = tmpdoc;
89
90                 Runnable JavaDoc task = new Runnable JavaDoc() {
91                     public void run() {
92                         try {
93                             sdoc.remove (0, sdoc.getLength()); // right alternative
94

95                             // we are at Unicode level
96
sdoc.insertString (0, in, null);
97                         } catch (Exception JavaDoc iex) {
98                             taskEx[0] = iex;
99                         }
100                     }
101                 };
102
103                 // perform document modification
104

105                 org.openide.text.NbDocument.runAtomicAsUser(sdoc, task);
106
107                 //??? setModified (true);
108

109                 //restore editor position
110

111                 if (eps != null) {
112                     try {
113                         pane.setCaretPosition (caretPosition);
114                     } catch (IllegalArgumentException JavaDoc e) {
115                     }
116                     port.setViewPosition (viewPosition);
117                 }
118
119                 if (taskEx[0]!=null) {
120                     throw taskEx[0];
121                 }
122
123             } //es!=null
124

125         } catch (Exception JavaDoc e) {
126             ErrorManager.getDefault().notify(e);
127         }
128     }
129     
130     
131     /**
132      * Update the representation without marking it as modified.
133      */

134     public void update(Object JavaDoc change) {
135         if (change instanceof String JavaDoc) {
136             String JavaDoc update = (String JavaDoc) change;
137             updateText(update);
138         }
139     }
140
141     /**
142      * Is this representation modified since last sync?
143      */

144     public boolean isModified() {
145         return false; //!!! es.isModified();
146
}
147
148
149     private JViewport JavaDoc getParentViewport (JEditorPane JavaDoc component) {
150         Component JavaDoc pc = component.getParent();
151         return (pc instanceof JViewport JavaDoc) ? (JViewport JavaDoc)pc : null;
152     }
153     
154 }
155
Popular Tags