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.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;

import org.openide.awt.Actions;
import org.openide.awt.JMenuPlus;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.loaders.InstanceDataObject;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.Repository;
import org.openide.util.actions.Presenter;
import org.openide.util.actions.SystemAction;
import org.openide.util.NbBundle;
import org.openide.util.HelpCtx;
import org.openide.util.WeakListeners;
import org.openide.util.Utilities;


/** Only purpose of this dummy action is to present Places/Bookmarks folder in menu.
 *
 * @author Ian Formanek, Peter Zavadsky
 * @author  Marian Petras
 * @see org.openide.Places#bookmarks
 */
public class BookmarksAction extends SystemAction implements Presenter.Menu {

    /** Icon. */
    private static ImageIcon icon;
    
    /** Generated serial veriosn UID. */
    static final long serialVersionUID =2183102479251760123L;
    

    /** */
    public String getName() {
        return NbBundle.getMessage(BookmarksAction.class,
                                   "ACT_Bookmarks");                    //NOI18N
    }

    /** */
    public HelpCtx getHelpCtx() {
        return new HelpCtx(BookmarksAction.class);
    }

    /** */
    protected String iconResource() {
        return "org/netbeans/modules/url/bookmarks.gif"; // NOI18N
    }

    /** */
    public JMenuItem getMenuPresenter() {
        JMenuItem menu = null;
        FileObject bookmarksFolder = Repository.getDefault()
                                     .getDefaultFileSystem()
                                     .findResource("Bookmarks");        //NOI18N
        if (bookmarksFolder != null && bookmarksFolder.isFolder()) {
            String menuLabel = NbBundle.getMessage(BookmarksAction.class,
                                                   "Bookmarks");        //NOI18N
            menu = new LazyMenu(
                    DataFolder.findFolder(bookmarksFolder),
                    menuLabel);
        }
        return menu;
    }

    /** Dummy implementation of superclass abstract method. */
    public void actionPerformed(ActionEvent evt) {}


    /**
     * Menu item which will create its items lazilly when the popup be first asked.
     * Performance savings.
     */
    private static class LazyMenu extends JMenuPlus {

        /** 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 LazyMenu(DataFolder folder, String name) {
            this.folder = folder;
            Actions.setMenuText(this, name, true);
            
            if (icon == null) {
                icon = new ImageIcon(Utilities.loadImage(
                        "org/netbeans/modules/url/bookmarks.gif"));     //NOI18N
            }
            setIcon(icon);

            this.folder.addPropertyChangeListener(WeakListeners.propertyChange(propertyListener = new PropertyChangeListener() {
                public void propertyChange(PropertyChangeEvent evt) {
                    String propName = evt.getPropertyName();
                    if (propName != null) {
                        if (propName.equals(DataFolder.PROP_CHILDREN)
                                || propName.equals(DataFolder.PROP_ORDER)) {
                            uptodate = false;                    
                        } else if (propName.equals(DataFolder.PROP_NAME)) {
                            Actions.setMenuText(LazyMenu.this,
                                                LazyMenu.this.folder.getName(),
                                                true);
                        }
                    }
                }
            }, this.folder));
        }

        /** Gets popup menu. Adds lazy menu items creation. */
        public JPopupMenu getPopupMenu() {
            if (!uptodate) {
                createMenuItems();
            }
            return super.getPopupMenu();
        }
  
        /** Creates items when actually needed. */
        private void createMenuItems() {
            uptodate = true;
            removeAll();

            DataObject[] childrenDataObjects = folder.getChildren();
            
            for (int i = 0; i < childrenDataObjects.length; i++) {
                DataObject childDO = childrenDataObjects[i];
                if (childDO instanceof URLDataObject) {
                    URLDataObject urlDataObject = (URLDataObject) childDO;
                    add(new URLPresenter(urlDataObject).getMenuPresenter());
                } else if (childDO instanceof InstanceDataObject) {
                    InstanceDataObject instanceDO = (InstanceDataObject)
                                                    childDO;
                    if (instanceDO.instanceOf(JSeparator.class)) {
                        addSeparator();
                    }
                } else if (childDO instanceof DataFolder) {
                    DataFolder dataFolder = (DataFolder) childDO;
                    add(new LazyMenu(dataFolder, 
                                     dataFolder.getNodeDelegate().getName())
                    );
                }
            }
            
            if (getItemCount() == 0) {
                String itemName = NbBundle.getMessage(
                        BookmarksAction.class,
                        "LBL_EmptyMenuItem");                           //NOI18N
                JMenuItem emptyMenuItem = new JMenuItem(itemName); 
                emptyMenuItem.setEnabled(false);
                
                add(emptyMenuItem);
            }
        }
        
    } // End of class LazyMenu.

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.