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: package org.netbeans.modules.compapp.casaeditor;
042:
043: import org.openide.filesystems.FileObject;
044: import org.openide.loaders.DataObjectExistsException;
045: import org.openide.loaders.MultiDataObject;
046: import org.openide.nodes.CookieSet;
047: import org.openide.nodes.Node;
048:
049: import java.io.IOException;
050: import java.util.concurrent.atomic.AtomicBoolean;
051: import java.util.concurrent.atomic.AtomicReference;
052: import org.netbeans.modules.compapp.casaeditor.design.CasaModelGraphUtilities;
053: import org.netbeans.modules.compapp.projects.jbi.api.JbiBuildListener;
054: import org.netbeans.modules.compapp.projects.jbi.api.JbiBuildTask;
055: import org.openide.cookies.SaveCookie;
056: import org.openide.loaders.DataFolder;
057: import org.openide.loaders.MultiFileLoader;
058: import org.openide.util.HelpCtx;
059: import org.openide.util.lookup.InstanceContent;
060:
061: /**
062: * @author tli
063: */
064: public class CasaDataObject extends MultiDataObject {
065:
066: static final long serialVersionUID = 7527025549386876556L;
067:
068: public static final String CASA_ICON_BASE_WITH_EXT = "org/netbeans/modules/compapp/casaeditor/resources/service_composition_16.png"; // NOI18N
069:
070: private transient CasaDataEditorSupport editorSupport;
071:
072: private transient Node.Cookie mBuildCookie = new JbiBuildCookie();
073: private transient boolean mIsBuilding;
074:
075: private transient AtomicReference<InstanceContent> myServices = new AtomicReference<InstanceContent>();
076: private transient AtomicBoolean isLookupInit = new AtomicBoolean(
077: false);
078:
079: public CasaDataObject(FileObject pf, MultiFileLoader loader)
080: throws DataObjectExistsException {
081: super (pf, loader);
082:
083: editorSupport = new CasaDataEditorSupport(this );
084:
085: CookieSet set = getCookieSet();
086: set.add(editorSupport);
087: getCookieSet().add(mBuildCookie);
088: }
089:
090: public CasaDataEditorSupport getEditorSupport() {
091: return editorSupport;
092: }
093:
094: @Override
095: protected Node createNodeDelegate() {
096: return new CasaDataNode(this );
097: }
098:
099: @Override
100: public HelpCtx getHelpCtx() {
101: return new HelpCtx(CasaDataObject.class);
102: }
103:
104: @Override
105: protected void handleDelete() throws IOException {
106: if (isModified()) {
107: setModified(false);
108: }
109: getEditorSupport().getEnv().unmarkModified();
110: super .handleDelete();
111: }
112:
113: @Override
114: protected FileObject handleMove(DataFolder df) throws IOException {
115: //TODO:make sure we save file before moving This is what jave move does.
116: //It also launch move refactoring dialog which we should be doing
117: //as well
118: if (isModified()) {
119: SaveCookie sCookie = this .getCookie(SaveCookie.class);
120: if (sCookie != null) {
121: sCookie.save();
122: }
123: }
124: return super .handleMove(df);
125: }
126:
127: @Override
128: public void setModified(boolean modified) {
129: super .setModified(modified);
130: if (modified) {
131: getCookieSet().add(getSaveCookie());
132: if (isLookupInit.get()) {
133: myServices.get().add(getSaveCookie());
134: }
135: } else {
136: getCookieSet().remove(getSaveCookie());
137: if (isLookupInit.get()) {
138: myServices.get().remove(getSaveCookie());
139: }
140: }
141: }
142:
143: private SaveCookie getSaveCookie() {
144: return new SaveCookie() {
145:
146: public void save() throws IOException {
147: getEditorSupport().saveDocument();
148: }
149:
150: @Override
151: public int hashCode() {
152: return getClass().hashCode();
153: }
154:
155: @Override
156: public boolean equals(Object other) {
157: return other != null
158: && getClass().equals(other.getClass());
159: }
160: };
161: }
162:
163: public boolean isBuilding() {
164: return mIsBuilding;
165: }
166:
167: private class JbiBuildCookie implements Node.Cookie,
168: JbiBuildListener {
169: public void buildStarted(JbiBuildTask task) {
170: mIsBuilding = true;
171: if (editorSupport != null
172: && editorSupport.getScene() != null) {
173: // Immediately disable edits to the scene.
174: CasaModelGraphUtilities.setSceneEnabled(editorSupport
175: .getScene(), task);
176: }
177: }
178:
179: public void buildCompleted(boolean isSuccessful) {
180: mIsBuilding = false;
181: if (!isSuccessful) {
182: if (editorSupport != null
183: && editorSupport.getScene() != null) {
184: CasaModelGraphUtilities.setSceneEnabled(
185: editorSupport.getScene(), true);
186: }
187: } else {
188: // Do not set the scene enabled at this point, because the build
189: // is still processing the model. We simply set the scene enabled
190: // at render time, which is a safe time to allow editability.
191: }
192: }
193: }
194: }
|