KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > RadioMenu


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.SelectionAdapter;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.widgets.Menu;
21 import org.eclipse.swt.widgets.MenuItem;
22
23 /**
24  * Represents a group of radio buttons in a menu. Each menu item
25  * is mapped onto a particular value. The RadioMenu reports its state
26  * using the attached Model object. That is, Model.getState() will
27  * return the value of the currently selected radio button and Model.setState(value)
28  * will select the radio button associated with the given value.
29  */

30 public class RadioMenu implements IChangeListener {
31
32     private Model data;
33
34     private Menu parent;
35
36     private List JavaDoc items = new ArrayList JavaDoc();
37
38     SelectionAdapter selectionAdapter = new SelectionAdapter() {
39         public void widgetSelected(SelectionEvent e) {
40             Object JavaDoc newState = e.widget.getData();
41
42             data.setState(newState, RadioMenu.this);
43         }
44     };
45
46     /**
47      * Creates a set of radio menu items on the given menu.
48      *
49      * @param parent menu that will contain the menu items
50      * @param newData the model that will store the value of the currently selected item
51      */

52     public RadioMenu(Menu parent, Model newData) {
53         this.parent = parent;
54         this.data = newData;
55
56         newData.addChangeListener(this);
57     }
58
59     /**
60      * Returns true iff the given values are considered equal.
61      *
62      * @param value1
63      * @param value2
64      * @return
65      */

66     private static boolean isEqual(Object JavaDoc value1, Object JavaDoc value2) {
67         if (value1 == null) {
68             return value2 == null;
69         } else if (value2 == null) {
70             return false;
71         }
72
73         return value1.equals(value2);
74     }
75
76     /**
77      * Creates a new menu item with the given text and value. When
78      * the item is selected, the state of the model will change to
79      * match the given value.
80      *
81      * @param text
82      * @param value
83      */

84     public void addMenuItem(String JavaDoc text, Object JavaDoc value) {
85         MenuItem newItem = new MenuItem(parent, SWT.RADIO);
86
87         newItem.setSelection(isEqual(data.getState(), value));
88         newItem.setText(text);
89         newItem.setData(value);
90         items.add(newItem);
91
92         newItem.addSelectionListener(selectionAdapter);
93     }
94
95     /**
96      * Disposes all menu items
97      */

98     public void dispose() {
99         Iterator JavaDoc iter = items.iterator();
100         while (iter.hasNext()) {
101             MenuItem next = (MenuItem) iter.next();
102
103             if (!next.isDisposed()) {
104                 next.removeSelectionListener(selectionAdapter);
105                 next.dispose();
106             }
107         }
108
109         items.clear();
110     }
111
112     /**
113      * Refreshes the selected menu items to match the current state of the model.
114      */

115     private void refreshSelection() {
116         Iterator JavaDoc iter = items.iterator();
117         while (iter.hasNext()) {
118             MenuItem next = (MenuItem) iter.next();
119
120             if (!next.isDisposed()) {
121                 next.setSelection(isEqual(data.getState(), next.getData()));
122             }
123         }
124     }
125
126     /* (non-Javadoc)
127      * @see org.eclipse.ui.internal.controls.IView#changed()
128      */

129     public void update(boolean changed) {
130         refreshSelection();
131     }
132
133 }
134
Popular Tags