001    /* JCheckBoxMenuItem.java --
002     Copyright (C) 2002, 2004, 2006, Free Software Foundation, Inc.
003    
004     This file is part of GNU Classpath.
005    
006     GNU Classpath is free software; you can redistribute it and/or modify
007     it under the terms of the GNU General Public License as published by
008     the Free Software Foundation; either version 2, or (at your option)
009     any later version.
010    
011     GNU Classpath is distributed in the hope that it will be useful, but
012     WITHOUT ANY WARRANTY; without even the implied warranty of
013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     General Public License for more details.
015    
016     You should have received a copy of the GNU General Public License
017     along with GNU Classpath; see the file COPYING.  If not, write to the
018     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019     02110-1301 USA.
020    
021     Linking this library statically or dynamically with other modules is
022     making a combined work based on this library.  Thus, the terms and
023     conditions of the GNU General Public License cover the whole
024     combination.
025    
026     As a special exception, the copyright holders of this library give you
027     permission to link this library with independent modules to produce an
028     executable, regardless of the license terms of these independent
029     modules, and to copy and distribute the resulting executable under
030     terms of your choice, provided that you also meet, for each linked
031     independent module, the terms and conditions of the license of that
032     module.  An independent module is a module which is not derived from
033     or based on this library.  If you modify this library, you may extend
034     this exception to your version of the library, but you are not
035     obligated to do so.  If you do not wish to do so, delete this
036     exception statement from your version. */
037    
038    
039    package javax.swing;
040    
041    import javax.accessibility.Accessible;
042    import javax.accessibility.AccessibleContext;
043    import javax.accessibility.AccessibleRole;
044    
045    /**
046     * A menu item that displays a checkbox. Its behaviour is very similar to
047     * {@link JCheckBox}. Just like the <code>JCheckBox</code>, user can check
048     * and uncheck this menu item by clicking on it. Also
049     * {@link AbstractButton#setSelected} and {@link #setState} can be use used for
050     * the same purpose. <code>JCheckBoxMenuItem</code> uses
051     * <code>ToggleButtonModel</code> to keep track of its selection.
052     * 
053     * @author original author unknown
054     */
055    public class JCheckBoxMenuItem
056        extends JMenuItem
057        implements SwingConstants, Accessible
058    {
059      private static final long serialVersionUID = - 6676402307973384715L;
060    
061      /** name for the UI delegate for this menuItem. */
062      private static final String uiClassID = "CheckBoxMenuItemUI";
063    
064      /** Indicates whether this menu item is checked. */
065      private boolean state;
066    
067      /**
068       * This array contains text of this menu item if this menu item is in checked
069       * state and null it is not.
070       */
071      private Object[] selectedObjects = new Object[1];
072    
073      /**
074       * Creates a new JCheckBoxMenuItem object.
075       */
076      public JCheckBoxMenuItem()
077      {
078        this(null, null);
079      }
080    
081      /**
082       * Creates a new JCheckBoxMenuItem with given icon
083       * 
084       * @param icon Icon for this menu item
085       */
086      public JCheckBoxMenuItem(Icon icon)
087      {
088        this(null, icon);
089      }
090    
091      /**
092       * Creates a new JCheckBoxMenuItem with given label
093       * 
094       * @param text Label for this menu item
095       */
096      public JCheckBoxMenuItem(String text)
097      {
098        this(text, null);
099      }
100    
101      /**
102       * Creates a new JCheckBoxMenuItem using given action
103       * 
104       * @param action Action for this menu item.
105       */
106      public JCheckBoxMenuItem(Action action)
107      {
108        this();
109        setAction(action);
110      }
111    
112      /**
113       * Creates a new JCheckBoxMenuItem object with given label and icon
114       * 
115       * @param text Label for this menu item
116       * @param icon Icon for this menu item
117       */
118      public JCheckBoxMenuItem(String text, Icon icon)
119      {
120        this(text, icon, false);
121      }
122    
123      /**
124       * Creates a new JCheckBoxMenuItem object using specified label and marked as
125       * checked if given 'state' is true.
126       * 
127       * @param text Label for this menu item
128       * @param state <code>true</code> if this item should be in checked state
129       *          and <code>false</code> otherwise
130       */
131      public JCheckBoxMenuItem(String text, boolean state)
132      {
133        this(text, null, state);
134      }
135    
136      /**
137       * Creates a new JCheckBoxMenuItem object with given label, icon, and marked
138       * as checked if given 'state' is true.
139       * 
140       * @param text Label for this menu item
141       * @param icon icon for this menu item
142       * @param state <code>true</code> if this item should be in checked state
143       *          and false otherwise
144       */
145      public JCheckBoxMenuItem(String text, Icon icon, boolean state)
146      {
147        super(text, icon);
148        setModel(new JToggleButton.ToggleButtonModel());
149        this.state = state;
150        if (state == true)
151          this.setSelected(true);
152        setFocusable(false);
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 and false
170       *         otherwise.
171       */
172      public boolean getState()
173      {
174        return state;
175      }
176    
177      /**
178       * Sets state for this check box menu item. If given 'state' is true, then
179       * mark menu item as checked, and uncheck this menu item otherwise.
180       * 
181       * @param state new state for this menu item
182       */
183      public synchronized void setState(boolean state)
184      {
185        this.state = state;
186      }
187    
188      /**
189       * This method returns array containing label of this menu item if it is
190       * selected and null otherwise.
191       * 
192       * @return Array containing label of this menu item if this menu item is
193       *         selected or null otherwise.
194       */
195      public Object[] getSelectedObjects()
196      {
197        if (state == true)
198          selectedObjects[0] = this.getText();
199        else
200          selectedObjects[0] = null;
201    
202        return selectedObjects;
203      }
204    
205      /**
206       * This method overrides JComponent.requestFocus with an empty implementation,
207       * since JCheckBoxMenuItems should not receive focus in general.
208       */
209      public void requestFocus()
210      {
211        // Should do nothing here
212      }
213    
214      /**
215       * Returns a string describing the attributes for the
216       * <code>JCheckBoxMenuItem</code> component, for use in debugging. The
217       * return value is guaranteed to be non-<code>null</code>, but the format
218       * of the string may vary between implementations.
219       * 
220       * @return A string describing the attributes of the
221       *         <code>JCheckBoxMenuItem</code>.
222       */
223      protected String paramString()
224      {
225        // calling super seems to be sufficient to match the reference
226        // implementation here...
227        return super.paramString();
228      }
229    
230      /**
231       * Returns the object that provides accessibility features for this
232       * <code>JCheckBoxMenuItem</code> component.
233       * 
234       * @return The accessible context (an instance of
235       *         {@link AccessibleJCheckBoxMenuItem}).
236       */
237      public AccessibleContext getAccessibleContext()
238      {
239        if (accessibleContext == null)
240          accessibleContext = new AccessibleJCheckBoxMenuItem();
241    
242        return accessibleContext;
243      }
244    
245      /**
246       * Provides the accessibility features for the <code>JCheckBoxMenuItem</code>
247       * component.
248       * 
249       * @see JCheckBoxMenuItem#getAccessibleContext()
250       */
251      protected class AccessibleJCheckBoxMenuItem
252          extends AccessibleJMenuItem
253      {
254        private static final long serialVersionUID = 1079958073579370777L;
255    
256        /**
257         * Creates a new <code>AccessibleJCheckBoxMenuItem</code> instance.
258         */
259        protected AccessibleJCheckBoxMenuItem()
260        {
261          // Nothing to do here.
262        }
263    
264        /**
265         * Returns the accessible role for the <code>JCheckBoxMenuItem</code>
266         * component.
267         * 
268         * @return {@link AccessibleRole#CHECK_BOX}.
269         */
270        public AccessibleRole getAccessibleRole()
271        {
272          return AccessibleRole.CHECK_BOX;
273        }
274      }
275    }