~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/core/src/org/netbeans/modules/xml/core/sync/DataObjectSyncSupport.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
package org.netbeans.modules.xml.core.sync;
 
42
 
 
43
import java.io.IOException;
 
44
import java.util.Iterator;
 
45
import java.util.Vector;
 
46
 
 
47
import javax.swing.text.Document;
 
48
 
 
49
import org.openide.cookies.EditorCookie;
 
50
import org.openide.filesystems.FileObject;
 
51
import org.openide.loaders.DataObject;
 
52
 
 
53
import org.netbeans.modules.xml.core.*;
 
54
import org.netbeans.modules.xml.core.cookies.*;
 
55
 
 
56
/**
 
57
 * Simple representations manager. It handles mutual exclusivity of File and
 
58
 * Text representations because of possible problems with modified status and
 
59
 * save cookie management.
 
60
 * <p>
 
61
 * It also always adds Text representation if Tree representation was required.
 
62
 * This is workaround adding undo feature to tree operations via text undo.
 
63
 * Also only Text representation needs to take care about modifications and save().
 
64
 *
 
65
 * @author  Petr Kuzel
 
66
 * @version 
 
67
 */
 
68
public class DataObjectSyncSupport extends SyncSupport implements Synchronizator {
 
69
    
 
70
    private final Vector reps;
 
71
        
 
72
    private final CookieManagerCookie cookieMgr;
 
73
 
 
74
    
 
75
    /** Creates new DataObjectSyncSupport */
 
76
    public DataObjectSyncSupport(XMLDataObjectLook dobj) {
 
77
        super((DataObject)dobj);
 
78
        reps = new Vector(3);
 
79
        cookieMgr = dobj.getCookieManager();
 
80
 
 
81
        Representation basic = new FileRepresentation (getDO(), this);
 
82
        reps.add(basic);
 
83
    }
 
84
 
 
85
 
 
86
    public void representationChanged(Class type) {
 
87
        super.representationChanged(type);
 
88
    }
 
89
    
 
90
    /*
 
91
     * Return conditional set of representations.
 
92
     *
 
93
     */
 
94
    protected Representation[] getRepresentations() {
 
95
        return (Representation[]) reps.toArray(new Representation[0]);
 
96
    }
 
97
 
 
98
    /**
 
99
     * Select from loaded representation proper one that can be used as primary.
 
100
     */
 
101
    public Representation getPrimaryRepresentation() {
 
102
        
 
103
        final Class priority[] = new Class[] {  //??? it should be provided by protected method
 
104
            Document.class, 
 
105
//              TreeDocumentRoot.class,
 
106
            FileObject.class
 
107
        };
 
108
        
 
109
        Representation all[] = getRepresentations();
 
110
        
 
111
        for (int i = 0; i<priority.length; i++) {
 
112
            for (int r = 0; r<all.length; r++) {
 
113
                Representation rep = all[r];
 
114
                if (rep.isValid() == false) 
 
115
                    continue;
 
116
                if (rep.represents(priority[i])) {
 
117
                    if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Primary rep = " + rep); // NOI18N
 
118
 
 
119
                    return rep;
 
120
                }
 
121
            }
 
122
        }
 
123
        
 
124
        throw new IllegalStateException("No primary representation found: " + reps); // NOI18N
 
125
    }
 
126
    
 
127
    /*
 
128
     * Manipulate appropriare cookies at data object.
 
129
     * Keep text and file rpresentation as mutually exclusive.
 
130
     */
 
131
    public void addRepresentation(Representation rep) {
 
132
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Sync addRepresentation " + rep); // NOI18N
 
133
 
 
134
        if (rep.represents(Document.class)) {
 
135
            for (Iterator it = reps.iterator(); it.hasNext();) {
 
136
                Representation next = (Representation) it.next();
 
137
                if (next.represents(FileObject.class)) {
 
138
                    it.remove();
 
139
                }                               
 
140
            }
 
141
        } else if (rep.level() > 1) {
 
142
            
 
143
            // load also text representation, tree cannot live without it
 
144
            
 
145
            loadTextRepresentation();
 
146
        }
 
147
        reps.add(rep);
 
148
    }
 
149
 
 
150
    
 
151
 
 
152
    /*
 
153
     * Manipulate appropriare cookies at data object.
 
154
     * Keep text and file rpresentation as mutually exclusive.
 
155
     */    
 
156
    public void removeRepresentation(Representation rep) {
 
157
        if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Sync removeRepresentation " + rep); // NOI18N
 
158
        
 
159
        boolean modelLoaded = false;
 
160
 
 
161
        if (rep.represents(Document.class)) {
 
162
            
 
163
            // check whether tree representation is loaded
 
164
            
 
165
 
 
166
            for (Iterator it = reps.iterator(); it.hasNext();) {
 
167
                Representation next = (Representation) it.next();
 
168
                if (next.level() > 1) {
 
169
                    modelLoaded = true;
 
170
                }                               
 
171
            }
 
172
 
 
173
            if (modelLoaded == false) {
 
174
            
 
175
                Representation basic = new FileRepresentation (getDO(), this);
 
176
                reps.add(basic);
 
177
            
 
178
            } else {     
 
179
                
 
180
                // reload text representation, tree cannot live without it
 
181
 
 
182
                loadTextRepresentation();
 
183
            }
 
184
        }                        
 
185
        reps.remove(rep);
 
186
 
 
187
        if ( modelLoaded ) {//&& ( getDO().isValid() ) ) {
 
188
            representationChanged (Document.class);
 
189
        }
 
190
    }
 
191
 
 
192
    
 
193
    private void loadTextRepresentation() {
 
194
        if ( getDO().isValid() ) { // because of remove modified document
 
195
            try {
 
196
                EditorCookie editor = getDO().getCookie(EditorCookie.class);
 
197
                editor.openDocument();
 
198
            } catch (IOException ex) {
 
199
                //??? ignore it just now
 
200
            }
 
201
        }
 
202
    }
 
203
}