Source for javax.swing.JCheckBoxMenuItem

   1: /* JCheckBoxMenuItem.java --
   2:    Copyright (C) 2002, 2004 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package javax.swing;
  40: 
  41: import java.io.IOException;
  42: import java.io.ObjectOutputStream;
  43: 
  44: import javax.accessibility.Accessible;
  45: import javax.accessibility.AccessibleContext;
  46: import javax.accessibility.AccessibleRole;
  47: 
  48: /**
  49:  * This class represents JCheckBoxMenuItem. Its behaviour is very similar
  50:  * to JCheckBoxButton. Just like the JCheckBoxButton, user can check and
  51:  * uncheck this menu item by clicking on it. Also setSelected()/setState()
  52:  * can be use used for the same purpose. JCheckBoxMenuItem uses
  53:  * ToggleButtonModel to keep track of its selection.
  54:  */
  55: public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants,
  56:                                                             Accessible
  57: {
  58:   private static final long serialVersionUID = -6676402307973384715L;
  59: 
  60:   /** name for the UI delegate for this menuItem. */
  61:   private static final String uiClassID = "CheckBoxMenuItemUI";
  62: 
  63:   /** Indicates whether this menu item is checked. */
  64:   private boolean state;
  65: 
  66:   /**
  67:    * This array contains text of this menu item if this menu item is in
  68:    * checked state and null it is not.
  69:    */
  70:   private Object[] selectedObjects = new Object[1];
  71: 
  72:   /**
  73:    * Creates a new JCheckBoxMenuItem object.
  74:    */
  75:   public JCheckBoxMenuItem()
  76:   {
  77:     this(null, null);
  78:   }
  79: 
  80:   /**
  81:    * Creates a new JCheckBoxMenuItem with given icon
  82:    *
  83:    * @param icon Icon for this menu item
  84:    */
  85:   public JCheckBoxMenuItem(Icon icon)
  86:   {
  87:     this(null, icon);
  88:   }
  89: 
  90:   /**
  91:    * Creates a new JCheckBoxMenuItem with given label
  92:    *
  93:    * @param text Label for this menu item
  94:    */
  95:   public JCheckBoxMenuItem(String text)
  96:   {
  97:     this(text, null);
  98:   }
  99: 
 100:   /**
 101:    * Creates a new JCheckBoxMenuItem using given action
 102:    *
 103:    * @param action Action for this menu item.
 104:    */
 105:   public JCheckBoxMenuItem(Action action)
 106:   {
 107:     this();
 108:     setAction(action);
 109:   }
 110: 
 111:   /**
 112:    * Creates a new JCheckBoxMenuItem object with given label and icon
 113:    *
 114:    * @param text Label for this menu item
 115:    * @param icon Icon for this menu item
 116:    */
 117:   public JCheckBoxMenuItem(String text, Icon icon)
 118:   {
 119:     this(text, icon, false);
 120:   }
 121: 
 122:   /**
 123:    * Creates a new JCheckBoxMenuItem object using specified label and
 124:    * marked as checked if given 'state' is true.
 125:    *
 126:    * @param text Label for this menu item
 127:    * @param state <code>true</code> if this item should be in checked state and
 128:    *     <code>false</code> otherwise
 129:    */
 130:   public JCheckBoxMenuItem(String text, boolean state)
 131:   {
 132:     this(text, null, state);
 133:   }
 134: 
 135:   /**
 136:    * Creates a new JCheckBoxMenuItem object with given label, icon,
 137:    * and marked as checked if given 'state' is true.
 138:    *
 139:    * @param text Label for this menu item
 140:    * @param icon icon for this menu item
 141:    * @param state <code>true</code> if this item should be in checked state and
 142:    *     false otherwise
 143:    */
 144:   public JCheckBoxMenuItem(String text, Icon icon, boolean state)
 145:   {
 146:     super(text, icon);
 147:     setModel(new JToggleButton.ToggleButtonModel());
 148:     this.state = state;
 149:   }
 150: 
 151:   private void writeObject(ObjectOutputStream stream) throws IOException
 152:   {
 153:   }
 154: 
 155:   /**
 156:    * This method returns a name to identify which look and feel class will be
 157:    * the UI delegate for the menuItem.
 158:    *
 159:    * @return The Look and Feel classID. "JCheckBoxMenuItemUI"
 160:    */
 161:   public String getUIClassID()
 162:   {
 163:     return uiClassID;
 164:   }
 165: 
 166:   /**
 167:    * Returns checked state for this check box menu item.
 168:    *
 169:    * @return Returns true if this menu item is in checked state
 170:    * and false otherwise.
 171:    */
 172:   public boolean getState()
 173:   {
 174:     return state;
 175:   }
 176: 
 177:   /**
 178:    * Sets state for this check box menu item. If
 179:    * given 'state' is true, then mark menu item as checked,
 180:    * and uncheck this menu item otherwise.
 181:    *
 182:    * @param state new state for this menu item
 183:    */
 184:   public synchronized void setState(boolean state)
 185:   {
 186:     this.state = state;
 187:   }
 188: 
 189:   /**
 190:    * This method returns array containing label of this
 191:    * menu item if it is selected and null otherwise.
 192:    *
 193:    * @return Array containing label of this
 194:    *     menu item if this menu item is selected or null otherwise.
 195:    */
 196:   public Object[] getSelectedObjects()
 197:   {
 198:     if (state == true)
 199:       selectedObjects[0] = this.getText();
 200:     else
 201:       selectedObjects[0] = null;
 202: 
 203:     return selectedObjects;
 204:   }
 205: 
 206:   /**
 207:     * This method overrides JComponent.requestFocus with an empty
 208:     * implementation, since JCheckBoxMenuItems should not
 209:     * receve focus in general.
 210:     */
 211:   public void requestFocus()
 212:   {
 213:     //  Should do nothing here
 214:   }
 215: 
 216:   /**
 217:    * A string that describes this JCheckBoxMenuItem. Normally only used
 218:    * for debugging.
 219:    *
 220:    * @return A string describing this JCheckBoxMenuItem
 221:    */
 222:   protected String paramString()
 223:   {
 224:     return "JCheckBoxMenuItem";
 225:   }
 226: 
 227:   public AccessibleContext getAccessibleContext()
 228:   {
 229:     if (accessibleContext == null)
 230:       accessibleContext = new AccessibleJCheckBoxMenuItem();
 231: 
 232:     return accessibleContext;
 233:   }
 234: 
 235:   protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem
 236:   {
 237:     private static final long serialVersionUID = 1079958073579370777L;
 238: 
 239:     /**
 240:      * Creates a new AccessibleJCheckBoxMenuItem object.
 241:      */
 242:     protected AccessibleJCheckBoxMenuItem()
 243:     {
 244:     }
 245: 
 246:     public AccessibleRole getAccessibleRole()
 247:     {
 248:       return AccessibleRole.CHECK_BOX;
 249:     }
 250:   }
 251: }