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-2006 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.form;
043:
044: import org.netbeans.api.java.loaders.JavaDataSupport;
045: import org.openide.filesystems.FileObject;
046: import org.openide.filesystems.FileUtil;
047: import org.openide.loaders.DataObjectExistsException;
048: import org.openide.loaders.FileEntry;
049: import org.openide.loaders.MultiDataObject;
050: import org.openide.loaders.MultiFileLoader;
051:
052: /** Loader for Forms. Recognizes file with extension .form and .java and with extension class if
053: * there is their source and form file.
054: *
055: * @author Ian Formanek
056: */
057: public class FormDataLoader extends MultiFileLoader {
058: /** The standard extensions of the recognized files */
059: public static final String FORM_EXTENSION = "form"; // NOI18N
060: /** The standard extension for Java source files. */
061: public static final String JAVA_EXTENSION = "java"; // NOI18N
062:
063: static final long serialVersionUID = 7259146057404524013L;
064:
065: /** Constructs a new FormDataLoader */
066: public FormDataLoader() {
067: super ("org.netbeans.modules.form.FormDataObject"); // NOI18N
068: }
069:
070: /** Gets default display name. Overides superclass method. */
071: @Override
072: protected String defaultDisplayName() {
073: return org.openide.util.NbBundle
074: .getBundle(FormDataLoader.class).getString(
075: "PROP_FormLoader_Name"); // NOI18N
076: }
077:
078: @Override
079: protected String actionsContext() {
080: return "Loaders/text/x-java/Actions/"; // NOI18N
081: }
082:
083: /** For a given file finds a primary file.
084: * @param fo the file to find primary file for
085: *
086: * @return the primary file for the file or null if the file is not
087: * recognized by this loader
088: */
089: @Override
090: protected FileObject findPrimaryFile(FileObject fo) {
091: // never recognize folders.
092: if (fo.isFolder())
093: return null;
094: String ext = fo.getExt();
095: if (ext.equals(FORM_EXTENSION))
096: return FileUtil.findBrother(fo, JAVA_EXTENSION);
097:
098: FileObject javaFile = findJavaPrimaryFile(fo);
099: return javaFile != null
100: && FileUtil.findBrother(javaFile, FORM_EXTENSION) != null ? javaFile
101: : null;
102: }
103:
104: /** Creates the right data object for given primary file.
105: * It is guaranteed that the provided file is realy primary file
106: * returned from the method findPrimaryFile.
107: *
108: * @param primaryFile the primary file
109: * @return the data object for this file
110: * @exception DataObjectExistsException if the primary file already has data object
111: */
112: @Override
113: protected MultiDataObject createMultiObject(FileObject primaryFile)
114: throws DataObjectExistsException {
115: return new FormDataObject(FileUtil.findBrother(primaryFile,
116: FORM_EXTENSION), primaryFile, this );
117: }
118:
119: // from JavaDataLoader
120: // [?] Probably needed in case FormDataObject is deserialized, then the
121: // secondary entry is created additionally.
122: @Override
123: protected MultiDataObject.Entry createSecondaryEntry(
124: MultiDataObject obj, FileObject secondaryFile) {
125: assert FORM_EXTENSION.equals(secondaryFile.getExt());
126:
127: FileEntry formEntry = new FileEntry(obj, secondaryFile);
128: ((FormDataObject) obj).formEntry = formEntry;
129: return formEntry;
130: }
131:
132: @Override
133: protected MultiDataObject.Entry createPrimaryEntry(
134: MultiDataObject obj, FileObject primaryFile) {
135: return JavaDataSupport.createJavaFileEntry(obj, primaryFile);
136: }
137:
138: private FileObject findJavaPrimaryFile(FileObject fo) {
139: if (fo.getExt().equals(JAVA_EXTENSION))
140: return fo;
141: return null;
142: }
143: }
|