Merge from the pain train
[official-gcc.git] / libjava / javax / swing / JCheckBoxMenuItem.java
blob090c3c2044a227d296f3d4d15413299c3c66e4de
1 /* JCheckBoxMenuItem.java --
2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
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.
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. */
39 package javax.swing;
41 import java.io.IOException;
42 import java.io.ObjectOutputStream;
44 import javax.accessibility.Accessible;
45 import javax.accessibility.AccessibleContext;
46 import javax.accessibility.AccessibleRole;
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.
55 public class JCheckBoxMenuItem extends JMenuItem implements SwingConstants,
56 Accessible
58 private static final long serialVersionUID = -6676402307973384715L;
60 /** name for the UI delegate for this menuItem. */
61 private static final String uiClassID = "CheckBoxMenuItemUI";
63 /** Indicates whether this menu item is checked. */
64 private boolean state;
66 /**
67 * This array contains text of this menu item if this menu item is in
68 * checked state and null it is not.
70 private Object[] selectedObjects = new Object[1];
72 /**
73 * Creates a new JCheckBoxMenuItem object.
75 public JCheckBoxMenuItem()
77 this(null, null);
80 /**
81 * Creates a new JCheckBoxMenuItem with given icon
83 * @param icon Icon for this menu item
85 public JCheckBoxMenuItem(Icon icon)
87 this(null, icon);
90 /**
91 * Creates a new JCheckBoxMenuItem with given label
93 * @param text Label for this menu item
95 public JCheckBoxMenuItem(String text)
97 this(text, null);
101 * Creates a new JCheckBoxMenuItem using given action
103 * @param action Action for this menu item.
105 public JCheckBoxMenuItem(Action action)
107 this();
108 setAction(action);
112 * Creates a new JCheckBoxMenuItem object with given label and icon
114 * @param text Label for this menu item
115 * @param icon Icon for this menu item
117 public JCheckBoxMenuItem(String text, Icon icon)
119 this(text, icon, false);
123 * Creates a new JCheckBoxMenuItem object using specified label and
124 * marked as checked if given 'state' is true
126 * @param text Label for this menu item
127 * @param state True if this item should be in checked state and false otherwise
129 public JCheckBoxMenuItem(String text, boolean state)
131 this(text, null, state);
135 * Creates a new JCheckBoxMenuItem object with given label, icon,
136 * and marked as checked if given 'state' is true
138 * @param text Label for this menu item
139 * @param icon icon for this menu item
140 * @param state True if this item should be in checked state and false otherwise
142 public JCheckBoxMenuItem(String text, Icon icon, boolean state)
144 super(text, icon);
145 setModel(new JToggleButton.ToggleButtonModel());
146 this.state = state;
149 private void writeObject(ObjectOutputStream stream) throws IOException
154 * This method returns a name to identify which look and feel class will be
155 * the UI delegate for the menuItem.
157 * @return The Look and Feel classID. "JCheckBoxMenuItemUI"
159 public String getUIClassID()
161 return uiClassID;
165 * Returns checked state for this check box menu item.
167 * @return Returns true if this menu item is in checked state
168 * and false otherwise.
170 public boolean getState()
172 return state;
176 * Sets state for this check box menu item. If
177 * given 'state' is true, then mark menu item as checked,
178 * and uncheck this menu item otherwise.
180 * @param state new state for this menu item
183 public synchronized void setState(boolean state)
185 this.state = state;
189 * This method returns array containing label of this
190 * menu item if it is selected and null otherwise.
192 * @return Array containing label of this
193 * menu item if this menu item is selected or null otherwise.
195 public Object[] getSelectedObjects()
197 if (state == true)
198 selectedObjects[0] = this.getText();
199 else
200 selectedObjects[0] = null;
202 return selectedObjects;
206 * This method overrides JComponent.requestFocus with an empty
207 * implementation, since JCheckBoxMenuItems should not
208 * receve focus in general.
210 public void requestFocus()
212 // Should do nothing here
216 * A string that describes this JCheckBoxMenuItem. Normally only used
217 * for debugging.
219 * @return A string describing this JCheckBoxMenuItem
221 protected String paramString()
223 return "JCheckBoxMenuItem";
226 public AccessibleContext getAccessibleContext()
228 if (accessibleContext == null)
229 accessibleContext = new AccessibleJCheckBoxMenuItem();
231 return accessibleContext;
234 protected class AccessibleJCheckBoxMenuItem extends AccessibleJMenuItem
236 private static final long serialVersionUID = 1079958073579370777L;
239 * Creates a new AccessibleJCheckBoxMenuItem object.
241 protected AccessibleJCheckBoxMenuItem()
245 public AccessibleRole getAccessibleRole()
247 return AccessibleRole.CHECK_BOX;