1 /******************************************************************************** 2 * Copyright (c) 2000, 2003 IBM Corporation and others. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Common Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/cpl-v10.html 7 * 8 * Contributors: 9 * IBM Corporation - initial API and implementation 10 *******************************************************************************/ 11 package org.un4gvn.editorviewer.internal.action; 12 13 14 import org.eclipse.jface.action.ContributionItem; 15 import org.eclipse.jface.action.IAction; 16 import org.eclipse.jface.action.IMenuManager; 17 import org.eclipse.jface.action.Separator; 18 import org.eclipse.jface.resource.ImageDescriptor; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.SelectionAdapter; 21 import org.eclipse.swt.events.SelectionEvent; 22 import org.eclipse.swt.widgets.Menu; 23 import org.eclipse.swt.widgets.MenuItem; 24 import org.eclipse.ui.actions.ActionGroup; 25 26 /*** 27 * A MultiActionGroup will display a list of IActions in a menu by transforming them 28 * into MenuItems. The list of labels given will be what is displayed in the ViewMenu for 29 * the corresponding action (the action at the same position in the action array). 30 * The actions are currently implemented as state based 31 * so that after an action is executed the label will have a selection check. 32 * 33 * @since 2.1 34 */ 35 public class MultiActionGroup extends ActionGroup { 36 37 protected IAction[] fActions; 38 39 private int fCurrentSelection; 40 private MenuItem[] fItems; 41 42 43 /*** 44 * Creates a new action group with a given set of actions. 45 * 46 * @param actions the actions for this multi group 47 * @param currentSelection decides which action is selected in the menu on start up. 48 * Denotes the location in the actions array of the current 49 * selected state. It cannot be null. 50 */ 51 public MultiActionGroup(IAction[] actions, int currentSelection) { 52 super(); 53 54 fCurrentSelection = currentSelection; 55 fActions = actions; 56 } 57 58 public int getSelection(){ 59 return fCurrentSelection; 60 } 61 62 /*** 63 * Add the actions to the given menu manager. 64 */ 65 protected void addActions(IMenuManager viewMenu) { 66 67 viewMenu.add(new Separator()); 68 fItems= new MenuItem[fActions.length]; 69 70 for (int i= 0; i < fActions.length; i++) { 71 final int j= i; 72 73 viewMenu.add(new ContributionItem() { 74 75 public void fill(Menu menu, int index) { 76 MenuItem mi= new MenuItem(menu, SWT.CHECK, index); 77 ImageDescriptor d = fActions[j].getImageDescriptor(); 78 mi.setImage(d.createImage());//JavaPlugin.getImageDescriptorRegistry().get(d)); 79 fItems[j]= mi; 80 mi.setText(fActions[j].getText()); 81 mi.setSelection(fCurrentSelection == j); 82 mi.addSelectionListener(new SelectionAdapter() { 83 84 public void widgetSelected(SelectionEvent e) { 85 if (fCurrentSelection == j) { 86 fItems[fCurrentSelection].setSelection(true); 87 return; 88 } 89 fActions[j].run(); 90 91 // Update checked state 92 fItems[fCurrentSelection].setSelection(false); 93 fCurrentSelection= j; 94 fItems[fCurrentSelection].setSelection(true); 95 } 96 97 }); 98 } 99 public boolean isDynamic() { 100 return false; 101 } 102 }); 103 } 104 } 105 }

This page was automatically generated by Maven