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.
 */
/*
 * TestsAction.java
 *
 * Created on January 19, 2001, 1:00 PM
 */

package org.netbeans.modules.junit;

import java.awt.Component;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
import org.openide.cookies.SourceCookie;
import org.openide.loaders.DataFolder;
import org.openide.src.ClassElement;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CookieAction;
import org.openide.util.actions.Presenter;
import org.openide.util.actions.SystemAction;
import javax.swing.SwingConstants;
import org.openide.util.Utilities;

/** Action which just holds a few other SystemAction's for grouping purposes.
 *
 * @author  vstejskal
 * @version 1.0
 */
public class TestsAction extends CookieAction implements Presenter.Menu, Presenter.Popup, Presenter.Toolbar {

    /*
    public void actionPerformed (ActionEvent ev) {
        // do nothing; should not be called
    }
     */

    protected Class[] cookieClasses() {
        return new Class[] { DataFolder.class, SourceCookie.class, ClassElement.class };
    }
    
    protected int mode() {
        return MODE_ALL;
    }
    
    protected void performAction(org.openide.nodes.Node[] node) {
        // do nothing; should not be called        
    }
    
    public String getName () {
        return NbBundle.getMessage (TestsAction.class, "LBL_Action_Tests");
    }

    protected String iconResource () {
        return "resources/JUnitLogo.gif";
    }

    public HelpCtx getHelpCtx () {
        return new HelpCtx(TestsAction.class);
    }

    /** Perform extra initialization of this action's singleton.
     * PLEASE do not use constructors for this purpose!
    protected void initialize () {
	super.initialize ();
	putProperty ("someProp", value);
    }
    */
    
    
    /** List of system actions to be displayed within this one's toolbar or submenu. */
    private static final SystemAction[] grouped = new SystemAction[] {
                SystemAction.get (CreateTestAction.class),
                SystemAction.get (OpenTestAction.class),
            };

    public JMenuItem getMenuPresenter () {
        JMenu menu = new LazyMenu (getName ());
        menu.setIcon(new ImageIcon(Utilities.loadImage("org/netbeans/modules/junit/resources/empty.gif"))); //NOI18N
        return menu;
    }

    public JMenuItem getPopupPresenter () {
        JMenu menu = new JMenu (getName ());
        // Conventional not to set an icon here.
        for (int i = 0; i < grouped.length; i++) {
            SystemAction action = grouped[i];
            if (action == null) {
                menu.addSeparator ();
            } else if (action instanceof Presenter.Popup) {
                menu.add (((Presenter.Popup) action).getPopupPresenter ());
            }
        }
        return menu;
    }

    public Component getToolbarPresenter () {
        JToolBar toolbar = new JToolBar (/* In JDK 1.3 you may add: getName () */);
        for (int i = 0; i < grouped.length; i++) {
            SystemAction action = grouped[i];
            if (action == null) {
                toolbar.addSeparator ();
            } else if (action instanceof Presenter.Toolbar) {
                toolbar.add (((Presenter.Toolbar) action).getToolbarPresenter ());
            }
        }
        return toolbar;
    }

    /**
     * Lazy menu which when added to its parent menu, will begin creating the
     * list of submenu items and finding their presenters.
     */
    private final class LazyMenu extends JMenu {
        
        public LazyMenu(String name) {
            super(name);
        }
                
        public JPopupMenu getPopupMenu() {
            if (getItemCount() == 0) {
                for (int i = 0; i < grouped.length; i++) {
                    SystemAction action = grouped[i];
                    if (action == null) {
                        addSeparator ();
                    } else if (action instanceof Presenter.Menu) {
                        add (((Presenter.Menu) action).getMenuPresenter ());
                    }
                }
            }
            return super.getPopupMenu();
        }
        
    }

}
... 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.