1 19 20 package org.netbeans.modules.j2ee.ddloaders.common; 21 22 import org.openide.cookies.SaveCookie; 23 import org.openide.filesystems.FileObject; 24 import org.openide.loaders.MultiFileLoader; 25 import org.openide.util.RequestProcessor; 26 import org.netbeans.modules.j2ee.ddloaders.common.xmlutils.XMLJ2eeDataObject; 27 import org.netbeans.modules.j2ee.ddloaders.common.xmlutils.XMLJ2eeUtils; 28 29 import javax.swing.*; 30 import javax.swing.text.Document ; 31 import java.io.IOException ; 32 33 37 public abstract class DD2beansDataObject extends XMLJ2eeDataObject implements org.openide.nodes.CookieSet.Factory{ 38 39 private static final int DELAY_FOR_TIMER=200; 40 41 protected final static RequestProcessor RP = new RequestProcessor("XML Parsing"); private final RequestProcessor.Task generationTask; 43 private String prefixMark; 45 46 private static final long serialVersionUID = -5363900668319174348L; 47 48 public DD2beansDataObject(FileObject pf, MultiFileLoader loader) 49 throws org.openide.loaders.DataObjectExistsException { 50 this (pf, loader,true); 51 } 52 53 public DD2beansDataObject(FileObject pf, MultiFileLoader loader, final boolean saveAfterNodeChanges) 54 throws org.openide.loaders.DataObjectExistsException { 55 super (pf, loader); 56 57 generationTask = RP.create(new Runnable () { 58 int numberOfStartedGens; 59 public void run() { 60 numberOfStartedGens++; 61 final String newDoc = generateDocument(); 62 SwingUtilities.invokeLater(new Runnable () { 63 public void run() { 64 try { 65 Document doc = getEditorSupport().openDocument(); 66 XMLJ2eeUtils.replaceDocument(doc, newDoc, prefixMark); 67 setDocumentValid(true); 68 if (saveAfterNodeChanges) { 69 SaveCookie savec = (SaveCookie) getCookie(SaveCookie.class); 70 if (savec != null) { 71 savec.save(); 72 } 73 } 74 getEditorSupport().getUndo().discardAllEdits(); 76 } catch (javax.swing.text.BadLocationException e) { 77 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e); 78 } catch (IOException e) { 79 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e); 80 } finally { 81 synchronized (generationTask) { 82 numberOfStartedGens--; 83 if (numberOfStartedGens == 0) { 84 nodeDirty = false; 85 } 86 } 87 } 88 } 89 }); 90 } 91 }); 92 } 93 94 97 protected abstract String generateDocument(); 98 99 103 protected final void setPrefixMark(String prefix) { 104 this.prefixMark=prefix; 105 } 106 109 protected final String getPrefixMark() { 110 return prefixMark; 111 } 112 113 116 public void setNodeDirty(boolean dirty){ 117 if (dirty) { 119 synchronized (this) { 120 nodeDirty=true; 121 restartGen(); 122 } 123 } 124 } 125 126 public RequestProcessor.Task getGenerationTask(){ 127 return generationTask; 128 } 129 130 protected void restartGen() { 131 generationTask.schedule(DELAY_FOR_TIMER); 132 } 133 } 134 | Popular Tags |