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: package org.netbeans.modules.vmd.io.javame;
042:
043: import org.netbeans.modules.mobility.editor.pub.J2MEDataObject;
044: import org.netbeans.modules.vmd.api.io.providers.DataObjectInterface;
045: import org.netbeans.modules.vmd.api.io.providers.IOSupport;
046: import org.netbeans.modules.vmd.api.io.providers.DocumentSerializer;
047: import org.netbeans.modules.vmd.api.model.Debug;
048: import org.openide.filesystems.*;
049: import org.openide.loaders.DataObjectExistsException;
050: import org.openide.loaders.MultiFileLoader;
051: import org.openide.nodes.CookieSet;
052: import org.openide.nodes.Node;
053: import org.openide.util.WeakListeners;
054: import org.openide.util.NbBundle;
055: import org.openide.windows.TopComponent;
056: import org.openide.NotifyDescriptor;
057: import org.openide.DialogDisplayer;
058:
059: import javax.swing.text.StyledDocument;
060: import javax.swing.*;
061: import java.io.IOException;
062: import java.util.concurrent.atomic.AtomicBoolean;
063:
064: /**
065: * @author David Kaspar
066: */
067: public final class MEDesignDataObject extends J2MEDataObject implements
068: DataObjectInterface, FileChangeListener {
069:
070: // private FileObject javaFile;
071: // private FileObject designFile;
072: private MEDesignEditorSupport editorSupport;
073: private AtomicBoolean dirty = new AtomicBoolean(false);
074:
075: public MEDesignDataObject(FileObject javaFile,
076: FileObject designFile, MultiFileLoader loader)
077: throws DataObjectExistsException {
078: super (javaFile, loader);
079: ((MEDesignDataLoader) loader).createSecondaryEntry(this ,
080: designFile);
081: //this.javaFile = javaFile;
082: //this.designFile = designFile;
083:
084: editorSupport = new MEDesignEditorSupport(this );
085:
086: CookieSet cookies = getCookieSet();
087: cookies.add(editorSupport);
088:
089: designFile.addFileChangeListener(WeakListeners.create(
090: FileChangeListener.class, this , designFile));
091: // javaFile.addFileChangeListener (WeakListeners.create (FileChangeListener.class, this, javaFile)); // handled by the CloneableEditorSupport
092: }
093:
094: public Node createNodeDelegate() {
095: return new MEDesignNode(this );
096: }
097:
098: // public void addSaveCookie (SaveCookie save) {
099: // getCookieSet ().add (save);
100: // }
101:
102: // public void removeSaveCookie (SaveCookie save) {
103: // getCookieSet ().remove (save);
104: // }
105:
106: // public FileObject getJavaFile () {
107: // return javaFile;
108: // }
109:
110: public FileObject getDesignFile() {
111: return FileUtil.findBrother(getPrimaryFile(), "vmd"); //NOI18N
112: }
113:
114: public void discardAllEditorSupportEdits() {
115: editorSupport.discardAllEdits();
116: }
117:
118: public void notifyEditorSupportModified() {
119: editorSupport.notifyModified();
120: }
121:
122: public void setMVTC(TopComponent multiViewTopComponent) {
123: editorSupport.setMVTC(multiViewTopComponent);
124: }
125:
126: public StyledDocument getEditorDocument() {
127: try {
128: return editorSupport.openDocument();
129: } catch (IOException e) {
130: throw Debug.error(e);
131: }
132: }
133:
134: public MEDesignEditorSupport getEditorSupport() {
135: return editorSupport;
136: }
137:
138: @Override
139: protected synchronized J2MEEditorSupport createJavaEditorSupport() {
140: return editorSupport;
141: }
142:
143: public void fileFolderCreated(FileEvent fe) {
144: // do nothing
145: }
146:
147: public void fileDataCreated(FileEvent fe) {
148: // do nothing
149: }
150:
151: public void fileChanged(FileEvent fe) {
152: if (!fe.firedFrom(editorSupport.getAtomicSaveAction()))
153: reloadDesign();
154: }
155:
156: public void fileDeleted(FileEvent fe) {
157: // do nothing
158: }
159:
160: public void fileRenamed(FileRenameEvent fe) {
161: // do nothing
162: }
163:
164: public void fileAttributeChanged(FileAttributeEvent fe) {
165: // do nothing
166: }
167:
168: private void reloadDesign() {
169: if (dirty.compareAndSet(true, true))
170: return;
171: SwingUtilities.invokeLater(new Runnable() {
172: public void run() {
173: dirty.set(false);
174: NotifyDescriptor.Confirmation confirmation = new NotifyDescriptor.Confirmation(
175: NbBundle.getMessage(MEDesignDataObject.class,
176: "MSG_ConfirmReload", getDesignFile()
177: .getPath()), // NOI18N
178: NotifyDescriptor.YES_NO_OPTION,
179: NotifyDescriptor.QUESTION_MESSAGE);
180: if (DialogDisplayer.getDefault().notify(confirmation) != NotifyDescriptor.YES_OPTION)
181: return;
182: DocumentSerializer documentSerializer = IOSupport
183: .getDocumentSerializer(MEDesignDataObject.this);
184: if (documentSerializer.isLoadingOrLoaded())
185: documentSerializer.restartLoadingDocument();
186: }
187: });
188: }
189:
190: }
|