Source code: com/flexstor/common/awt/menu/MenuItemGroup.java
1 /*
2 * MenuItemGroup.java
3 *
4 * Copyright $Date: 2003/08/11 02:22:42 $ FLEXSTOR.net Inc.
5 *
6 * This work is licensed for use and distribution under license terms found at
7 * http://www.flexstor.org/license.html
8 *
9 */
10
11 package com.flexstor.common.awt.menu;
12
13 import java.util.Enumeration;
14 import java.util.Observable;
15
16 import com.flexstor.common.util.FlexVector;
17
18 /**
19 * This class implements a group of menu items. Within
20 * the group, only one and exactly one item can be selected. Selection
21 * of one item automatically deselects the previous item.
22 * MenuItemgroup is an Observer of CheckboxMenuItem.
23 * @see com.flexstor.common.awt.menu.CheckboxMenuItem
24 */
25 public class MenuItemGroup
26 implements java.util.Observer
27 {
28 FlexVector items = new FlexVector(4);
29 CheckboxMenuItem currentItem = null;
30
31 /**
32 * Add a menu item to this group and sets up observer relationship.
33 * @param item the item to add to the group
34 */
35 public void add(CheckboxMenuItem item)
36 {
37 items.addElement(item);
38 // set up observer/observable
39 item.addObserver(this);
40 }
41
42 /**
43 * Implementation of Observer. Reacts to a notification when
44 * state of a CheckboxMenuItem changes.
45 * @param o the observable (in this case a delegator, ignore it)
46 * @param arg the actual observable, the CheckboxMenuItem
47 * @see com.flexstor.common.awt.menu.CheckboxMenuItem
48 */
49 public void update(Observable o, Object checkboxMenuItem)
50 {
51 CheckboxMenuItem selectedItem = (CheckboxMenuItem)checkboxMenuItem;
52
53 if (selectedItem.getState())
54 {
55 // deselect all other items
56 for(Enumeration e = items.elements(); e.hasMoreElements();)
57 {
58 CheckboxMenuItem item = (CheckboxMenuItem)e.nextElement();
59 if (selectedItem != item)
60 {
61 item.setStateUnNotified(false);
62 }
63 }
64 currentItem = selectedItem;
65 }
66 else
67 {
68 // MenuGroups must always have one item selected,
69 // deselect is only possible by selecting another item.
70 // Therefore, reselect item
71 selectedItem.setStateUnNotified(true);
72 }
73 //vector
74 }
75
76 /**
77 * Gets the currently selected item
78 * @return the item or null
79 */
80 public CheckboxMenuItem getCurrent()
81 {
82 return currentItem;
83 }
84
85 }