|
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 |
} |