KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > multiview > DDMultiViewDataObject


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.ddloaders.multiview;
20
21 import javax.swing.SwingUtilities JavaDoc;
22 import org.netbeans.modules.j2ee.dd.impl.common.DDProviderDataObject;
23 import org.netbeans.modules.j2ee.dd.api.common.RootInterface;
24 import org.netbeans.modules.j2ee.common.TransactionSupport;
25 import org.netbeans.modules.j2ee.common.Transaction;
26 import org.netbeans.modules.xml.multiview.XmlMultiViewDataObject;
27 import org.netbeans.modules.xml.multiview.XmlMultiViewDataSynchronizer;
28 import org.openide.DialogDisplayer;
29 import org.openide.ErrorManager;
30 import org.openide.NotifyDescriptor;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileLock;
33 import org.openide.loaders.DataObjectExistsException;
34 import org.openide.loaders.MultiFileLoader;
35 import org.openide.util.NbBundle;
36
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.ByteArrayOutputStream JavaDoc;
40 import java.io.Reader JavaDoc;
41 import java.util.Date JavaDoc;
42 import java.lang.ref.WeakReference JavaDoc;
43 import org.openide.util.RequestProcessor;
44
45 /**
46  * @author pfiala
47  */

48 public abstract class DDMultiViewDataObject extends XmlMultiViewDataObject
49         implements DDProviderDataObject, TransactionSupport {
50
51
52     private WeakReference JavaDoc transactionReference = null;
53     private static final int HANDLE_UNPARSABLE_TIMEOUT = 2000;
54     private DDMultiViewDataObject.ModelSynchronizer modelSynchronizer;
55
56     public DDMultiViewDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException {
57         super(pf, loader);
58         modelSynchronizer = new ModelSynchronizer(this);
59     }
60
61     public void modelUpdatedFromUI() {
62         modelSynchronizer.requestUpdateData();
63     }
64
65     public XmlMultiViewDataSynchronizer getModelSynchronizer() {
66         return modelSynchronizer;
67     }
68
69     public void checkParseable() {
70         if (!isDocumentParseable()) {
71             NotifyDescriptor desc = new org.openide.NotifyDescriptor.Message(
72                     NbBundle.getMessage(DDMultiViewDataObject.class, "TXT_DocumentUnparsable",
73                             getPrimaryFile().getNameExt()), NotifyDescriptor.WARNING_MESSAGE);
74             DialogDisplayer.getDefault().notify(desc);
75             // postpone the "Switch to XML View" action to the end of event dispatching thread
76
// this enables to finish the current action first (e.g. painting particular view)
77
// see the issue 67580
78
SwingUtilities.invokeLater(new Runnable JavaDoc(){
79                 public void run() {
80                     goToXmlView();
81                 }
82             });
83         }
84     }
85
86     public InputStream JavaDoc createInputStream() {
87         return getDataCache().createInputStream();
88     }
89
90     public Reader JavaDoc createReader() throws IOException JavaDoc {
91         return getDataCache().createReader();
92     }
93
94     public void writeModel(RootInterface model) throws IOException JavaDoc {
95         if (transactionReference != null && transactionReference.get() != null) {
96             return;
97         }
98         FileLock dataLock = waitForLock();
99         if (dataLock == null) {
100             return;
101         }
102         try {
103             if (((ModelSynchronizer) getModelSynchronizer()).mayUpdateData(true)) {
104                 writeModel(model, dataLock);
105             }
106         } finally {
107             dataLock.releaseLock();
108         }
109     }
110
111     public void writeModel(RootInterface model, FileLock dataLock) {
112         ModelSynchronizer synchronizer = (ModelSynchronizer) getModelSynchronizer();
113         modelSynchronizer.getReloadTask().cancel();
114         ((RootInterface) synchronizer.getModel()).merge(model, RootInterface.MERGE_UPDATE);
115         synchronizer.updateData(dataLock, false);
116     }
117
118     public FileLock getDataLock() {
119         try {
120             return getModelSynchronizer().takeLock();
121         } catch (IOException JavaDoc e) {
122             ErrorManager.getDefault().notify(e);
123         }
124         return null;
125     }
126
127     /**
128      * Used to detect if data model has already been created or not.
129      * Method is called before switching to the design view from XML view when the document isn't parseable.
130      */

131     protected abstract boolean isModelCreated();
132
133     /**
134      * @throws IOException
135      */

136     protected abstract void parseDocument() throws IOException JavaDoc;
137
138     /**
139      * @throws IOException
140      */

141     protected abstract void validateDocument() throws IOException JavaDoc;
142
143     /**
144      * Update text document from data model. Called when something is changed in visual editor.
145      * @param model
146      */

147     protected String JavaDoc generateDocumentFromModel(RootInterface model) {
148         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
149         try {
150             model.write(out);
151             out.close();
152             return out.toString("UTF8"); //NOI18N
153
} catch (IOException JavaDoc e) {
154             ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);
155         } catch (IllegalStateException JavaDoc e) {
156             ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);
157         }
158         return out.toString ();
159     }
160
161     /**
162      * Returns model of the deployment descriptor
163      * @return the model
164      */

165     protected abstract RootInterface getDDModel();
166
167     /**
168      * Returns true if xml file is parseable(data model can be created),
169      * Method is called before switching to the design view from XML view when the document isn't parseable.
170      */

171     protected abstract boolean isDocumentParseable();
172
173     public Transaction openTransaction() {
174         final XmlMultiViewDataSynchronizer.Transaction synchronizerTransaction = getModelSynchronizer().openTransaction();
175         if (synchronizerTransaction == null) {
176             return null;
177         } else {
178             Transaction transaction = new Transaction() {
179                 public void rollback() {
180                     synchronizerTransaction.rollback();
181                     transactionReference = null;
182                 }
183
184                 public void commit() throws IOException JavaDoc {
185                     synchronizerTransaction.commit();
186                     transactionReference = null;
187                 }
188             };
189             transactionReference = new WeakReference JavaDoc(transaction);
190             return transaction;
191         }
192     }
193
194     private class ModelSynchronizer extends XmlMultiViewDataSynchronizer {
195         private long handleUnparseableTimeout = 0;
196         private Boolean JavaDoc overwriteUnparseable = Boolean.TRUE;
197
198         public ModelSynchronizer(XmlMultiViewDataObject dataObject) {
199             super(dataObject, 300);
200             handleUnparseableTimeout = 0;
201             overwriteUnparseable = Boolean.TRUE;
202         }
203
204         protected boolean mayUpdateData(boolean allowDialog) {
205             if (isDocumentParseable()) {
206                 return true;
207             }
208             if (!allowDialog) {
209                 return false;
210             }
211             if (handleUnparseableTimeout != -1) {
212                 long time = new Date JavaDoc().getTime();
213                 if (time > handleUnparseableTimeout) {
214                     handleUnparseableTimeout = -1;
215                     org.netbeans.modules.xml.multiview.Utils.runInAwtDispatchThread(new Runnable JavaDoc() {
216                         public void run() {
217                             String JavaDoc message = NbBundle.getMessage(XmlMultiViewDataObject.class,
218                                     "TXT_OverwriteUnparsableDocument", getPrimaryFile().getNameExt());
219                             NotifyDescriptor desc = new NotifyDescriptor.Confirmation(message,
220                                     NotifyDescriptor.YES_NO_OPTION, NotifyDescriptor.WARNING_MESSAGE);
221                             DialogDisplayer.getDefault().notify(desc);
222                             overwriteUnparseable = Boolean.valueOf(desc.getValue() == NotifyDescriptor.YES_OPTION);
223                             handleUnparseableTimeout = new Date JavaDoc().getTime() + HANDLE_UNPARSABLE_TIMEOUT;
224                         }
225                     });
226                 }
227             }
228             return overwriteUnparseable.booleanValue();
229         }
230
231         public void updateData(FileLock dataLock, boolean modify) {
232             super.updateData(dataLock, modify);
233             try {
234                 validateDocument();
235             } catch (IOException JavaDoc e) {
236                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
237             }
238         }
239
240         protected void updateDataFromModel(Object JavaDoc model, FileLock lock, boolean modify) {
241             String JavaDoc newDocument = generateDocumentFromModel((RootInterface) model);
242
243             try {
244                 getDataCache().setData(lock, newDocument, modify);
245             } catch (IOException JavaDoc e) {
246                 ErrorManager.getDefault().notify(e);
247             }
248         }
249
250         protected Object JavaDoc getModel() {
251             return getDDModel();
252         }
253
254         protected void reloadModelFromData() {
255             try {
256                 parseDocument();
257             } catch (IOException JavaDoc e) {
258                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
259             }
260         }
261     }
262 }
263
Popular Tags