KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > WorkingSetMenuContributionItem


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
29  * Menu contribution item which shows a working set.
30  *
31  * @since 2.1
32  */

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     /**
43      * Returns the id of this menu contribution item
44      *
45      * @param id numerical id
46      * @return String string id
47      */

48     public static String JavaDoc getId(int id) {
49         return WorkingSetMenuContributionItem.class.getName() + "." + id; //$NON-NLS-1$
50
}
51
52     /**
53      * Creates a new instance of the receiver.
54      *
55      * @param id sequential id of the new instance
56      * @param actionGroup the action group this contribution item is created in
57      */

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     /**
69      * Adds a menu item for the working set.
70      * Overrides method from ContributionItem.
71      *
72      * @see org.eclipse.jface.action.ContributionItem#fill(Menu,int)
73      */

74     public void fill(Menu menu, int index) {
75         MenuItem mi = new MenuItem(menu, SWT.RADIO, index);
76         mi.setText("&" + id + " " + workingSet.getLabel()); //$NON-NLS-1$ //$NON-NLS-2$
77
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     /**
95      * Overridden to always return true and force dynamic menu building.
96      */

97     public boolean isDynamic() {
98         return true;
99     }
100
101     /*
102      * @see org.eclipse.jface.action.ContributionItem#dispose()
103      * @since 3.3
104      */

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