1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.workingsets;
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.graphics.Image;
17 import org.eclipse.swt.widgets.Menu;
18 import org.eclipse.swt.widgets.MenuItem;
20 import org.eclipse.core.runtime.Assert;
22 import org.eclipse.jface.action.ContributionItem;
23 import org.eclipse.jface.resource.ImageDescriptor;
25 import org.eclipse.ui.IWorkingSet;
26 import org.eclipse.ui.IWorkingSetManager;
27 import org.eclipse.ui.PlatformUI;
30 * Menu contribution item which shows and lets select a working set.
34 public class WorkingSetMenuContributionItem extends ContributionItem {
37 private IWorkingSet fWorkingSet;
38 private WorkingSetFilterActionGroup fActionGroup;
42 * Constructor for WorkingSetMenuContributionItem.
45 * @param actionGroup the action group
46 * @param workingSet the working set
48 public WorkingSetMenuContributionItem(int id, WorkingSetFilterActionGroup actionGroup, IWorkingSet workingSet) {
50 Assert.isNotNull(actionGroup);
51 Assert.isNotNull(workingSet);
53 fActionGroup= actionGroup;
54 fWorkingSet= workingSet;
58 * Overrides method from ContributionItem.
61 public void fill(Menu menu, int index) {
62 MenuItem mi= new MenuItem(menu, SWT.RADIO, index);
64 String name= fWorkingSet.getLabel();
66 mi.setText("&" + fId + " " + name); //$NON-NLS-1$ //$NON-NLS-2$
68 ImageDescriptor imageDescriptor= fWorkingSet.getImageDescriptor();
69 if (imageDescriptor != null)
70 fImage= imageDescriptor.createImage();
73 mi.setSelection(fWorkingSet.equals(fActionGroup.getWorkingSet()));
74 mi.addSelectionListener(new SelectionAdapter() {
76 public void widgetSelected(SelectionEvent e) {
77 IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager();
78 fActionGroup.setWorkingSet(fWorkingSet, true);
79 manager.addRecentWorkingSet(fWorkingSet);
85 * @see org.eclipse.jface.action.ContributionItem#dispose()
89 public void dispose() {
90 if (fImage != null && !fImage.isDisposed())
98 * @see org.eclipse.jface.action.IContributionItem#isDynamic()
101 public boolean isDynamic() {
105 static String getId(int id) {
106 return WorkingSetMenuContributionItem.class.getName() + "." + id; //$NON-NLS-1$