001    /* ========================================================================
002     * JCommon : a free general purpose class library for the Java(tm) platform
003     * ========================================================================
004     *
005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006     * 
007     * Project Info:  http://www.jfree.org/jcommon/index.html
008     *
009     * This library is free software; you can redistribute it and/or modify it 
010     * under the terms of the GNU Lesser General Public License as published by 
011     * the Free Software Foundation; either version 2.1 of the License, or 
012     * (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but 
015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017     * License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this library; if not, write to the Free Software
021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022     * USA.  
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025     * in the United States and other countries.]
026     * 
027     * -----------------------
028     * ActionConcentrator.java
029     * -----------------------
030     * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
031     *
032     * Original Author:  Thomas Morgner;
033     * Contributor(s):   David Gilbert (for Object Refinery Limited);
034     *
035     * $Id: ActionConcentrator.java,v 1.3 2005/10/18 13:22:13 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 24-Aug-2003 : Initial version
040     * 07-Jun-2004 : Corrected source headers (DG);
041     */
042    
043    package org.jfree.ui.action;
044    
045    import java.util.ArrayList;
046    
047    import javax.swing.Action;
048    
049    /**
050     * This class is used to collect actions to be enabled or disabled
051     * by a sinle call.
052     * 
053     * @author Thomas Morgner
054     */
055    public class ActionConcentrator {
056    
057        /** The collection used to store the actions of this concentrator. */
058        private final ArrayList actions;
059    
060        /**
061         * DefaultConstructor.
062         */
063        public ActionConcentrator() {
064            this.actions = new ArrayList();
065        }
066    
067        /**
068         * Adds the action to this concentrator.
069         * 
070         * @param a the action to be added.
071         */
072        public void addAction(final Action a) {
073            if (a == null) {
074                throw new NullPointerException();
075            }
076            this.actions.add(a);
077        }
078    
079        /**
080         * Removes the action from this concentrator.
081         * 
082         * @param a the action to be removed.
083         */
084        public void removeAction(final Action a) {
085            if (a == null) {
086                throw new NullPointerException();
087            }
088            this.actions.remove(a);
089        }
090    
091        /**
092         * Defines the state for all actions. 
093         * 
094         * @param b the new state for all actions.
095         */
096        public void setEnabled(final boolean b) {
097            for (int i = 0; i < this.actions.size(); i++) {
098                final Action a = (Action) this.actions.get(i);
099                a.setEnabled(b);
100            }
101        }
102    
103        /**
104         * Returns, whether all actions are disabled.
105         * If one action is enabled, then this method will return
106         * true.
107         * 
108         * @return true, if at least one action is enabled, false
109         * otherwise.
110         */
111        public boolean isEnabled() {
112            for (int i = 0; i < this.actions.size(); i++) {
113                final Action a = (Action) this.actions.get(i);
114                if (a.isEnabled()) {
115                    return true;
116                }
117            }
118            return false;
119        }
120        
121    }