1 11 package org.eclipse.jdt.internal.ui.actions; 12 13 import org.eclipse.swt.SWT; 14 import org.eclipse.swt.events.SelectionAdapter; 15 import org.eclipse.swt.events.SelectionEvent; 16 import org.eclipse.swt.widgets.Menu; 17 import org.eclipse.swt.widgets.MenuItem; 18 19 import org.eclipse.jface.action.ContributionItem; 20 import org.eclipse.jface.action.IAction; 21 import org.eclipse.jface.action.IMenuManager; 22 import org.eclipse.jface.action.Separator; 23 import org.eclipse.jface.resource.ImageDescriptor; 24 25 import org.eclipse.ui.actions.ActionGroup; 26 27 import org.eclipse.jdt.internal.ui.JavaPlugin; 28 29 38 public class MultiActionGroup extends ActionGroup { 39 40 public IAction[] NO_ACTIONS = new IAction[0]; 41 42 private IAction[] fActions; 43 44 private int fCurrentSelection; 45 private MenuItem[] fItems; 46 47 55 public MultiActionGroup(IAction[] actions, int currentSelection) { 56 super(); 57 setActions(actions, currentSelection); 58 } 59 60 64 protected MultiActionGroup() { 65 super(); 66 } 67 68 76 protected final void setActions(IAction[] actions, int currentSelection) { 77 fCurrentSelection= currentSelection; 78 fActions = actions; 79 } 80 81 84 protected void addActions(IMenuManager viewMenu) { 85 86 viewMenu.add(new Separator()); 87 fItems= new MenuItem[fActions.length]; 88 89 for (int i= 0; i < fActions.length; i++) { 90 final int j= i; 91 92 viewMenu.add(new ContributionItem() { 93 94 public void fill(Menu menu, int index) { 95 96 int style= SWT.CHECK; 97 if ((fActions[j].getStyle() & IAction.AS_RADIO_BUTTON) != 0) 98 style= SWT.RADIO; 99 100 MenuItem mi= new MenuItem(menu, style, index); 101 ImageDescriptor d= fActions[j].getImageDescriptor(); 102 mi.setImage(JavaPlugin.getImageDescriptorRegistry().get(d)); 103 fItems[j]= mi; 104 mi.setText(fActions[j].getText()); 105 mi.setSelection(fCurrentSelection == j); 106 mi.addSelectionListener(new SelectionAdapter() { 107 108 public void widgetSelected(SelectionEvent e) { 109 if (fCurrentSelection == j) { 110 fItems[fCurrentSelection].setSelection(true); 111 return; 112 } 113 fActions[j].run(); 114 115 fItems[fCurrentSelection].setSelection(false); 117 fCurrentSelection= j; 118 fItems[fCurrentSelection].setSelection(true); 119 } 120 121 }); 122 } 123 public boolean isDynamic() { 124 return false; 125 } 126 }); 127 } 128 } 129 } 130 | Popular Tags |