| 1 | /* CheckboxMenuItem.java -- A menu option with a checkbox on it.
|
|---|
| 2 | Copyright (C) 1999, 2000, 2001, 2002 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., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 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 java.awt;
|
|---|
| 40 |
|
|---|
| 41 | import java.awt.peer.CheckboxMenuItemPeer;
|
|---|
| 42 | import java.awt.peer.MenuItemPeer;
|
|---|
| 43 | import java.awt.peer.MenuComponentPeer;
|
|---|
| 44 | import java.awt.event.ItemEvent;
|
|---|
| 45 | import java.awt.event.ItemListener;
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * This class implements a menu item that has a checkbox on it indicating
|
|---|
| 49 | * the selected state of some option.
|
|---|
| 50 | *
|
|---|
| 51 | * @author Aaron M. Renn ([email protected])
|
|---|
| 52 | * @author Tom Tromey <[email protected]>
|
|---|
| 53 | */
|
|---|
| 54 | public class CheckboxMenuItem extends MenuItem implements ItemSelectable,
|
|---|
| 55 | java.io.Serializable
|
|---|
| 56 | {
|
|---|
| 57 |
|
|---|
| 58 | /*
|
|---|
| 59 | * Static Variables
|
|---|
| 60 | */
|
|---|
| 61 |
|
|---|
| 62 | // Serialization constant
|
|---|
| 63 | private static final long serialVersionUID = 6190621106981774043L;
|
|---|
| 64 |
|
|---|
| 65 | /*
|
|---|
| 66 | * Instance Variables
|
|---|
| 67 | */
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * @serial The state of the checkbox, with <code>true</code> being on and
|
|---|
| 71 | * <code>false</code> being off.
|
|---|
| 72 | */
|
|---|
| 73 | private boolean state;
|
|---|
| 74 |
|
|---|
| 75 | // List of registered ItemListeners
|
|---|
| 76 | private transient ItemListener item_listeners;
|
|---|
| 77 |
|
|---|
| 78 | /*************************************************************************/
|
|---|
| 79 |
|
|---|
| 80 | /*
|
|---|
| 81 | * Constructors
|
|---|
| 82 | */
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * Initializes a new instance of <code>CheckboxMenuItem</code> with no
|
|---|
| 86 | * label and an initial state of off.
|
|---|
| 87 | */
|
|---|
| 88 | public
|
|---|
| 89 | CheckboxMenuItem()
|
|---|
| 90 | {
|
|---|
| 91 | this("", false);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /*************************************************************************/
|
|---|
| 95 |
|
|---|
| 96 | /**
|
|---|
| 97 | * Initializes a new instance of <code>CheckboxMenuItem</code> with the
|
|---|
| 98 | * specified label and an initial state of off.
|
|---|
| 99 | *
|
|---|
| 100 | * @param label The label of the menu item.
|
|---|
| 101 | */
|
|---|
| 102 | public
|
|---|
| 103 | CheckboxMenuItem(String label)
|
|---|
| 104 | {
|
|---|
| 105 | this(label, false);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /*************************************************************************/
|
|---|
| 109 |
|
|---|
| 110 | /**
|
|---|
| 111 | * Initializes a new instance of <code>CheckboxMenuItem</code> with the
|
|---|
| 112 | * specified label and initial state.
|
|---|
| 113 | *
|
|---|
| 114 | * @param label The label of the menu item.
|
|---|
| 115 | * @param state The initial state of the menu item, where <code>true</code>
|
|---|
| 116 | * is on, and <code>false</code> is off.
|
|---|
| 117 | */
|
|---|
| 118 | public
|
|---|
| 119 | CheckboxMenuItem(String label, boolean state)
|
|---|
| 120 | {
|
|---|
| 121 | super(label);
|
|---|
| 122 | this.state = state;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /*************************************************************************/
|
|---|
| 126 |
|
|---|
| 127 | /*
|
|---|
| 128 | * Instance Methods
|
|---|
| 129 | */
|
|---|
| 130 |
|
|---|
| 131 | /**
|
|---|
| 132 | * Returns the state of this menu item.
|
|---|
| 133 | *
|
|---|
| 134 | * @return The state of this menu item.
|
|---|
| 135 | */
|
|---|
| 136 | public boolean
|
|---|
| 137 | getState()
|
|---|
| 138 | {
|
|---|
| 139 | return(state);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /*************************************************************************/
|
|---|
| 143 |
|
|---|
| 144 | /**
|
|---|
| 145 | * Sets the state of this menu item.
|
|---|
| 146 | *
|
|---|
| 147 | * @param state The initial state of the menu item, where <code>true</code>
|
|---|
| 148 | * is on, and <code>false</code> is off.
|
|---|
| 149 | */
|
|---|
| 150 | public synchronized void
|
|---|
| 151 | setState(boolean state)
|
|---|
| 152 | {
|
|---|
| 153 | this.state = state;
|
|---|
| 154 | if (peer != null)
|
|---|
| 155 | {
|
|---|
| 156 | CheckboxMenuItemPeer cp = (CheckboxMenuItemPeer) peer;
|
|---|
| 157 | cp.setState (state);
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | /*************************************************************************/
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * Returns an array of length 1 with the menu item label for this object
|
|---|
| 165 | * if the state is on. Otherwise <code>null</code> is returned.
|
|---|
| 166 | *
|
|---|
| 167 | * @param An array with this menu item's label if it has a state of on,
|
|---|
| 168 | * or <code>null</code> otherwise.
|
|---|
| 169 | */
|
|---|
| 170 | public Object[]
|
|---|
| 171 | getSelectedObjects()
|
|---|
| 172 | {
|
|---|
| 173 | if (state == false)
|
|---|
| 174 | return(null);
|
|---|
| 175 |
|
|---|
| 176 | Object[] obj = new Object[1];
|
|---|
| 177 | obj[0] = getLabel();
|
|---|
| 178 |
|
|---|
| 179 | return(obj);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /*************************************************************************/
|
|---|
| 183 |
|
|---|
| 184 | /**
|
|---|
| 185 | * Create's this object's native peer
|
|---|
| 186 | */
|
|---|
| 187 | public synchronized void
|
|---|
| 188 | addNotify()
|
|---|
| 189 | {
|
|---|
| 190 | if (peer != null)
|
|---|
| 191 | {
|
|---|
| 192 | // This choice of toolkit seems unsatisfying, but I'm not sure
|
|---|
| 193 | // what else to do.
|
|---|
| 194 | peer = getToolkit().createCheckboxMenuItem(this);
|
|---|
| 195 | }
|
|---|
| 196 | super.addNotify ();
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | /*************************************************************************/
|
|---|
| 200 |
|
|---|
| 201 | /**
|
|---|
| 202 | * Adds the specified listener to the list of registered item listeners
|
|---|
| 203 | * for this object.
|
|---|
| 204 | *
|
|---|
| 205 | * @param listener The listener to add.
|
|---|
| 206 | */
|
|---|
| 207 | public synchronized void
|
|---|
| 208 | addItemListener(ItemListener listener)
|
|---|
| 209 | {
|
|---|
| 210 | item_listeners = AWTEventMulticaster.add(item_listeners, listener);
|
|---|
| 211 |
|
|---|
| 212 | enableEvents(AWTEvent.ITEM_EVENT_MASK);
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | /*************************************************************************/
|
|---|
| 216 |
|
|---|
| 217 | /**
|
|---|
| 218 | * Removes the specified listener from the list of registered item
|
|---|
| 219 | * listeners for this object.
|
|---|
| 220 | *
|
|---|
| 221 | * @param listener The listener to remove.
|
|---|
| 222 | */
|
|---|
| 223 | public synchronized void
|
|---|
| 224 | removeItemListener(ItemListener listener)
|
|---|
| 225 | {
|
|---|
| 226 | item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | /*************************************************************************/
|
|---|
| 230 |
|
|---|
| 231 | /**
|
|---|
| 232 | * Processes the specified event by calling <code>processItemEvent()</code>
|
|---|
| 233 | * if it is an instance of <code>ItemEvent</code> or calling the superclass
|
|---|
| 234 | * method otherwise.
|
|---|
| 235 | *
|
|---|
| 236 | * @param event The event to process.
|
|---|
| 237 | */
|
|---|
| 238 | protected void
|
|---|
| 239 | processEvent(AWTEvent event)
|
|---|
| 240 | {
|
|---|
| 241 | if (event instanceof ItemEvent)
|
|---|
| 242 | processItemEvent((ItemEvent)event);
|
|---|
| 243 | else
|
|---|
| 244 | super.processEvent(event);
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | /*************************************************************************/
|
|---|
| 248 |
|
|---|
| 249 | /**
|
|---|
| 250 | * Processes the specified event by dispatching it to any registered listeners.
|
|---|
| 251 | *
|
|---|
| 252 | * @param event The event to process.
|
|---|
| 253 | */
|
|---|
| 254 | protected void
|
|---|
| 255 | processItemEvent(ItemEvent event)
|
|---|
| 256 | {
|
|---|
| 257 | if (item_listeners != null)
|
|---|
| 258 | item_listeners.itemStateChanged(event);
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | /*************************************************************************/
|
|---|
| 262 |
|
|---|
| 263 | /**
|
|---|
| 264 | * Returns a debugging string for this object.
|
|---|
| 265 | *
|
|---|
| 266 | * @return A debugging string for this object.
|
|---|
| 267 | */
|
|---|
| 268 | public String
|
|---|
| 269 | paramString()
|
|---|
| 270 | {
|
|---|
| 271 | return ("label=" + getLabel() + ",state=" + state
|
|---|
| 272 | + "," + super.paramString());
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | } // class CheckboxMenuItem
|
|---|
| 276 |
|
|---|