|
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.launchConfigurations.properties; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.Arrays; |
| 15 |
import java.util.Collections; |
| 16 |
import java.util.Comparator; |
| 17 |
import java.util.Iterator; |
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import org.eclipse.core.resources.IResource; |
| 21 |
import org.eclipse.core.runtime.CoreException; |
| 22 |
import org.eclipse.core.runtime.IAdaptable; |
| 23 |
import org.eclipse.core.runtime.IPath; |
| 24 |
import org.eclipse.debug.core.DebugPlugin; |
| 25 |
import org.eclipse.debug.core.ILaunchConfiguration; |
| 26 |
import org.eclipse.debug.core.ILaunchConfigurationType; |
| 27 |
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; |
| 28 |
import org.eclipse.debug.core.ILaunchManager; |
| 29 |
import org.eclipse.debug.internal.ui.DebugUIPlugin; |
| 30 |
import org.eclipse.debug.internal.ui.DefaultLabelProvider; |
| 31 |
import org.eclipse.debug.internal.ui.IDebugHelpContextIds; |
| 32 |
import org.eclipse.debug.internal.ui.SWTUtil; |
| 33 |
import org.eclipse.debug.ui.DebugUITools; |
| 34 |
import org.eclipse.debug.ui.ILaunchGroup; |
| 35 |
import org.eclipse.jface.viewers.ArrayContentProvider; |
| 36 |
import org.eclipse.jface.viewers.CheckStateChangedEvent; |
| 37 |
import org.eclipse.jface.viewers.CheckboxTableViewer; |
| 38 |
import org.eclipse.jface.viewers.ICheckStateListener; |
| 39 |
import org.eclipse.jface.viewers.ISelection; |
| 40 |
import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 41 |
import org.eclipse.jface.viewers.IStructuredSelection; |
| 42 |
import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 43 |
import org.eclipse.jface.viewers.StructuredSelection; |
| 44 |
import org.eclipse.jface.window.Window; |
| 45 |
import org.eclipse.swt.SWT; |
| 46 |
import org.eclipse.swt.events.SelectionEvent; |
| 47 |
import org.eclipse.swt.events.SelectionListener; |
| 48 |
import org.eclipse.swt.layout.GridData; |
| 49 |
import org.eclipse.swt.layout.GridLayout; |
| 50 |
import org.eclipse.swt.widgets.Button; |
| 51 |
import org.eclipse.swt.widgets.Composite; |
| 52 |
import org.eclipse.swt.widgets.Control; |
| 53 |
import org.eclipse.swt.widgets.Table; |
| 54 |
import org.eclipse.ui.PlatformUI; |
| 55 |
import org.eclipse.ui.dialogs.ListDialog; |
| 56 |
import org.eclipse.ui.dialogs.PropertyPage; |
| 57 |
|
| 58 |
import com.ibm.icu.text.MessageFormat; |
| 59 |
|
| 60 |
/** |
| 61 |
* Displays execution settings for a resource - associated launch configurations. |
| 62 |
* |
| 63 |
* @since 3.3 |
| 64 |
*/ |
| 65 |
public class ExecutionPropertiesPage extends PropertyPage implements ICheckStateListener, ISelectionChangedListener, SelectionListener { |
| 66 |
|
| 67 |
private CheckboxTableViewer viewer; |
| 68 |
private Button newButton; |
| 69 |
private Button copyButton; |
| 70 |
private Button editButton; |
| 71 |
private Button deleteButton; |
| 72 |
|
| 73 |
/** |
| 74 |
* List of configurations to be deleted |
| 75 |
*/ |
| 76 |
private List deletedConfigurations = new ArrayList(); |
| 77 |
|
| 78 |
/** |
| 79 |
* List of original default candidates for the resource |
| 80 |
*/ |
| 81 |
private List originalCandidates; |
| 82 |
|
| 83 |
/* (non-Javadoc) |
| 84 |
* @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite) |
| 85 |
*/ |
| 86 |
protected Control createContents(Composite parent) { |
| 87 |
// TODO: add help to documentation |
| 88 |
PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IDebugHelpContextIds.EXECUTION_PROPERTY_PAGE); |
| 89 |
|
| 90 |
Composite composite = new Composite(parent, SWT.NONE); |
| 91 |
composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); |
| 92 |
GridLayout layout = new GridLayout(); |
| 93 |
layout.marginHeight = 0; |
| 94 |
layout.marginWidth = 0; |
| 95 |
composite.setLayout(layout); |
| 96 |
|
| 97 |
SWTUtil.createLabel(composite, MessageFormat.format("&Select default launch settings for {0}:", new String[]{getResource().getName()}), 2); |
| 98 |
|
| 99 |
Composite tableAndButtons = new Composite(composite, SWT.NONE); |
| 100 |
tableAndButtons.setLayoutData(new GridData(GridData.FILL_BOTH)); |
| 101 |
layout = new GridLayout(); |
| 102 |
layout.marginHeight = 0; |
| 103 |
layout.marginWidth = 0; |
| 104 |
layout.numColumns = 2; |
| 105 |
tableAndButtons.setLayout(layout); |
| 106 |
|
| 107 |
viewer= CheckboxTableViewer.newCheckList(tableAndButtons, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER); |
| 108 |
viewer.setLabelProvider(new DefaultLabelProvider()); |
| 109 |
viewer.setContentProvider(new ArrayContentProvider()); |
| 110 |
viewer.setComparator(new LaunchConfigurationComparator()); |
| 111 |
viewer.addCheckStateListener(this); |
| 112 |
// Only filter private configurations and external tools to avoid filtering a default |
| 113 |
viewer.addFilter(new PrivateConfigurationFilter()); |
| 114 |
viewer.addFilter(new CategoryFilter()); |
| 115 |
Table builderTable= viewer.getTable(); |
| 116 |
GridData gridData = new GridData(GridData.FILL_BOTH); |
| 117 |
gridData.heightHint = 300; |
| 118 |
builderTable.setLayoutData(gridData); |
| 119 |
|
| 120 |
IResource resource = getResource(); |
| 121 |
originalCandidates = getDefaultCandidates(resource); |
| 122 |
viewer.setInput(originalCandidates); |
| 123 |
try { |
| 124 |
ILaunchConfiguration configuration = DebugPlugin.getDefault().getLaunchManager().getDefaultConfiguration(resource); |
| 125 |
if (configuration != null) { |
| 126 |
Iterator iterator = originalCandidates.iterator(); |
| 127 |
while (iterator.hasNext()) { |
| 128 |
ILaunchConfigurationWorkingCopy wc = (ILaunchConfigurationWorkingCopy) iterator.next(); |
| 129 |
if (configuration.equals(wc.getOriginal())) { |
| 130 |
viewer.setChecked(wc, true); |
| 131 |
break; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
} catch (CoreException e) { |
| 136 |
setErrorMessage(e.getMessage()); |
| 137 |
} |
| 138 |
|
| 139 |
Composite buttonArea = new Composite(tableAndButtons, SWT.NONE); |
| 140 |
layout = new GridLayout(); |
| 141 |
layout.marginHeight = 0; |
| 142 |
layout.marginWidth = 0; |
| 143 |
buttonArea.setLayout(layout); |
| 144 |
buttonArea.setLayoutData(new GridData(GridData.FILL_VERTICAL)); |
| 145 |
newButton = SWTUtil.createPushButton(buttonArea, "New...", null); |
| 146 |
newButton.addSelectionListener(this); |
| 147 |
copyButton = SWTUtil.createPushButton(buttonArea, "Copy...", null); |
| 148 |
copyButton.addSelectionListener(this); |
| 149 |
editButton = SWTUtil.createPushButton(buttonArea, "Edit...", null); |
| 150 |
editButton.addSelectionListener(this); |
| 151 |
deleteButton = SWTUtil.createPushButton(buttonArea, "Delete", null); |
| 152 |
deleteButton.addSelectionListener(this); |
| 153 |
|
| 154 |
viewer.addSelectionChangedListener(this); |
| 155 |
applyDialogFont(composite); |
| 156 |
updateButtons(new StructuredSelection()); |
| 157 |
return composite; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Returns the resource this property page is open on. |
| 162 |
* |
| 163 |
* @return resource |
| 164 |
*/ |
| 165 |
protected IResource getResource() { |
| 166 |
Object element = getElement(); |
| 167 |
IResource resource = null; |
| 168 |
if (element instanceof IResource) { |
| 169 |
resource = (IResource) element; |
| 170 |
} else if (element instanceof IAdaptable) { |
| 171 |
resource = (IResource) ((IAdaptable)element).getAdapter(IResource.class); |
| 172 |
} |
| 173 |
return resource; |
| 174 |
} |
| 175 |
|
| 176 |
/* (non-Javadoc) |
| 177 |
* @see org.eclipse.jface.viewers.ICheckStateListener#checkStateChanged(org.eclipse.jface.viewers.CheckStateChangedEvent) |
| 178 |
*/ |
| 179 |
public void checkStateChanged(CheckStateChangedEvent event) { |
| 180 |
ILaunchConfiguration configuration = (ILaunchConfiguration) event.getElement(); |
| 181 |
// only allow one check |
| 182 |
viewer.setCheckedElements(new Object[]{configuration}); |
| 183 |
// validate |
| 184 |
if (!configuration.isLocal()) { |
| 185 |
if (!getResource().getProject().equals(configuration.getFile().getProject())) { |
| 186 |
// a config must be stored in the same project that it is a default config for |
| 187 |
setErrorMessage(MessageFormat.format("Configuration must be located in project {0}", new String[]{getResource().getProject().getName()})); |
| 188 |
setValid(false); |
| 189 |
return; |
| 190 |
} |
| 191 |
} |
| 192 |
setErrorMessage(null); |
| 193 |
setValid(true); |
| 194 |
} |
| 195 |
|
| 196 |
/* (non-Javadoc) |
| 197 |
* @see org.eclipse.jface.preference.PreferencePage#performOk() |
| 198 |
*/ |
| 199 |
public boolean performOk() { |
| 200 |
Object[] checked = viewer.getCheckedElements(); |
| 201 |
try { |
| 202 |
ILaunchConfiguration def = null; |
| 203 |
if (checked.length == 1) { |
| 204 |
def = (ILaunchConfiguration) checked[0]; |
| 205 |
} |
| 206 |
DebugPlugin.getDefault().getLaunchManager().setDefaultConfiguration(getResource(), def); |
| 207 |
} catch (CoreException e) { |
| 208 |
setErrorMessage(e.getMessage()); |
| 209 |
return false; |
| 210 |
} |
| 211 |
return super.performOk(); |
| 212 |
} |
| 213 |
|
| 214 |
/* (non-Javadoc) |
| 215 |
* @see org.eclipse.jface.preference.PreferencePage#performDefaults() |
| 216 |
*/ |
| 217 |
protected void performDefaults() { |
| 218 |
viewer.setAllChecked(false); |
| 219 |
setErrorMessage(null); |
| 220 |
setValid(true); |
| 221 |
super.performDefaults(); |
| 222 |
} |
| 223 |
|
| 224 |
/* (non-Javadoc) |
| 225 |
* @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent) |
| 226 |
*/ |
| 227 |
public void selectionChanged(SelectionChangedEvent event) { |
| 228 |
ISelection s = event.getSelection(); |
| 229 |
if (s instanceof IStructuredSelection) { |
| 230 |
updateButtons((IStructuredSelection) s); |
| 231 |
} |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Update buttons based on the given selection. |
| 237 |
* |
| 238 |
* @param selection |
| 239 |
*/ |
| 240 |
private void updateButtons(IStructuredSelection selection) { |
| 241 |
if (selection.size() == 0) { |
| 242 |
editButton.setEnabled(false); |
| 243 |
copyButton.setEnabled(false); |
| 244 |
deleteButton.setEnabled(false); |
| 245 |
} else if (selection.size() == 1) { |
| 246 |
editButton.setEnabled(true); |
| 247 |
copyButton.setEnabled(true); |
| 248 |
deleteButton.setEnabled(true); |
| 249 |
} else { |
| 250 |
editButton.setEnabled(false); |
| 251 |
copyButton.setEnabled(false); |
| 252 |
deleteButton.setEnabled(true); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Returns a list of potential default configurations candidates for the given |
| 258 |
* resource. The configurations are working copies. |
| 259 |
* |
| 260 |
* @param resource resource |
| 261 |
* @return list of default candidates |
| 262 |
*/ |
| 263 |
private List getDefaultCandidates(IResource resource) { |
| 264 |
IPath resourcePath = resource.getFullPath(); |
| 265 |
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager(); |
| 266 |
List list = new ArrayList(); |
| 267 |
try { |
| 268 |
ILaunchConfiguration[] configurations = manager.getLaunchConfigurations(); |
| 269 |
for (int i = 0; i < configurations.length; i++) { |
| 270 |
ILaunchConfiguration configuration = configurations[i]; |
| 271 |
IResource[] resources = configuration.getMappedResources(); |
| 272 |
if (resources != null) { |
| 273 |
for (int j = 0; j < resources.length; j++) { |
| 274 |
IResource mappedResource = resources[j]; |
| 275 |
if (resource.equals(mappedResource) || |
| 276 |
resourcePath.isPrefixOf(mappedResource.getFullPath())) { |
| 277 |
list.add(configuration.getWorkingCopy()); |
| 278 |
break; |
| 279 |
} |
| 280 |
} |
| 281 |
} else { |
| 282 |
// 1. similar to launch dialog - if no resource mapping, display the config |
| 283 |
// 2. only consider the default launch category (applications, *not* external tools) |
| 284 |
if (configuration.getType().getCategory() == null) { |
| 285 |
list.add(configuration.getWorkingCopy()); |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
} catch (CoreException e) { |
| 290 |
list.clear(); |
| 291 |
DebugUIPlugin.log(e); |
| 292 |
} |
| 293 |
return list; |
| 294 |
} |
| 295 |
|
| 296 |
/* (non-Javadoc) |
| 297 |
* @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent) |
| 298 |
*/ |
| 299 |
public void widgetDefaultSelected(SelectionEvent e) { |
| 300 |
} |
| 301 |
|
| 302 |
/* (non-Javadoc) |
| 303 |
* @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
| 304 |
*/ |
| 305 |
public void widgetSelected(SelectionEvent e) { |
| 306 |
if (e.widget == newButton) { |
| 307 |
handleNew(); |
| 308 |
} else if (e.widget == copyButton) { |
| 309 |
handleCopy(); |
| 310 |
} else if (e.widget == deleteButton) { |
| 311 |
handleDelete(); |
| 312 |
} else if (e.widget == editButton) { |
| 313 |
handleEdit(); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Returns selected configurations. |
| 319 |
* |
| 320 |
* @return selected configurations |
| 321 |
*/ |
| 322 |
private ILaunchConfigurationWorkingCopy[] getSelectedConfigurations() { |
| 323 |
Object[] array = ((IStructuredSelection)viewer.getSelection()).toArray(); |
| 324 |
ILaunchConfigurationWorkingCopy[] lcs = new ILaunchConfigurationWorkingCopy[array.length]; |
| 325 |
System.arraycopy(array, 0, lcs, 0, array.length); |
| 326 |
return lcs; |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Copy the selection |
| 331 |
*/ |
| 332 |
private void handleCopy() { |
| 333 |
ILaunchConfigurationWorkingCopy configuration = getSelectedConfigurations()[0]; |
| 334 |
try { |
| 335 |
ILaunchConfigurationWorkingCopy copy = configuration.copy( |
| 336 |
DebugPlugin.getDefault().getLaunchManager().generateUniqueLaunchConfigurationNameFrom(configuration.getName())); |
| 337 |
copy.setAttributes(configuration.getAttributes()); |
| 338 |
int code = edit(copy); |
| 339 |
if (code == Window.OK) { |
| 340 |
originalCandidates.add(copy); |
| 341 |
viewer.refresh(); |
| 342 |
viewer.setSelection(new StructuredSelection(copy)); |
| 343 |
} |
| 344 |
} catch (CoreException e) { |
| 345 |
setErrorMessage(e.getMessage()); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Delete the selection |
| 351 |
*/ |
| 352 |
private void handleDelete() { |
| 353 |
Table table = viewer.getTable(); |
| 354 |
int[] indices = table.getSelectionIndices(); |
| 355 |
Arrays.sort(indices); |
| 356 |
ILaunchConfiguration[] configurations = getSelectedConfigurations(); |
| 357 |
for (int i = 0; i < configurations.length; i++) { |
| 358 |
deletedConfigurations.add(configurations[i]); |
| 359 |
originalCandidates.remove(configurations[i]); |
| 360 |
} |
| 361 |
viewer.refresh(); |
| 362 |
if (indices[0] < table.getItemCount()) { |
| 363 |
viewer.setSelection(new StructuredSelection(table.getItem(indices[0]).getData())); |
| 364 |
} else if (table.getItemCount() > 0) { |
| 365 |
viewer.setSelection(new StructuredSelection(table.getItem(table.getItemCount() - 1).getData())); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Edit the selection |
| 371 |
*/ |
| 372 |
private void handleEdit() { |
| 373 |
edit(getSelectedConfigurations()[0]); |
| 374 |
viewer.refresh(); |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Edits the given configuration as a nested working copy. |
| 379 |
* Returns the code from the dialog used to edit the configuration. |
| 380 |
* |
| 381 |
* @param configuration |
| 382 |
* @return dialog return code - OK or CANCEL |
| 383 |
*/ |
| 384 |
private int edit(ILaunchConfigurationWorkingCopy configuration) { |
| 385 |
try { |
| 386 |
ILaunchConfigurationWorkingCopy copy = configuration.getNestedWorkingCopy(); |
| 387 |
ILaunchGroup group = DebugUITools.getLaunchGroup(configuration, ILaunchManager.RUN_MODE); |
| 388 |
if (group == null) { |
| 389 |
group = DebugUITools.getLaunchGroup(configuration, ILaunchManager.DEBUG_MODE); |
| 390 |
} |
| 391 |
int code = DebugUITools.openLaunchConfigurationPropertiesDialog(getShell(), copy, group.getIdentifier()); |
| 392 |
if (code == Window.OK) { |
| 393 |
copy.doSave(); |
| 394 |
} |
| 395 |
return code; |
| 396 |
} catch (CoreException e) { |
| 397 |
setErrorMessage(e.getMessage()); |
| 398 |
} |
| 399 |
return Window.CANCEL; |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Create a new configuration |
| 404 |
*/ |
| 405 |
private void handleNew() { |
| 406 |
ILaunchConfigurationType[] types = DebugPlugin.getDefault().getLaunchManager().getLaunchConfigurationTypes(); |
| 407 |
List avail = new ArrayList(types.length); |
| 408 |
for (int i = 0; i < types.length; i++) { |
| 409 |
ILaunchConfigurationType type = types[i]; |
| 410 |
if (type.getCategory() == null) { |
| 411 |
// TODO: external tools? |
| 412 |
avail.add(type); |
| 413 |
} |
| 414 |
} |
| 415 |
Collections.sort(avail, new Comparator() { |
| 416 |
public int compare(Object o1, Object o2) { |
| 417 |
ILaunchConfigurationType t1 = (ILaunchConfigurationType) o1; |
| 418 |
ILaunchConfigurationType t2 = (ILaunchConfigurationType) o2; |
| 419 |
return t1.getName().compareTo(t2.getName()); |
| 420 |
} |
| 421 |
|
| 422 |
}); |
| 423 |
ListDialog dialog = new ListDialog(getShell()); |
| 424 |
dialog.setTitle("Selection Configuration Type"); |
| 425 |
dialog.setContentProvider(new ArrayContentProvider()); |
| 426 |
dialog.setLabelProvider(new DefaultLabelProvider()); |
| 427 |
dialog.setAddCancelButton(true); |
| 428 |
dialog.setMessage("&Select the kind of configuration to create:"); |
| 429 |
dialog.setInput(avail); |
| 430 |
if (dialog.open() == Window.OK) { |
| 431 |
Object[] result = dialog.getResult(); |
| 432 |
if (result.length == 1) { |
| 433 |
ILaunchConfigurationType type = (ILaunchConfigurationType) result[0]; |
| 434 |
try { |
| 435 |
ILaunchConfigurationWorkingCopy wc = type.newInstance(null, DebugPlugin.getDefault().getLaunchManager(). |
| 436 |
generateUniqueLaunchConfigurationNameFrom("New Configuration")); |
| 437 |
if (edit(wc) == Window.OK) { |
| 438 |
originalCandidates.add(wc); |
| 439 |
viewer.refresh(); |
| 440 |
viewer.setSelection(new StructuredSelection(wc)); |
| 441 |
} |
| 442 |
} catch (CoreException e) { |
| 443 |
setErrorMessage(e.getMessage()); |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
} |