1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 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 30 public class RadioMenu implements IChangeListener { 31 32 private Model data; 33 34 private Menu parent; 35 36 private List items = new ArrayList (); 37 38 SelectionAdapter selectionAdapter = new SelectionAdapter() { 39 public void widgetSelected(SelectionEvent e) { 40 Object newState = e.widget.getData(); 41 42 data.setState(newState, RadioMenu.this); 43 } 44 }; 45 46 52 public RadioMenu(Menu parent, Model newData) { 53 this.parent = parent; 54 this.data = newData; 55 56 newData.addChangeListener(this); 57 } 58 59 66 private static boolean isEqual(Object value1, Object 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 84 public void addMenuItem(String text, Object 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 98 public void dispose() { 99 Iterator 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 115 private void refreshSelection() { 116 Iterator 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 129 public void update(boolean changed) { 130 refreshSelection(); 131 } 132 133 } 134 | Popular Tags |