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

package org.netbeans.modules.form.palette;

import javax.swing.*;
import java.awt.event.*;
import java.util.ResourceBundle;
import java.text.MessageFormat;

import org.openide.awt.JPopupMenuPlus;
import org.openide.*;
import org.openide.nodes.*;

/**
 * Popup menu for compoennt palette (possibly with a context of a category).
 *
 * @author Tomas Pavek, Jan Stola
 */

class PalettePopupMenu extends JPopupMenuPlus {

    /** Node of the category under the mouse click. */
    private Node categoryNode;
    /** Component palette manager. */
    private CPManager manager;

    private static final String NEW_CATEGORY = "CTL_CreateCategory"; // NOI18N
    private static final String DELETE_CATEGORY = "CTL_DeleteCategory"; // NOI18N
    private static final String RENAME_CATEGORY = "CTL_RenameCategory"; // NOI18N
    private static final String ORDER_CATEGORIES = "CTL_OrderCategories"; // NOI18N
    private static final String REFRESH_PALETTE = "CTL_RefreshPalette"; // NOI18N

    /**
     * Constructor.
     *
     * @param categoryNode node of the category under the mouse click
     *        (can be null).
     * @param manager component palette manager.
     */
    PalettePopupMenu(Node categoryNode, CPManager manager) {
        this.categoryNode = categoryNode;
        this.manager = manager;

        JMenuItem menuItem;
        ActionListener listener = new Listener();

        ResourceBundle bundle = CPManager.getBundle();

        menuItem = new JMenuItem(bundle.getString(NEW_CATEGORY));
        menuItem.setActionCommand(NEW_CATEGORY);
        menuItem.addActionListener(listener);
        add(menuItem);
        
        addSeparator();
        
        if (categoryNode != null) {
            menuItem = new JMenuItem(bundle.getString(DELETE_CATEGORY));
            menuItem.setActionCommand(DELETE_CATEGORY);
            menuItem.addActionListener(listener);
            add(menuItem);
            
            menuItem = new JMenuItem(bundle.getString(RENAME_CATEGORY));
            menuItem.setActionCommand(RENAME_CATEGORY);
            menuItem.addActionListener(listener);
            add(menuItem);
            
            addSeparator();
        }

        menuItem = new JMenuItem(bundle.getString(ORDER_CATEGORIES));
        menuItem.setEnabled(PaletteNode.getPaletteNode().getCookie(Index.class) != null);
        menuItem.setActionCommand(ORDER_CATEGORIES);
        menuItem.addActionListener(listener);
        add(menuItem);
        
        addSeparator();

        menuItem = new JMenuItem(bundle.getString(REFRESH_PALETTE));
        menuItem.setActionCommand(REFRESH_PALETTE);
        menuItem.addActionListener(listener);
        add(menuItem);
    }

    // -------

    private void createCategory() {
        try {
            PaletteNode.getPaletteNode().createNewCategory();
        }
        catch (java.io.IOException e) {
            ErrorManager.getDefault().notify(e);
        }
    }
    
    private void deleteCategory() {
        // first user confirmation...
        String message = MessageFormat.format(
            PaletteUtils.getBundleString("FMT_ConfirmCategoryDelete"), // NOI18N
            new Object [] { categoryNode.getName() });

        NotifyDescriptor desc = new NotifyDescriptor.Confirmation(message,
                    PaletteUtils.getBundleString("CTL_ConfirmCategoryTitle"), // NOI18N
                    NotifyDescriptor.YES_NO_OPTION);

        if (NotifyDescriptor.YES_OPTION.equals(DialogDisplayer.getDefault().notify(desc))) {
            try {
                categoryNode.destroy();
            } 
            catch (java.io.IOException e) {
                ErrorManager.getDefault().notify(e);
            }
        }
    }

    private void renameCategory() {
        NotifyDescriptor.InputLine desc = new NotifyDescriptor.InputLine(
            PaletteUtils.getBundleString("CTL_NewName"), // NOI18N
            PaletteUtils.getBundleString("CTL_Rename")); // NOI18N
        desc.setInputText(categoryNode.getName());

        if (NotifyDescriptor.OK_OPTION.equals(
                    DialogDisplayer.getDefault().notify(desc)))
        {
            String newName = null;
            try {
                newName = desc.getInputText();
                if (!"".equals(newName)) // NOI18N
                    categoryNode.setName(newName);
            }
            catch (IllegalArgumentException e) {
                ErrorManager.getDefault().notify(e);
            }
        }
    }

    private void reorderCategories() {
        Index order = (Index)PaletteNode.getPaletteNode().getCookie(Index.class);
        if (order != null)
            order.reorder();
    }
    
    // -------

    private class Listener implements ActionListener {
        public void actionPerformed(ActionEvent ev) {
            if (ev.getActionCommand().equals(NEW_CATEGORY))
                createCategory();
            else if (ev.getActionCommand().equals(DELETE_CATEGORY))
                deleteCategory();
            else if (ev.getActionCommand().equals(RENAME_CATEGORY))
                renameCategory();
            else if (ev.getActionCommand().equals(ORDER_CATEGORIES))
                reorderCategories();
            else if (ev.getActionCommand().equals(REFRESH_PALETTE))
                manager.resetPalette();
        }
    }
}
... 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.