Source for org.jfree.ui.action.ActionConcentrator

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * -----------------------
  28:  * ActionConcentrator.java
  29:  * -----------------------
  30:  * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
  31:  *
  32:  * Original Author:  Thomas Morgner;
  33:  * Contributor(s):   David Gilbert (for Object Refinery Limited);
  34:  *
  35:  * $Id: ActionConcentrator.java,v 1.3 2005/10/18 13:22:13 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 24-Aug-2003 : Initial version
  40:  * 07-Jun-2004 : Corrected source headers (DG);
  41:  */
  42: 
  43: package org.jfree.ui.action;
  44: 
  45: import java.util.ArrayList;
  46: 
  47: import javax.swing.Action;
  48: 
  49: /**
  50:  * This class is used to collect actions to be enabled or disabled
  51:  * by a sinle call.
  52:  * 
  53:  * @author Thomas Morgner
  54:  */
  55: public class ActionConcentrator {
  56: 
  57:     /** The collection used to store the actions of this concentrator. */
  58:     private final ArrayList actions;
  59: 
  60:     /**
  61:      * DefaultConstructor.
  62:      */
  63:     public ActionConcentrator() {
  64:         this.actions = new ArrayList();
  65:     }
  66: 
  67:     /**
  68:      * Adds the action to this concentrator.
  69:      * 
  70:      * @param a the action to be added.
  71:      */
  72:     public void addAction(final Action a) {
  73:         if (a == null) {
  74:             throw new NullPointerException();
  75:         }
  76:         this.actions.add(a);
  77:     }
  78: 
  79:     /**
  80:      * Removes the action from this concentrator.
  81:      * 
  82:      * @param a the action to be removed.
  83:      */
  84:     public void removeAction(final Action a) {
  85:         if (a == null) {
  86:             throw new NullPointerException();
  87:         }
  88:         this.actions.remove(a);
  89:     }
  90: 
  91:     /**
  92:      * Defines the state for all actions. 
  93:      * 
  94:      * @param b the new state for all actions.
  95:      */
  96:     public void setEnabled(final boolean b) {
  97:         for (int i = 0; i < this.actions.size(); i++) {
  98:             final Action a = (Action) this.actions.get(i);
  99:             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: }