2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
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]"
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.
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.
41
package org.netbeans.modules.xml.core.sync;
43
import java.io.IOException;
44
import java.util.Iterator;
45
import java.util.Vector;
47
import javax.swing.text.Document;
49
import org.openide.cookies.EditorCookie;
50
import org.openide.filesystems.FileObject;
51
import org.openide.loaders.DataObject;
53
import org.netbeans.modules.xml.core.*;
54
import org.netbeans.modules.xml.core.cookies.*;
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.
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().
68
public class DataObjectSyncSupport extends SyncSupport implements Synchronizator {
70
private final Vector reps;
72
private final CookieManagerCookie cookieMgr;
75
/** Creates new DataObjectSyncSupport */
76
public DataObjectSyncSupport(XMLDataObjectLook dobj) {
77
super((DataObject)dobj);
79
cookieMgr = dobj.getCookieManager();
81
Representation basic = new FileRepresentation (getDO(), this);
86
public void representationChanged(Class type) {
87
super.representationChanged(type);
91
* Return conditional set of representations.
94
protected Representation[] getRepresentations() {
95
return (Representation[]) reps.toArray(new Representation[0]);
99
* Select from loaded representation proper one that can be used as primary.
101
public Representation getPrimaryRepresentation() {
103
final Class priority[] = new Class[] { //??? it should be provided by protected method
105
// TreeDocumentRoot.class,
109
Representation all[] = getRepresentations();
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)
116
if (rep.represents(priority[i])) {
117
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Primary rep = " + rep); // NOI18N
124
throw new IllegalStateException("No primary representation found: " + reps); // NOI18N
128
* Manipulate appropriare cookies at data object.
129
* Keep text and file rpresentation as mutually exclusive.
131
public void addRepresentation(Representation rep) {
132
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Sync addRepresentation " + rep); // NOI18N
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)) {
141
} else if (rep.level() > 1) {
143
// load also text representation, tree cannot live without it
145
loadTextRepresentation();
153
* Manipulate appropriare cookies at data object.
154
* Keep text and file rpresentation as mutually exclusive.
156
public void removeRepresentation(Representation rep) {
157
if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Sync removeRepresentation " + rep); // NOI18N
159
boolean modelLoaded = false;
161
if (rep.represents(Document.class)) {
163
// check whether tree representation is loaded
166
for (Iterator it = reps.iterator(); it.hasNext();) {
167
Representation next = (Representation) it.next();
168
if (next.level() > 1) {
173
if (modelLoaded == false) {
175
Representation basic = new FileRepresentation (getDO(), this);
180
// reload text representation, tree cannot live without it
182
loadTextRepresentation();
187
if ( modelLoaded ) {//&& ( getDO().isValid() ) ) {
188
representationChanged (Document.class);
193
private void loadTextRepresentation() {
194
if ( getDO().isValid() ) { // because of remove modified document
196
EditorCookie editor = getDO().getCookie(EditorCookie.class);
197
editor.openDocument();
198
} catch (IOException ex) {
199
//??? ignore it just now