KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > sync > DataObjectSyncSupport


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.sync;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import javax.swing.text.Document JavaDoc;
26
27 import org.openide.cookies.EditorCookie;
28 import org.openide.filesystems.FileObject;
29 import org.openide.loaders.DataObject;
30
31 import org.netbeans.modules.xml.core.*;
32 import org.netbeans.modules.xml.core.cookies.*;
33
34 /**
35  * Simple representations manager. It handles mutual exclusivity of File and
36  * Text representations because of possible problems with modified status and
37  * save cookie management.
38  * <p>
39  * It also always adds Text representation if Tree representation was required.
40  * This is workaround adding undo feature to tree operations via text undo.
41  * Also only Text representation needs to take care about modifications and save().
42  *
43  * @author Petr Kuzel
44  * @version
45  */

46 public class DataObjectSyncSupport extends SyncSupport implements Synchronizator {
47     
48     private final Vector JavaDoc reps;
49         
50     private final CookieManagerCookie cookieMgr;
51
52     
53     /** Creates new DataObjectSyncSupport */
54     public DataObjectSyncSupport(XMLDataObjectLook dobj) {
55         super((DataObject)dobj);
56         reps = new Vector JavaDoc(3);
57         cookieMgr = dobj.getCookieManager();
58
59         Representation basic = new FileRepresentation (getDO(), this);
60         reps.add(basic);
61     }
62
63
64     public void representationChanged(Class JavaDoc type) {
65         super.representationChanged(type);
66     }
67     
68     /*
69      * Return conditional set of representations.
70      *
71      */

72     protected Representation[] getRepresentations() {
73         return (Representation[]) reps.toArray(new Representation[0]);
74     }
75
76     /**
77      * Select from loaded representation proper one that can be used as primary.
78      */

79     public Representation getPrimaryRepresentation() {
80         
81         final Class JavaDoc priority[] = new Class JavaDoc[] { //??? it should be provided by protected method
82
Document JavaDoc.class,
83 // TreeDocumentRoot.class,
84
FileObject.class
85         };
86         
87         Representation all[] = getRepresentations();
88         
89         for (int i = 0; i<priority.length; i++) {
90             for (int r = 0; r<all.length; r++) {
91                 Representation rep = all[r];
92                 if (rep.isValid() == false)
93                     continue;
94                 if (rep.represents(priority[i])) {
95                     if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Primary rep = " + rep); // NOI18N
96

97                     return rep;
98                 }
99             }
100         }
101         
102         throw new IllegalStateException JavaDoc("No primary representation found: " + reps); // NOI18N
103
}
104     
105     /*
106      * Manipulate appropriare cookies at data object.
107      * Keep text and file rpresentation as mutually exclusive.
108      */

109     public void addRepresentation(Representation rep) {
110         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Sync addRepresentation " + rep); // NOI18N
111

112         if (rep.represents(Document JavaDoc.class)) {
113             for (Iterator JavaDoc it = reps.iterator(); it.hasNext();) {
114                 Representation next = (Representation) it.next();
115                 if (next.represents(FileObject.class)) {
116                     it.remove();
117                 }
118             }
119         } else if (rep.level() > 1) {
120             
121             // load also text representation, tree cannot live without it
122

123             loadTextRepresentation();
124         }
125         reps.add(rep);
126     }
127
128     
129
130     /*
131      * Manipulate appropriare cookies at data object.
132      * Keep text and file rpresentation as mutually exclusive.
133      */

134     public void removeRepresentation(Representation rep) {
135         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Sync removeRepresentation " + rep); // NOI18N
136

137         boolean modelLoaded = false;
138
139         if (rep.represents(Document JavaDoc.class)) {
140             
141             // check whether tree representation is loaded
142

143
144             for (Iterator JavaDoc it = reps.iterator(); it.hasNext();) {
145                 Representation next = (Representation) it.next();
146                 if (next.level() > 1) {
147                     modelLoaded = true;
148                 }
149             }
150
151             if (modelLoaded == false) {
152             
153                 Representation basic = new FileRepresentation (getDO(), this);
154                 reps.add(basic);
155             
156             } else {
157                 
158                 // reload text representation, tree cannot live without it
159

160                 loadTextRepresentation();
161             }
162         }
163         reps.remove(rep);
164
165         if ( modelLoaded ) {//&& ( getDO().isValid() ) ) {
166
representationChanged (Document JavaDoc.class);
167         }
168     }
169
170     
171     private void loadTextRepresentation() {
172         if ( getDO().isValid() ) { // because of remove modified document
173
try {
174                 EditorCookie editor = (EditorCookie) getDO().getCookie(EditorCookie.class);
175                 editor.openDocument();
176             } catch (IOException JavaDoc ex) {
177                 //??? ignore it just now
178
}
179         }
180     }
181 }
182
Popular Tags