| 1 | /* Menu.java -- A Java AWT Menu
|
|---|
| 2 | Copyright (C) 1999, 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.MenuPeer;
|
|---|
| 42 | import java.awt.peer.MenuItemPeer;
|
|---|
| 43 | import java.awt.peer.MenuComponentPeer;
|
|---|
| 44 | import java.io.Serializable;
|
|---|
| 45 | import java.util.Vector;
|
|---|
| 46 | import java.util.Enumeration;
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * This class represents a pull down or tear off menu in Java's AWT.
|
|---|
| 50 | *
|
|---|
| 51 | * @author Aaron M. Renn ([email protected])
|
|---|
| 52 | */
|
|---|
| 53 | public class Menu extends MenuItem implements MenuContainer, Serializable
|
|---|
| 54 | {
|
|---|
| 55 |
|
|---|
| 56 | /*
|
|---|
| 57 | * Static Variables
|
|---|
| 58 | */
|
|---|
| 59 |
|
|---|
| 60 | // Serialization Constant
|
|---|
| 61 | private static final long serialVersionUID = -8809584163345499784L;
|
|---|
| 62 |
|
|---|
| 63 | /*************************************************************************/
|
|---|
| 64 |
|
|---|
| 65 | /*
|
|---|
| 66 | * Instance Variables
|
|---|
| 67 | */
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * @serial The actual items in the menu
|
|---|
| 71 | */
|
|---|
| 72 | private Vector items = new Vector();
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * @serial Flag indicating whether or not this menu is a tear off
|
|---|
| 76 | */
|
|---|
| 77 | private boolean isTearOff;
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * @serial Indicates whether or not this is a help menu.
|
|---|
| 81 | */
|
|---|
| 82 | private boolean isHelpMenu;
|
|---|
| 83 |
|
|---|
| 84 | // From the serialization spec. FIXME: what should it be?
|
|---|
| 85 | private int menuSerializedDataVersion;
|
|---|
| 86 |
|
|---|
| 87 | static final MenuItem separator = new MenuItem("-");
|
|---|
| 88 |
|
|---|
| 89 | /*************************************************************************/
|
|---|
| 90 |
|
|---|
| 91 | /*
|
|---|
| 92 | * Constructors
|
|---|
| 93 | */
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Initializes a new instance of <code>Menu</code> with no label and that
|
|---|
| 97 | * is not a tearoff;
|
|---|
| 98 | *
|
|---|
| 99 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
|
|---|
| 100 | */
|
|---|
| 101 | public
|
|---|
| 102 | Menu()
|
|---|
| 103 | {
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /*************************************************************************/
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * Initializes a new instance of <code>Menu</code> that is not a tearoff and
|
|---|
| 110 | * that has the specified label.
|
|---|
| 111 | *
|
|---|
| 112 | * @param label The menu label.
|
|---|
| 113 | *
|
|---|
| 114 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
|
|---|
| 115 | */
|
|---|
| 116 | public
|
|---|
| 117 | Menu(String label)
|
|---|
| 118 | {
|
|---|
| 119 | this(label, false);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /*************************************************************************/
|
|---|
| 123 |
|
|---|
| 124 | /**
|
|---|
| 125 | * Initializes a new instance of <code>Menu</code> with the specified
|
|---|
| 126 | * label and tearoff status.
|
|---|
| 127 | *
|
|---|
| 128 | * @param label The label for this menu
|
|---|
| 129 | * @param isTearOff <code>true</code> if this menu is a tear off menu,
|
|---|
| 130 | * <code>false</code> otherwise.
|
|---|
| 131 | *
|
|---|
| 132 | * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
|
|---|
| 133 | */
|
|---|
| 134 | public
|
|---|
| 135 | Menu(String label, boolean isTearOff)
|
|---|
| 136 | {
|
|---|
| 137 | super(label);
|
|---|
| 138 |
|
|---|
| 139 | this.isTearOff = isTearOff;
|
|---|
| 140 |
|
|---|
| 141 | if (label.equals("Help"))
|
|---|
| 142 | isHelpMenu = true;
|
|---|
| 143 |
|
|---|
| 144 | if (GraphicsEnvironment.isHeadless())
|
|---|
| 145 | throw new HeadlessException ();
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /*************************************************************************/
|
|---|
| 149 |
|
|---|
| 150 | /*
|
|---|
| 151 | * Instance Methods
|
|---|
| 152 | */
|
|---|
| 153 |
|
|---|
| 154 | /**
|
|---|
| 155 | * Tests whether or not this menu is a tearoff.
|
|---|
| 156 | *
|
|---|
| 157 | * @return <code>true</code> if this menu is a tearoff, <code>false</code>
|
|---|
| 158 | * otherwise.
|
|---|
| 159 | */
|
|---|
| 160 | public boolean
|
|---|
| 161 | isTearOff()
|
|---|
| 162 | {
|
|---|
| 163 | return(isTearOff);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /*************************************************************************/
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| 169 | * Returns the number of items in this menu.
|
|---|
| 170 | *
|
|---|
| 171 | * @return The number of items in this menu.
|
|---|
| 172 | */
|
|---|
| 173 | public int
|
|---|
| 174 | getItemCount()
|
|---|
| 175 | {
|
|---|
| 176 | return(items.size());
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /*************************************************************************/
|
|---|
| 180 |
|
|---|
| 181 | /**
|
|---|
| 182 | * Returns the number of items in this menu.
|
|---|
| 183 | *
|
|---|
| 184 | * @return The number of items in this menu.
|
|---|
| 185 | *
|
|---|
| 186 | * @deprecated This method is deprecated in favor of <code>getItemCount()</code>.
|
|---|
| 187 | */
|
|---|
| 188 | public int
|
|---|
| 189 | count()
|
|---|
| 190 | {
|
|---|
| 191 | return(items.size());
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | /*************************************************************************/
|
|---|
| 195 |
|
|---|
| 196 | /**
|
|---|
| 197 | * Returns the item at the specified index.
|
|---|
| 198 | *
|
|---|
| 199 | * @return The item at the specified index.
|
|---|
| 200 | *
|
|---|
| 201 | * @exception ArrayIndexOutOfBoundsException If the index value is not valid.
|
|---|
| 202 | */
|
|---|
| 203 | public MenuItem
|
|---|
| 204 | getItem(int index)
|
|---|
| 205 | {
|
|---|
| 206 | return((MenuItem)items.elementAt(index));
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /*************************************************************************/
|
|---|
| 210 |
|
|---|
| 211 | /**
|
|---|
| 212 | * Adds the specified item to this menu. If it was previously part of
|
|---|
| 213 | * another menu, it is first removed from that menu.
|
|---|
| 214 | *
|
|---|
| 215 | * @param item The new item to add.
|
|---|
| 216 | *
|
|---|
| 217 | * @return The item that was added.
|
|---|
| 218 | */
|
|---|
| 219 | public MenuItem
|
|---|
| 220 | add(MenuItem item)
|
|---|
| 221 | {
|
|---|
| 222 | items.addElement(item);
|
|---|
| 223 | if (item.parent != null)
|
|---|
| 224 | {
|
|---|
| 225 | item.parent.remove(item);
|
|---|
| 226 | }
|
|---|
| 227 | item.parent = this;
|
|---|
| 228 |
|
|---|
| 229 | if (peer != null)
|
|---|
| 230 | {
|
|---|
| 231 | MenuPeer mp = (MenuPeer) peer;
|
|---|
| 232 | mp.addItem(item);
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | return item;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | /*************************************************************************/
|
|---|
| 239 |
|
|---|
| 240 | /**
|
|---|
| 241 | * Add an item with the specified label to this menu.
|
|---|
| 242 | *
|
|---|
| 243 | * @param label The label of the menu item to add.
|
|---|
| 244 | */
|
|---|
| 245 | public void
|
|---|
| 246 | add(String label)
|
|---|
| 247 | {
|
|---|
| 248 | add(new MenuItem(label));
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /*************************************************************************/
|
|---|
| 252 |
|
|---|
| 253 | /**
|
|---|
| 254 | * Inserts the specified menu item into this menu at the specified index.
|
|---|
| 255 | *
|
|---|
| 256 | * @param item The menu item to add.
|
|---|
| 257 | * @param index The index of the menu item.
|
|---|
| 258 | *
|
|---|
| 259 | * XXX: FIXME
|
|---|
| 260 | *
|
|---|
| 261 | * @exception IllegalArgumentException If the index is less than zero.
|
|---|
| 262 | * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
|
|---|
| 263 | */
|
|---|
| 264 | public void
|
|---|
| 265 | insert(MenuItem item, int index)
|
|---|
| 266 | {
|
|---|
| 267 | if (index < 0)
|
|---|
| 268 | throw new IllegalArgumentException("Index is less than zero");
|
|---|
| 269 |
|
|---|
| 270 | items.insertElementAt(item, index);
|
|---|
| 271 |
|
|---|
| 272 | MenuPeer mp = (MenuPeer)getPeer();
|
|---|
| 273 | // FIXME: Need to add a peer method here.
|
|---|
| 274 | // if (mp != null)
|
|---|
| 275 | // mp.insertItem(item, index);
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /*************************************************************************/
|
|---|
| 279 |
|
|---|
| 280 | /**
|
|---|
| 281 | * Inserts an item with the specified label into this menu at the specified index.
|
|---|
| 282 | *
|
|---|
| 283 | * @param label The label of the item to add.
|
|---|
| 284 | * @param index The index of the menu item.
|
|---|
| 285 | *
|
|---|
| 286 | * @exception IllegalArgumentException If the index is less than zero.
|
|---|
| 287 | * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
|
|---|
| 288 | */
|
|---|
| 289 | public void
|
|---|
| 290 | insert(String label, int index)
|
|---|
| 291 | {
|
|---|
| 292 | insert(new MenuItem(label), index);
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | /*************************************************************************/
|
|---|
| 296 |
|
|---|
| 297 | /**
|
|---|
| 298 | * Adds a separator bar at the current menu location.
|
|---|
| 299 | */
|
|---|
| 300 | public void
|
|---|
| 301 | addSeparator()
|
|---|
| 302 | {
|
|---|
| 303 | add(separator);
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | /*************************************************************************/
|
|---|
| 307 |
|
|---|
| 308 | /**
|
|---|
| 309 | * Inserts a separator bar at the specified index value.
|
|---|
| 310 | *
|
|---|
| 311 | * @param index The index at which to insert a separator bar.
|
|---|
| 312 | *
|
|---|
| 313 | * XXX: FIXME
|
|---|
| 314 | *
|
|---|
| 315 | * @exception IllegalArgumentException If the index is less than zero.
|
|---|
| 316 | * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
|
|---|
| 317 | */
|
|---|
| 318 | public void
|
|---|
| 319 | insertSeparator(int index)
|
|---|
| 320 | {
|
|---|
| 321 | insert(separator, index);
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | /*************************************************************************/
|
|---|
| 325 |
|
|---|
| 326 | /**
|
|---|
| 327 | * Deletes the item at the specified index from this menu.
|
|---|
| 328 | *
|
|---|
| 329 | * @param index The index of the item to remove.
|
|---|
| 330 | *
|
|---|
| 331 | * @exception ArrayIndexOutOfBoundsException If the index is otherwise invalid.
|
|---|
| 332 | */
|
|---|
| 333 | public synchronized void
|
|---|
| 334 | remove(int index)
|
|---|
| 335 | {
|
|---|
| 336 | items.removeElementAt(index);
|
|---|
| 337 |
|
|---|
| 338 | MenuPeer mp = (MenuPeer)getPeer();
|
|---|
| 339 | if (mp != null)
|
|---|
| 340 | mp.delItem(index);
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | /*************************************************************************/
|
|---|
| 344 |
|
|---|
| 345 | /**
|
|---|
| 346 | * Removes the specifed item from the menu. If the specified component
|
|---|
| 347 | * does not exist, this method does nothing. // FIXME: Right?
|
|---|
| 348 | *
|
|---|
| 349 | * @param item The component to remove.
|
|---|
| 350 | */
|
|---|
| 351 | public void
|
|---|
| 352 | remove(MenuComponent item)
|
|---|
| 353 | {
|
|---|
| 354 | int index = items.indexOf(item);
|
|---|
| 355 | if (index == -1)
|
|---|
| 356 | return;
|
|---|
| 357 |
|
|---|
| 358 | remove(index);
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | /*************************************************************************/
|
|---|
| 362 |
|
|---|
| 363 | /**
|
|---|
| 364 | * Removes all the elements from this menu.
|
|---|
| 365 | */
|
|---|
| 366 | public synchronized void
|
|---|
| 367 | removeAll()
|
|---|
| 368 | {
|
|---|
| 369 | int count = getItemCount();
|
|---|
| 370 | for(int i = 0; i < count; i++)
|
|---|
| 371 | {
|
|---|
| 372 | // We must always remove item 0.
|
|---|
| 373 | remove(0);
|
|---|
| 374 | }
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | /*************************************************************************/
|
|---|
| 378 |
|
|---|
| 379 | /**
|
|---|
| 380 | * Creates the native peer for this object.
|
|---|
| 381 | */
|
|---|
| 382 | public void
|
|---|
| 383 | addNotify()
|
|---|
| 384 | {
|
|---|
| 385 | if (peer != null)
|
|---|
| 386 | peer = getToolkit().createMenu(this);
|
|---|
| 387 | super.addNotify ();
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | /*************************************************************************/
|
|---|
| 391 |
|
|---|
| 392 | /**
|
|---|
| 393 | * Destroys the native peer for this object.
|
|---|
| 394 | */
|
|---|
| 395 | public void
|
|---|
| 396 | removeNotify()
|
|---|
| 397 | {
|
|---|
| 398 | super.removeNotify();
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 | /*************************************************************************/
|
|---|
| 402 |
|
|---|
| 403 | /**
|
|---|
| 404 | * Returns a debugging string for this menu.
|
|---|
| 405 | *
|
|---|
| 406 | * @return A debugging string for this menu.
|
|---|
| 407 | */
|
|---|
| 408 | public String
|
|---|
| 409 | paramString()
|
|---|
| 410 | {
|
|---|
| 411 | return (",isTearOff=" + isTearOff + ",isHelpMenu=" + isHelpMenu
|
|---|
| 412 | + super.paramString());
|
|---|
| 413 | }
|
|---|
| 414 |
|
|---|
| 415 | // Accessibility API not yet implemented.
|
|---|
| 416 | // public AccessibleContext getAccessibleContext()
|
|---|
| 417 |
|
|---|
| 418 | } // class Menu
|
|---|