1 19 package org.netbeans.modules.j2ee.ddloaders.multiview; 20 21 import javax.swing.SwingUtilities ; 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 ; 38 import java.io.InputStream ; 39 import java.io.ByteArrayOutputStream ; 40 import java.io.Reader ; 41 import java.util.Date ; 42 import java.lang.ref.WeakReference ; 43 import org.openide.util.RequestProcessor; 44 45 48 public abstract class DDMultiViewDataObject extends XmlMultiViewDataObject 49 implements DDProviderDataObject, TransactionSupport { 50 51 52 private WeakReference 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 SwingUtilities.invokeLater(new Runnable (){ 79 public void run() { 80 goToXmlView(); 81 } 82 }); 83 } 84 } 85 86 public InputStream createInputStream() { 87 return getDataCache().createInputStream(); 88 } 89 90 public Reader createReader() throws IOException { 91 return getDataCache().createReader(); 92 } 93 94 public void writeModel(RootInterface model) throws IOException { 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 e) { 122 ErrorManager.getDefault().notify(e); 123 } 124 return null; 125 } 126 127 131 protected abstract boolean isModelCreated(); 132 133 136 protected abstract void parseDocument() throws IOException ; 137 138 141 protected abstract void validateDocument() throws IOException ; 142 143 147 protected String generateDocumentFromModel(RootInterface model) { 148 ByteArrayOutputStream out = new ByteArrayOutputStream (); 149 try { 150 model.write(out); 151 out.close(); 152 return out.toString("UTF8"); } catch (IOException e) { 154 ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e); 155 } catch (IllegalStateException e) { 156 ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e); 157 } 158 return out.toString (); 159 } 160 161 165 protected abstract RootInterface getDDModel(); 166 167 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 { 185 synchronizerTransaction.commit(); 186 transactionReference = null; 187 } 188 }; 189 transactionReference = new WeakReference (transaction); 190 return transaction; 191 } 192 } 193 194 private class ModelSynchronizer extends XmlMultiViewDataSynchronizer { 195 private long handleUnparseableTimeout = 0; 196 private Boolean 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 ().getTime(); 213 if (time > handleUnparseableTimeout) { 214 handleUnparseableTimeout = -1; 215 org.netbeans.modules.xml.multiview.Utils.runInAwtDispatchThread(new Runnable () { 216 public void run() { 217 String 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 ().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 e) { 236 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 237 } 238 } 239 240 protected void updateDataFromModel(Object model, FileLock lock, boolean modify) { 241 String newDocument = generateDocumentFromModel((RootInterface) model); 242 243 try { 244 getDataCache().setData(lock, newDocument, modify); 245 } catch (IOException e) { 246 ErrorManager.getDefault().notify(e); 247 } 248 } 249 250 protected Object getModel() { 251 return getDDModel(); 252 } 253 254 protected void reloadModelFromData() { 255 try { 256 parseDocument(); 257 } catch (IOException e) { 258 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 259 } 260 } 261 } 262 } 263 | Popular Tags |