001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.schema;
043:
044: import java.io.IOException;
045: import javax.swing.Action;
046: import javax.xml.transform.Source;
047: import org.netbeans.modules.xml.api.XmlFileEncodingQueryImpl;
048: import org.netbeans.modules.xml.refactoring.CannotRefactorException;
049: import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
050: import org.netbeans.modules.xml.refactoring.ui.ModelProvider;
051: import org.netbeans.modules.xml.schema.actions.SchemaViewOpenAction;
052: import org.netbeans.modules.xml.schema.multiview.SchemaMultiViewSupport;
053: import org.netbeans.modules.xml.schema.model.SchemaModel;
054: import org.netbeans.spi.queries.FileEncodingQueryImplementation;
055: import org.netbeans.spi.xml.cookies.CheckXMLSupport;
056: import org.netbeans.spi.xml.cookies.DataObjectAdapters;
057: import org.netbeans.spi.xml.cookies.TransformableSupport;
058: import org.openide.DialogDisplayer;
059: import org.openide.NotifyDescriptor;
060: import org.openide.filesystems.FileObject;
061: import org.openide.cookies.SaveCookie;
062: import org.openide.loaders.DataFolder;
063: import org.openide.loaders.DataNode;
064: import org.openide.loaders.DataObjectExistsException;
065: import org.openide.loaders.MultiDataObject;
066: import org.openide.loaders.UniFileLoader;
067: import org.openide.nodes.Children;
068: import org.openide.nodes.CookieSet;
069: import org.openide.nodes.Node;
070: import org.openide.util.NbBundle;
071: import org.openide.util.HelpCtx;
072: import org.openide.util.Lookup;
073: import org.openide.util.actions.SystemAction;
074: import org.openide.util.lookup.Lookups;
075: import org.xml.sax.InputSource;
076:
077: /**
078: * XML Schema owner. It provides text editing and validation cookies support.
079: *
080: * @author Petr Kuzel
081: * @author Jeri Lockhart
082: */
083: public final class SchemaDataObject extends MultiDataObject {
084:
085: /**
086: * Creates a new instance of SchemaDataObject.
087: *
088: * @param obj file object containing data.
089: * @param loader the file loader.
090: */
091: public SchemaDataObject(FileObject obj, UniFileLoader loader)
092: throws DataObjectExistsException {
093: super (obj, loader);
094: CookieSet set = getCookieSet();
095: // editor support defines MIME type understood by EditorKits registry
096: schemaEditorSupport = new SchemaEditorSupport(this );
097: set.add(schemaEditorSupport);
098: // Add check and validate cookies
099: 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[] { super .getLookup(), this ,
111: XmlFileEncodingQueryImpl.singleton() });
112: }
113:
114: public void addSaveCookie(SaveCookie cookie) {
115: getCookieSet().add(cookie);
116: }
117:
118: public void removeSaveCookie() {
119: Node.Cookie cookie = getCookie(SaveCookie.class);
120: if (cookie != null)
121: getCookieSet().remove(cookie);
122: }
123:
124: /**
125: * Return the editor support for this data object.
126: *
127: * @return schema editor support.
128: */
129: public SchemaEditorSupport getSchemaEditorSupport() {
130: return schemaEditorSupport;
131: }
132:
133: @Override
134: public void handleDelete() throws IOException {
135: if (isModified()) {
136: setModified(false);
137: }
138: getSchemaEditorSupport().getEnv().unmarkModified();
139: super .handleDelete();
140: }
141:
142: protected Node createNodeDelegate() {
143: SchemaNode n = new SchemaNode(this );
144: n.setIconBaseWithExtension(SCHEMA_ICON_BASE_WITH_EXT);
145: n.setShortDescription(NbBundle.getMessage(
146: SchemaDataObject.class, "LBL_SchemaNode_desc"));
147:
148: return n;
149: }
150:
151: static class SchemaNode extends DataNode implements ModelProvider {
152: public SchemaNode(SchemaDataObject dobj) {
153: super (dobj, Children.LEAF);
154: getCookieSet().add(this );
155: }
156:
157: public Action getPreferredAction() {
158: return SystemAction.get(SchemaViewOpenAction.class);
159: }
160:
161: public void setName(String name, boolean rename) {
162: if (!rename || name != null
163: && 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(
176: SchemaDataObject.class, "MSG_UnableToRename",
177: ex.getMessage());
178: NotifyDescriptor nd = new NotifyDescriptor.Message(msg,
179: NotifyDescriptor.ERROR_MESSAGE);
180: DialogDisplayer.getDefault().notify(nd);
181: }
182: }
183:
184: public SchemaModel getModel() {
185: try {
186: SchemaDataObject dobj = (SchemaDataObject) getDataObject();
187: return dobj.getSchemaEditorSupport().getModel();
188: } catch (IOException ex) {
189: String msg = NbBundle.getMessage(
190: SchemaDataObject.class,
191: "MSG_UnableToLoadSchema", ex.getMessage());
192: NotifyDescriptor nd = new NotifyDescriptor.Message(msg,
193: NotifyDescriptor.ERROR_MESSAGE);
194: DialogDisplayer.getDefault().notify(nd);
195: }
196: return null;
197: }
198: }
199:
200: @Override
201: public void setModified(boolean modified) {
202: super .setModified(modified);
203: if (modified) {
204: getCookieSet().add(getSaveCookie());
205: } else {
206: getCookieSet().remove(getSaveCookie());
207: }
208: }
209:
210: private SaveCookie getSaveCookie() {
211: return new SaveCookie() {
212: public void save() throws IOException {
213: getSchemaEditorSupport().saveDocument();
214: }
215:
216: @Override
217: public int hashCode() {
218: return getClass().hashCode();
219: }
220:
221: @Override
222: public boolean equals(Object other) {
223: return other != null
224: && getClass().equals(other.getClass());
225: }
226: };
227: }
228:
229: @Override
230: public HelpCtx getHelpCtx() {
231: return HelpCtx.DEFAULT_HELP;
232: }
233:
234: protected FileObject handleMove(DataFolder df) throws IOException {
235: // TODO: Launch refactor file dialog
236: if (isModified()) {
237: SaveCookie sCookie = this .getCookie(SaveCookie.class);
238: if (sCookie != null) {
239: sCookie.save();
240: }
241: }
242: return super .handleMove(df);
243: }
244:
245: ////////////////////////////////////////////////////////////////////////////
246: // Class variables
247: ////////////////////////////////////////////////////////////////////////////
248:
249: public static final String SCHEMA_ICON_BASE_WITH_EXT = "org/netbeans/modules/xml/schema/resources/Schema_File.png"; // NOI18N
250: private static final long serialVersionUID = -8229569186860053169L;
251:
252: ////////////////////////////////////////////////////////////////////////////
253: // Instance variables
254: ////////////////////////////////////////////////////////////////////////////
255: private transient SchemaEditorSupport schemaEditorSupport;
256: }
|