KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > core > SchemaDataObject


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.core;
21
22 import java.io.IOException JavaDoc;
23 import javax.swing.Action JavaDoc;
24 import javax.xml.transform.Source JavaDoc;
25 import org.netbeans.modules.xml.refactoring.CannotRefactorException;
26 import org.netbeans.modules.xml.refactoring.FileRenameRequest;
27 import org.netbeans.modules.xml.refactoring.RefactoringManager;
28 import org.netbeans.modules.xml.refactoring.ui.ModelProvider;
29 //import org.netbeans.modules.xml.refactoring.ui.util.AnalysisUtilities;
30
import org.netbeans.modules.xml.schema.core.actions.SchemaViewOpenAction;
31 import org.netbeans.modules.xml.schema.core.multiview.SchemaMultiViewSupport;
32 import org.netbeans.modules.xml.schema.model.SchemaModel;
33 import org.netbeans.spi.xml.cookies.CheckXMLSupport;
34 import org.netbeans.spi.xml.cookies.DataObjectAdapters;
35 import org.netbeans.spi.xml.cookies.TransformableSupport;
36 import org.openide.DialogDisplayer;
37 import org.openide.NotifyDescriptor;
38 import org.openide.filesystems.FileObject;
39 import org.openide.cookies.SaveCookie;
40 import org.openide.loaders.DataFolder;
41 import org.openide.loaders.DataNode;
42 import org.openide.loaders.DataObjectExistsException;
43 import org.openide.loaders.MultiDataObject;
44 import org.openide.loaders.UniFileLoader;
45 import org.openide.nodes.Children;
46 import org.openide.nodes.CookieSet;
47 import org.openide.nodes.Node;
48 import org.openide.util.NbBundle;
49 import org.openide.util.HelpCtx;
50 import org.openide.util.actions.SystemAction;
51 import org.xml.sax.InputSource JavaDoc;
52
53 /**
54  * XML Schema owner. It provides text editing and validation cookies support.
55  *
56  * @author Petr Kuzel
57  * @author Jeri Lockhart
58  */

59 public final class SchemaDataObject extends MultiDataObject {
60     
61     /**
62      * Creates a new instance of SchemaDataObject.
63      *
64      * @param obj file object containing data.
65      * @param loader the file loader.
66      */

67     public SchemaDataObject(FileObject obj, UniFileLoader loader) throws
68             DataObjectExistsException {
69         super(obj, loader);
70
71         CookieSet set = getCookieSet();
72         // editor support defines MIME type understood by EditorKits registry
73
schemaEditorSupport = new SchemaEditorSupport(this);
74         set.add(schemaEditorSupport);
75         // Add check and validate cookies
76
InputSource JavaDoc is = DataObjectAdapters.inputSource(this);
77         set.add(new CheckXMLSupport(is));
78         // Add TransformableCookie
79
Source JavaDoc source = DataObjectAdapters.source(this);
80         set.add(new TransformableSupport(source));
81         set.add(new SchemaValidateXMLCookie(this));
82         // ViewComponentCookie implementation
83
set.add(new SchemaMultiViewSupport(this));
84     }
85
86     /**
87      * Return the editor support for this data object.
88      *
89      * @return schema editor support.
90      */

91     public SchemaEditorSupport getSchemaEditorSupport() {
92     return schemaEditorSupport;
93     }
94
95     @Override JavaDoc
96     public void handleDelete() throws IOException JavaDoc {
97     if (isModified()) {
98         setModified(false);
99     }
100     getSchemaEditorSupport().getEnv().unmarkModified();
101     super.handleDelete();
102     }
103     
104     protected Node createNodeDelegate() {
105     SchemaNode n = new SchemaNode(this);
106     n.setIconBaseWithExtension(SCHEMA_ICON_BASE_WITH_EXT);
107     n.setShortDescription(NbBundle.getMessage(SchemaDataObject.class,
108         "LBL_SchemaNode_desc"));
109     
110     return n;
111     }
112
113     static class SchemaNode extends DataNode implements ModelProvider {
114         public SchemaNode(SchemaDataObject dobj) {
115             super(dobj, Children.LEAF);
116         }
117
118         public Action JavaDoc getPreferredAction() {
119             return SystemAction.get(SchemaViewOpenAction.class);
120         }
121
122         public void setName(String JavaDoc name, boolean rename) {
123             if (! rename || name != null && name.equals(this.getDataObject().getName())) {
124                 return;
125             }
126             
127             SchemaModel model = getModel();
128             FileRenameRequest request = new FileRenameRequest(model, name);
129             try {
130                 RefactoringManager.getInstance().execute(request, true);
131             } catch(CannotRefactorException ex) {
132   // AnalysisUtilities.showRefactoringUI(request);
133
} catch(IOException JavaDoc ex) {
134                 String JavaDoc msg = NbBundle.getMessage(SchemaDataObject.class, "MSG_UnableToRename", ex.getMessage());
135                 NotifyDescriptor nd = new NotifyDescriptor.Message(
136                     msg, NotifyDescriptor.ERROR_MESSAGE);
137                 DialogDisplayer.getDefault().notify(nd);
138             }
139         }
140
141         public SchemaModel getModel() {
142             try {
143                 SchemaDataObject dobj = (SchemaDataObject) getDataObject();
144                 return dobj.getSchemaEditorSupport().getModel();
145             } catch(IOException JavaDoc ex) {
146                 String JavaDoc msg = NbBundle.getMessage(SchemaDataObject.class, "MSG_UnableToLoadSchema", ex.getMessage());
147                 NotifyDescriptor nd = new NotifyDescriptor.Message(
148                     msg, NotifyDescriptor.ERROR_MESSAGE);
149                 DialogDisplayer.getDefault().notify(nd);
150             }
151             return null;
152         }
153     }
154     
155     @Override JavaDoc
156     public void setModified(boolean modified) {
157     super.setModified(modified);
158     if (modified) {
159         getCookieSet().add(getSaveCookie());
160     } else {
161         getCookieSet().remove(getSaveCookie());
162     }
163     }
164     
165     private SaveCookie getSaveCookie() {
166     return new SaveCookie() {
167         public void save() throws IOException JavaDoc {
168         getSchemaEditorSupport().saveDocument();
169         }
170         
171         @Override JavaDoc
172         public int hashCode() {
173         return getClass().hashCode();
174         }
175         
176         @Override JavaDoc
177         public boolean equals(Object JavaDoc other) {
178         return other != null && getClass().equals(other.getClass());
179         }
180     };
181     }
182
183     @Override JavaDoc
184     public HelpCtx getHelpCtx() {
185     return HelpCtx.DEFAULT_HELP;
186     }
187     
188     protected FileObject handleMove(DataFolder df) throws IOException JavaDoc {
189         // TODO: Launch refactor file dialog
190
if(isModified()) {
191             SaveCookie sCookie = (SaveCookie) this.getCookie(SaveCookie.class);
192             if(sCookie != null) {
193                 sCookie.save();
194             }
195         }
196         return super.handleMove(df);
197     }
198
199     ////////////////////////////////////////////////////////////////////////////
200
// Class variables
201
////////////////////////////////////////////////////////////////////////////
202

203     public static final String JavaDoc SCHEMA_ICON_BASE_WITH_EXT =
204     "org/netbeans/modules/xml/schema/core/resources/Schema_File.png"; // NOI18N
205
private static final long serialVersionUID = -8229569186860053169L;
206     
207     
208     ////////////////////////////////////////////////////////////////////////////
209
// Instance variables
210
////////////////////////////////////////////////////////////////////////////
211
private transient SchemaEditorSupport schemaEditorSupport;
212 }
213
Popular Tags