~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to j2ee/ddloaders/src/org/netbeans/modules/j2ee/ddloaders/common/DD2beansDataObject.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.j2ee.ddloaders.common;
 
43
 
 
44
import org.openide.cookies.SaveCookie;
 
45
import org.openide.filesystems.FileObject;
 
46
import org.openide.loaders.MultiFileLoader;
 
47
import org.openide.util.RequestProcessor;
 
48
import org.netbeans.modules.j2ee.ddloaders.common.xmlutils.XMLJ2eeDataObject;
 
49
import org.netbeans.modules.j2ee.ddloaders.common.xmlutils.XMLJ2eeUtils;
 
50
 
 
51
import javax.swing.*;
 
52
import javax.swing.text.Document;
 
53
import java.io.IOException;
 
54
import java.util.logging.Level;
 
55
import java.util.logging.Logger;
 
56
 
 
57
/** Represents a DD2beansDataObject in the Repository.
 
58
 *
 
59
 * @author  mkuchtiak
 
60
 */
 
61
public abstract class DD2beansDataObject extends XMLJ2eeDataObject implements org.openide.nodes.CookieSet.Factory{
 
62
 
 
63
    private static final int DELAY_FOR_TIMER=200;
 
64
    /** Private request processor for parsing and text generating tasks */
 
65
    protected final static RequestProcessor RP = new RequestProcessor("XML Parsing"); //NOI18N
 
66
    private final RequestProcessor.Task generationTask;
 
67
    // constructor settings
 
68
    private String prefixMark;
 
69
 
 
70
    private static final long serialVersionUID = -5363900668319174348L;
 
71
 
 
72
    public DD2beansDataObject(FileObject pf, MultiFileLoader loader)
 
73
        throws org.openide.loaders.DataObjectExistsException {
 
74
        this (pf, loader,true);
 
75
    }
 
76
 
 
77
    public DD2beansDataObject(FileObject pf, MultiFileLoader loader, final boolean saveAfterNodeChanges)
 
78
        throws org.openide.loaders.DataObjectExistsException {
 
79
        super (pf, loader);
 
80
 
 
81
        generationTask = RP.create(new Runnable() {
 
82
            int numberOfStartedGens;
 
83
            public void run() {
 
84
                numberOfStartedGens++;
 
85
                final String newDoc = generateDocument();
 
86
                SwingUtilities.invokeLater(new Runnable() {
 
87
                    public void run() {
 
88
                        try {
 
89
                            Document doc = getEditorSupport().openDocument();
 
90
                            XMLJ2eeUtils.replaceDocument(doc, newDoc, prefixMark);
 
91
                            setDocumentValid(true);
 
92
                            if (saveAfterNodeChanges) {
 
93
                                SaveCookie savec = (SaveCookie) getCookie(SaveCookie.class);
 
94
                                if (savec != null) {
 
95
                                    savec.save();
 
96
                                }
 
97
                            }
 
98
                            // this is necessary for correct undo behaviour
 
99
                            getEditorSupport().getUndo().discardAllEdits();
 
100
                        } catch (javax.swing.text.BadLocationException e) {
 
101
                            Logger.getLogger("global").log(Level.INFO, null, e);
 
102
                        } catch (IOException e) {
 
103
                            Logger.getLogger("global").log(Level.INFO, null, e);
 
104
                        } finally {
 
105
                            synchronized (generationTask) {
 
106
                                numberOfStartedGens--;
 
107
                                if (numberOfStartedGens == 0) {
 
108
                                    nodeDirty = false;
 
109
                                }
 
110
                            }
 
111
                        }
 
112
                    }
 
113
                });
 
114
            }
 
115
        });
 
116
    }
 
117
 
 
118
    /** Create document from the Node. This method is called after Node (Node properties)is changed.
 
119
    *  The document is generated from data modul (isDocumentGenerable=true) 
 
120
    */
 
121
    protected abstract String generateDocument();
 
122
 
 
123
    /** setter for prefixMark. This is information, which prefix in xml document should be preserved
 
124
    * after replacing by new generated document (This is mainly for preserving comments at the beginning)
 
125
    * @param prefix prefixMark
 
126
    */
 
127
    protected final void setPrefixMark(String prefix) {
 
128
        this.prefixMark=prefix;
 
129
    }
 
130
    /** gettert for prefixMark
 
131
    * @return prefixMark
 
132
    */
 
133
    protected final String getPrefixMark() {
 
134
        return prefixMark;
 
135
    }
 
136
 
 
137
    /** Setter for property nodeDirty.
 
138
     * @param dirty New value of property nodeDirty.
 
139
     */
 
140
    public void setNodeDirty(boolean dirty){
 
141
        //System.out.println("setNodeDirty("+dirty+")");
 
142
        if (dirty) {
 
143
            synchronized (this) {
 
144
                nodeDirty=true;
 
145
                restartGen();
 
146
            }
 
147
        }
 
148
    }
 
149
 
 
150
    public RequestProcessor.Task getGenerationTask(){
 
151
        return generationTask;
 
152
    }
 
153
 
 
154
    protected void restartGen() {
 
155
        generationTask.schedule(DELAY_FOR_TIMER);
 
156
    }
 
157
}