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.i18n;


import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JMenuItem;

import org.openide.filesystems.FileStateInvalidException;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.nodes.Node;
import org.openide.util.actions.Presenter;
import org.openide.util.RequestProcessor;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;


/** 
 * I18n group action which is present in nodes Tools popup menu if the nodes 
 * contains some "i18n-izable" data objects. I never presents icon.
 *
 * @author Peter Zavadsky
 */
public class I18nGroupPopupAction extends I18nGroupAction implements Presenter.Popup {

    /** Generated serial version UID. */
    static final long serialVersionUID = -2577653086544960835L;

    /**
     * Queried by any popup containg ToolsAction. It does not proxy
     * to enable(Activated nodes).
     */
    public boolean isEnabled() {
        //??? if you see here a performance problem please change superclass
        // to extend NodeAction instead of using hard to understand ad-hoc logic
        return enable(WindowManager.getDefault().getRegistry().getActivatedNodes());
    }
    
    /** 
     * Indicates if action becomes enabled based on activated nodes. Implements superclass abstract method. 
     * @return true if one of activate nodes is data object which is able to i18n-ize
     * or is data folder (it's not scaned inside -> too costly operation), it's supposed it can contain such data objects.
     * @see FactoryRegistry#hasFactory 
     */
    protected boolean enable(Node[] activatedNodes) {
        if (activatedNodes == null) return false;

        for (int i = 0; i < activatedNodes.length; i++) {
            DataObject dataObject = (DataObject)activatedNodes[i].getCookie(DataObject.class);

            if (dataObject == null) continue;

            if (dataObject instanceof DataFolder) {
                // Workaround #14740 disable action on SystemFileSystem.
                try {
                    if(dataObject.getPrimaryFile().getFileSystem().isDefault()) {
                        continue;
                    }
                } catch(FileStateInvalidException fsie) {
                    continue;
                }
                
                return true;
            } else if (FactoryRegistry.hasFactory(dataObject.getClass())) {
                return true;
            }
        }

        return false;
    }

    /** Implements Presenter.Popup interface. */
    public JMenuItem getPopupPresenter() {
        return I18nGroupAction.LazyPopup.createLazyPopup(false, this);
    }
            
}
... 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.