001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.jface.action.ContributionItem;
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.events.SelectionAdapter;
016: import org.eclipse.swt.events.SelectionEvent;
017: import org.eclipse.swt.graphics.Image;
018: import org.eclipse.swt.widgets.Menu;
019: import org.eclipse.swt.widgets.MenuItem;
020: import org.eclipse.ui.IWorkingSet;
021: import org.eclipse.ui.IWorkingSetManager;
022: import org.eclipse.ui.PlatformUI;
023: import org.eclipse.ui.actions.WorkingSetFilterActionGroup;
024: import org.eclipse.jface.resource.ImageDescriptor;
025:
026: /**
027: * Menu contribution item which shows a working set.
028: *
029: * @since 2.1
030: */
031: public class WorkingSetMenuContributionItem extends ContributionItem {
032: private Image image;
033:
034: private int id;
035:
036: private IWorkingSet workingSet;
037:
038: private WorkingSetFilterActionGroup actionGroup;
039:
040: /**
041: * Returns the id of this menu contribution item
042: *
043: * @param id numerical id
044: * @return String string id
045: */
046: public static String getId(int id) {
047: return WorkingSetMenuContributionItem.class.getName()
048: + "." + id; //$NON-NLS-1$
049: }
050:
051: /**
052: * Creates a new instance of the receiver.
053: *
054: * @param id sequential id of the new instance
055: * @param actionGroup the action group this contribution item is created in
056: */
057: public WorkingSetMenuContributionItem(int id,
058: WorkingSetFilterActionGroup actionGroup,
059: IWorkingSet workingSet) {
060: super (getId(id));
061: Assert.isNotNull(actionGroup);
062: Assert.isNotNull(workingSet);
063: this .id = id;
064: this .actionGroup = actionGroup;
065: this .workingSet = workingSet;
066: }
067:
068: /**
069: * Adds a menu item for the working set.
070: * Overrides method from ContributionItem.
071: *
072: * @see org.eclipse.jface.action.ContributionItem#fill(Menu,int)
073: */
074: public void fill(Menu menu, int index) {
075: MenuItem mi = new MenuItem(menu, SWT.RADIO, index);
076: mi.setText("&" + id + " " + workingSet.getLabel()); //$NON-NLS-1$ //$NON-NLS-2$
077: mi.setSelection(workingSet.equals(actionGroup.getWorkingSet()));
078: mi.addSelectionListener(new SelectionAdapter() {
079: public void widgetSelected(SelectionEvent e) {
080: IWorkingSetManager manager = PlatformUI.getWorkbench()
081: .getWorkingSetManager();
082: actionGroup.setWorkingSet(workingSet);
083: manager.addRecentWorkingSet(workingSet);
084: }
085: });
086: if (image == null) {
087: ImageDescriptor imageDescriptor = workingSet
088: .getImageDescriptor();
089: if (imageDescriptor != null)
090: image = imageDescriptor.createImage();
091: }
092: mi.setImage(image);
093: }
094:
095: /**
096: * Overridden to always return true and force dynamic menu building.
097: */
098: public boolean isDynamic() {
099: return true;
100: }
101:
102: /*
103: * @see org.eclipse.jface.action.ContributionItem#dispose()
104: * @since 3.3
105: */
106: public void dispose() {
107: if (image != null && !image.isDisposed())
108: image.dispose();
109: image = null;
110:
111: super.dispose();
112: }
113: }
|