alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.web.dd;

import java.awt.event.ActionListener;
import org.openide.cookies.SaveCookie;
import org.openide.filesystems.FileObject;
import org.openide.loaders.MultiFileLoader;
import org.openide.util.RequestProcessor;
import org.netbeans.modules.web.xmlutils.XMLJ2eeDataObject;
import org.netbeans.modules.web.xmlutils.XMLJ2eeUtils;

import java.io.*;
import javax.swing.Timer;   
import org.openide.util.WeakListeners;

/** Represents a DD2beansDataObject in the Repository.
 *
 * @author  mkuchtiak
 */
public abstract class DD2beansDataObject extends XMLJ2eeDataObject implements org.openide.nodes.CookieSet.Factory{
       
    private static final int DELAY_FOR_TIMER=200;
    /** Private request processor for parsing and text generating tasks */
    protected final static RequestProcessor RP = new RequestProcessor("XML Parsing"); //NOI18N
    private RequestProcessor.Task generationTask;
    // constructor settings
    private boolean saveAfterNodeChanges;
    private Timer timer; 
    private String prefixMark;
    private int numberOfStartedGens;
    ActionListener timerListener;
    
    private static final long serialVersionUID = -5363900668319174348L; 
    
    public DD2beansDataObject(FileObject pf, MultiFileLoader loader) 
        throws org.openide.loaders.DataObjectExistsException {
        this (pf, loader,true);
    }

    public DD2beansDataObject(FileObject pf, MultiFileLoader loader, boolean saveAfterNodeChanges)
        throws org.openide.loaders.DataObjectExistsException {
        super (pf, loader);
        //System.out.println("DD2beansDataObject() "+this+" "+pf.hashCode());
        //Thread.dumpStack();
        this.saveAfterNodeChanges=saveAfterNodeChanges;  
        init();
    }

    private void init () {
        timerListener = new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent e) {
                //updatingDocumentFromNode=true;
                restartGen();    
            }
        };
        // initialize timer
        timer = new Timer(0, null);
        timer.addActionListener ((ActionListener) WeakListeners.create (ActionListener.class, timerListener, timer));
        timer.setInitialDelay(DELAY_FOR_TIMER);
        timer.setRepeats(false);
    }
        
    private synchronized void restartTimer(){
        if (!timer.isRunning()){
            numberOfStartedGens++;                   
        }
        timer.restart();
    }

    /** Create document from the Node. This method is called after Node (Node properties)is changed.
    *  The document is generated from data modul (isDocumentGenerable=true) 
    */  
    protected abstract String generateDocument();
    
    /** setter for prefixMark. This is information, which prefix in xml document should be preserved
    * after replacing by new generated document (This is mainly for preserving comments at the beginning)
    * @param prefix prefixMark
    */            
    protected final void setPrefixMark(String prefix) {
        this.prefixMark=prefix;
    }
    /** gettert for prefixMark
    * @return prefixMark
    */    
    protected final String getPrefixMark() {
        return prefixMark;
    }

    /** Setter for property nodeDirty.
     * @param dirty New value of property nodeDirty.
     */
    public void setNodeDirty(boolean dirty){
        //System.out.println("setNodeDirty("+dirty+")");
        if (dirty) {
            synchronized (this) {
                nodeDirty=true;
                restartTimer();
            }
        }
    }

    public RequestProcessor.Task getGenerationTask(){
        return generationTask;
    }
    
    protected void restartGen() {
        //System.out.println("restart Gen");
        generationTask = null;
        generationTask = RP.postRequest(new Runnable() {
            public void run() {              
                final String newDoc = generateDocument();
                javax.swing.SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        try {
                            javax.swing.text.Document doc = getEditorSupport().openDocument();
                            XMLJ2eeUtils.replaceDocument(doc,newDoc,prefixMark);
                            setDocumentValid(true);
                            if (saveAfterNodeChanges){
                                SaveCookie savec = (SaveCookie) getCookie(SaveCookie.class);
                                if (savec!=null) savec.save();
                            }
                            // this is necessary for correct undo behaviour
                            getEditorSupport().getUndo().discardAllEdits();
                        }
                        catch (javax.swing.text.BadLocationException e) {
                            org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);
                        }

                        catch (IOException e) {
                            org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, e);
                        }
                        finally {
                            synchronized (DD2beansDataObject.this) {
                                numberOfStartedGens--;
                                if (numberOfStartedGens==0) nodeDirty=false;
                            }                                
                        }
                    }
                });
            }
        });
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.