KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > ddloaders > common > 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.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 JavaDoc;
31 import java.io.IOException JavaDoc;
32
33 /** Represents a DD2beansDataObject in the Repository.
34  *
35  * @author mkuchtiak
36  */

37 public abstract class DD2beansDataObject extends XMLJ2eeDataObject implements org.openide.nodes.CookieSet.Factory{
38
39     private static final int DELAY_FOR_TIMER=200;
40     /** Private request processor for parsing and text generating tasks */
41     protected final static RequestProcessor RP = new RequestProcessor("XML Parsing"); //NOI18N
42
private final RequestProcessor.Task generationTask;
43     // constructor settings
44
private String JavaDoc 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 JavaDoc() {
58             int numberOfStartedGens;
59             public void run() {
60                 numberOfStartedGens++;
61                 final String JavaDoc newDoc = generateDocument();
62                 SwingUtilities.invokeLater(new Runnable JavaDoc() {
63                     public void run() {
64                         try {
65                             Document JavaDoc 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                             // this is necessary for correct undo behaviour
75
getEditorSupport().getUndo().discardAllEdits();
76                         } catch (javax.swing.text.BadLocationException JavaDoc e) {
77                             org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);
78                         } catch (IOException JavaDoc 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     /** Create document from the Node. This method is called after Node (Node properties)is changed.
95     * The document is generated from data modul (isDocumentGenerable=true)
96     */

97     protected abstract String JavaDoc generateDocument();
98
99     /** setter for prefixMark. This is information, which prefix in xml document should be preserved
100     * after replacing by new generated document (This is mainly for preserving comments at the beginning)
101     * @param prefix prefixMark
102     */

103     protected final void setPrefixMark(String JavaDoc prefix) {
104         this.prefixMark=prefix;
105     }
106     /** gettert for prefixMark
107     * @return prefixMark
108     */

109     protected final String JavaDoc getPrefixMark() {
110         return prefixMark;
111     }
112
113     /** Setter for property nodeDirty.
114      * @param dirty New value of property nodeDirty.
115      */

116     public void setNodeDirty(boolean dirty){
117         //System.out.println("setNodeDirty("+dirty+")");
118
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