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-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.xml.core.actions;

import javax.swing.JComboBox;

import java.awt.Dialog;
import javax.swing.ComboBoxModel;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import org.openide.nodes.Node;
import org.openide.util.HelpCtx;
import org.openide.util.actions.CookieAction;
import org.openide.loaders.DataObject;

import javax.swing.event.ListDataListener;
import org.netbeans.modules.xml.api.cookies.ScenarioCookie;
import org.openide.windows.TopComponent;
import org.netbeans.modules.xml.core.actions.CollectXMLAction;

/**
 * This is the action which is used to customize scenarios and select the active
 * scenario.  In a toolbar it is represented as ComboBox where the user can
 * select the active scenario.  The last item in the ComboBox will cause the
 * ScenarioAction to be performed.  Performing of this action will call
 * customizeScenarios() on the ScenarioCookie of the selected node and it
 * will display the Scenario customization dialog.
 *
 * @author asgeir@dimonsoftware.com
 */
public class ScenarioAction extends CookieAction implements CollectXMLAction.XMLAction {
    /** Serial Version UID */
    private static final long serialVersionUID = -640535981015880507L;
    
    /** @link dependency */
    /*# ScenarioCookie lnkScenarioCookie; */
    
    protected Class[] cookieClasses() {
        return new Class[] {ScenarioCookie.class};
    }
    
    protected int mode() {
        return MODE_EXACTLY_ONE;
    }
    
    protected void performAction(Node[] nodes) {
        if (nodes.length > 0) {
            ScenarioCookie scenarioCookie = (ScenarioCookie)nodes[0].getCookie(ScenarioCookie.class);
            if (scenarioCookie != null) {
                scenarioCookie.customizeScenarios();
            }
        }
    }
    
    public String getName() {
        return Util.THIS.getString("LBL_Scenario_Action");
    }
    
    protected String iconResource() {
        return "org/netbeans/modules/xml/core/resources/scenarioAction.gif"; // NOI18N
    }
    
    public java.awt.Component getToolbarPresenter() {
        return new ToolbarPresenter();
    }
    
    public HelpCtx getHelpCtx() {
        return new HelpCtx (ScenarioAction.class);
    }
    
    private class ToolbarPresenter extends JComboBox {
        private static final int FIXED_WIDTH = 150;
        
        
        public ToolbarPresenter() {
            super();
            addAncestorListener(new AncestorListener() {
                public void ancestorAdded(AncestorEvent event) {
                    java.awt.Component parent = event.getAncestor();
                    while (parent != null) {
                        if (parent instanceof TopComponent) {
                            Node[] nodes = ((TopComponent)parent).getActivatedNodes();
                            if (nodes != null && nodes.length > 0) {
                                DataObject dataObj = (DataObject)nodes[0].getCookie(DataObject.class);
                                if (dataObj != null) {
                                    ScenarioCookie scenarioCookie = (ScenarioCookie)dataObj.getCookie(ScenarioCookie.class);
                                    if (scenarioCookie != null) {
                                        ExScenarioModel model = new ExScenarioModel(scenarioCookie);
                                        setModel(model);
                                        setEnabled(true);
                                        if (model.getSize() == 1) {
                                            setSelectedIndex(-1);
                                        }
                                        model.setAllowCustomizing(true);
                                        return;
                                    }
                                }
                            }
                            setEnabled(false);
                            break;
                        }
                        parent = parent.getParent();
                    }
                }
                public void ancestorMoved(AncestorEvent event) {}
                public void ancestorRemoved(AncestorEvent event) {}
            });
        }
        
        public java.awt.Dimension getMinimumSize() {
            // minimum width to prevent excessive horizontal shrinking
            return new java.awt.Dimension(FIXED_WIDTH, getPreferredSize().height);
        }
        
        public java.awt.Dimension getMaximumSize() {
            // minimum width to prevent moving of the remaining contents of the toolbar
            return new java.awt.Dimension(FIXED_WIDTH, getPreferredSize().height);
        }
    }
    
    
    /**
     * This class adds one item to the end of the ComboBoxModel which performs
     * the scenario action when selected.
     */
    private class ExScenarioModel implements ComboBoxModel {
        
        private ScenarioCookie scenarioCookie;
        
        private ComboBoxModel scenarioModel;
        
        private String customizeValue;
        
        private boolean allowCustomizing = false;
        
        public ExScenarioModel(ScenarioCookie scenarioCookie) {
            this.scenarioCookie = scenarioCookie;
            this.scenarioModel = scenarioCookie.getModel();
            this.customizeValue = "[ " + Util.THIS.getString("LBL_Scenario_Action") + "]";
        }
        
        /** Specifies whether scenario customizer should be started when the customizeValue
         * is selected.  The default value is false */
        public void setAllowCustomizing(boolean allow) {
            allowCustomizing = allow;
        }
        
        public Object getElementAt(int index) {
            if (index == scenarioModel.getSize()) {
                return customizeValue;
            } else {
                return scenarioModel.getElementAt(index);
            }
        }
        
        public void setSelectedItem(Object anObject) {
            if (anObject == customizeValue && allowCustomizing) {
                scenarioCookie.customizeScenarios();
            } else {
                scenarioModel.setSelectedItem(anObject);
            }
        }
        
        public int getSize() {
            return scenarioModel.getSize() + 1;
        }
        
        public void addListDataListener(ListDataListener l) {
            scenarioModel.addListDataListener(l);
        }
        
        public Object getSelectedItem() {
            return scenarioModel.getSelectedItem();
        }
        
        public void removeListDataListener(ListDataListener l) {
            scenarioModel.removeListDataListener(l);
        }
    }
}
... 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.