1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
package org.eclipse.debug.internal.ui.actions;
/*
* (c) Copyright IBM Corp. 2000, 2001.
* All Rights Reserved.
*/
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.ILaunchHistoryChangedListener;
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationHistoryElement;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.SubContributionItem;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IActionDelegateWithEvent;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2;
/**
* Superclass of run & debug pulldown actions.
*/
public abstract class LaunchDropDownAction implements IWorkbenchWindowPulldownDelegate2,
IActionDelegateWithEvent,
ILaunchHistoryChangedListener {
private ExecutionAction fLaunchAction;
private Menu fCreatedMenu;
private DebugWithConfigurationAction fDebugWithAction;
private RunWithConfigurationAction fRunWithAction;
/**
* This action delegate's proxy.
*/
private IAction fActionProxy;
/**
* The tooltip that was specified in XML for this action.
*/
private String fOriginalTooltip;
protected void setActionProxy(IAction action) {
fActionProxy = action;
}
protected IAction getActionProxy() {
return fActionProxy;
}
public LaunchDropDownAction(ExecutionAction launchAction) {
setLaunchAction(launchAction);
}
private void createMenuForAction(Menu parent, IAction action, int count) {
if (count > 0) {
StringBuffer label= new StringBuffer();
if (count < 10) {
//add the numerical accelerator
label.append('&');
label.append(count);
label.append(' ');
}
label.append(action.getText());
action.setText(label.toString());
}
ActionContributionItem item= new ActionContributionItem(action);
item.fill(parent, -1);
}
/**
* Initialize this action so that it can dynamically set its tooltip.
*/
protected void initialize(IAction action) {
DebugUIPlugin.getLaunchConfigurationManager().addLaunchHistoryListener(this);
fOriginalTooltip = action.getToolTipText();
setActionProxy(action);
updateTooltip();
}
/**
* Updates this action's tooltip to correspond to the most recent launch.
*/
protected void updateTooltip() {
LaunchConfigurationHistoryElement lastLaunched = getLastLaunch();
String tooltip= fOriginalTooltip;
if (lastLaunched != null) {
tooltip= getLastLaunchPrefix() + lastLaunched.getLaunchConfiguration().getName();
} else {
tooltip= getStaticTooltip();
}
getActionProxy().setToolTipText(tooltip);
}
/**
* @see ILaunchHistoryChangedListener#launchHistoryChanged()
*/
public void launchHistoryChanged() {
updateTooltip();
}
/**
* @see IWorkbenchWindowActionDelegate#dispose()
*/
public void dispose() {
if (getCreatedMenu() != null) {
getCreatedMenu().dispose();
}
DebugUIPlugin.getLaunchConfigurationManager().removeLaunchHistoryListener(this);
}
/**
* Return the last launch that occurred in the workspace.
*/
protected LaunchConfigurationHistoryElement getLastLaunch() {
return DebugUIPlugin.getLaunchConfigurationManager().getLastLaunch();
}
/**
* @see IWorkbenchWindowPulldownDelegate#getMenu(Control)
*/
public Menu getMenu(Control parent) {
if (getCreatedMenu() != null) {
getCreatedMenu().dispose();
}
setCreatedMenu(new Menu(parent));
return createMenu(getCreatedMenu());
}
/**
* @see IMenuCreator#getMenu(Menu)
*/
public Menu getMenu(Menu parent) {
if (getCreatedMenu() != null) {
getCreatedMenu().dispose();
}
setCreatedMenu(new Menu(parent));
return createMenu(getCreatedMenu());
}
/**
* Create the drop-down menu based on whether the config style pref is set
*/
protected Menu createMenu(Menu menu) {
LaunchConfigurationHistoryElement[] historyList= getHistory();
LaunchConfigurationHistoryElement[] favoriteList = getFavorites();
// Add any favorites
int total = 0;
for (int i = 0; i < favoriteList.length; i++) {
LaunchConfigurationHistoryElement launch= favoriteList[i];
RelaunchHistoryLaunchAction newAction= new RelaunchHistoryLaunchAction(launch);
createMenuForAction(menu, newAction, total + 1);
total++;
}
// Separator between favorites and history
if (favoriteList.length > 0) {
new MenuItem(menu, SWT.SEPARATOR);
}
// Add history launches next
for (int i = 0; i < historyList.length; i++) {
LaunchConfigurationHistoryElement launch= historyList[i];
RelaunchHistoryLaunchAction newAction= new RelaunchHistoryLaunchAction(launch);
createMenuForAction(menu, newAction, total+1);
total++;
}
// Separator between history and common actions
if (historyList.length > 0) {
new MenuItem(menu, SWT.SEPARATOR);
}
// Add the actions to bring up the dialog
if (getLaunchAction() != null) {
// Cascading menu for config type 'shortcuts'
if (getMode() == ILaunchManager.DEBUG_MODE) {
createMenuForAction(menu, getDebugWithAction(), -1);
} else {
createMenuForAction(menu, getRunWithAction(), -1);
}
// Add non-shortcutted access to the launch configuration dialog
OpenLaunchConfigurationsAction action = null;
if (getMode() == ILaunchManager.DEBUG_MODE) {
action = new OpenDebugConfigurations();
} else {
action = new OpenRunConfigurations();
}
createMenuForAction(menu, action, -1);
}
return menu;
}
/**
* Create a separator at the top of the history/favorites area. This method may
* be overridden to prevent a separator from being placed at the top.
*/
protected void createTopSeparator(Menu menu) {
new MenuItem(menu, SWT.SEPARATOR);
}
/**
* @see runWithEvent(IAction, Event)
* @see IActionDelegate#run(IAction)
*/
public void run(IAction action) {
}
/**
* @see IActionDelegateWithEvent#runWithEvent(IAction, Event)
*/
public void runWithEvent(IAction action, Event event) {
getLaunchAction().runWithEvent(action, event);
}
/**
* @see IActionDelegate#selectionChanged(IAction, ISelection)
*/
public void selectionChanged(IAction action, ISelection selection){
if (fActionProxy == null) {
initialize(action);
}
}
/**
* @see IWorkbenchWindowActionDelegate#init(IWorkbenchWindow)
*/
public void init(IWorkbenchWindow window){
}
/**
* Returns an array of previous launches applicable to this drop down.
*/
public abstract LaunchConfigurationHistoryElement[] getHistory();
/**
* Returns an array of favorites applicable to this drop down.
*/
public abstract LaunchConfigurationHistoryElement[] getFavorites();
/**
* Returns the mode (e.g., 'run' or 'debug') of this drop down.
*/
public abstract String getMode();
/**
* Return the String to use as the first part of the tooltip for this action
* when there is no launch history.
*/
protected abstract String getStaticTooltip();
/**
* Return the String to use as the first part of the tooltip for this action
* when there is a launch history.
*/
protected abstract String getLastLaunchPrefix();
protected ExecutionAction getLaunchAction() {
return fLaunchAction;
}
protected void setLaunchAction(ExecutionAction launchAction) {
fLaunchAction = launchAction;
}
/**
* A menu listener that is used to constantly flag the debug
* action set menu as dirty so that any underlying changes to the
* contributions will be shown.
* As well, if there are no history items, disable the action.
*/
protected MenuListener getDebugActionSetMenuListener() {
return new MenuListener() {
public void menuShown(MenuEvent e) {
getActionProxy().setEnabled(getHistory().length > 0 || getFavorites().length > 0);
IWorkbenchWindow window= DebugUIPlugin.getActiveWorkbenchWindow();
if (window instanceof ApplicationWindow) {
ApplicationWindow appWindow= (ApplicationWindow)window;
IMenuManager manager= appWindow.getMenuBarManager();
IContributionItem actionSetItem= manager.findUsingPath("org.eclipse.ui.run"); //$NON-NLS-1$
if (actionSetItem instanceof SubContributionItem) {
IContributionItem item= ((SubContributionItem)actionSetItem).getInnerItem();
if (item instanceof IMenuManager) {
((IMenuManager)item).markDirty();
}
}
}
}
public void menuHidden(MenuEvent e) {
}
};
}
protected Menu getCreatedMenu() {
return fCreatedMenu;
}
protected void setCreatedMenu(Menu createdMenu) {
fCreatedMenu = createdMenu;
}
protected IAction getDebugWithAction() {
if (fDebugWithAction == null) {
fDebugWithAction= new DebugWithConfigurationAction();
} else {
fDebugWithAction.dispose();
}
return fDebugWithAction;
}
protected IAction getRunWithAction() {
if (fRunWithAction == null) {
fRunWithAction= new RunWithConfigurationAction();
} else {
fRunWithAction.dispose();
}
return fRunWithAction;
}
}
|