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-2007 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.
42
package org.netbeans.modules.xml.schema;
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;
78
* XML Schema owner. It provides text editing and validation cookies support.
81
* @author Jeri Lockhart
83
public final class SchemaDataObject extends MultiDataObject {
86
* Creates a new instance of SchemaDataObject.
88
* @param obj file object containing data.
89
* @param loader the file loader.
91
public SchemaDataObject(FileObject obj, UniFileLoader loader) throws
92
DataObjectExistsException {
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));
109
public final Lookup getLookup() {
110
return Lookups.fixed(new Object[]{
113
XmlFileEncodingQueryImpl.singleton()});
116
public void addSaveCookie(SaveCookie cookie){
117
getCookieSet().add(cookie);
120
public void removeSaveCookie(){
121
Node.Cookie cookie = getCookie(SaveCookie.class);
122
if (cookie!=null) getCookieSet().remove(cookie);
126
* Return the editor support for this data object.
128
* @return schema editor support.
130
public SchemaEditorSupport getSchemaEditorSupport() {
131
return schemaEditorSupport;
135
public void handleDelete() throws IOException {
139
getSchemaEditorSupport().getEnv().unmarkModified();
140
super.handleDelete();
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"));
152
static class SchemaNode extends DataNode implements ModelProvider {
153
public SchemaNode(SchemaDataObject dobj) {
154
super(dobj, Children.LEAF);
155
getCookieSet().add(this);
158
public Action getPreferredAction() {
159
return SystemAction.get(SchemaViewOpenAction.class);
162
public void setName(String name, boolean rename) {
163
if (! rename || name != null && name.equals(this.getDataObject().getName())) {
166
//System.out.println("SchemaDataObject:: setName :: ");
167
SchemaModel model = getModel();
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);
182
public SchemaModel getModel() {
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);
197
public void setModified(boolean modified) {
198
super.setModified(modified);
200
getCookieSet().add(getSaveCookie());
202
getCookieSet().remove(getSaveCookie());
206
private SaveCookie getSaveCookie() {
207
return new SaveCookie() {
208
public void save() throws IOException {
209
getSchemaEditorSupport().saveDocument();
213
public int hashCode() {
214
return getClass().hashCode();
218
public boolean equals(Object other) {
219
return other != null && getClass().equals(other.getClass());
225
public HelpCtx getHelpCtx() {
226
return HelpCtx.DEFAULT_HELP;
229
protected FileObject handleMove(DataFolder df) throws IOException {
230
// TODO: Launch refactor file dialog
232
SaveCookie sCookie = this.getCookie(SaveCookie.class);
233
if(sCookie != null) {
237
return super.handleMove(df);
240
////////////////////////////////////////////////////////////////////////////
242
////////////////////////////////////////////////////////////////////////////
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;
249
////////////////////////////////////////////////////////////////////////////
250
// Instance variables
251
////////////////////////////////////////////////////////////////////////////
252
private transient SchemaEditorSupport schemaEditorSupport;