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

package org.netbeans.modules.tasklist.core;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import org.openide.awt.Actions;

import org.openide.util.actions.SystemAction;
import org.openide.util.actions.Presenter.Menu;
import org.openide.awt.StatusDisplayer;

import org.openide.awt.JMenuPlus;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CallableSystemAction;
import org.netbeans.modules.tasklist.core.translators.FormatTranslator;
import org.openide.awt.Mnemonics;

/**
 * Export action which provides a pullright menu selecting the Export format
 * to use.
 *
 * @author Tor Norbye
 * @author Petr Kuzel, toolbar presenter
 */
public final class ExportAction extends CallableSystemAction implements Menu {

    static final long serialVersionUID = 1L;

    protected boolean asynchronous() {
        return false;
    }

    public String getName() {
        return NbBundle.getMessage(ExportAction.class, "LBL_Export"); // NOI18N
    }

    protected String iconResource() {
        return "org/netbeans/modules/tasklist/core/exportAction.gif"; // NOI18N
    }

    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
        // If you will provide context help then use:
        // return new HelpCtx (NewTodoItemAction.class);
    }

    /* Returns a Component that presents the Action, that implements this
    * interface, in a ToolBar.
    * @return the Component representation for the Action
    */
    public Component getToolbarPresenter() {
        final Component original = super.getToolbarPresenter();
        AbstractButton ab = (AbstractButton) original;
        ab.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JPopupMenu menu = new JPopupMenu();

                // XXX missing common super interface for JPopupMenu and JMenu
                // <<<< from menu presenter
                TaskListView view = TaskListView.getCurrent();
                if (view == null) {
                    return;
                }
                TaskList tasklist = view.getList();
                FormatTranslator translators[] = tasklist.getTranslators();
                if (translators == null) {
                    // TODO - disable the menu itself?
                    return;
                }
                for (int i = 0; i < translators.length; i++) {
                    if (translators[i].supportsExport()) {
                        menu.add(createMenuItem(translators[i]));
                    }
                }
                // >>>> from menu presenter

                menu.show(original, original.getWidth(), 0);
            }
        });
        return original;
    }

    /* Returns a submenu that will present this action in a Menu.
    * @return the JMenuItem representation for this action
    */
    public JMenuItem getMenuPresenter() {
        JMenu mainItem = new JMenuPlus();
        Mnemonics.setLocalizedText(mainItem, getName());
        mainItem.setIcon(SystemAction.get(
                ExportAction.class).getIcon());
        HelpCtx.setHelpIDString(mainItem,
                ExportAction.class.getName());
        mainItem.addMenuListener(new MainItemListener());
        return mainItem;
    }

    /* Returns a submenu that will present this action in a PopupMenu.
    * @return the JMenuItem representation for this action
    */
    public JMenuItem getPopupPresenter() {
        JMenu mainItem = new JMenuPlus();
        Mnemonics.setLocalizedText(mainItem, getName());
        HelpCtx.setHelpIDString(mainItem,
                ExportAction.class.getName());
        mainItem.addMenuListener(new MainItemListener());
        return mainItem; 
    }

    public void performAction() {
        // all functionality is accomplished by menu listeners
    }

    final private static String PROPNAME = "translator"; // NOI18N

    private static JMenuItem createMenuItem(FormatTranslator translator) {
        JMenuItem curMenuItem = new JMenuItem();
        Mnemonics.setLocalizedText(curMenuItem, translator.getExportName());
        curMenuItem.putClientProperty(PROPNAME, translator);
        curMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                TaskListView view = TaskListView.getCurrent();
                if (view == null) {
                    return;
                }
                TaskList tasklist = view.getList();
                JComponent jc = (JComponent) evt.getSource();
                FormatTranslator translator =
                        (FormatTranslator) jc.getClientProperty(PROPNAME);
                if (translator != null) {
                    boolean success = translator.write(tasklist, null, null, null,
                            true, false);
                    if (!success) {
                        StatusDisplayer.getDefault().setStatusText(
                                NbBundle.getMessage(ExportAction.class,
                                        "ExportFailed")); // NOI18N
                    }
                }
            }
        });
        return curMenuItem;
    }

    // innerclasses .......................................................

    /** Listens to selecting of main item and expands it to the
     * submenu of exiting and new modes
     */
    private static final class MainItemListener implements MenuListener {

        /** Source of the events */
        private JMenu menu;

        public void menuCanceled(MenuEvent e) {
        }

        public void menuDeselected(MenuEvent e) {
            JMenu menu = (JMenu) e.getSource();
            menu.removeAll();
        }

        public void menuSelected(MenuEvent e) {
            this.menu = (JMenu) e.getSource();

            // Add the import filters to the menu

            // TODO Use lookup to find the filters such that
            // other modules can register their own.
            // Call translator.supportsImport() to determine
            // if the translator is eligible for import.


            // CRAP. I have to add lookup for these suckers already
            // since I can't include stuff in the submodules (usertasks)

            // Also note: the specific tasks which can be imported depends
            // on the window, right? (Well, the list really.)
            //   So perhaps I can just ask the list to provide it for me?
            //

            // XXX missing common super interface for JPopupMenu and JMenu

            TaskListView view = TaskListView.getCurrent();
            if (view == null) {
                return;
            }
            TaskList tasklist = view.getList();
            FormatTranslator translators[] = tasklist.getTranslators();
            if (translators == null) {
                // TODO - disable the menu itself?
                return;
            }
            for (int i = 0; i < translators.length; i++) {
                if (translators[i].supportsExport()) {
                    menu.add(createMenuItem(translators[i]));
                }
            }
        }
    }
}
... 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.