KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tax > cookies > TreeRepresentation


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.tax.cookies;
20
21 import java.io.Reader JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24
25 import org.xml.sax.*;
26
27 import org.netbeans.tax.*;
28 import org.netbeans.tax.io.*;
29 import org.netbeans.modules.xml.core.sync.*;
30 import org.netbeans.modules.xml.tax.cookies.TreeEditorCookie;
31 import org.netbeans.modules.xml.tax.cookies.TreeEditorCookieImpl;
32
33 /**
34  * Manages tree model and its editor interaction.
35  *
36  * @author Petr Kuzel
37  * @version
38  */

39 public abstract class TreeRepresentation extends SyncRepresentation {
40
41     protected final TreeEditorCookieImpl editor;
42     
43     /** Creates new TreeRepresentation */
44     public TreeRepresentation(TreeEditorCookieImpl editor, Synchronizator sync) {
45         super(sync);
46         this.editor = editor;
47     }
48
49     /**
50      * Does this representation wraps given model?
51      */

52     public boolean represents(Class JavaDoc type) {
53         return TreeDocumentRoot.class.isAssignableFrom(type);
54     }
55
56     public int level() {
57         return 2;
58     }
59
60     /**
61      * Return accepted update class
62      */

63     public Class JavaDoc getUpdateClass() {
64         return InputSource.class;
65     }
66     
67     /**
68      * @return select button diplay name used during notifying concurent modification
69      * conflict.
70      */

71     public String JavaDoc getDisplayName() {
72         return Util.THIS.getString ("PROP_Tree_representation");
73     }
74
75     /**
76      * Return modification passed as update parameter to all slave representations.
77      */

78     public Object JavaDoc getChange(Class JavaDoc type) {
79         if (type == null || type.isAssignableFrom(Reader JavaDoc.class)) {
80
81             try {
82                 return new TreeReader(editor.openDocumentRoot());
83             } catch (IOException JavaDoc ex) {
84                 return null;
85             } catch (TreeException ex) {
86                 return null;
87             }
88             
89         } else if (type.isAssignableFrom(String JavaDoc.class)) {
90
91             try {
92                 return Convertors.treeToString(editor.openDocumentRoot());
93
94             } catch (IOException JavaDoc ex) {
95                 ex.printStackTrace();
96                 return null;
97             } catch (TreeException ex) {
98                 ex.printStackTrace();
99                 return null;
100             }
101
102         } else if (type.isAssignableFrom(InputStream JavaDoc.class)) {
103             
104             try {
105                 return new TreeInputStream(editor.openDocumentRoot());
106             } catch (IOException JavaDoc ex) {
107                 return null;
108             } catch (TreeException ex) {
109                 return null;
110             }
111         }
112
113         return null;
114     }
115     
116     /**
117      * Valid only if tree is property parsed.
118      */

119     public boolean isValid() {
120         return editor.getStatus() == TreeEditorCookie.STATUS_OK;
121     }
122 }
123
Popular Tags