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

package org.netbeans.modules.url;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import org.openide.awt.*;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.WeakListeners;
import org.openide.util.actions.Presenter;
import org.openide.util.actions.SystemAction;
import org.netbeans.modules.url.URLDataObject;
import org.openide.ErrorManager;
import org.openide.filesystems.Repository;

/** The action that create popup menu with urls to IDE resources.
* Only purpose of this dummy action is to present Places/Resources folder in toolbar/menu.
*
* @author Gabriel Tichy
*/

public class ResourcesAction extends SystemAction
    implements Presenter.Toolbar, Presenter.Menu {

    static final long serialVersionUID = 2183102479251760124L;

    /** Toolbar button for toolbar presenter. Icon will be action's icon. */
    ToolbarButton toolbarButton;

    /** Popup menu (for toolbar) with resources bookmarks. */
    private JPopupMenuPlus popupMenu;

    /** Action icon. */
    private static javax.swing.ImageIcon icon;
    
    /** Gets help context for this action. Implements superclass abstract method.
     * @return the help context for this action */
    public HelpCtx getHelpCtx () {
        return new HelpCtx (ResourcesAction.class);
    }

    /** Gets name of action. Implements superclass abstract method.
     * @return the name of the action */
    public String getName () {
        return NbBundle.getMessage (ResourcesAction.class, "ACT_IDEResources"); // NOI18N
    }

    /** Specifies icon resource name. Overrides superclass method.
     * @return name of resource for icon  */
    protected String iconResource () {
        return "org/netbeans/modules/url/webLink.gif"; // NOI18N
    }

    /** Gets presenter for menu. Overrides superclass method. */
    public JMenuItem getMenuPresenter () {
        JMenuItem menu = new LazyMenu (
            findSessionFolder ("Resources"), // NOI18N
            NbBundle.getBundle (ResourcesAction.class).getString ("Resources") // NOI18N
        );

        return menu;
    }

    /** Returns a Component that presents the Action, that implements this
     * interface, in a ToolBar.
     * @return the Component representation for the Action
     */
    public java.awt.Component getToolbarPresenter () {
        if (toolbarButton != null)
            return toolbarButton;
        
        synchronized (ResourcesAction.class) {
            if (toolbarButton != null)
                return toolbarButton;
            
            toolbarButton = new ToolbarButton (
                new ImageIcon (getClass ().getResource ("/org/netbeans/modules/url/resourcesButton.gif"))); // NOI18N
            toolbarButton.setToolTipText (Actions.cutAmpersand (getName ()));
            toolbarButton.addActionListener (new ActionListener() {
                    public void actionPerformed (ActionEvent evt) {
                        showPopupMenu ();
                    }
                });
            
            return toolbarButton;
        }
    }

    private void showPopupMenu () {
        java.awt.Point p = toolbarButton.getLocation ();
        p.y += toolbarButton.getHeight ();
        
        if (popupMenu == null)
            popupMenu = new LazyPopup (findSessionFolder ("Resources")); // NOI18N

        popupMenu.show (toolbarButton.getParent (), p.x, p.y);
    }

    public void actionPerformed (java.awt.event.ActionEvent actionEvent) {
    }

    /** Menu item which will create its items lazilly when the popup will becomming visible.
     * Performance savings.*/
    private static class LazyMenu extends JInlineMenu implements PopupMenuListener {

        /** Bookmarks node on which menu to create. */
        private DataFolder folder;
        
        /** Sub menu. */
        private final JMenu menu;

        /** Indicates whether menu items were uptodate. */
        private boolean uptodate = false;
        
        /** Listener on changes in folder (children/order). */
        private PropertyChangeListener propertyListener;
        
        /** Constructor. */
        public LazyMenu (DataFolder folder, String name) {
            this.folder = folder;
            this.menu = new JMenuPlus ();

            Actions.setMenuText (menu, name, true);
            
            if (icon == null)
                icon = new ImageIcon (ResourcesAction.class.getResource (((ResourcesAction)SystemAction.get (ResourcesAction.class)).iconResource ()));
            
            menu.setIcon (icon);
            
            menu.getPopupMenu ().addPopupMenuListener (this);
            setMenuItems (new JMenuItem[] {menu});
            
            this.folder.addPropertyChangeListener (WeakListeners.propertyChange (propertyListener = new PropertyChangeListener () {
                public void propertyChange (PropertyChangeEvent evt) {
                    if (DataFolder.PROP_CHILDREN.equals (evt.getPropertyName ())
                    || DataFolder.PROP_ORDER.equals(evt.getPropertyName())) {
                        uptodate = false;                    
                    }
                }
            }, this.folder));
        }


        /** Dummy implementation of PopupMenuListener. */
        public void popupMenuCanceled (PopupMenuEvent evt) {}

        /** Dummy implementation of PopupMenuListener. */
        public void popupMenuWillBecomeInvisible (PopupMenuEvent evt) {}

        /** Implemsnts PopupMenuListener method. */
        public void popupMenuWillBecomeVisible (PopupMenuEvent evt) {
            // We are AWT-Event-queue.
            if (!uptodate)
                createMenuItems (); 
        }

        /** Creates items when actually needed. */
        private void createMenuItems () {
            uptodate = true;
            menu.removeAll ();

            DataObject[] childrenDataObjects = folder.getChildren ();
            
            for (int i = 0; i < childrenDataObjects.length; i++) {
                if (childrenDataObjects[i] instanceof URLDataObject) {
                    //Presenter.Menu presenter = (Presenter.Menu)((URLDataObject)childrenDataObjects[i]).createURLPresenter ();
                    try {
                        Presenter.Menu presenter = (Presenter.Menu)((URLDataObject)childrenDataObjects[i]).instanceCreate ();
                        menu.add (presenter.getMenuPresenter ());
                    }
                    catch (java.io.IOException ioe) {}
                    catch (java.lang.ClassNotFoundException ex) {};
                } else if (childrenDataObjects[i] instanceof DataFolder) {
                    menu.add (new LazyMenu (
                        (DataFolder)childrenDataObjects[i], 
                        childrenDataObjects[i].getNodeDelegate ().getName ())
                    );
                }
            }
            
            if (menu.getItemCount () == 0) {
                JMenuItem emptyMenuItem = new JMenuItem( NbBundle.getBundle (ResourcesAction.class).getString ("LBL_EmptyMenuItem")); // NOI18N
                emptyMenuItem.setEnabled (false);
                
                menu.add (emptyMenuItem);
            }
        }
    } // End of class LazyMenu.

    /** JPopupMenu which will create its items lazilly when the popup will becomming visible.
     * Performance savings.*/
    private static class LazyPopup extends JPopupMenuPlus implements PopupMenuListener {

        /** Bookmarks node on which menu to create. */
        private DataFolder folder;
        
        /** Indicates whether menu items were uptodate. */
        private boolean uptodate = false;
        
        /** Listener on changes in folder (children/order). */
        private PropertyChangeListener propertyListener;
        
        /** Constructor. */
        public LazyPopup (DataFolder folder) {
            this.folder = folder;
            
            addPopupMenuListener (this);
            
            this.folder.addPropertyChangeListener (WeakListeners.propertyChange (propertyListener = new PropertyChangeListener () {
                public void propertyChange (PropertyChangeEvent evt) {
                    if(DataFolder.PROP_CHILDREN.equals (evt.getPropertyName ())
                    || DataFolder.PROP_ORDER.equals (evt.getPropertyName ())) {
                        uptodate = false;                    
                    }
                }
            }, this.folder));
        }

        /** Dummy implementation of PopupMenuListener. */
        public void popupMenuCanceled (PopupMenuEvent evt) {}

        /** Dummy implementation of PopupMenuListener. */
        public void popupMenuWillBecomeInvisible (PopupMenuEvent evt) {}

        /** Implements PopupMenuListener method. */
        public void popupMenuWillBecomeVisible (PopupMenuEvent evt) {
            // We are AWT-Event-queue.
            if (!uptodate)
                createMenuItems (); 
        }

        /** Creates items when actually needed. */
        private void createMenuItems () {
            uptodate = true;
            removeAll ();

            DataObject[] childrenDataObjects = folder.getChildren ();
            
            for (int i = 0; i < childrenDataObjects.length; i++) {
                if (childrenDataObjects[i] instanceof URLDataObject) {
                    Presenter.Popup presenter;
                    try {
                         presenter = (Presenter.Popup)((URLDataObject)childrenDataObjects[i]).instanceCreate ();
                        JMenuItem item = presenter.getPopupPresenter ();
                        item.setMnemonic (0);
                        add (item);
                    }
                    catch (java.io.IOException ex) {}
                    catch (java.lang.ClassNotFoundException e) {};
                } else if (childrenDataObjects[i] instanceof DataFolder) {
                    add (new LazyMenu (
                        (DataFolder)childrenDataObjects[i], 
                        childrenDataObjects[i].getNodeDelegate ().getName ())
                    );
                }
            }
            
            if (getComponentCount () == 0) {
                JMenuItem emptyMenuItem = new JMenuItem (NbBundle.getBundle (ResourcesAction.class).getString ("LBL_EmptyMenuItem")); // NOI18N
                emptyMenuItem.setEnabled (false);
                
                add (emptyMenuItem);
            }
        }
    } // End of class LazyPopup.

    /**
    * Returns a DataFolder subfolder of the session folder. In the DataFolder
    * folders go first (sorted by name) followed by the rest of objects sorted
    * by name.
    */
    static synchronized DataFolder findSessionFolder (String name) {
        try {
            org.openide.filesystems.FileSystem fs
                    = Repository.getDefault().getDefaultFileSystem();
            org.openide.filesystems.FileObject fo = fs.findResource (name);
            if (fo == null) {
                // resource not found, try to create new folder
                fo = fs.getRoot ().createFolder (name);
            }
            DataFolder df = DataFolder.findFolder (fo);
            return df;
        } catch (java.io.IOException ex) {
            Error e = new InternalError ("Folder not found and cannot be created: " + name); // NOI18N
            ErrorManager.getDefault().annotate(e, ex);
            throw e;
        }
    }
    
}
... 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.