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 javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import org.openide.NotifyDescriptor;

import org.openide.NotifyDescriptor.Message;
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.DialogDisplayer;

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.netbeans.modules.tasklist.core.translators.UnknownFormatException;
import org.openide.awt.Mnemonics;

/**
   Import action which provides a pullright menu selecting the Import format
   to use.
   @author Tor Norbye
*/
public final class ImportAction extends CallableSystemAction implements Menu {

    static final long serialVersionUID = 1L;

    /* Constructs a import action */
    public ImportAction() {
    }

    protected boolean asynchronous() {
        return false;
    }
    
    public String getName() {
        return NbBundle.getMessage(ImportAction.class, "LBL_Import"); // NOI18N
    }

    /*
    protected String iconResource() {
        return "org/netbeans/modules/tasklist/core/import.gif"; // NOI18N
    }
    */
    
    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
        // If you will provide context help then use:
        // return new HelpCtx(ImportAction.class);
    }
    
    /* Returns a submneu 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(
			   ImportAction.class).getIcon());
        HelpCtx.setHelpIDString (mainItem,
				 ImportAction.class.getName());
        mainItem.addMenuListener(new MainItemListener());
        return mainItem;
    }

    /* Returns a submneu 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,
				 ImportAction.class.getName());
        mainItem.addMenuListener(new MainItemListener());
        return mainItem;
    }

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

    // innerclasses .......................................................
    
    /** Listens to selecting of main item and expands it to the
     * submenu of exiting and new modes
     */
    private static final class MainItemListener
        implements ActionListener, 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.

            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].supportsImport()) {
                    menu.add(createMenuItem(translators[i]));
                }
            }
        }
        
        final private static String PROPNAME = "translator"; // NOI18N

        private JMenuItem createMenuItem(FormatTranslator translator) {
	    JMenuItem curMenuItem = new JMenuItem();
            Mnemonics.setLocalizedText(curMenuItem, translator.getImportName());
	    curMenuItem.putClientProperty(PROPNAME, translator);
	    curMenuItem.addActionListener(this);
	    return curMenuItem;
        }

	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 = true;
                try {
                    tasklist.setDontSave(true);
                    tasklist.setSilentUpdate(true, false);
                    success = translator.read(tasklist, null, null, true);
                } catch (UnknownFormatException e) {
                    // NOTE the exception text should be localized!
                    DialogDisplayer.getDefault().notify(new Message(e.getMessage(),
                                                   NotifyDescriptor.ERROR_MESSAGE));
                    success = true; // Do not bring up another message box ;)
                } finally {
                    tasklist.setDontSave(false);
                    tasklist.setSilentUpdate(false, true);
                }

                if (!success) {
                    StatusDisplayer.getDefault().setStatusText(
                          NbBundle.getMessage(ExportAction.class,
                                              "ImportFailed")); // NOI18N
                }
            }
        }
    }
}
... 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.