49 * class. It can be an instance of <code>MenuItem</code>, a submenu
50 * (an instance of <code>Menu</code>), or a check box (an instance of
51 * <code>CheckboxMenuItem</code>).
52 *
53 * @author Sami Shaio
54 * @see java.awt.MenuItem
55 * @see java.awt.CheckboxMenuItem
56 * @since JDK1.0
57 */
58 public class Menu extends MenuItem implements MenuContainer, Accessible {
59
60 static {
61 /* ensure that the necessary native libraries are loaded */
62 Toolkit.loadLibraries();
63 if (!GraphicsEnvironment.isHeadless()) {
64 initIDs();
65 }
66
67 AWTAccessor.setMenuAccessor(
68 new AWTAccessor.MenuAccessor() {
69 public Vector<MenuComponent> getItems(Menu menu) {
70 return menu.items;
71 }
72 });
73 }
74
75 /**
76 * A vector of the items that will be part of the Menu.
77 *
78 * @serial
79 * @see #countItems()
80 */
81 Vector<MenuComponent> items = new Vector<>();
82
83 /**
84 * This field indicates whether the menu has the
85 * tear of property or not. It will be set to
86 * <code>true</code> if the menu has the tear off
87 * property and it will be set to <code>false</code>
88 * if it does not.
89 * A torn off menu can be deleted by a user when
90 * it is no longer needed.
91 *
92 * @serial
93 * @see #isTearOff()
94 */
95 boolean tearOff;
96
97 /**
98 * This field will be set to <code>true</code>
99 * if the Menu in question is actually a help
100 * menu. Otherwise it will be set to <code>
101 * false</code>.
235 * be called on the toolkit thread.
236 */
237 final int countItemsImpl() {
238 return items.size();
239 }
240
241 /**
242 * Gets the item located at the specified index of this menu.
243 * @param index the position of the item to be returned.
244 * @return the item located at the specified index.
245 */
246 public MenuItem getItem(int index) {
247 return getItemImpl(index);
248 }
249
250 /*
251 * This is called by the native code, so client code can't
252 * be called on the toolkit thread.
253 */
254 final MenuItem getItemImpl(int index) {
255 return (MenuItem)items.elementAt(index);
256 }
257
258 /**
259 * Adds the specified menu item to this menu. If the
260 * menu item has been part of another menu, removes it
261 * from that menu.
262 *
263 * @param mi the menu item to be added
264 * @return the menu item added
265 * @see java.awt.Menu#insert(java.lang.String, int)
266 * @see java.awt.Menu#insert(java.awt.MenuItem, int)
267 */
268 public MenuItem add(MenuItem mi) {
269 synchronized (getTreeLock()) {
270 if (mi.parent != null) {
271 mi.parent.remove(mi);
272 }
273 items.addElement(mi);
274 mi.parent = this;
275 MenuPeer peer = (MenuPeer)this.peer;
527 s.defaultWriteObject();
528 }
529
530 /**
531 * Reads the <code>ObjectInputStream</code>.
532 * Unrecognized keys or values will be ignored.
533 *
534 * @param s the <code>ObjectInputStream</code> to read
535 * @exception HeadlessException if
536 * <code>GraphicsEnvironment.isHeadless</code> returns
537 * <code>true</code>
538 * @see java.awt.GraphicsEnvironment#isHeadless
539 * @see #writeObject(ObjectOutputStream)
540 */
541 private void readObject(ObjectInputStream s)
542 throws IOException, ClassNotFoundException, HeadlessException
543 {
544 // HeadlessException will be thrown from MenuComponent's readObject
545 s.defaultReadObject();
546 for(int i = 0; i < items.size(); i++) {
547 MenuItem item = (MenuItem)items.elementAt(i);
548 item.parent = this;
549 }
550 }
551
552 /**
553 * Returns a string representing the state of this <code>Menu</code>.
554 * This method is intended to be used only for debugging purposes, and the
555 * content and format of the returned string may vary between
556 * implementations. The returned string may be empty but may not be
557 * <code>null</code>.
558 *
559 * @return the parameter string of this menu
560 */
561 public String paramString() {
562 String str = ",tearOff=" + tearOff+",isHelpMenu=" + isHelpMenu;
563 return super.paramString() + str;
564 }
565
566 /**
567 * Initialize JNI field and method IDs
|
49 * class. It can be an instance of <code>MenuItem</code>, a submenu
50 * (an instance of <code>Menu</code>), or a check box (an instance of
51 * <code>CheckboxMenuItem</code>).
52 *
53 * @author Sami Shaio
54 * @see java.awt.MenuItem
55 * @see java.awt.CheckboxMenuItem
56 * @since JDK1.0
57 */
58 public class Menu extends MenuItem implements MenuContainer, Accessible {
59
60 static {
61 /* ensure that the necessary native libraries are loaded */
62 Toolkit.loadLibraries();
63 if (!GraphicsEnvironment.isHeadless()) {
64 initIDs();
65 }
66
67 AWTAccessor.setMenuAccessor(
68 new AWTAccessor.MenuAccessor() {
69 public Vector<MenuItem> getItems(Menu menu) {
70 return menu.items;
71 }
72 });
73 }
74
75 /**
76 * A vector of the items that will be part of the Menu.
77 *
78 * @serial
79 * @see #countItems()
80 */
81 Vector<MenuItem> items = new Vector<>();
82
83 /**
84 * This field indicates whether the menu has the
85 * tear of property or not. It will be set to
86 * <code>true</code> if the menu has the tear off
87 * property and it will be set to <code>false</code>
88 * if it does not.
89 * A torn off menu can be deleted by a user when
90 * it is no longer needed.
91 *
92 * @serial
93 * @see #isTearOff()
94 */
95 boolean tearOff;
96
97 /**
98 * This field will be set to <code>true</code>
99 * if the Menu in question is actually a help
100 * menu. Otherwise it will be set to <code>
101 * false</code>.
235 * be called on the toolkit thread.
236 */
237 final int countItemsImpl() {
238 return items.size();
239 }
240
241 /**
242 * Gets the item located at the specified index of this menu.
243 * @param index the position of the item to be returned.
244 * @return the item located at the specified index.
245 */
246 public MenuItem getItem(int index) {
247 return getItemImpl(index);
248 }
249
250 /*
251 * This is called by the native code, so client code can't
252 * be called on the toolkit thread.
253 */
254 final MenuItem getItemImpl(int index) {
255 return items.elementAt(index);
256 }
257
258 /**
259 * Adds the specified menu item to this menu. If the
260 * menu item has been part of another menu, removes it
261 * from that menu.
262 *
263 * @param mi the menu item to be added
264 * @return the menu item added
265 * @see java.awt.Menu#insert(java.lang.String, int)
266 * @see java.awt.Menu#insert(java.awt.MenuItem, int)
267 */
268 public MenuItem add(MenuItem mi) {
269 synchronized (getTreeLock()) {
270 if (mi.parent != null) {
271 mi.parent.remove(mi);
272 }
273 items.addElement(mi);
274 mi.parent = this;
275 MenuPeer peer = (MenuPeer)this.peer;
527 s.defaultWriteObject();
528 }
529
530 /**
531 * Reads the <code>ObjectInputStream</code>.
532 * Unrecognized keys or values will be ignored.
533 *
534 * @param s the <code>ObjectInputStream</code> to read
535 * @exception HeadlessException if
536 * <code>GraphicsEnvironment.isHeadless</code> returns
537 * <code>true</code>
538 * @see java.awt.GraphicsEnvironment#isHeadless
539 * @see #writeObject(ObjectOutputStream)
540 */
541 private void readObject(ObjectInputStream s)
542 throws IOException, ClassNotFoundException, HeadlessException
543 {
544 // HeadlessException will be thrown from MenuComponent's readObject
545 s.defaultReadObject();
546 for(int i = 0; i < items.size(); i++) {
547 MenuItem item = items.elementAt(i);
548 item.parent = this;
549 }
550 }
551
552 /**
553 * Returns a string representing the state of this <code>Menu</code>.
554 * This method is intended to be used only for debugging purposes, and the
555 * content and format of the returned string may vary between
556 * implementations. The returned string may be empty but may not be
557 * <code>null</code>.
558 *
559 * @return the parameter string of this menu
560 */
561 public String paramString() {
562 String str = ",tearOff=" + tearOff+",isHelpMenu=" + isHelpMenu;
563 return super.paramString() + str;
564 }
565
566 /**
567 * Initialize JNI field and method IDs
|