1 11 12 package org.eclipse.ui.internal; 13 14 import org.eclipse.core.runtime.Assert; 15 import org.eclipse.jface.action.ContributionItem; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.events.SelectionAdapter; 18 import org.eclipse.swt.events.SelectionEvent; 19 import org.eclipse.swt.graphics.Image; 20 import org.eclipse.swt.widgets.Menu; 21 import org.eclipse.swt.widgets.MenuItem; 22 import org.eclipse.ui.IWorkingSet; 23 import org.eclipse.ui.IWorkingSetManager; 24 import org.eclipse.ui.PlatformUI; 25 import org.eclipse.ui.actions.WorkingSetFilterActionGroup; 26 import org.eclipse.jface.resource.ImageDescriptor; 27 28 33 public class WorkingSetMenuContributionItem extends ContributionItem { 34 private Image image; 35 36 private int id; 37 38 private IWorkingSet workingSet; 39 40 private WorkingSetFilterActionGroup actionGroup; 41 42 48 public static String getId(int id) { 49 return WorkingSetMenuContributionItem.class.getName() + "." + id; } 51 52 58 public WorkingSetMenuContributionItem(int id, 59 WorkingSetFilterActionGroup actionGroup, IWorkingSet workingSet) { 60 super(getId(id)); 61 Assert.isNotNull(actionGroup); 62 Assert.isNotNull(workingSet); 63 this.id = id; 64 this.actionGroup = actionGroup; 65 this.workingSet = workingSet; 66 } 67 68 74 public void fill(Menu menu, int index) { 75 MenuItem mi = new MenuItem(menu, SWT.RADIO, index); 76 mi.setText("&" + id + " " + workingSet.getLabel()); mi.setSelection(workingSet.equals(actionGroup.getWorkingSet())); 78 mi.addSelectionListener(new SelectionAdapter() { 79 public void widgetSelected(SelectionEvent e) { 80 IWorkingSetManager manager = PlatformUI.getWorkbench() 81 .getWorkingSetManager(); 82 actionGroup.setWorkingSet(workingSet); 83 manager.addRecentWorkingSet(workingSet); 84 } 85 }); 86 if (image == null) { 87 ImageDescriptor imageDescriptor = workingSet.getImageDescriptor(); 88 if (imageDescriptor != null) 89 image = imageDescriptor.createImage(); 90 } 91 mi.setImage(image); 92 } 93 94 97 public boolean isDynamic() { 98 return true; 99 } 100 101 105 public void dispose() { 106 if (image != null && !image.isDisposed()) 107 image.dispose(); 108 image = null; 109 110 super.dispose(); 111 } 112 } 113 | Popular Tags |