KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > wsdl > config > DD2beansDataObject


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
20 package org.netbeans.modules.websvc.wsdl.config;
21
22 import java.awt.event.ActionListener JavaDoc;
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 JavaDoc;
32 import org.openide.util.WeakListeners;
33
34 /** Represents a DD2beansDataObject in the Repository.
35  *
36  * @author mkuchtiak
37  */

38 public abstract class DD2beansDataObject extends XMLJ2eeDataObject implements org.openide.nodes.CookieSet.Factory{
39
40     private static final int DELAY_FOR_TIMER=200;
41     /** Private request processor for parsing and text generating tasks */
42     protected final static RequestProcessor RP = new RequestProcessor("XML Parsing"); //NOI18N
43
private RequestProcessor.Task generationTask;
44     // constructor settings
45
private boolean saveAfterNodeChanges;
46     private Timer JavaDoc timer;
47     private String JavaDoc prefixMark;
48     private int numberOfStartedGens;
49     ActionListener JavaDoc 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         //System.out.println("DD2beansDataObject() "+this+" "+pf.hashCode());
62
//Thread.dumpStack();
63
this.saveAfterNodeChanges=saveAfterNodeChanges;
64         init();
65     }
66
67     private void init () {
68         timerListener = new java.awt.event.ActionListener JavaDoc() {
69             public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
70                 //updatingDocumentFromNode=true;
71
restartGen();
72             }
73         };
74         // initialize timer
75
timer = new Timer JavaDoc(0, null);
76         timer.addActionListener ((ActionListener JavaDoc) WeakListeners.create (ActionListener JavaDoc.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     /** Create document from the Node. This method is called after Node (Node properties)is changed.
89     * The document is generated from data modul (isDocumentGenerable=true)
90     */

91     protected abstract String JavaDoc generateDocument();
92
93     /** setter for prefixMark. This is information, which prefix in xml document should be preserved
94     * after replacing by new generated document (This is mainly for preserving comments at the beginning)
95     * @param prefix prefixMark
96     */

97     protected final void setPrefixMark(String JavaDoc prefix) {
98         this.prefixMark=prefix;
99     }
100     /** gettert for prefixMark
101     * @return prefixMark
102     */

103     protected final String JavaDoc getPrefixMark() {
104         return prefixMark;
105     }
106
107     /** Setter for property nodeDirty.
108      * @param dirty New value of property nodeDirty.
109      */

110     public void setNodeDirty(boolean dirty){
111         //System.out.println("setNodeDirty("+dirty+")");
112
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         //System.out.println("restart Gen");
126
generationTask = null;
127         generationTask = RP.postRequest(new Runnable JavaDoc() {
128             public void run() {
129                 final String JavaDoc newDoc = generateDocument();
130                 javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc() {
131                     public void run() {
132                         try {
133                             javax.swing.text.Document JavaDoc 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                             // this is necessary for correct undo behaviour
143
getEditorSupport().getUndo().discardAllEdits();
144                         } catch (javax.swing.text.BadLocationException JavaDoc 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