KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > actions > BuildSetMenu


1 /*******************************************************************************
2  * Copyright (c) 2004, 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 - Initial API and implementation
10  * hzhou@actuate.com - Fix for Bug 71695 -
11  * [WorkingSets]Removed Working Set is still shown under the menu item
12  * when it is the recently used working set
13  *******************************************************************************/

14 package org.eclipse.ui.internal.ide.actions;
15
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.jface.action.ActionContributionItem;
18 import org.eclipse.jface.action.ContributionItem;
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.action.IMenuListener;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.MenuManager;
23 import org.eclipse.jface.action.Separator;
24 import org.eclipse.swt.widgets.Menu;
25 import org.eclipse.ui.IWorkbenchWindow;
26 import org.eclipse.ui.IWorkingSet;
27 import org.eclipse.ui.application.IActionBarConfigurer;
28
29 /**
30  * Sub-menu off project menu for showing MRU list of working set builds.
31  *
32  * @since 3.0
33  */

34 public class BuildSetMenu extends ContributionItem {
35     private IActionBarConfigurer actionBars;
36
37     boolean dirty = true;
38
39     private IMenuListener menuListener = new IMenuListener() {
40         public void menuAboutToShow(IMenuManager manager) {
41             manager.markDirty();
42             dirty = true;
43         }
44     };
45
46     private IAction selectBuildWorkingSetAction;
47
48     private IWorkbenchWindow window;
49
50     /**
51      * Create a new instance of the receiver.
52      * @param window
53      * @param actionBars
54      */

55     public BuildSetMenu(IWorkbenchWindow window, IActionBarConfigurer actionBars) {
56         this.window = window;
57         this.actionBars = actionBars;
58         selectBuildWorkingSetAction = new SelectBuildWorkingSetAction(window,
59                 actionBars);
60     }
61
62     /**
63      * Adds a mnemonic accelerator to actions in the MRU list of
64      * recently built working sets.
65      * @param action the action to add
66      * @param index the index to add it at
67      */

68     private void addMnemonic(BuildSetAction action, int index) {
69         StringBuffer JavaDoc label = new StringBuffer JavaDoc();
70         //add the numerical accelerator
71
if (index < 9) {
72             label.append('&');
73             label.append(index);
74             label.append(' ');
75         }
76         label.append(action.getWorkingSet().getLabel());
77         action.setText(label.toString());
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.jface.action.IContributionItem#fill(org.eclipse.swt.widgets.Menu, int)
82      */

83     public void fill(Menu menu, int index) {
84         if (getParent() instanceof MenuManager) {
85             ((MenuManager) getParent()).addMenuListener(menuListener);
86         }
87         if (!dirty) {
88             return;
89         }
90         fillMenu(menu);
91         dirty = false;
92     }
93
94     /**
95      * Fills the menu with Show View actions.
96      * @param menu The menu being filled.
97      */

98     private void fillMenu(Menu menu) {
99         boolean isAutoBuilding = ResourcesPlugin.getWorkspace()
100                 .isAutoBuilding();
101
102         //build MRU list of recently built working sets:
103
IWorkingSet[] sets = window.getWorkbench().getWorkingSetManager()
104                 .getRecentWorkingSets();
105         BuildSetAction last = BuildSetAction.lastBuilt;
106         IWorkingSet lastSet = null;
107         //add build action for the last working set that was built
108
int accel = 1;
109         if (last != null) {
110             // add it only if it has not been removed
111
boolean found = false;
112             for (int i = 0; i < sets.length; i++) {
113                 if (sets[i].equals(last.getWorkingSet())){
114                     found = true;
115                     break;
116                 }
117             }
118             if (found) {
119                 last.setChecked(true);
120                 last.setEnabled(!isAutoBuilding);
121                 last.setActionDefinitionId("org.eclipse.ui.project.buildLast"); //$NON-NLS-1$
122
addMnemonic(last, accel++);
123                 new ActionContributionItem(last).fill(menu, -1);
124                 lastSet = last.getWorkingSet();
125             } else {
126                 BuildSetAction.lastBuilt = null;
127             }
128         }
129         //add build actions for the most recently used working sets
130
for (int i = 0; i < sets.length; i++) {
131             if (lastSet != null && lastSet.equals(sets[i])) {
132                 continue;
133             }
134             BuildSetAction action = new BuildSetAction(sets[i], window,
135                     actionBars);
136             addMnemonic(action, accel++);
137             action.setEnabled(!isAutoBuilding);
138             new ActionContributionItem(action).fill(menu, -1);
139         }
140         //add the action to select a different working set
141
if (sets.length > 0) {
142             new Separator().fill(menu, -1);
143         }
144         selectBuildWorkingSetAction.setEnabled(!isAutoBuilding);
145         new ActionContributionItem(selectBuildWorkingSetAction).fill(menu, -1);
146     }
147
148     public boolean isDirty() {
149         return dirty;
150     }
151
152     /**
153      * Overridden to always return true and force dynamic menu building.
154      */

155     public boolean isDynamic() {
156         return true;
157     }
158 }
159
Popular Tags