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 java.util.ResourceBundle;
import java.text.MessageFormat;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;

import org.openide.awt.JPopupMenuPlus;
import org.openide.nodes.*;
import org.openide.*;
import org.openide.util.Lookup;
import org.openide.util.datatransfer.PasteType;
import org.openide.util.datatransfer.ExClipboard;

/**
 * Context menu for palette items (invoked on category presenter).
 *
 * @author Tomas Pavek, Jan Stola
 */

class ItemPopupMenu extends JPopupMenuPlus {

    /** Node of the corresponding palette category. */
    private Node categoryNode;
    /** Node of the palette item. */
    private Node itemNode;
    /** Component palette content manager. */
    private CPManager manager;

    private static final String SHOW_NAMES = "CTL_ShowNames"; // NOI18N
    private static final String CUT = "CTL_Cut"; // NOI18N
    private static final String COPY = "CTL_Copy"; // NOI18N
    private static final String PASTE = "CTL_Paste"; // NOI18N
    private static final String DELETE = "CTL_Delete"; // NOI18N
    private static final String ORDER_ITEMS = "CTL_OrderItems"; // NOI18N

    /**
     * Constructor.
     *
     * @param categoryNode node of the corresponding category.
     * @param itemNode node of the item (can be null).
     * @param manager component palette manager.
     */
    ItemPopupMenu(Node categoryNode, Node itemNode, CPManager manager) {
        this.categoryNode = categoryNode;
        this.itemNode = itemNode;
        this.manager = manager;

        JMenuItem menuItem;
        ActionListener listener = new Listener();
        Index indexCookie = (Index)categoryNode.getCookie(Index.class);

        ResourceBundle bundle = CPManager.getBundle();

        menuItem = new JCheckBoxMenuItem(bundle.getString(SHOW_NAMES));
        menuItem.setSelected(manager.getShowComponentsNames());
        menuItem.setActionCommand(SHOW_NAMES);
        menuItem.addActionListener(listener);
        add(menuItem);

        addSeparator();

        if (itemNode != null) {
            menuItem = new JMenuItem(bundle.getString(CUT));
            menuItem.setActionCommand(CUT);
            menuItem.addActionListener(listener);
            add(menuItem);

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

        menuItem = new JMenuItem(bundle.getString(PASTE));
        menuItem.setEnabled(getPasteType() != null);
        menuItem.setActionCommand(PASTE);
        menuItem.addActionListener(listener);
        add(menuItem);

        addSeparator();

        if (itemNode != null) {
            menuItem = new JMenuItem(bundle.getString(DELETE));
            menuItem.setActionCommand(DELETE);
            menuItem.addActionListener(listener);
            add(menuItem);

            addSeparator();
        }

        menuItem = new JMenuItem(bundle.getString(ORDER_ITEMS));
        menuItem.setEnabled(indexCookie != null);
        menuItem.setActionCommand(ORDER_ITEMS);
        menuItem.addActionListener(listener);
        add(menuItem);
    }

    // -------

    private void showNames(boolean show) {
        manager.setShowComponentsNames(show);
    }

//    private void showBigIcons(boolean big) {
//    }

    private void cutNode() {
        try {
            Transferable trans = itemNode.clipboardCut();
            if (trans != null) {
                Clipboard clipboard = (Clipboard)
                    Lookup.getDefault().lookup(ExClipboard.class);
                clipboard.setContents(trans, new StringSelection("")); // NOI18N
            }
        }
        catch (java.io.IOException e) {
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
        }
    }

    private void copyNode() {
        try {
            Transferable trans = itemNode.clipboardCopy();
            if (trans != null) {
                Clipboard clipboard = (Clipboard)
                    Lookup.getDefault().lookup(ExClipboard.class);
                clipboard.setContents(trans, new StringSelection("")); // NOI18N
            }
        }
        catch (java.io.IOException e) {
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
        }
    }

    private void deleteNode() {
        // first user confirmation...
        String message = MessageFormat.format(
            PaletteUtils.getBundleString("FMT_ConfirmBeanDelete"), // NOI18N
            new Object[] { itemNode.getDisplayName() });

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

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

    private void reorderItems() {
        Index order = (Index)categoryNode.getCookie(Index.class);
        if (order != null)
            order.reorder();
    }

    private void pasteBean() {
        PasteType type = getPasteType();
        if (type != null) {
            try {
                Transferable trans = type.paste();
                if (trans != null) {
                    ClipboardOwner owner = trans instanceof ClipboardOwner ?
                                             (ClipboardOwner)trans :
                                             new StringSelection(""); // NOI18N
                    Clipboard clipboard = (Clipboard)
                        Lookup.getDefault().lookup(ExClipboard.class);
                    clipboard.setContents(trans, owner);
                }
            }
            catch (java.io.IOException e) {
                ErrorManager.getDefault().notify(e);
            }
        }
    }

    private PasteType getPasteType() {
        Clipboard clipboard = (Clipboard) Lookup.getDefault().lookup(ExClipboard.class);
        Transferable trans = clipboard.getContents(this);
        if (trans != null) {
            PasteType[] pasteTypes = categoryNode.getPasteTypes(trans);
            if (pasteTypes != null && pasteTypes.length != 0)
                return pasteTypes[0];
        }
        return null;
    }

    // -------

    private class Listener implements ActionListener {
        public void actionPerformed(ActionEvent ev) {
            if (ev.getActionCommand().equals(SHOW_NAMES)) {
                JCheckBoxMenuItem menuItem = (JCheckBoxMenuItem) ev.getSource();
                showNames(menuItem.isSelected());
            }
            else if (ev.getActionCommand().equals(CUT))
                cutNode();
            else if (ev.getActionCommand().equals(COPY))
                copyNode();
            else if (ev.getActionCommand().equals(PASTE))
                pasteBean();
            else if (ev.getActionCommand().equals(DELETE))
                deleteNode();
            else if (ev.getActionCommand().equals(ORDER_ITEMS))
                reorderItems();
        }
    }
}
... 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.