001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.events.SelectionAdapter;
018: import org.eclipse.swt.events.SelectionEvent;
019: import org.eclipse.swt.widgets.Menu;
020: import org.eclipse.swt.widgets.MenuItem;
021:
022: /**
023: * Represents a group of radio buttons in a menu. Each menu item
024: * is mapped onto a particular value. The RadioMenu reports its state
025: * using the attached Model object. That is, Model.getState() will
026: * return the value of the currently selected radio button and Model.setState(value)
027: * will select the radio button associated with the given value.
028: */
029: public class RadioMenu implements IChangeListener {
030:
031: private Model data;
032:
033: private Menu parent;
034:
035: private List items = new ArrayList();
036:
037: SelectionAdapter selectionAdapter = new SelectionAdapter() {
038: public void widgetSelected(SelectionEvent e) {
039: Object newState = e.widget.getData();
040:
041: data.setState(newState, RadioMenu.this );
042: }
043: };
044:
045: /**
046: * Creates a set of radio menu items on the given menu.
047: *
048: * @param parent menu that will contain the menu items
049: * @param newData the model that will store the value of the currently selected item
050: */
051: public RadioMenu(Menu parent, Model newData) {
052: this .parent = parent;
053: this .data = newData;
054:
055: newData.addChangeListener(this );
056: }
057:
058: /**
059: * Returns true iff the given values are considered equal.
060: *
061: * @param value1
062: * @param value2
063: * @return
064: */
065: private static boolean isEqual(Object value1, Object value2) {
066: if (value1 == null) {
067: return value2 == null;
068: } else if (value2 == null) {
069: return false;
070: }
071:
072: return value1.equals(value2);
073: }
074:
075: /**
076: * Creates a new menu item with the given text and value. When
077: * the item is selected, the state of the model will change to
078: * match the given value.
079: *
080: * @param text
081: * @param value
082: */
083: public void addMenuItem(String text, Object value) {
084: MenuItem newItem = new MenuItem(parent, SWT.RADIO);
085:
086: newItem.setSelection(isEqual(data.getState(), value));
087: newItem.setText(text);
088: newItem.setData(value);
089: items.add(newItem);
090:
091: newItem.addSelectionListener(selectionAdapter);
092: }
093:
094: /**
095: * Disposes all menu items
096: */
097: public void dispose() {
098: Iterator iter = items.iterator();
099: while (iter.hasNext()) {
100: MenuItem next = (MenuItem) iter.next();
101:
102: if (!next.isDisposed()) {
103: next.removeSelectionListener(selectionAdapter);
104: next.dispose();
105: }
106: }
107:
108: items.clear();
109: }
110:
111: /**
112: * Refreshes the selected menu items to match the current state of the model.
113: */
114: private void refreshSelection() {
115: Iterator iter = items.iterator();
116: while (iter.hasNext()) {
117: MenuItem next = (MenuItem) iter.next();
118:
119: if (!next.isDisposed()) {
120: next.setSelection(isEqual(data.getState(), next
121: .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: }
|