Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 74480 | Differences between
and this patch

Collapse All | Expand All

(-)ui/org/eclipse/debug/ui/actions/AbstractLaunchToolbarAction.java (+113 lines)
Lines 11-23 Link Here
11
package org.eclipse.debug.ui.actions;
11
package org.eclipse.debug.ui.actions;
12
12
13
13
14
import org.eclipse.core.resources.IResource;
14
import org.eclipse.debug.core.ILaunchConfiguration;
15
import org.eclipse.debug.core.ILaunchConfiguration;
15
import org.eclipse.debug.internal.ui.DebugUIPlugin;
16
import org.eclipse.debug.internal.ui.DebugUIPlugin;
17
import org.eclipse.debug.internal.ui.contexts.launch.LaunchContext;
18
import org.eclipse.debug.internal.ui.contexts.launch.LaunchContextManager;
19
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension;
16
import org.eclipse.debug.internal.ui.launchConfigurations.OrganizeFavoritesAction;
20
import org.eclipse.debug.internal.ui.launchConfigurations.OrganizeFavoritesAction;
17
import org.eclipse.debug.ui.DebugUITools;
21
import org.eclipse.debug.ui.DebugUITools;
22
import org.eclipse.debug.ui.ILaunchShortcut;
18
import org.eclipse.jface.action.IAction;
23
import org.eclipse.jface.action.IAction;
24
import org.eclipse.jface.viewers.ArrayContentProvider;
25
import org.eclipse.jface.viewers.BaseLabelProvider;
26
import org.eclipse.jface.viewers.ILabelProvider;
27
import org.eclipse.jface.viewers.ISelection;
28
import org.eclipse.jface.viewers.IStructuredSelection;
19
import org.eclipse.jface.viewers.StructuredSelection;
29
import org.eclipse.jface.viewers.StructuredSelection;
30
import org.eclipse.jface.window.Window;
31
import org.eclipse.swt.graphics.Image;
20
import org.eclipse.swt.widgets.Menu;
32
import org.eclipse.swt.widgets.Menu;
33
import org.eclipse.ui.IEditorPart;
34
import org.eclipse.ui.IWorkbenchPage;
35
import org.eclipse.ui.IWorkbenchWindow;
36
import org.eclipse.ui.dialogs.ListSelectionDialog;
21
37
22
/**
38
/**
23
 * A launch history action that also includes launch shortcut actions (run/debug
39
 * A launch history action that also includes launch shortcut actions (run/debug
Lines 29-34 Link Here
29
 */
45
 */
30
public class AbstractLaunchToolbarAction extends AbstractLaunchHistoryAction {
46
public class AbstractLaunchToolbarAction extends AbstractLaunchHistoryAction {
31
47
48
	private IWorkbenchWindow fWindow = null;
49
	
50
	private class LabelProvider extends BaseLabelProvider implements ILabelProvider {
51
52
		/* (non-Javadoc)
53
		 * @see org.eclipse.jface.viewers.ILabelProvider#getImage(java.lang.Object)
54
		 */
55
		public Image getImage(Object element) {
56
			// TODO Auto-generated method stub
57
			return null;
58
		}
59
60
		/* (non-Javadoc)
61
		 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
62
		 */
63
		public String getText(Object element) {
64
			return ((LaunchShortcutExtension)element).getLabel();
65
		}
66
		
67
	}
32
68
33
	/**
69
	/**
34
	 * Constructs a launch toolbar action.
70
	 * Constructs a launch toolbar action.
Lines 41-46 Link Here
41
		super(launchGroupIdentifier);
77
		super(launchGroupIdentifier);
42
	}
78
	}
43
79
80
	/* (non-Javadoc)
81
	 * @see org.eclipse.debug.ui.actions.AbstractLaunchHistoryAction#dispose()
82
	 */
83
	public void dispose() {
84
		super.dispose();
85
		fWindow = null;
86
	}
87
88
	/* (non-Javadoc)
89
	 * @see org.eclipse.debug.ui.actions.AbstractLaunchHistoryAction#init(org.eclipse.ui.IWorkbenchWindow)
90
	 */
91
	public void init(IWorkbenchWindow window) {
92
		super.init(window);
93
		fWindow = window;
94
	}
95
44
	/**
96
	/**
45
	 * Fills the drop-down menu with favorites and launch history,
97
	 * Fills the drop-down menu with favorites and launch history,
46
	 * launch shortcuts, and an action to open the launch configuration dialog.
98
	 * launch shortcuts, and an action to open the launch configuration dialog.
Lines 74-79 Link Here
74
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
126
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
75
	 */
127
	 */
76
	public void run(IAction action) {
128
	public void run(IAction action) {
129
		IWorkbenchPage page = fWindow.getActivePage();
130
		if (page != null) {
131
			IStructuredSelection selection = null;
132
			IEditorPart editor = null;
133
			if (page.getActivePart() == page.getActiveEditor()) {
134
				editor = page.getActiveEditor();
135
				if (editor != null) {
136
					selection = new StructuredSelection(editor.getEditorInput());
137
				}
138
			} else {
139
				ISelection sel = page.getSelection();
140
				if (sel instanceof IStructuredSelection) {
141
					selection = (IStructuredSelection) sel;
142
				}
143
			}
144
			if (selection != null) {
145
				LaunchContext context = new LaunchContext(selection, getMode());
146
				LaunchContextManager manager = LaunchContextManager.getDefault();
147
				ILaunchShortcut shortcut = manager.getCachedShortcut(context);
148
				if (shortcut == null) {
149
					ILaunchShortcut[] shortcuts = context.getApplicableShortcuts();
150
					if (shortcuts.length == 1) {
151
						shortcut = shortcuts[0];
152
					} else {
153
						if (shortcuts.length == 0) {
154
							IResource resource = context.getResource();
155
							if (resource != null) {
156
								context = new LaunchContext(new StructuredSelection(resource.getProject()), getMode());
157
								shortcut = manager.getCachedShortcut(context);
158
								if (shortcut == null) {
159
									shortcuts = context.getApplicableShortcuts();
160
								}
161
							}
162
						}
163
						if (shortcuts.length > 0) {
164
							ListSelectionDialog dialog = new ListSelectionDialog(fWindow.getShell(), shortcuts, new ArrayContentProvider(),
165
									new LabelProvider(), "What do you want to launch today?");
166
							int open = dialog.open();
167
							if (open == Window.OK) {
168
								Object[] result = dialog.getResult();
169
								if (result.length == 1) {
170
									shortcut = (ILaunchShortcut) result[0];
171
									manager.cacheShortcut(context, shortcut);
172
								}
173
							} else {
174
								return;
175
							}
176
						}
177
					}
178
				}
179
				if (shortcut != null) {
180
					if (editor != null) {
181
						shortcut.launch(editor, getMode());
182
					} else {
183
						shortcut.launch(selection, getMode());
184
					}
185
					return;
186
				}
187
			}
188
		}
189
		
77
		ILaunchConfiguration configuration = getLastLaunch();
190
		ILaunchConfiguration configuration = getLastLaunch();
78
		if (configuration == null) {
191
		if (configuration == null) {
79
			DebugUITools.openLaunchConfigurationDialogOnGroup(DebugUIPlugin.getShell(), new StructuredSelection(), getLaunchGroupIdentifier());
192
			DebugUITools.openLaunchConfigurationDialogOnGroup(DebugUIPlugin.getShell(), new StructuredSelection(), getLaunchGroupIdentifier());
(-)ui/org/eclipse/debug/internal/ui/actions/LaunchShortcutAction.java (+10 lines)
Lines 18-27 Link Here
18
import org.eclipse.core.expressions.IEvaluationContext;
18
import org.eclipse.core.expressions.IEvaluationContext;
19
import org.eclipse.core.runtime.CoreException;
19
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.debug.internal.ui.DebugUIPlugin;
20
import org.eclipse.debug.internal.ui.DebugUIPlugin;
21
import org.eclipse.debug.internal.ui.contexts.launch.LaunchContext;
22
import org.eclipse.debug.internal.ui.contexts.launch.LaunchContextManager;
21
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension;
23
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension;
22
import org.eclipse.jface.action.Action;
24
import org.eclipse.jface.action.Action;
23
import org.eclipse.jface.viewers.ISelection;
25
import org.eclipse.jface.viewers.ISelection;
24
import org.eclipse.jface.viewers.IStructuredSelection;
26
import org.eclipse.jface.viewers.IStructuredSelection;
27
import org.eclipse.jface.viewers.StructuredSelection;
25
import org.eclipse.ui.IEditorPart;
28
import org.eclipse.ui.IEditorPart;
26
import org.eclipse.ui.IWorkbenchPage;
29
import org.eclipse.ui.IWorkbenchPage;
27
import org.eclipse.ui.IWorkbenchWindow;
30
import org.eclipse.ui.IWorkbenchWindow;
Lines 57-73 Link Here
57
		if (wb != null) {
60
		if (wb != null) {
58
			IWorkbenchPage page = wb.getActivePage();
61
			IWorkbenchPage page = wb.getActivePage();
59
			if (page != null) {
62
			if (page != null) {
63
				IStructuredSelection contextSelection = null;
60
				if (page.getActivePart() == page.getActiveEditor()) {
64
				if (page.getActivePart() == page.getActiveEditor()) {
61
					IEditorPart editor = page.getActiveEditor();
65
					IEditorPart editor = page.getActiveEditor();
62
					if (editor != null) {
66
					if (editor != null) {
67
						contextSelection = new StructuredSelection(editor.getEditorInput());
63
						fShortcut.launch(editor, fMode);
68
						fShortcut.launch(editor, fMode);
64
					}
69
					}
65
				} else {
70
				} else {
66
					ISelection selection = page.getSelection();
71
					ISelection selection = page.getSelection();
67
					if (selection instanceof IStructuredSelection) {
72
					if (selection instanceof IStructuredSelection) {
73
						contextSelection = (IStructuredSelection) selection;
68
						fShortcut.launch(selection, fMode);
74
						fShortcut.launch(selection, fMode);
69
					}
75
					}
70
				}
76
				}
77
				if (contextSelection != null) {
78
					LaunchContext context = new LaunchContext(contextSelection, fMode);
79
					LaunchContextManager.getDefault().cacheShortcut(context, fShortcut);
80
				}
71
			}
81
			}
72
		}
82
		}
73
	}
83
	}
(-)ui/org/eclipse/debug/internal/ui/contexts/launch/LaunchContextManager.java (+59 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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.debug.internal.ui.contexts.launch;
12
13
import java.util.HashMap;
14
import java.util.Map;
15
16
import org.eclipse.debug.ui.ILaunchShortcut;
17
18
/**
19
 * @since 3.3
20
 */
21
public class LaunchContextManager {
22
	
23
	private static LaunchContextManager fgDefault = null;
24
	
25
	private Map fMap = new HashMap();
26
	
27
	public static LaunchContextManager getDefault() {
28
		if (fgDefault == null) {
29
			fgDefault = new LaunchContextManager();
30
		}
31
		return fgDefault;
32
	}
33
	
34
	/**
35
	 * Caches the shortcut to use for the given context.
36
	 * 
37
	 * @param context
38
	 * @param shortcut
39
	 */
40
	public void cacheShortcut(LaunchContext context, ILaunchShortcut shortcut) {
41
		fMap.put(context.getContextKey(), shortcut);
42
		fMap.put(context.getResourceContextKey(), shortcut);
43
	}
44
	
45
	/**
46
	 * Returns the cached shortcut for the given context, or <code>null</code>.
47
	 * 
48
	 * @param context
49
	 * @return shortcut or <code>null</code>.
50
	 */
51
	public ILaunchShortcut getCachedShortcut(LaunchContext context) {
52
		ILaunchShortcut shortcut = (ILaunchShortcut) fMap.get(context.getResourceContextKey());
53
		if (shortcut == null) {
54
			shortcut = (ILaunchShortcut) fMap.get(context.getContextKey());
55
		}
56
		return shortcut;
57
	}
58
59
}
(-)ui/org/eclipse/debug/internal/ui/contexts/launch/LaunchContext.java (+261 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 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.debug.internal.ui.contexts.launch;
12
13
import java.util.ArrayList;
14
import java.util.HashSet;
15
import java.util.Iterator;
16
import java.util.List;
17
import java.util.Set;
18
19
import org.eclipse.core.expressions.EvaluationContext;
20
import org.eclipse.core.expressions.Expression;
21
import org.eclipse.core.expressions.IEvaluationContext;
22
import org.eclipse.core.resources.IFile;
23
import org.eclipse.core.resources.IProject;
24
import org.eclipse.core.resources.IResource;
25
import org.eclipse.core.runtime.CoreException;
26
import org.eclipse.core.runtime.IAdaptable;
27
import org.eclipse.debug.core.DebugPlugin;
28
import org.eclipse.debug.core.ILaunchConfiguration;
29
import org.eclipse.debug.internal.ui.DebugUIPlugin;
30
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationManager;
31
import org.eclipse.debug.internal.ui.launchConfigurations.LaunchShortcutExtension;
32
import org.eclipse.debug.ui.ILaunchShortcut;
33
import org.eclipse.jface.viewers.IStructuredSelection;
34
import org.eclipse.ui.IFileEditorInput;
35
import org.eclipse.ui.activities.WorkbenchActivityHelper;
36
37
/**
38
 * @since 3.3
39
 */
40
public class LaunchContext {
41
	
42
	private IStructuredSelection fSelection;
43
	private String fMode;
44
	private Set fNatures;
45
	private IResource fResource;
46
	
47
	private ILaunchShortcut[] fShortcuts;
48
	
49
	/**
50
	 * Constructs a launch context for the given selection.
51
	 * 
52
	 * @param selection
53
	 */
54
	public LaunchContext(IStructuredSelection selection, String mode) {
55
		fSelection = selection;
56
		fMode = mode;
57
	}
58
	
59
	/**
60
	 * Returns a collection of project nature id's associated with this context's project
61
	 * or <code>null</code> if none.
62
	 * 
63
	 * @return project natures or <code>null</code>
64
	 */
65
	public Set getProjectNatureIds() {
66
		if (fNatures == null) {
67
			fNatures = new HashSet();
68
			IResource resource = getResource();
69
			if (resource != null) {
70
				IProject project = resource.getProject();
71
				try {
72
					String[] natureIds = project.getDescription().getNatureIds();
73
					for (int i = 0; i < natureIds.length; i++) {
74
						fNatures.add(natureIds[i]);
75
					}
76
				} catch (CoreException e) {
77
				}
78
			}
79
		}
80
		return fNatures;
81
	}
82
	
83
	/**
84
	 * Return's this context's associated resource or <code>null</code>.
85
	 * 
86
	 * @return resource or <code>null</code>
87
	 */
88
	public IResource getResource() {
89
		if (fResource == null) {
90
			Object element = fSelection.getFirstElement();
91
			if (element != null) {
92
				if (element instanceof IResource) {
93
					fResource = (IResource) element;
94
				} else if (element instanceof IAdaptable) {
95
					fResource = (IResource) ((IAdaptable)element).getAdapter(IResource.class);
96
				}
97
			}
98
		}
99
		return fResource;		
100
	}
101
	
102
	/**
103
	 * Returns launch shortcuts applicable to this context, possibly an empty collection.
104
	 * 
105
	 * @param mode launch mode
106
	 * @return launch shortcuts
107
	 */
108
	public ILaunchShortcut[] getApplicableShortcuts() {
109
		if (fShortcuts == null) {
110
			IEvaluationContext context = createContext();
111
			
112
			//add in any selected shared config's before the rest of the items to launch as
113
			//feature fix for 
114
			if(!fSelection.isEmpty()) {
115
				Object obj = fSelection.getFirstElement();
116
				if(isSharedConfig(obj)) {
117
					// TODO: just run the config
118
				} 
119
				else if(isSharedConfigEditorInput(obj)) {
120
					// TODO: just run the config
121
				}
122
			}
123
			
124
			// gather all shortcuts and run their filters so that we only run the
125
			// filters one time for each shortcut. Running filters can be expensive.
126
			// Also, only *LOADED* plug-ins get their filters run.
127
			List /* <LaunchShortcutExtension> */ allShortCuts = getLaunchConfigurationManager().getLaunchShortcuts();
128
			Iterator iter = allShortCuts.iterator();
129
			List filteredShortCuts = new ArrayList(10);
130
			while (iter.hasNext()) {
131
				LaunchShortcutExtension ext = (LaunchShortcutExtension) iter.next();
132
				if (ext.getModes().contains(fMode)) {
133
					try {
134
						if (!WorkbenchActivityHelper.filterItem(ext) && isApplicable(ext, context)) {
135
							filteredShortCuts.add(ext);
136
						}
137
					} catch (CoreException e) {
138
						// not supported
139
					}
140
				}
141
			}
142
			fShortcuts = (ILaunchShortcut[]) filteredShortCuts.toArray(new ILaunchShortcut[filteredShortCuts.size()]);
143
		}
144
		return fShortcuts;
145
	}
146
	
147
	/**
148
	 * Returns the launch configuration manager.
149
	 *
150
	 * @return launch configuration manager
151
	 */
152
	private LaunchConfigurationManager getLaunchConfigurationManager() {
153
		return DebugUIPlugin.getDefault().getLaunchConfigurationManager();
154
	}	
155
	
156
	/**
157
	 * Returns a key used to this launch context that does *not* include the specific
158
	 * resource to be launched. The key considers associated project natures
159
	 * and applicable launch shortcuts.
160
	 * 
161
	 * @return key
162
	 */
163
	public Object getContextKey() {
164
		Set key = new HashSet();
165
		key.addAll(getProjectNatureIds());
166
		ILaunchShortcut[] applicableShortcuts = getApplicableShortcuts();
167
		for (int i = 0; i < applicableShortcuts.length; i++) {
168
			key.add(((LaunchShortcutExtension)applicableShortcuts[i]).getId());
169
		}
170
		return key;
171
	}
172
	
173
	/**
174
	 * Returns a key used to this launch context that *does* include the specific
175
	 * resource to be launched.
176
	 * 
177
	 * @return key
178
	 */
179
	public Object getResourceContextKey() {
180
		Set key = new HashSet();
181
		key.addAll((Set)getContextKey());
182
		IResource resource = getResource();
183
		if (resource != null) {
184
			key.add(resource.getFullPath().toPortableString());
185
		}
186
		return key;
187
	}
188
189
	/**
190
	 * @return an Evaluation context with default variable = selection
191
	 */
192
	private IEvaluationContext createContext() {
193
		// create a default evaluation context with default variable of the user selection
194
		List selection = getSelectedElements();
195
		IEvaluationContext context = new EvaluationContext(null, selection);
196
		context.setAllowPluginActivation(true);
197
		context.addVariable("selection", selection); //$NON-NLS-1$
198
		
199
		return context;
200
	}
201
	
202
	/**
203
	 * @return current selection as a List.
204
	 */
205
	private List getSelectedElements() {
206
		ArrayList result = new ArrayList();
207
		Iterator iter = fSelection.iterator();
208
		while (iter.hasNext()) {
209
			result.add(iter.next());
210
		}
211
		return result;
212
	}	
213
	
214
	/**
215
	 * This method is used to determine if the selected object is in fact a shared launch
216
	 * configuration that can be launched
217
	 * @return true if the item is a shared config , false otherwise
218
	 * @since 3.3
219
	 */
220
	private boolean isSharedConfig(Object receiver) {
221
		if(receiver instanceof IFile) {
222
			IFile file = (IFile) receiver;
223
			String ext = file.getFileExtension();
224
			if(ext == null) {
225
				return false;
226
			}
227
			if(ext.equals("launch")) { //$NON-NLS-1$
228
				ILaunchConfiguration config = DebugPlugin.getDefault().getLaunchManager().getLaunchConfiguration(file);
229
				if(config != null && config.exists()) {
230
					return true;
231
				}
232
			}
233
		}
234
		return false;
235
	}
236
	
237
	/**
238
	 * This method return if the editor input is from a shared java launch configuration file or not
239
	 * @param receiver the editor input to examine
240
	 * @return true if the editor input is from a shared launch configuration file, false otherwise.
241
	 */
242
	private boolean isSharedConfigEditorInput(Object receiver) {
243
		if(receiver instanceof IFileEditorInput) {
244
			IFileEditorInput input = (IFileEditorInput) receiver;
245
			return isSharedConfig(input.getFile());
246
		}
247
		return false;
248
	}	
249
	
250
	/**
251
	 * Evaluate the enabled logic in the contextualLaunch
252
	 * element description. A true result means that we should
253
	 * include this shortcut in the context menu.
254
	 * @return true iff shortcut should appear in context menu
255
	 */
256
	private boolean isApplicable(LaunchShortcutExtension ext, IEvaluationContext context) throws CoreException {
257
		Expression expr = ext.getContextualLaunchEnablementExpression();
258
		return ext.evalEnablementExpression(context, expr);
259
	}	
260
	
261
}

Return to bug 74480