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:  * A menu item that displays a checkbox. Its behaviour is very similar
  50:  * to {@link JCheckBox}. Just like the <code>JCheckBox</code>, user can check
  51:  * and uncheck this menu item by clicking on it. Also {@link #setSelected()}
  52:  * and {@link #setState()} can be use used for the same purpose.
  53:  * <code>JCheckBoxMenuItem</code> uses
  54:  * <code>ToggleButtonModel</code> to keep track of its selection.
  55:  *
  56:  * @author original author unknown
  57:  */
  58: public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants,
  59:                                                             Accessible
  60: {
  61:   private static final long serialVersionUID = -6676402307973384715L;
  62: 
  63:   /** name for the UI delegate for this menuItem. */
  64:   private static final String uiClassID = "CheckBoxMenuItemUI";
  65: 
  66:   /** Indicates whether this menu item is checked. */
  67:   private boolean state;
  68: 
  69:   /**
  70:    * This array contains text of this menu item if this menu item is in
  71:    * checked state and null it is not.
  72:    */
  73:   private Object[] selectedObjects = new Object[1];
  74: 
  75:   /**
  76:    * Creates a new JCheckBoxMenuItem object.
  77:    */
  78:   public JCheckBoxMenuItem()
  79:   {
  80:     this(null, null);
  81:   }
  82: 
  83:   /**
  84:    * Creates a new JCheckBoxMenuItem with given icon
  85:    *
  86:    * @param icon Icon for this menu item
  87:    */
  88:   public JCheckBoxMenuItem(Icon icon)
  89:   {
  90:     this(null, icon);
  91:   }
  92: 
  93:   /**
  94:    * Creates a new JCheckBoxMenuItem with given label
  95:    *
  96:    * @param text Label for this menu item
  97:    */
  98:   public JCheckBoxMenuItem(String text)
  99:   {
 100:     this(text, null);
 101:   }
 102: 
 103:   /**
 104:    * Creates a new JCheckBoxMenuItem using given action
 105:    *
 106:    * @param action Action for this menu item.
 107:    */
 108:   public JCheckBoxMenuItem(Action action)
 109:   {
 110:     this();
 111:     setAction(action);
 112:   }
 113: 
 114:   /**
 115:    * Creates a new JCheckBoxMenuItem object with given label and icon
 116:    *
 117:    * @param text Label for this menu item
 118:    * @param icon Icon for this menu item
 119:    */
 120:   public JCheckBoxMenuItem(String text, Icon icon)
 121:   {
 122:     this(text, icon, false);
 123:   }
 124: 
 125:   /**
 126:    * Creates a new JCheckBoxMenuItem object using specified label and
 127:    * marked as checked if given 'state' is true.
 128:    *
 129:    * @param text Label for this menu item
 130:    * @param state <code>true</code> if this item should be in checked state and
 131:    *     <code>false</code> otherwise
 132:    */
 133:   public JCheckBoxMenuItem(String text, boolean state)
 134:   {
 135:     this(text, null, state);
 136:   }
 137: 
 138:   /**
 139:    * Creates a new JCheckBoxMenuItem object with given label, icon,
 140:    * and marked as checked if given 'state' is true.
 141:    *
 142:    * @param text Label for this menu item
 143:    * @param icon icon for this menu item
 144:    * @param state <code>true</code> if this item should be in checked state and
 145:    *     false otherwise
 146:    */
 147:   public JCheckBoxMenuItem(String text, Icon icon, boolean state)
 148:   {
 149:     super(text, icon);
 150:     setModel(new JToggleButton.ToggleButtonModel());
 151:     this.state = state;
 152:     this.setVisible(true);
 153:   }
 154: 
 155:   private void writeObject(ObjectOutputStream stream) throws IOException
 156:   {
 157:   }
 158: 
 159:   /**
 160:    * This method returns a name to identify which look and feel class will be
 161:    * the UI delegate for the menuItem.
 162:    *
 163:    * @return The Look and Feel classID. "JCheckBoxMenuItemUI"
 164:    */
 165:   public String getUIClassID()
 166:   {
 167:     return uiClassID;
 168:   }
 169: 
 170:   /**
 171:    * Returns checked state for this check box menu item.
 172:    *
 173:    * @return Returns true if this menu item is in checked state
 174:    * and false otherwise.
 175:    */
 176:   public boolean getState()
 177:   {
 178:     return state;
 179:   }
 180: 
 181:   /**
 182:    * Sets state for this check box menu item. If
 183:    * given 'state' is true, then mark menu item as checked,
 184:    * and uncheck this menu item otherwise.
 185:    *
 186:    * @param state new state for this menu item
 187:    */
 188:   public synchronized void setState(boolean state)
 189:   {
 190:     this.state = state;
 191:   }
 192: 
 193:   /**
 194:    * This method returns array containing label of this
 195:    * menu item if it is selected and null otherwise.
 196:    *
 197:    * @return Array containing label of this
 198:    *     menu item if this menu item is selected or null otherwise.
 199:    */
 200:   public Object[] getSelectedObjects()
 201:   {
 202:     if (state == true)
 203:       selectedObjects[0] = this.getText();
 204:     else
 205:       selectedObjects[0] = null;
 206: 
 207:     return selectedObjects;
 208:   }
 209: 
 210:   /**
 211:     * This method overrides JComponent.requestFocus with an empty
 212:     * implementation, since JCheckBoxMenuItems should not
 213:     * receve focus in general.
 214:     */
 215:   public void requestFocus()
 216:   {
 217:     //  Should do nothing here
 218:   }
 219: 
 220:   /**
 221:    * A string that describes this JCheckBoxMenuItem. Normally only used
 222:    * for debugging.
 223:    *
 224:    * @return A string describing this JCheckBoxMenuItem
 225:    */
 226:   protected String paramString()
 227:   {
 228:     return "JCheckBoxMenuItem";
 229:   }
 230: 
 231:   public AccessibleContext getAccessibleContext()
 232:   {
 233:     if (accessibleContext == null)
 234:       accessibleContext = new AccessibleJCheckBoxMenuItem();
 235: 
 236:     return accessibleContext;
 237:   }
 238: 
 239:   /**
 240:    * Accessibility support for <code>JCheckBoxMenuItem</code>.
 241:    */
 242:   protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem
 243:   {
 244:     private static final long serialVersionUID = 1079958073579370777L;
 245: 
 246:     /**
 247:      * Creates a new AccessibleJCheckBoxMenuItem object.
 248:      */
 249:     protected AccessibleJCheckBoxMenuItem()
 250:     {
 251:     }
 252: 
 253:     public AccessibleRole getAccessibleRole()
 254:     {
 255:       return AccessibleRole.CHECK_BOX;
 256:     }
 257:   }
 258: }