]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/workingsets/WorkingSetMenuContributionItem.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / workingsets / WorkingSetMenuContributionItem.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.ui.workingsets;
12
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;
19
20 import org.eclipse.core.runtime.Assert;
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         @Override
61         public void fill(Menu menu, int index) {
62                 MenuItem mi= new MenuItem(menu, SWT.RADIO, index);
63
64                 String name= fWorkingSet.getLabel();
65
66                 mi.setText("&" + fId + " " + name);  //$NON-NLS-1$  //$NON-NLS-2$
67                 if (fImage == null) {
68                         ImageDescriptor imageDescriptor= fWorkingSet.getImageDescriptor();
69                         if (imageDescriptor != null)
70                                 fImage= imageDescriptor.createImage();
71                 }
72                 mi.setImage(fImage);
73                 mi.setSelection(fWorkingSet.equals(fActionGroup.getWorkingSet()));
74                 mi.addSelectionListener(new SelectionAdapter() {
75                         @Override
76                         public void widgetSelected(SelectionEvent e) {
77                                 IWorkingSetManager manager= PlatformUI.getWorkbench().getWorkingSetManager();
78                                 fActionGroup.setWorkingSet(fWorkingSet, true);
79                                 manager.addRecentWorkingSet(fWorkingSet);
80                         }
81                 });
82         }
83
84         /*
85          * @see org.eclipse.jface.action.ContributionItem#dispose()
86          * @since 3.0
87          */
88         @Override
89         public void dispose() {
90                 if (fImage != null && !fImage.isDisposed())
91                         fImage.dispose();
92                 fImage= null;
93
94                 super.dispose();
95         }
96
97         /*
98          * @see org.eclipse.jface.action.IContributionItem#isDynamic()
99          */
100         @Override
101         public boolean isDynamic() {
102                 return true;
103         }
104
105         static String getId(int id) {
106                 return WorkingSetMenuContributionItem.class.getName() + "." + id;  //$NON-NLS-1$
107         }
108 }