1 19 20 package org.netbeans.modules.websvc.wsdl.config; 21 22 import java.awt.event.ActionListener ; 23 import org.openide.cookies.SaveCookie; 24 import org.openide.filesystems.FileObject; 25 import org.openide.loaders.MultiFileLoader; 26 import org.openide.util.RequestProcessor; 27 import org.netbeans.modules.websvc.wsdl.xmlutils.XMLJ2eeDataObject; 28 import org.netbeans.modules.websvc.wsdl.xmlutils.XMLJ2eeUtils; 29 30 import java.io.*; 31 import javax.swing.Timer ; 32 import org.openide.util.WeakListeners; 33 34 38 public abstract class DD2beansDataObject extends XMLJ2eeDataObject implements org.openide.nodes.CookieSet.Factory{ 39 40 private static final int DELAY_FOR_TIMER=200; 41 42 protected final static RequestProcessor RP = new RequestProcessor("XML Parsing"); private RequestProcessor.Task generationTask; 44 private boolean saveAfterNodeChanges; 46 private Timer timer; 47 private String prefixMark; 48 private int numberOfStartedGens; 49 ActionListener timerListener; 50 51 private static final long serialVersionUID = -5363900668319174348L; 52 53 public DD2beansDataObject(FileObject pf, MultiFileLoader loader) 54 throws org.openide.loaders.DataObjectExistsException { 55 this (pf, loader,true); 56 } 57 58 public DD2beansDataObject(FileObject pf, MultiFileLoader loader, boolean saveAfterNodeChanges) 59 throws org.openide.loaders.DataObjectExistsException { 60 super (pf, loader); 61 this.saveAfterNodeChanges=saveAfterNodeChanges; 64 init(); 65 } 66 67 private void init () { 68 timerListener = new java.awt.event.ActionListener () { 69 public void actionPerformed(java.awt.event.ActionEvent e) { 70 restartGen(); 72 } 73 }; 74 timer = new Timer (0, null); 76 timer.addActionListener ((ActionListener ) WeakListeners.create (ActionListener .class, timerListener, timer)); 77 timer.setInitialDelay(DELAY_FOR_TIMER); 78 timer.setRepeats(false); 79 } 80 81 private synchronized void restartTimer(){ 82 if (!timer.isRunning()){ 83 numberOfStartedGens++; 84 } 85 timer.restart(); 86 } 87 88 91 protected abstract String generateDocument(); 92 93 97 protected final void setPrefixMark(String prefix) { 98 this.prefixMark=prefix; 99 } 100 103 protected final String getPrefixMark() { 104 return prefixMark; 105 } 106 107 110 public void setNodeDirty(boolean dirty){ 111 if (dirty) { 113 synchronized (this) { 114 nodeDirty=true; 115 restartTimer(); 116 } 117 } 118 } 119 120 public RequestProcessor.Task getGenerationTask(){ 121 return generationTask; 122 } 123 124 protected void restartGen() { 125 generationTask = null; 127 generationTask = RP.postRequest(new Runnable () { 128 public void run() { 129 final String newDoc = generateDocument(); 130 javax.swing.SwingUtilities.invokeLater(new Runnable () { 131 public void run() { 132 try { 133 javax.swing.text.Document doc = getEditorSupport().openDocument(); 134 XMLJ2eeUtils.replaceDocument(doc,newDoc,prefixMark); 135 setDocumentValid(true); 136 if (saveAfterNodeChanges) { 137 SaveCookie savec = (SaveCookie) getCookie(SaveCookie.class); 138 if (savec!=null) { 139 savec.save(); 140 } 141 } 142 getEditorSupport().getUndo().discardAllEdits(); 144 } catch (javax.swing.text.BadLocationException e) { 145 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e); 146 } catch (IOException e) { 147 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e); 148 } finally { 149 synchronized (DD2beansDataObject.this) { 150 numberOfStartedGens--; 151 if (numberOfStartedGens==0) { 152 nodeDirty=false; 153 } 154 } 155 } 156 } 157 }); 158 } 159 }); 160 } 161 } 162 | Popular Tags |