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 java.io.IOException;
045: import org.openide.cookies.EditCookie;
046: import org.openide.cookies.OpenCookie;
047: import org.openide.filesystems.FileObject;
048: import org.openide.loaders.DataFolder;
049: import org.openide.loaders.DataObject;
050: import org.openide.loaders.DataObjectExistsException;
051: import org.openide.loaders.FileEntry;
052: import org.openide.loaders.MultiDataObject;
053: import org.openide.loaders.SaveAsCapable;
054: import org.openide.nodes.Node;
055: import org.openide.nodes.Node.Cookie;
056: import org.openide.util.Lookup;
057:
058: /** The DataObject for forms.
059: *
060: * @author Ian Formanek, Petr Hamernik
061: */
062: public class FormDataObject extends MultiDataObject {
063: /** generated Serialized Version UID */
064: // static final long serialVersionUID = 7952143476761137063L;
065: //--------------------------------------------------------------------
066: // Private variables
067: // /** If true, a postInit method is called after reparsing - used after createFromTemplate */
068: // transient private boolean templateInit;
069: // /** If true, the form is marked as modified after regeneration - used if created from template */
070: // transient private boolean modifiedInit;
071: // /** A flag to prevent multiple registration of ComponentRefListener */
072: // transient private boolean componentRefRegistered;
073: transient private FormEditorSupport formEditor;
074:
075: transient private OpenEdit openEdit;
076:
077: /** The entry for the .form file */
078: FileEntry formEntry;
079:
080: //--------------------------------------------------------------------
081: // Constructors
082:
083: static final long serialVersionUID = -975322003627854168L;
084:
085: public FormDataObject(FileObject ffo, FileObject jfo,
086: FormDataLoader loader) throws DataObjectExistsException {
087: super (jfo, loader);
088: formEntry = (FileEntry) registerEntry(ffo);
089: getCookieSet().assign(SaveAsCapable.class, new SaveAsCapable() {
090: public void saveAs(FileObject folder, String fileName)
091: throws IOException {
092: getFormEditorSupport().saveAs(folder, fileName);
093: }
094: });
095: }
096:
097: //--------------------------------------------------------------------
098: // Other methods
099:
100: @Override
101: public <T extends Cookie> T getCookie(Class<T> type) {
102: T retValue;
103:
104: if (OpenCookie.class.equals(type)
105: || EditCookie.class.equals(type)) {
106: if (openEdit == null)
107: openEdit = new OpenEdit();
108: retValue = type.cast(openEdit);
109: } else if (type.isAssignableFrom(FormEditorSupport.class)) {
110: retValue = (T) getFormEditorSupport();
111: } else {
112: retValue = super .getCookie(type);
113: }
114: return retValue;
115: }
116:
117: @Override
118: public Lookup getLookup() {
119: return isValid() ? getNodeDelegate().getLookup() : Lookup.EMPTY;
120: }
121:
122: private class OpenEdit implements OpenCookie, EditCookie {
123: public void open() {
124: // open form editor with form designer selected
125: getFormEditorSupport().openFormEditor(true);
126: }
127:
128: public void edit() {
129: // open form editor with java editor selected (form not loaded)
130: getFormEditorSupport().open();
131: }
132: }
133:
134: public FileObject getFormFile() {
135: return formEntry.getFile();
136: }
137:
138: public boolean isReadOnly() {
139: FileObject javaFO = getPrimaryFile();
140: FileObject formFO = formEntry.getFile();
141: return !javaFO.canWrite() || !formFO.canWrite();
142: }
143:
144: public boolean formFileReadOnly() {
145: return !formEntry.getFile().canWrite();
146: }
147:
148: public synchronized FormEditorSupport getFormEditorSupport() {
149: if (formEditor == null) {
150: formEditor = new FormEditorSupport(getPrimaryEntry(), this ,
151: getCookieSet());
152: }
153: return formEditor;
154: }
155:
156: // PENDING remove when form_new_layout is merged to trunk
157: public FormEditorSupport getFormEditor() {
158: return getFormEditorSupport();
159: }
160:
161: // END of PENDING
162:
163: FileEntry getFormEntry() {
164: return formEntry;
165: }
166:
167: /** Provides node that should represent this data object. When a node for
168: * representation in a parent is requested by a call to getNode(parent) it
169: * is the exact copy of this node with only parent changed. This
170: * implementation creates instance <CODE>DataNode</CODE>. <P> This method
171: * is called only once.
172: *
173: * @return the node representation for this data object
174: * @see DataNode
175: */
176: @Override
177: protected Node createNodeDelegate() {
178: FormDataNode node = new FormDataNode(this );
179: return node;
180: }
181:
182: //--------------------------------------------------------------------
183: // Serialization
184:
185: private void readObject(java.io.ObjectInputStream is)
186: throws java.io.IOException, ClassNotFoundException {
187: is.defaultReadObject();
188: }
189:
190: @Override
191: protected DataObject handleCopyRename(DataFolder df, String name,
192: String ext) throws IOException {
193: FileObject fo = getPrimaryEntry().copyRename(
194: df.getPrimaryFile(), name, ext);
195: return DataObject.find(fo);
196: }
197:
198: }
|