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

« back to all changes in this revision

Viewing changes to xml/schema/src/org/netbeans/modules/xml/schema/SchemaDataObject.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-2007 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
 
 
42
package org.netbeans.modules.xml.schema;
 
43
 
 
44
import java.io.IOException;
 
45
import javax.swing.Action;
 
46
import javax.xml.transform.Source;
 
47
import org.netbeans.modules.xml.api.XmlFileEncodingQueryImpl;
 
48
import org.netbeans.modules.xml.refactoring.CannotRefactorException;
 
49
import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
 
50
import org.netbeans.modules.xml.refactoring.ui.ModelProvider;
 
51
import org.netbeans.modules.xml.schema.actions.SchemaViewOpenAction;
 
52
import org.netbeans.modules.xml.schema.multiview.SchemaMultiViewSupport;
 
53
import org.netbeans.modules.xml.schema.model.SchemaModel;
 
54
import org.netbeans.spi.queries.FileEncodingQueryImplementation;
 
55
import org.netbeans.spi.xml.cookies.CheckXMLSupport;
 
56
import org.netbeans.spi.xml.cookies.DataObjectAdapters;
 
57
import org.netbeans.spi.xml.cookies.TransformableSupport;
 
58
import org.openide.DialogDisplayer;
 
59
import org.openide.NotifyDescriptor;
 
60
import org.openide.filesystems.FileObject;
 
61
import org.openide.cookies.SaveCookie;
 
62
import org.openide.loaders.DataFolder;
 
63
import org.openide.loaders.DataNode;
 
64
import org.openide.loaders.DataObjectExistsException;
 
65
import org.openide.loaders.MultiDataObject;
 
66
import org.openide.loaders.UniFileLoader;
 
67
import org.openide.nodes.Children;
 
68
import org.openide.nodes.CookieSet;
 
69
import org.openide.nodes.Node;
 
70
import org.openide.util.NbBundle;
 
71
import org.openide.util.HelpCtx;
 
72
import org.openide.util.Lookup;
 
73
import org.openide.util.actions.SystemAction;
 
74
import org.openide.util.lookup.Lookups;
 
75
import org.xml.sax.InputSource;
 
76
 
 
77
/**
 
78
 * XML Schema owner. It provides text editing and validation cookies support.
 
79
 *
 
80
 * @author  Petr Kuzel
 
81
 * @author  Jeri Lockhart
 
82
 */
 
83
public final class SchemaDataObject extends MultiDataObject {
 
84
    
 
85
    /**
 
86
     * Creates a new instance of SchemaDataObject.
 
87
     *
 
88
     * @param  obj     file object containing data.
 
89
     * @param  loader  the file loader.
 
90
     */
 
91
    public SchemaDataObject(FileObject obj, UniFileLoader loader) throws
 
92
            DataObjectExistsException {
 
93
        super(obj, loader);
 
94
        CookieSet set = getCookieSet();
 
95
        // editor support defines MIME type understood by EditorKits registry
 
96
        schemaEditorSupport = new SchemaEditorSupport(this);
 
97
        set.add(schemaEditorSupport);
 
98
        // Add check and validate cookies
 
99
        InputSource is = DataObjectAdapters.inputSource(this);
 
100
        set.add(new CheckXMLSupport(is));
 
101
        // Add TransformableCookie
 
102
        Source source = DataObjectAdapters.source(this);
 
103
        set.add(new TransformableSupport(source));
 
104
        set.add(new SchemaValidateXMLCookie(this));
 
105
        // ViewComponentCookie implementation
 
106
        set.add(new SchemaMultiViewSupport(this));
 
107
    }
 
108
 
 
109
    public final Lookup getLookup() {
 
110
        return Lookups.fixed(new Object[]{
 
111
            super.getLookup(),
 
112
            this,
 
113
            XmlFileEncodingQueryImpl.singleton()});
 
114
    }
 
115
    
 
116
    public void addSaveCookie(SaveCookie cookie){
 
117
        getCookieSet().add(cookie);
 
118
    }
 
119
    
 
120
    public void removeSaveCookie(){
 
121
        Node.Cookie cookie = getCookie(SaveCookie.class);
 
122
        if (cookie!=null) getCookieSet().remove(cookie);
 
123
    }    
 
124
    
 
125
    /**
 
126
     * Return the editor support for this data object.
 
127
     *
 
128
     * @return  schema editor support.
 
129
     */
 
130
    public SchemaEditorSupport getSchemaEditorSupport() {
 
131
        return schemaEditorSupport;
 
132
    }
 
133
 
 
134
    @Override
 
135
    public void handleDelete() throws IOException {
 
136
        if (isModified()) {
 
137
            setModified(false);
 
138
        }
 
139
        getSchemaEditorSupport().getEnv().unmarkModified();
 
140
        super.handleDelete();
 
141
    }
 
142
    
 
143
    protected Node createNodeDelegate() {
 
144
        SchemaNode n = new SchemaNode(this);
 
145
        n.setIconBaseWithExtension(SCHEMA_ICON_BASE_WITH_EXT);
 
146
        n.setShortDescription(NbBundle.getMessage(SchemaDataObject.class,
 
147
            "LBL_SchemaNode_desc"));
 
148
        
 
149
        return n;
 
150
    }
 
151
 
 
152
    static class SchemaNode extends DataNode implements ModelProvider {
 
153
        public SchemaNode(SchemaDataObject dobj) {
 
154
            super(dobj, Children.LEAF);
 
155
            getCookieSet().add(this);
 
156
        }
 
157
 
 
158
        public Action getPreferredAction() {
 
159
            return SystemAction.get(SchemaViewOpenAction.class);
 
160
        }
 
161
 
 
162
        public void setName(String name, boolean rename) {
 
163
            if (! rename || name != null && name.equals(this.getDataObject().getName())) {
 
164
                return;
 
165
            }
 
166
            //System.out.println("SchemaDataObject:: setName :: ");
 
167
            SchemaModel model = getModel();
 
168
            
 
169
            try {
 
170
            //    RefactoringManager.getInstance().execute(request, true);
 
171
                SharedUtils.silentFileRefactor(model, name, true);
 
172
            } catch(CannotRefactorException ex) {
 
173
                SharedUtils.showFileRenameRefactoringUI(model, name);
 
174
            } catch(IOException ex) {
 
175
                String msg = NbBundle.getMessage(SchemaDataObject.class, "MSG_UnableToRename", ex.getMessage());
 
176
                NotifyDescriptor nd = new NotifyDescriptor.Message(
 
177
                    msg, NotifyDescriptor.ERROR_MESSAGE);
 
178
                DialogDisplayer.getDefault().notify(nd);
 
179
            }
 
180
        }
 
181
 
 
182
        public SchemaModel getModel() {
 
183
            try {
 
184
                SchemaDataObject dobj = (SchemaDataObject) getDataObject();
 
185
                return dobj.getSchemaEditorSupport().getModel();
 
186
            } catch(IOException ex) {
 
187
                String msg = NbBundle.getMessage(SchemaDataObject.class, "MSG_UnableToLoadSchema", ex.getMessage());
 
188
                NotifyDescriptor nd = new NotifyDescriptor.Message(
 
189
                    msg, NotifyDescriptor.ERROR_MESSAGE);
 
190
                DialogDisplayer.getDefault().notify(nd);
 
191
            }
 
192
            return null;
 
193
        }
 
194
    }
 
195
    
 
196
    @Override
 
197
    public void setModified(boolean modified) {
 
198
        super.setModified(modified);
 
199
        if (modified) {
 
200
            getCookieSet().add(getSaveCookie());
 
201
        } else {
 
202
            getCookieSet().remove(getSaveCookie());
 
203
        }
 
204
    }
 
205
    
 
206
    private SaveCookie getSaveCookie() {
 
207
        return new SaveCookie() {
 
208
            public void save() throws IOException {
 
209
                getSchemaEditorSupport().saveDocument();
 
210
            }
 
211
            
 
212
            @Override
 
213
            public int hashCode() {
 
214
                return getClass().hashCode();
 
215
            }
 
216
            
 
217
            @Override
 
218
            public boolean equals(Object other) {
 
219
                return other != null && getClass().equals(other.getClass());
 
220
            }
 
221
        };
 
222
    }
 
223
 
 
224
    @Override
 
225
    public HelpCtx getHelpCtx() {
 
226
        return HelpCtx.DEFAULT_HELP;
 
227
    }
 
228
    
 
229
    protected FileObject handleMove(DataFolder df) throws IOException {
 
230
        // TODO: Launch refactor file dialog
 
231
        if(isModified()) {
 
232
            SaveCookie sCookie = this.getCookie(SaveCookie.class);
 
233
            if(sCookie != null) {
 
234
                sCookie.save();
 
235
            }
 
236
        }
 
237
        return super.handleMove(df);
 
238
    }
 
239
 
 
240
    ////////////////////////////////////////////////////////////////////////////
 
241
    // Class variables
 
242
    ////////////////////////////////////////////////////////////////////////////
 
243
    
 
244
    public static final String SCHEMA_ICON_BASE_WITH_EXT =
 
245
        "org/netbeans/modules/xml/schema/resources/Schema_File.png"; // NOI18N
 
246
    private static final long serialVersionUID = -8229569186860053169L;
 
247
    
 
248
    
 
249
    ////////////////////////////////////////////////////////////////////////////
 
250
    // Instance variables
 
251
    ////////////////////////////////////////////////////////////////////////////
 
252
    private transient SchemaEditorSupport schemaEditorSupport;
 
253
}