KickJava   Java API By Example, From Geeks To Geeks.

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


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.Reader JavaDoc;
22 import java.io.StringReader JavaDoc;
23
24 import javax.swing.text.Document JavaDoc;
25
26 import org.xml.sax.*;
27
28 import org.netbeans.modules.xml.core.*;
29 import org.netbeans.modules.xml.core.sync.*;
30 import org.netbeans.modules.xml.core.lib.*;
31 import java.net.URL JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 /**
35  * Manages text representation and its editor interaction.
36  * Takes care for proper event propagation.
37  *
38  * It is related to Document.
39  * <p>Update accepts: String.
40  * <p>Change provides: Document, Reader, InputSource, String
41  *
42  *
43  * @author Petr Kuzel
44  * @version
45  */

46 public abstract class TextRepresentation extends SyncRepresentation {
47
48     /**
49      * Holds reference to editor support.
50      */

51     protected final TextEditorSupport editor;
52     
53     /** Creates new TextRepresentation */
54     public TextRepresentation(TextEditorSupport editor, Synchronizator sync) {
55         super(sync);
56         this.editor = editor;
57     }
58     
59     /**
60      * Does this representation wraps given model?
61      */

62     public boolean represents(Class JavaDoc type) {
63         return Document JavaDoc.class.isAssignableFrom(type);
64     }
65
66     public int level() {
67         return 1;
68     }
69
70     /**
71      * Return modification passed as update parameter to all slave representations.
72      */

73     public Object JavaDoc getChange(Class JavaDoc type) {
74         if (type == null || type.isAssignableFrom(Document JavaDoc.class)) {
75             return editor.getDocument();
76         } else if (type.isAssignableFrom(String JavaDoc.class)) {
77             try {
78                 return Convertors.documentToString(editor.openDocument());
79             } catch (IOException JavaDoc ex) {
80                 Util.THIS.debug(ex);
81                 return null;
82             }
83         } else if (type.isAssignableFrom(InputSource.class)) {
84             
85             InputSource in = null;
86             try {
87                 in = Convertors.documentToInputSource(editor.openDocument());
88             } catch (IOException JavaDoc ex) {
89                 Util.THIS.debug(ex);
90                 return null;
91             }
92             try {
93                 URL JavaDoc baseURL = editor.getDataObject().getPrimaryFile().getURL();
94                 String JavaDoc systemId = baseURL.toExternalForm();
95                 in.setSystemId(systemId);
96             } catch (IOException JavaDoc ex) {
97                 // file object diappeared, we can not parse relative external entities then
98
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Warning: missing file object, external entities cannot be parsed."); // NOI18N
99
}
100             return in;
101         } else if (type.isAssignableFrom(Reader JavaDoc.class)) {
102             try {
103                 return new StringReader JavaDoc(Convertors.documentToString(editor.openDocument()));
104             } catch (IOException JavaDoc ex) {
105                 Util.THIS.debug(ex);
106                 return null;
107             }
108         }
109
110         throw new RuntimeException JavaDoc("Unsupported type: " + type); // NOI18N
111
}
112     
113     /**
114      * @return select button diplay name used during notifying concurent modification
115      * conflict.
116      */

117     public String JavaDoc getDisplayName() {
118         return Util.THIS.getString ("PROP_Text_representation");
119     }
120     
121     /**
122      * Return accepted update class
123      */

124     public Class JavaDoc getUpdateClass() {
125         return String JavaDoc.class;
126     }
127
128 }
129
Popular Tags