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

package org.netbeans.modules.ant.freeform.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Image;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.tree.TreeSelectionModel;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectManager;
import org.netbeans.modules.ant.freeform.FreeformProject;
import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
import org.netbeans.modules.ant.freeform.FreeformProjectType;
import org.netbeans.spi.project.AuxiliaryConfiguration;
import org.netbeans.spi.project.support.ant.AntProjectHelper;
import org.openide.DialogDescriptor;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.view.BeanTreeView;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.Utilities;
import org.w3c.dom.Element;

/**
 *
 * @author Petr Hrebejk, David Konecny
 */
public class ProjectCustomizer extends javax.swing.JPanel implements HelpCtx.Provider {

    private Component currentCustomizer;
    private GridBagConstraints fillConstraints;
    private List panels;
    private AntProjectHelper helper;
    private ProjectModel model;
    private FreeformProject project;
    private String projectType;

    private DialogDescriptor dialogDescriptor;
    
    private static final String PROJECT_TYPE_J2SE = "j2se"; // NOI18N
    private static final String PROJECT_TYPE_WEB = "webapps"; // NOI18N
    private static final String GENERAL_ICON = "general.gif"; // NOI18N
    
    public ProjectCustomizer(FreeformProject project, AntProjectHelper helper) {
        this.helper = helper;
        this.project = project;
        initComponents();
        HelpCtx.setHelpIDString( customizerPanel, "org.netbeans.modules.ant.freeform.ui.ProjectCustomizer" ); // NOI18N
        
        fillConstraints = new GridBagConstraints();
        fillConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        fillConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
        fillConstraints.fill = java.awt.GridBagConstraints.BOTH;
        fillConstraints.weightx = 1.0;
        fillConstraints.weighty = 1.0;

        try {
            Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory());
            AuxiliaryConfiguration aux = (AuxiliaryConfiguration)p.getLookup().lookup(AuxiliaryConfiguration.class);
            Element data = aux.getConfigurationFragment("web-data", FreeformProjectType.NS_WEB, true); // NOI18N
            if (data == null)
                projectType = PROJECT_TYPE_J2SE;
            else
                projectType = PROJECT_TYPE_WEB;
        } catch (IOException exc) {
            projectType = PROJECT_TYPE_J2SE;
        }

        categoryPanel.add(new CategoryView(createRootNode()), fillConstraints);
    }

    void save() {
        Iterator it = panels.iterator();
        while (it.hasNext()) {
            Panel panel = (Panel)it.next();
            panel.storeValues(project, helper, getProjectDataModel());
        }
        ProjectModel.saveProject(helper, getProjectDataModel());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.GridBagConstraints gridBagConstraints;

        categoryPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        customizerPanel = new javax.swing.JPanel();

        setLayout(new java.awt.GridBagLayout());

        setPreferredSize(new java.awt.Dimension(750, 450));
        getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectCustomizer.class, "ACSD_ProjectCustomizer"));
        categoryPanel.setLayout(new java.awt.GridBagLayout());

        categoryPanel.setBorder(new javax.swing.border.EtchedBorder());
        categoryPanel.setMinimumSize(new java.awt.Dimension(220, 4));
        categoryPanel.setPreferredSize(new java.awt.Dimension(220, 4));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(8, 8, 8, 8);
        add(categoryPanel, gridBagConstraints);
        categoryPanel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ProjectCustomizer.class, "ACSN_ProjectCustomizer_categoryPanel"));
        categoryPanel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectCustomizer.class, "ACSD_ProjectCustomizer_categoryPanel"));

        jPanel1.setLayout(new java.awt.GridBagLayout());

        jPanel1.setBorder(new javax.swing.border.EtchedBorder());
        customizerPanel.setLayout(new java.awt.GridBagLayout());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(12, 12, 12, 12);
        jPanel1.add(customizerPanel, gridBagConstraints);
        customizerPanel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(ProjectCustomizer.class, "ACSN_ProjectCustomizer_customizerPanel"));
        customizerPanel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(ProjectCustomizer.class, "ACSD_ProjectCustomizer_customizerPanel"));

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(8, 4, 8, 8);
        add(jPanel1, gridBagConstraints);

    }//GEN-END:initComponents


     // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel categoryPanel;
    private javax.swing.JPanel customizerPanel;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

    public void setDialogDescriptor( DialogDescriptor dialogDescriptor ) {
        this.dialogDescriptor = dialogDescriptor;
    }
    
    // HelpCtx.Provider implementation -----------------------------------------
    
    public HelpCtx getHelpCtx() {
        if ( currentCustomizer != null ) {
            return HelpCtx.findHelp( currentCustomizer );
        }
        else {
            return HelpCtx.findHelp( customizerPanel );
        }
    }
    
    // Private innerclasses ----------------------------------------------------

    private class CategoryView extends JPanel implements ExplorerManager.Provider {

        private ExplorerManager manager;
        private BeanTreeView btv;

        CategoryView( Node rootNode ) {

            // See #36315
            manager = new ExplorerManager();

            setLayout( new BorderLayout() );

            Dimension size = new Dimension( 220, 4 );
            btv = new BeanTreeView();    // Add the BeanTreeView
            btv.setSelectionMode( TreeSelectionModel.SINGLE_TREE_SELECTION );
            btv.setPopupAllowed( false );
            btv.setRootVisible( false );
            btv.setDefaultActionAllowed( false );
            btv.setMinimumSize( size );
            btv.setPreferredSize( size );
            btv.setMaximumSize( size );
            this.add( btv, BorderLayout.CENTER );
            manager.setRootContext( rootNode );
            manager.addPropertyChangeListener( new ManagerChangeListener() );
            selectFirstNode();
            btv.expandAll();

        }

        public ExplorerManager getExplorerManager() {
            return manager;
        }

        public void addNotify() {
            super.addNotify();
            btv.expandAll();
        }

        private void selectFirstNode() {

            Children ch = manager.getRootContext().getChildren();
            if ( ch != null ) {
                Node nodes[] = ch.getNodes();

                if ( nodes != null && nodes.length > 0 ) {
                    try {
                        manager.setSelectedNodes( new Node[] { nodes[0] } );
                    }
                    catch ( PropertyVetoException e ) {
                        // No node will be selected
                    }
                }
            }

        }


        /** Listens to selection change and shows the customizers as
         *  panels
         */

        private class ManagerChangeListener implements PropertyChangeListener {

            public void propertyChange(PropertyChangeEvent evt) {
                if(evt.getSource() != manager) {
                    return;
                }

                if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
                    Node nodes[] = manager.getSelectedNodes();
                    if ( nodes == null || nodes.length <= 0 ) {
                        return;
                    }
                    Node node = nodes[0];

                    if ( currentCustomizer != null ) {
                        if ( currentCustomizer instanceof Panel ) {
                            ((Panel)currentCustomizer).hide(project, helper, getProjectDataModel());
                        }
                        customizerPanel.remove( currentCustomizer );
                    }
                    if ( node.hasCustomizer() ) {
                        currentCustomizer = node.getCustomizer();

                        if ( currentCustomizer instanceof Panel ) {
                            ((Panel)currentCustomizer).show(project, helper, getProjectDataModel());
                        }

                        /*
                        if ( currentCustomizer instanceof javax.swing.JComponent ) {
                            ((javax.swing.JComponent)currentCustomizer).setPreferredSize( new java.awt.Dimension( 600, 0 ) );
                        }
                        */
                        customizerPanel.add( currentCustomizer, fillConstraints );
                        customizerPanel.validate();
                        customizerPanel.repaint();
                        if ( ProjectCustomizer.this.dialogDescriptor != null ) {
                            ProjectCustomizer.this.dialogDescriptor.setHelpCtx( ProjectCustomizer.this.getHelpCtx() );
                        }


                    }
                    else {
                        currentCustomizer = null;
                    }

                    return;
                }
            }
        }
    }

    // Private methods ---------------------------------------------------------

    private Node createRootNode() {

        String ICON_FOLDER = "org/netbeans/modules/ant/freeform/resources/"; // NOI18N
        ResourceBundle bundle = NbBundle.getBundle(ProjectCustomizer.class);

        Component compGeneral = new GeneralPanel();
        Component compTargetMapping = new TargetMappingPanel(projectType);
        Component compSourceFolders = new SourceFoldersPanel();
        Component compClasspath = new ClasspathPanel();
        Component compOutput = new OutputPanel();
        
        ConfigurationDescription cdGeneral = new ConfigurationDescription(
            "General", // NOI18N
            org.openide.util.NbBundle.getMessage(ProjectCustomizer.class, "LBL_ProjectCustomizer_Category_General"),
            ICON_FOLDER + GENERAL_ICON,
            compGeneral,
            null );
        ConfigurationDescription cdTargets = new ConfigurationDescription(
            "Targets", // NOI18N
            org.openide.util.NbBundle.getMessage(ProjectCustomizer.class, "LBL_ProjectCustomizer_Category_Targets"),
            ICON_FOLDER + GENERAL_ICON,
            compTargetMapping,
            null );
        ConfigurationDescription cdSources = new ConfigurationDescription(
            "Sources", // NOI18N
            org.openide.util.NbBundle.getMessage(ProjectCustomizer.class, "LBL_ProjectCustomizer_Category_Sources"),
            ICON_FOLDER + GENERAL_ICON,
            compSourceFolders,
            null );
        ConfigurationDescription cdClasspath = new ConfigurationDescription(
            "Classpath", // NOI18N
            org.openide.util.NbBundle.getMessage(ProjectCustomizer.class, "LBL_ProjectCustomizer_Category_Classpath"),
            ICON_FOLDER + GENERAL_ICON,
            compClasspath,
            null );
        ConfigurationDescription cdOutput = new ConfigurationDescription(
            "Output", // NOI18N
            org.openide.util.NbBundle.getMessage(ProjectCustomizer.class, "LBL_ProjectCustomizer_Category_Output"),
            ICON_FOLDER + GENERAL_ICON,
            compOutput,
            null );

        ConfigurationDescription descriptions[];
        if (projectType.equals(PROJECT_TYPE_WEB)) {
            Component compWebSrc = new WebLocationsPanel();
            
            ConfigurationDescription cdWebSrc = new ConfigurationDescription(
                "Web Sources", // NOI18N
                org.openide.util.NbBundle.getMessage(ProjectCustomizer.class, "LBL_ProjectCustomizer_Category_Web"),
                ICON_FOLDER + GENERAL_ICON,
                compWebSrc,
                null );
            descriptions = new ConfigurationDescription[] {cdGeneral, cdTargets, cdWebSrc, cdSources, cdClasspath, cdOutput};
            panels = Arrays.asList(new Object[]{compGeneral, compTargetMapping, compWebSrc, compSourceFolders, compClasspath, compOutput});
        } else {
            descriptions = new ConfigurationDescription[] {cdGeneral, cdTargets, cdSources, cdClasspath, cdOutput};
            panels = Arrays.asList(new Object[]{compGeneral, compTargetMapping, compSourceFolders, compClasspath, compOutput});
        }

        ConfigurationDescription rootDescription = new ConfigurationDescription(
        "InvisibleRoot", "InvisibleRoot", null, null, descriptions );  // NOI18N

        return new ConfigurationNode( rootDescription );


    }

    private ProjectModel getProjectDataModel() {
        if (model == null) {
            model = ProjectModel.createModel(project, helper);
        }
        return model;
    }
    
    // Private meyhods ---------------------------------------------------------

    // XXX Remove when all panels have some options

    private static javax.swing.JLabel createEmptyLabel( String text ) {

        JLabel label;
        if ( text == null ) {
            label = new JLabel();
        }
        else {
            label = new JLabel( text );
            label.setHorizontalAlignment( JLabel.CENTER );
        }

        return label;
    }

    // Private innerclasses ----------------------------------------------------

    /** Class describing the configuration node. Prototype of the
     *  configuration node.
     */
    private static class ConfigurationDescription {


        private String name;
        private String displayName;
        private String iconResource;
        private Component customizer;
        private ConfigurationDescription[] children;
        // XXX Add Node.Properties

        ConfigurationDescription( String name,
        String displayName,
        String iconResource,
        Component customizer,
        ConfigurationDescription[] children ) {

            this.name = name;
            this.displayName = displayName;
            this.iconResource = iconResource;
            this.customizer = customizer;
            this.children = children;
        }

    }


    /** Node to be used for configuration
     */
    private static class ConfigurationNode extends AbstractNode {

        private Component customizer;
        /** resource path to icon, or null */
        private final String iconResource;

        public ConfigurationNode( ConfigurationDescription description ) {
            super( description.children == null ? Children.LEAF : new ConfigurationChildren( description.children ) );
            setName( description.name );
            setDisplayName( description.displayName );
            this.iconResource = description.iconResource;
            this.customizer = description.customizer;
        }

        public boolean hasCustomizer() {
            return true;
        }

        public Component getCustomizer() {
            return customizer;
        }

        public Image getIcon(int type) {
            if (iconResource != null) {
                return Utilities.loadImage(iconResource, true);
            } else {
                return super.getIcon(type);
            }
        }

        public Image getOpenedIcon(int type) {
            if (iconResource != null) {
                return Utilities.loadImage(iconResource, true);
            } else {
                return super.getOpenedIcon(type);
            }
        }

    }

    /** Children used for configuration
     */
    private static class ConfigurationChildren extends Children.Keys {

        private Collection descriptions;

        public ConfigurationChildren( ConfigurationDescription[] descriptions ) {
            this.descriptions = Arrays.asList( descriptions );
        }

        // Children.Keys impl --------------------------------------------------

        public void addNotify() {
            setKeys( descriptions );
        }

        public void removeNotify() {
            setKeys( Collections.EMPTY_LIST );
        }

        protected Node[] createNodes( Object key ) {
            return new Node[] { new ConfigurationNode( (ConfigurationDescription)key ) };
        }
    }

    /**
     * Interface to be implemented by the panels which are shown in the Project
     * Customizer. The methods allow to react on the UI changes that is: panel is
     * going to be visible; user switched to different panel; and user pressed Save
     * to persists customization done in the panel.
     */
    static interface Panel {

        /**
         * Called on your panel whenever it is going to be shown to user,
         * eg. when user selects its node in project customizer. The method
         * can be called multiple times when Project Customizer is visible.
         * @param project access to FreeformProject; useful for reading properties, etc.
         * @param helper AntProjectHelper
         * @param model project data model. Useful when the same project data are
         *    customized in more then one customizer panel. For example Java
         *    compilation unit is configured in two panels - classpath and output.
         *    Both these panels need to access the same memory representation
         *    of the compilation unit so that they can cooperate on them and save
         *    them when OK is pressed or thrown them away otherwise.
         */
        public void show(FreeformProject project, AntProjectHelper helper, ProjectModel model);

        /**
         * Called on your panel whenever it is hiding, that is when user 
         * selected different panel node in Project Customizer. This method
         * is not called when user closes the Project Customizer. If user pressed
         * Save then {@link #storeValues} method is called.
         * @param project access to FreeformProject; useful for reading properties, etc.
         * @param helper AntProjectHelper
         * @param model project data model. Useful when the same project data are
         *    customized in more then one customizer panel. For example Java
         *    compilation unit is configured in two panels - classpath and output.
         *    Both these panels need to access the same memory representation
         *    of the compilation unit so that they can cooperate on them and save
         *    them when OK is pressed or thrown them away otherwise.
         */
        public void hide(FreeformProject project, AntProjectHelper helper, ProjectModel model);

        /**
         * Persists customizations done in the panel. Called only when user
         * pressed OK in cusotmizer.
         * @param project access to FreeformProject; useful for reading properties, etc.
         * @param helper AntProjectHelper
         * @param model project data model. Useful when the same project data are
         *    customized in more then one customizer panel. For example Java
         *    compilation unit is configured in two panels - classpath and output.
         *    Both these panels need to access the same memory representation
         *    of the compilation unit so that they can cooperate on them and save
         *    them when OK is pressed or thrown them away otherwise.
         */
        public void storeValues(FreeformProject project, AntProjectHelper helper, ProjectModel model);

    }

}
... 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.