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.visualweb.project.jsfloader;
043:
044: import java.io.IOException;
045:
046: import org.openide.ErrorManager;
047: import org.openide.cookies.EditCookie;
048: import org.openide.cookies.OpenCookie;
049: import org.openide.cookies.SaveCookie;
050: import org.openide.filesystems.FileObject;
051: import org.openide.loaders.DataFolder;
052: import org.openide.loaders.DataObject;
053: import org.openide.loaders.DataObjectExistsException;
054: import org.openide.loaders.DataObjectNotFoundException;
055: import org.openide.loaders.MultiDataObject;
056: import org.openide.nodes.Node;
057: import org.openide.nodes.Node.Cookie;
058: import org.openide.util.HelpCtx;
059:
060: import org.netbeans.modules.visualweb.api.insync.JsfJavaDataObjectMarker;
061: import org.netbeans.modules.visualweb.project.jsf.api.JsfDataObjectException;
062: import org.openide.cookies.EditorCookie;
063: import org.openide.nodes.CookieSet;
064: import org.openide.util.Lookup;
065:
066: /**
067: * Object that represents one JSF java data object.
068: *
069: * @author Peter Zavadsky
070: */
071: public class JsfJavaDataObject extends MultiDataObject implements
072: JsfJavaDataObjectMarker, CookieSet.Factory {
073:
074: static final long serialVersionUID = 8354927561693097159L;
075: static final String JSF_ATTRIBUTE = "jsfjava"; // NOI18N
076:
077: public JsfJavaDataObject(FileObject pf, JsfJavaDataLoader loader)
078: throws DataObjectExistsException {
079: super (pf, loader);
080:
081: CookieSet set = getCookieSet();
082: set.add(OpenCookie.class, this );
083: set.add(EditCookie.class, this );
084: set.add(EditorCookie.class, this );
085: set.add(JsfJavaEditorSupport.class, this );
086: }
087:
088: /** Gets the superclass cookie, without hacking save cookie. */
089: <T extends Node.Cookie> T getPureCookie(Class<T> clazz) {
090: return super .getCookie(clazz);
091: }
092:
093: private OpenEdit openEdit = null;
094:
095: /** Overrides behaviour to provide compound save cookie. */
096: @Override
097: public <T extends Node.Cookie> T getCookie(Class<T> clazz) {
098: if (clazz == SaveCookie.class) {
099: FileObject primaryJsfFileObject = Utils
100: .findJspForJava(getPrimaryFile());
101: if (primaryJsfFileObject != null
102: && primaryJsfFileObject.isValid()) {
103: SaveCookie javaSaveCookie = (SaveCookie) super
104: .getCookie(clazz);
105: JsfJspDataObject jsfJspDataObject = (JsfJspDataObject) getCookie(JsfJspDataObject.class);
106: SaveCookie jspSaveCookie;
107: if (jsfJspDataObject == null) {
108: jspSaveCookie = null;
109: } else {
110: jspSaveCookie = (SaveCookie) jsfJspDataObject
111: .getPureCookie(clazz);
112: }
113:
114: if (javaSaveCookie == null && jspSaveCookie == null) {
115: return null;
116: } else {
117: return clazz.cast(new CompoundSaveCookie(
118: javaSaveCookie, jspSaveCookie));
119: }
120: }
121: } else if (clazz.isAssignableFrom(JsfJavaEditorSupport.class)) {
122: return clazz.cast(getJsfJavaEditorSupport());
123: }
124:
125: return super .getCookie(clazz);
126: }
127:
128: public <T extends Cookie> T createCookie(Class<T> klass) {
129: if (OpenCookie.class.equals(klass)
130: || EditCookie.class.equals(klass)) {
131: if (openEdit == null) {
132: openEdit = new OpenEdit();
133: }
134: return klass.cast(openEdit);
135: } else if (EditorCookie.class.equals(klass)
136: || JsfJavaEditorSupport.class.equals(klass)) {
137: return klass.cast(getJsfJavaEditorSupport());
138: } else {
139: return null;
140: }
141: }
142:
143: /** Hacking access to be able to add the save cookie, see the JsfJavaEditorSupport. */
144: void addSaveCookie(SaveCookie save) {
145: getCookieSet().add(save);
146: }
147:
148: @Override
149: protected Node createNodeDelegate() {
150: return new JsfJavaDataNode(this );
151: }
152:
153: @Override
154: public HelpCtx getHelpCtx() {
155: return new HelpCtx(
156: "org.netbeans.modules.visualweb.project.jsfloader.JsfJavaDataLoader"
157: + ".Obj"); // NOI18N
158: }
159:
160: @Override
161: public Lookup getLookup() {
162: return getCookieSet().getLookup();
163: }
164:
165: private JsfJavaEditorSupport jsfJavaEditor;
166:
167: protected JsfJavaEditorSupport getJsfJavaEditorSupport() {
168: if (jsfJavaEditor == null) {
169: jsfJavaEditor = new JsfJavaEditorSupport(this );
170: }
171: return jsfJavaEditor;
172: }
173:
174: //--------------------------------------------------------------------
175: // Serialization
176:
177: private void readObject(java.io.ObjectInputStream is)
178: throws IOException, ClassNotFoundException {
179: is.defaultReadObject();
180: }
181:
182: ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
183: // <rave>
184: // XXX Hacking problems with cut/copy of .jsp and .java backing file
185: //... dont address to NB, resolve by creating new dataobject type.. possibly maintained with the help of project.
186:
187: private static final ThreadLocal<Boolean> pureCopy = new ThreadLocal<Boolean>();
188:
189: /** Copies only this object without touching the corresponding jsf jsp one.
190: * Used when copying originated form corresponding file. */
191: void pureCopy(DataFolder folder) throws IOException {
192: try {
193: pureCopy.set(Boolean.TRUE);
194: copy(folder);
195: } finally {
196: pureCopy.set(Boolean.FALSE);
197: }
198: }
199:
200: /** Handles copy. Handles also copy of corresponding jsf jsp file. */
201: @Override
202: protected DataObject handleCopy(DataFolder folder)
203: throws IOException {
204: if (pureCopy.get() == Boolean.TRUE) {
205: return super .handleCopy(folder);
206: } else {
207: FileObject jspFile = Utils.findJspForJava(getPrimaryFile());
208: if (jspFile == null) {
209: throw new JsfDataObjectException(
210: "Can't find jsp file for " + this );
211: }
212:
213: DataObject dataObject = super .handleCopy(folder);
214:
215: try {
216: DataObject jspDataObject = DataObject.find(jspFile);
217: if (jspDataObject instanceof JsfJspDataObject) {
218: FileObject jspFolder = Utils
219: .findJspFolderForJava(dataObject
220: .getPrimaryFile());
221: DataFolder jspDataFolder = DataFolder
222: .findFolder(jspFolder);
223: ((JsfJspDataObject) jspDataObject)
224: .pureCopy(jspDataFolder);
225: }
226: } catch (DataObjectNotFoundException dnfe) {
227: ErrorManager.getDefault().notify(
228: ErrorManager.INFORMATIONAL, dnfe);
229: }
230:
231: return dataObject;
232: }
233: }
234:
235: /**
236: * Capture the name the data object had, before doing the rename and update my editor title.
237: *
238: */
239: @Override
240: protected FileObject handleRename(String name) throws IOException {
241: FileObject fo = super .handleRename(name);
242:
243: // XXX Also handle renaming of the multiview.
244: final JsfJavaEditorSupport jsfJavaEditorSupport = (JsfJavaEditorSupport) getCookie(JsfJavaEditorSupport.class);
245: if (jsfJavaEditorSupport != null) {
246: javax.swing.SwingUtilities.invokeLater(new Runnable() {
247: public void run() {
248: jsfJavaEditorSupport.updateMultiViewDisplayName();
249: }
250: });
251: }
252:
253: return fo;
254: }
255:
256: /** Hacking access to be able to remove the save cookie, see the JsfJavaEditorSupport. */
257: protected void removeSaveCookie(SaveCookie save) {
258: // This does not look quite right, but it should work.
259: if (save instanceof CompoundSaveCookie) {
260: CompoundSaveCookie compound = (CompoundSaveCookie) save;
261: SaveCookie pureSave = (SaveCookie) getPureCookie(SaveCookie.class);
262: if (compound.containsCookie(pureSave)) {
263: getCookieSet().remove(pureSave);
264: }
265: }
266: getCookieSet().remove(save);
267: }
268:
269: private class OpenEdit implements OpenCookie, EditCookie {
270: public void open() {
271: getJsfJavaEditorSupport().open();
272: }
273:
274: public void edit() {
275: getJsfJavaEditorSupport().open();
276: }
277: }
278: // </rave>
279: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
280: }
|