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-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

/*
 * ConfigBeanTopComponent.java
 *
 * Created on March 6, 2003, 2:09 PM
 */

package org.netbeans.modules.j2ee.deployment.config.ui;

import com.sun.tools.j2ee.editor.*;
import java.awt.BorderLayout;
import java.io.IOException;
import java.util.*;
import javax.enterprise.deploy.shared.ModuleType;
import javax.swing.JSplitPane;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.modules.j2ee.deployment.config.*;
import org.netbeans.modules.j2ee.deployment.devmodules.api.*;
import org.netbeans.modules.j2ee.deployment.devmodules.spi.J2eeModuleProvider;
import org.openide.*;
import org.openide.NotifyDescriptor.*;
import org.openide.cookies.SaveCookie;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;

import org.openide.nodes.*;
import org.openide.nodes.Children.Array;
import org.openide.text.CloneableEditorSupport;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
import org.openide.windows.*;
import org.openide.util.HelpCtx;


/**
 *
 * @author  Jeri Lockhart
 */
public class ConfigBeanTopComponent extends CloneableTopComponent {
    
    private ConfigDataObject configDO = null;
    private ConfigBeanNode rootConfigNode = null;   // ConfigBeanNode for the selNode
    private Node selNode = null;      // Node the user selected in the main project explorer
    private Node rootNode = null;   // Root node for the editor's explorer
    
    private ConfigurationStorageNode configStorageNode = null;
    private ConfigBeanPanelView panelView;
    
    
    public static final String APPLICATION_ICON_NORMAL =
    "org/netbeans/modules/j2ee/deployment/config/ui/resources/application";	// NOI18N
    public static final String EJBMODULE_ICON_NORMAL =
    "org/netbeans/modules/j2ee/deployment/config/ui/resources/ejbmodule";	// NOI18N
    public static final String WEBMODULE_ICON_NORMAL =
    "org/netbeans/modules/j2ee/deployment/config/ui/resources/webmodule";	// NOI18N
    // Also these types:
    //     ModuleType.RAR;
    //     ModuleType.CAR;
    
    private boolean appConfig = false;
    
    /** default constructor for deserialization */
    public ConfigBeanTopComponent() {
        // hint to windows system
        putClientProperty("PersistenceType", "Never"); //NOI18N
        //        putClientProperty("PersistenceType", "OnlyOpened"); // NOI18N
        
    }
    
    /** Creates a new instance of ConfigBeanTopComponent */
    public ConfigBeanTopComponent(ConfigDataObject dobj) {
        this();
        this.configDO = dobj;
        initialize(dobj);
    }
    
    public boolean isFor(ConfigDataObject configDO) {
        return (this.configDO == configDO);
    }
    
    public boolean isFor(FileObject document) {
        return (this.configDO.getPrimaryFile().equals(document));
    }
    
    ConfigurationStorage getConfigStorage() {
        if (configDO != null) {
            return (ConfigurationStorage) configDO.getCookie(ConfigurationStorage.class);
        }
        return null;
    }
    
    public static ConfigBeanTopComponent findByConfigStorage(ConfigurationStorage configStorage) {
        Iterator it  = TopComponent.getRegistry().getOpened().iterator();
        while (it.hasNext()) {
            TopComponent tc = (TopComponent) it.next();
            if (tc instanceof ConfigBeanTopComponent) {
                ConfigBeanTopComponent beanTC = (ConfigBeanTopComponent) tc;
                if (configStorage == beanTC.getConfigStorage()) {
                    return beanTC;
                }
            }
        }
        return null;
    }
    
    public void refresh() {
        try {
            rootConfigNode =  (ConfigBeanNode) getConfigStorage().getMainNode();
            configStorageNode.setConfigurationStorage(getConfigStorage());
        } catch (java.lang.Exception e) {
            ErrorManager.getDefault ().notify (e);
        }
        
        if (rootConfigNode == null) {
            ErrorManager.getDefault ().log (ErrorManager.INFORMATIONAL, "ConfigBeanTopComponent.initialize() ConfigBeanNode is null, returning.");
            return;
        }
        
        rootNode = buildTree();
        panelView.showSelection(new Node[] { rootNode });
    }
    
    public int getPersistenceType () {
        return PERSISTENCE_NEVER;
    }
    
    /** Initializes this instance. Used by construction and deserialization. */
    public void initialize(ConfigDataObject dobj) {
        this.configDO = dobj;
        this.selNode = dobj.getNodeDelegate ();

        try {
            if (getConfigStorage() == null) {
                throw new IllegalArgumentException("ConfigDataObject without ConfigurationStorage cookie!"); //NOI18N
            }
        
            if (configStorageNode == null) {
                this.configStorageNode = new ConfigurationStorageNode(Children.LEAF);
                configStorageNode.setConfigurationStorage(getConfigStorage());
            }

            rootConfigNode =  (ConfigBeanNode) getConfigStorage().getMainNode();
            if (rootConfigNode == null) {
                throw new IllegalArgumentException("ConfigBeanTopComponent.initialize() ConfigBeanNode is null"); //NOI18N
            }
        } catch (RuntimeException ex) {
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,  ex);
            return;
        }
        
        
        initComponents();
        
        setCloseOperation(TopComponent.CLOSE_LAST);
        
        
        //setName(NbBundle.getMessage (ConfigBeanTopComponent.class, "LBL_Server_specific_settings", selNode.getDisplayName()));
        setName(selNode.getDisplayName()+":"+getConfigStorage().getTargetServerName());//NOI18N
        String fsName = "";
        FileObject fo = configDO.getPrimaryFile ();
        try {
            fsName = fo.getFileSystem ().getDisplayName () + "/" + fo.getPath (); //NOI18N
        } catch (org.openide.filesystems.FileStateInvalidException fse) {
            fsName = fo.getPath ();
        }
        char sep = java.io.File.separatorChar;
        char another = sep == '/' ? '\\' : '/';
        fsName = fsName.replace (another, sep);
        setToolTipText (fsName);
        setActivatedNodes(new Node[] {selNode});
        setIcon (Utilities.loadImage ("org/netbeans/modules/j2ee/deployment/config/ui/resources/ConfigFile.gif")); //NOI18N
    }
    
    public void reset() {
        configDO = null;
        rootConfigNode = null;
        selNode = null;
        rootNode = null;
        configStorageNode = null;
        panelView = null;
        appConfig = false;
    }
    
    /** Inits the subcomponents. Sets layout for this top component .
     * @see BundleEditPanel */
    private void initComponents() {
        try {
            if (rootNode == null) {
                rootNode = buildTree();
            }

            setLayout(new BorderLayout());
            panelView = new ConfigBeanPanelView(rootNode);
            TwoPanelComponentPanel componentPanel = new TwoPanelComponentPanel(panelView);
            componentPanel.getExplorerManager().setRootContext(rootNode);
            componentPanel.getExplorerManager().setSelectedNodes(new Node[] { rootNode });
            add(BorderLayout.CENTER, componentPanel);
            panelView.showSelection(new Node[] { rootNode });
            
        } catch (java.lang.Exception e) {
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
        }
    }
    
    private J2eeModule getProvider() {
        J2eeModuleProvider provider = (J2eeModuleProvider) FileOwnerQuery.getOwner (configDO.getPrimaryFile ()).getLookup ().lookup (J2eeModuleProvider.class);
        return provider.getJ2eeModule ();
    }
    
    private Node buildTree() {
        Node filterRoot = null;
        
        if (selNode == null) {
            return null;
        }
        J2eeModule prov = getProvider();
        if (prov instanceof J2eeModuleContainer) {
            appConfig = true;
            AbstractNode root = null;
            Array  children = new Array();
            J2eeModuleContainer appProv = (J2eeModuleContainer) prov;
            filterRoot = selNode;
            ArrayList childArr = new ArrayList();
            // The first child is the App's ConfigBeanNode
            childArr.add(rootConfigNode);
            // Add nodes for modules
            J2eeModule[] mods = appProv.getModules(new ModuleListener() {
                public  void addModule(J2eeModule provider){
                    // add module to editor's explorer
                    System.out.println("ConfigBeanTopComponent.ModuleListener.addModule:  " + provider.getUrl());
                }
                public  void removeModule(J2eeModule provider){
                    // remove module from editor's explorer
                    System.out.println("ConfigBeanTopComponent.ModuleListener.removeModule:  " + provider.getUrl());
                }
            });
            for (int i = 0; i < mods.length; i++) {
                //                Get each module in the app, get the module's ConfigBeanNode and create an Node for the module
                Array modChildren = new Array();
                Node[] modConfigBeanNode = getConfigStorage().getNodes(mods[i]);
                for(int j = 0; j < modConfigBeanNode.length; j++)
                  if(modConfigBeanNode[j] != null)
                    modChildren.add(new Node[] { modConfigBeanNode[j] });
                AbstractNode modNode = new AbstractNode(modChildren);
                modNode.setName(mods[i].getUrl());
                if (mods[i].getModuleType() == ModuleType.EJB) {
                    modNode.setIconBase(EJBMODULE_ICON_NORMAL);
                }else if (mods[i].getModuleType() == ModuleType.WAR) {
                    modNode.setIconBase(WEBMODULE_ICON_NORMAL);
                }
                // Add the module node to the array of child nodes for the app
                childArr.add(modNode);
            }
            //            create the app Node
            children.add((Node[]) childArr.toArray(new Node[childArr.size()]));
            root = new AbstractNode(children);
            root.setName(filterRoot.getName());
            root.setIconBase(APPLICATION_ICON_NORMAL);
            rootNode = root;
        } else { // not a j2ee app
            filterRoot = selNode;
            rootNode = getConfigStorage().getMainNode();
 /*  PENDING do we need an extra node layer?  Maybe just a FilterNode over the ConfigBeanNode
  *  to change the display name, etc.
            for (int i = 0; i < mods.length; i++) {
                //                Get each module in the app, get the module's ConfigBeanNode and create an AbstractNode for the module
                Children.Array modChildren = new Children.Array();
                ConfigBeanNode modConfigBeanNode = (ConfigBeanNode) configStorage.getNode(mods[i]);
                modChildren.add(new Node[] {modConfigBeanNode});
                Node modNode = new AbstractNode(modChildren);
                modNode.setName(mods[i].getUrl());
                if (mods[i].getModuleType() == ModuleType.EJB) {
                    modNode.setIconBase(EJBMODULE_ICON_NORMAL);
                }else if (mods[i].getModuleType() == ModuleType.WAR) {
                    modNode.setIconBase(WEBMODULE_ICON_NORMAL);
                }
                // Add the module node to the array of child nodes for the app
                childArr.add(modNode);
            }
            root = new AbstractNode(children);
            root.setName(filterRoot.getName());
            root.setIconBase(APPLICATION_ICON_NORMAL);
  */
        }
        return rootNode;
    }
    
    /*
    private ConfigBeanNode getConfigRoot(Node modNode) {
        J2eeModuleContainer appProv = null;
        ConfigBeanNode moduleRoot = null;
        J2eeModule prov = (J2eeModule)selNode.getCookie(J2eeModule.class);
        if (prov instanceof J2eeModuleContainer) {
            appProv = (J2eeModuleContainer) prov;
        }
        
        J2eeModule[] mods = appProv.getModules(new ModuleListener() {
            public  void addModule(J2eeModule provider){
                // add module to editor's explorer
                System.out.println("ConfigBeanTopComponent.ModuleListener.addModule:  " + provider.getUrl());
                
            }
            public  void removeModule(J2eeModule provider){
                // remove module from editor's explorer
                System.out.println("ConfigBeanTopComponent.ModuleListener.removeModule:  " + provider.getUrl());
                //                removeModuleNode(provider);
            }
        });
        for (int i = 0; i < mods.length; i++) {
            if (mods[i].getUrl().equals(modNode.getName())) {
                moduleRoot = (ConfigBeanNode) configStorage.getNode(mods[i]);
            }
        }
        return moduleRoot;
    } */
    
    private void removeModuleNode(J2eeModule provider) {
        Node removeNode = this.rootNode.getChildren().findChild(provider.getUrl());
        if (removeNode != null) {
            rootNode.getChildren().remove(new Node[] {removeNode});
        }
    }
    
    protected void componentClosed () {
        super.componentClosed();
        configDO.editorClosed ();
    }
    
    public void open() {
        super.open();
        refresh();
    }
    
    /** Getter for property rootConfigNode.
     * @return Value of property rootConfigNode.
     *
     */
    public ConfigBeanNode getRootConfigNode() {
        return this.rootConfigNode;
    }
    
    /** Setter for property rootConfigNode.
     * @param rootConfigNode New value of property rootConfigNode.
     *
     */
    public void setRootConfigNode(ConfigBeanNode rootConfigNode) {
        this.rootConfigNode = rootConfigNode;
    }
    
    
    /** Getter for property selNode.
     * @return Value of property selNode.
     *
     */
    public Node getSelNode() {
        return this.selNode;
    }
    
    /** Setter for property selNode.
     * @param selNode New value of property selNode.
     *
     */
    public void setSelNode(Node selNode) {
        this.selNode = selNode;
    }
    
  /*
   *  Find the "root" App, Module or Ear Node for this node or subnode
   *  If the  currNode is the "root", return it.
   */
    //    private Node getRoot(Node currNode) {
    //        Node parent = currNode.getParentNode();
    //        if (parent != null) {
    //            if (parent.getCookie(J2eeComponentProvider.class) == null) {
    //                return currNode;
    //            }
    //            return getRoot(parent);
    //        }
    //        return currNode;
    //    }
    
    
    public boolean closeLast() {
        super.closeLast();
        // if there's data to be saved, prompt the user to save - Yes, No, or cancel
        if (configStorageNode == null) {
            return true;
        }
        if (configDO.isModified ()) {
            Confirmation cf = new Confirmation(NbBundle.getMessage(ConfigBeanTopComponent.class, "MSG_ConfirmSave", configDO.getName ()));
            DialogDisplayer.getDefault().notify(cf);
            if(cf.getValue()==NotifyDescriptor.YES_OPTION){
                try {
                    SaveCookie sc =  (SaveCookie)configDO.getCookie (SaveCookie.class);
                    if (sc != null) {
                        sc.save ();
                    }
                }
                catch (IOException e) {
                    DialogDisplayer.getDefault().notify(new Message(e.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
                }
            }
            else if (cf.getValue()==NotifyDescriptor.NO_OPTION){
                try {
                    configDO.setModified (false);
                    getConfigStorage().load ();
                } catch (java.lang.Exception e) {
                    DialogDisplayer.getDefault().notify(new Message(e.getMessage(), NotifyDescriptor.ERROR_MESSAGE));
                }
            }
            else {  // user pressed Cancel - don't close
                return false;
            }
        }
        // remove module nodes from app node
        if (appConfig = true && rootNode != null) {
            Children modules = rootNode.getChildren();
            Node[] modNodes = modules.getNodes();
            for (int i = 0; i < modNodes.length; i++) {
                if (modNodes[i] instanceof AbstractNode ) {
                    AbstractNode mod = (AbstractNode)modNodes[i];
                    Children ch = mod.getChildren();
                    ch.remove(ch.getNodes());
                }
            }
            modules.remove(modules.getNodes());
            
        }
        
        
        return true;
    }
    
    
    
    /**
     * Overrides superclass method.
     * Is called from the superclass clone method to create new component from this one.
     * This implementation only clones the object by calling super.clone method.
     * @return the copy of this object
     */
    protected CloneableTopComponent createClonedObject() {
        return new ConfigBeanTopComponent(this.configDO);
    }
    
    
    class ConfigurationStorageNode extends AbstractNode {
        
        /** Holds value of property configurationStorage. */
        private ConfigurationStorage configurationStorage;
        
        public ConfigurationStorageNode(Children children) {
            super(children);
            
        }
        
        /** Getter for property configurationStorage.
         * @return Value of property configurationStorage.
         *
         */
        public ConfigurationStorage getConfigurationStorage() {
            return this.configurationStorage;
        }
        
        /** Setter for property configurationStorage.
         * @param configurationStorage New value of property configurationStorage.
         *
         */
        public void setConfigurationStorage(ConfigurationStorage configurationStorage) {
            this.configurationStorage = configurationStorage;
        }
    }
    
    private class TwoPanelComponentPanel extends ComponentPanel {
        
        protected TwoPanelComponentPanel(){
            super();
        }
        /**
         * Creates a new instance of ComponentPanel
         * @param panel The PanelView which will provide the node tree for the structure view
         *              and the set of panels the nodes map to.
         */
        public TwoPanelComponentPanel(PanelView panel){
            super(panel);
        }
        
        public HelpCtx getHelpCtx () {
            Node[] nodes = this.getExplorerManager().getSelectedNodes();
            if (nodes.length > 0) {
                return nodes[0].getHelpCtx();
            } else if (rootNode != null) {
                return rootNode.getHelpCtx();
            } else if (configDO != null) {
                return configDO.getHelpCtx();
            } else {
                return HelpCtx.DEFAULT_HELP;
            }
        }
        
        protected void createHorizontalSplit() {
            if (panelOrientation == 1)
                split1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,getContentView(), getStructureView());
            else
                split1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,getStructureView(), getContentView());
            //split1.setDividerLocation(DEFAULT_CONTENT_SPLIT);
            split1.setDividerSize (4);
        }
        
        protected void createVerticalSplit() {
            
        }
        
    }
    
    
    
}


... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.