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.form.palette;

import javax.swing.*;
import java.awt.event.*;
import java.lang.ref.WeakReference;
import javax.swing.text.DefaultEditorKit;
import java.beans.*;

import org.openide.util.*;
import org.openide.explorer.*;
import org.openide.explorer.view.BeanTreeView;
import org.openide.*;
import org.openide.nodes.*;

import org.netbeans.modules.form.project.ClassSource;

/**
 * This class provides the UI for managing palette conent (adding components
 * etc). Shown to the user as "Palette Manager" window.
 *
 * @author Tomas Pavek
 */

public class PaletteManager extends JPanel implements ExplorerManager.Provider,
                                                      Lookup.Provider
{
    private static WeakReference dialogRef; // is weak reference necessary?

    private ExplorerManager explorerManager;
    private Lookup lookup;

    // ------------

    public static void showPaletteManager() {
        java.awt.Dialog dialog = null;
        if (dialogRef != null)
            dialog = (JDialog) dialogRef.get();
        if (dialog == null) {
            JButton closeButton = new JButton();
            org.openide.awt.Mnemonics.setLocalizedText(
                closeButton, PaletteUtils.getBundleString("CTL_Close_Button")); // NOI18N
            DialogDescriptor dd = new DialogDescriptor(
                new PaletteManager(),
                PaletteUtils.getBundleString("CTL_PaletteManager_Title"), // NOI18N
                false,
                new Object[] { closeButton },
                closeButton,
                DialogDescriptor.DEFAULT_ALIGN,
                null,
                null);
            dialog = DialogDisplayer.getDefault().createDialog(dd);
            dialogRef = new WeakReference(dialog);
        }
        dialog.setVisible(true);
    }

    /** Creates new PaletteManager */
    public PaletteManager() {
        explorerManager = new ExplorerManager();

        ActionMap map = getActionMap();
        map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(explorerManager));
        map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(explorerManager));
        map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(explorerManager));
        map.put("delete", ExplorerUtils.actionDelete(explorerManager, true)); // NOI18N

        lookup = ExplorerUtils.createLookup(explorerManager, map);

        explorerManager.setRootContext(PaletteNode.getPaletteNode());

        initComponents();

        BeanTreeView treeView = new BeanTreeView();
//        treeView.getAccessibleContext().setAccessibleName(
//            PaletteUtils.getBundleString("???")); // NOI18N
//        treeView.getAccessibleContext().setAccessibleDescription(
//            PaletteUtils.getBundleString("???")); // NOI18N
        treePanel.add(treeView, java.awt.BorderLayout.CENTER);
        captionLabel.setLabelFor(treeView);

        explorerManager.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent ev) {
                if (ExplorerManager.PROP_SELECTED_NODES.equals(ev.getPropertyName()))
                    updateInfoLabel(explorerManager.getSelectedNodes());
            }
        });
    }

    public void addNotify() {
        super.addNotify();
        ExplorerUtils.activateActions(explorerManager, true);
    }

    public void removeNotify() {
        ExplorerUtils.activateActions(explorerManager, false);
        super.removeNotify();
    }

    // ExplorerManager.Provider
    public ExplorerManager getExplorerManager() {
        return explorerManager;
    }

    // Lookup.Provider from TopComponent
    public Lookup getLookup() {
        return lookup;
    }

    // -------

    /** 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;

        captionLabel = new javax.swing.JLabel();
        treePanel = new javax.swing.JPanel();
        infoLabel = new javax.swing.JLabel();
        addJarButton = new javax.swing.JButton();
        addLibraryButton = new javax.swing.JButton();
        addProjectButton = new javax.swing.JButton();
        moveUpButton = new javax.swing.JButton();
        moveDownButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        newCategoryButton = new javax.swing.JButton();

        FormListener formListener = new FormListener();

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

        org.openide.awt.Mnemonics.setLocalizedText(captionLabel, PaletteUtils.getBundleString("CTL_Caption"));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(6, 10, 0, 10);
        add(captionLabel, gridBagConstraints);

        treePanel.setLayout(new java.awt.BorderLayout());

        treePanel.setBorder(new javax.swing.border.EtchedBorder());
        treePanel.setPreferredSize(new java.awt.Dimension(288, 336));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridheight = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(6, 10, 0, 0);
        add(treePanel, gridBagConstraints);

        infoLabel.setText(" ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 9;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 10);
        add(infoLabel, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(addJarButton, PaletteUtils.getBundleString("CTL_AddJAR_Button"));
        addJarButton.setToolTipText(PaletteUtils.getBundleString("HINT_AddJAR"));
        addJarButton.addActionListener(formListener);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(6, 12, 0, 10);
        add(addJarButton, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(addLibraryButton, PaletteUtils.getBundleString("CTL_AddLibrary_Button"));
        addLibraryButton.setToolTipText(PaletteUtils.getBundleString("HINT_AddLibrary"));
        addLibraryButton.addActionListener(formListener);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 10);
        add(addLibraryButton, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(addProjectButton, PaletteUtils.getBundleString("CTL_AddProject_Button"));
        addProjectButton.setToolTipText(PaletteUtils.getBundleString("HINT_AddProject"));
        addProjectButton.addActionListener(formListener);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 10);
        add(addProjectButton, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(moveUpButton, PaletteUtils.getBundleString("CTL_MoveUp_Button"));
        moveUpButton.addActionListener(formListener);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(28, 12, 0, 10);
        add(moveUpButton, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(moveDownButton, PaletteUtils.getBundleString("CTL_MoveDown_Button"));
        moveDownButton.addActionListener(formListener);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 10);
        add(moveDownButton, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(removeButton, PaletteUtils.getBundleString("CTL_Remove_Button"));
        removeButton.addActionListener(formListener);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 10);
        add(removeButton, gridBagConstraints);

        org.openide.awt.Mnemonics.setLocalizedText(newCategoryButton, PaletteUtils.getBundleString("CTL_NewCategory_Button"));
        newCategoryButton.addActionListener(formListener);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 7;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 12, 0, 10);
        add(newCategoryButton, gridBagConstraints);

    }

    // Code for dispatching events from components to event handlers.

    private class FormListener implements java.awt.event.ActionListener {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            if (evt.getSource() == addJarButton) {
                PaletteManager.this.addJarButtonActionPerformed(evt);
            }
            else if (evt.getSource() == addLibraryButton) {
                PaletteManager.this.addLibraryButtonActionPerformed(evt);
            }
            else if (evt.getSource() == addProjectButton) {
                PaletteManager.this.addProjectButtonActionPerformed(evt);
            }
            else if (evt.getSource() == moveUpButton) {
                PaletteManager.this.moveUpButtonActionPerformed(evt);
            }
            else if (evt.getSource() == moveDownButton) {
                PaletteManager.this.moveDownButtonActionPerformed(evt);
            }
            else if (evt.getSource() == removeButton) {
                PaletteManager.this.removeButtonActionPerformed(evt);
            }
            else if (evt.getSource() == newCategoryButton) {
                PaletteManager.this.newCategoryButtonActionPerformed(evt);
            }
        }
    }//GEN-END:initComponents

    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        Node[] selected = explorerManager.getSelectedNodes();
        if (selected.length == 0)
            return;

        // first user confirmation...
        NotifyDescriptor desc = new NotifyDescriptor.Confirmation(
            PaletteUtils.getBundleString("MSG_ConfirmPaletteDelete"), // NOI18N
            PaletteUtils.getBundleString("CTL_ConfirmDeleteTitle"), // NOI18N
            NotifyDescriptor.YES_NO_OPTION);

        if (NotifyDescriptor.YES_OPTION.equals(
                    DialogDisplayer.getDefault().notify(desc)))
        {
            try {
                for (int i=0; i < selected.length; i++)
                selected[i].destroy();
            }
            catch (java.io.IOException e) {
                ErrorManager.getDefault().notify(e);
            }
        }
    }//GEN-LAST:event_removeButtonActionPerformed

    private void moveDownButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveDownButtonActionPerformed
        moveNode(false);
    }//GEN-LAST:event_moveDownButtonActionPerformed

    private void moveUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveUpButtonActionPerformed
        moveNode(true);
    }//GEN-LAST:event_moveUpButtonActionPerformed

    private void newCategoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newCategoryButtonActionPerformed
        try {
            PaletteNode.getPaletteNode().createNewCategory();
        }
        catch (java.io.IOException e) {
            ErrorManager.getDefault().notify(e);
        }
    }//GEN-LAST:event_newCategoryButtonActionPerformed

    private void addProjectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addProjectButtonActionPerformed
        BeanInstaller.installBeans(ClassSource.PROJECT_SOURCE);
    }//GEN-LAST:event_addProjectButtonActionPerformed

    private void addLibraryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addLibraryButtonActionPerformed
        BeanInstaller.installBeans(ClassSource.LIBRARY_SOURCE);
    }//GEN-LAST:event_addLibraryButtonActionPerformed

    private void addJarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addJarButtonActionPerformed
        BeanInstaller.installBeans(ClassSource.JAR_SOURCE);
    }//GEN-LAST:event_addJarButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addJarButton;
    private javax.swing.JButton addLibraryButton;
    private javax.swing.JButton addProjectButton;
    private javax.swing.JLabel captionLabel;
    private javax.swing.JLabel infoLabel;
    private javax.swing.JButton moveDownButton;
    private javax.swing.JButton moveUpButton;
    private javax.swing.JButton newCategoryButton;
    private javax.swing.JButton removeButton;
    private javax.swing.JPanel treePanel;
    // End of variables declaration//GEN-END:variables

    private void moveNode(boolean up) {
        final Node[] selected = explorerManager.getSelectedNodes();
        if (selected.length != 1)
            return;

        Node node = selected[0];
        Node parent = node.getParentNode();
        if (parent == null)
            return;

        Index indexCookie = (Index) parent.getCookie(Index.class);
        if (indexCookie == null)
            return;

        int index = movePossible(node, parent, up);
        if (index != -1) {
            if (up)
                indexCookie.moveUp(index);
            else 
                indexCookie.moveDown(index);
        }
    }

    private static int movePossible(Node node, Node parentNode, boolean up) {
        if (parentNode == null)
            return -1;

        Node[] nodes = parentNode.getChildren().getNodes();
        for (int i=0; i < nodes.length; i++)
            if (nodes[i].getName().equals(node.getName()))
                return (up && i > 0) || (!up && i+1 < nodes.length) ? i : -1;

        return -1;
    }

    private void updateInfoLabel(org.openide.nodes.Node[] nodes) {
        String text = " "; // NOI18N
        if (nodes.length == 1) {
            PaletteItem item = (PaletteItem) nodes[0].getCookie(PaletteItem.class);
            if (item != null)
                text = PaletteUtils.getItemComponentDescription(item);
        }
        infoLabel.setText(text);
    }
}
... 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.