KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > workingsets > 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 package org.eclipse.jdt.internal.ui.workingsets;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.SelectionAdapter;
17 import org.eclipse.swt.events.SelectionEvent;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.widgets.Menu;
20 import org.eclipse.swt.widgets.MenuItem;
21
22 import org.eclipse.jface.action.ContributionItem;
23 import org.eclipse.jface.resource.ImageDescriptor;
24
25 import org.eclipse.ui.IWorkingSet;
26 import org.eclipse.ui.IWorkingSetManager;
27 import org.eclipse.ui.PlatformUI;
28
29 /**
30  * Menu contribution item which shows and lets select a working set.
31  *
32  * @since 2.0
33  */

34 public class WorkingSetMenuContributionItem extends ContributionItem {
35
36     private int fId;
37     private IWorkingSet fWorkingSet;
38     private WorkingSetFilterActionGroup fActionGroup;
39     private Image fImage;
40
41     /**
42      * Constructor for WorkingSetMenuContributionItem.
43      *
44      * @param id the id
45      * @param actionGroup the action group
46      * @param workingSet the working set
47      */

48     public WorkingSetMenuContributionItem(int id, WorkingSetFilterActionGroup actionGroup, IWorkingSet workingSet) {
49         super(getId(id));
50         Assert.isNotNull(actionGroup);
51         Assert.isNotNull(workingSet);
52         fId= id;
53         fActionGroup= actionGroup;
54         fWorkingSet= workingSet;
55     }
56
57     /*
58      * Overrides method from ContributionItem.
59      */

60     public void fill(Menu menu, int index) {
61         MenuItem mi= new MenuItem(menu, SWT.RADIO, index);
62         
63         String JavaDoc name= fWorkingSet.getLabel();
64         
65         mi.setText("&" + fId + " " + name); //$NON-NLS-1$ //$NON-NLS-2$
66
if (fImage == null) {
67             ImageDescriptor imageDescriptor= fWorkingSet.getImage();
68             if (imageDescriptor != null)
69                 fImage= imageDescriptor.createImage();
70         }
71         mi.setImage(fImage);
72         mi.setSelection(fWorkingSet.equals(fActionGroup.getWorkingSet()));
73         mi.addSelectionListener(new SelectionAdapter() {
74             public void widgetSelected(SelectionEvent e) {
75                 IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager();
76                 fActionGroup.setWorkingSet(fWorkingSet, true);
77                 manager.addRecentWorkingSet(fWorkingSet);
78             }
79         });
80     }
81     
82     /*
83      * @see org.eclipse.jface.action.ContributionItem#dispose()
84      * @since 3.0
85      */

86     public void dispose() {
87         if (fImage != null && !fImage.isDisposed())
88             fImage.dispose();
89         fImage= null;
90         
91         super.dispose();
92     }
93     
94     /*
95      * @see org.eclipse.jface.action.IContributionItem#isDynamic()
96      */

97     public boolean isDynamic() {
98         return true;
99     }
100
101     static String JavaDoc getId(int id) {
102         return WorkingSetMenuContributionItem.class.getName() + "." + id; //$NON-NLS-1$
103
}
104 }
105
Popular Tags