1 11 package org.eclipse.debug.internal.ui.preferences; 12 13 import java.util.Arrays ; 14 import java.util.Collections ; 15 import java.util.Comparator ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Set ; 20 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.runtime.CoreException; 23 import org.eclipse.core.runtime.IAdaptable; 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.internal.core.LaunchManager; 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.SWTFactory; 33 import org.eclipse.debug.internal.ui.launchConfigurations.AbstractDebugListSelectionDialog; 34 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationComparator; 35 import org.eclipse.debug.ui.IDebugUIConstants; 36 import org.eclipse.jface.dialogs.IDialogConstants; 37 import org.eclipse.jface.viewers.ArrayContentProvider; 38 import org.eclipse.jface.viewers.ISelection; 39 import org.eclipse.jface.viewers.ISelectionChangedListener; 40 import org.eclipse.jface.viewers.IStructuredSelection; 41 import org.eclipse.jface.viewers.SelectionChangedEvent; 42 import org.eclipse.jface.viewers.StructuredSelection; 43 import org.eclipse.jface.viewers.TableViewer; 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.PropertyPage; 56 import org.eclipse.ui.dialogs.SelectionDialog; 57 58 import com.ibm.icu.text.MessageFormat; 59 60 72 public class RunDebugPropertiesPage extends PropertyPage { 73 76 private Set fDeletedConfigurations = new HashSet (); 77 78 81 private Set fOriginalCandidates; 82 83 86 private Set fChangedConfigurations = new HashSet (); 87 88 91 private List fTypeCandidates = null; 92 93 private TableViewer fViewer; 95 private Button fNewButton = null; 96 private Button fEditButton = null; 97 private Button fDuplicateButton = null; 98 private Button fDeleteButton = null; 99 100 103 protected Control createContents(Composite parent) { 104 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IDebugHelpContextIds.RUN_DEBUG_RESOURCE_PROPERTY_PAGE); 105 collectConfigCandidates(getResource()); 106 Composite topComposite = SWTFactory.createComposite(parent, 2, 1, GridData.FILL_BOTH); 107 108 SWTFactory.createWrapLabel(topComposite, DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_0, 2, 300); 109 SWTFactory.createVerticalSpacer(topComposite, 2); 110 SWTFactory.createWrapLabel(topComposite, MessageFormat.format(DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_1, new String []{getResource().getName()}), 2, 300); 111 fViewer = createViewer(topComposite); 112 113 Composite buttonComp = SWTFactory.createComposite(topComposite, 1, 1, GridData.FILL_VERTICAL); 114 GridLayout layout = (GridLayout) buttonComp.getLayout(); 115 layout.marginHeight = 0; 116 fNewButton = SWTFactory.createPushButton(buttonComp, DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_2, null); 117 fNewButton.setToolTipText(DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_3); 118 fNewButton.addSelectionListener(new SelectionListener() { 119 public void widgetDefaultSelected(SelectionEvent e) {} 120 public void widgetSelected(SelectionEvent e) { 121 handleNew(); 122 } 123 }); 124 125 fDuplicateButton = SWTFactory.createPushButton(buttonComp, DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_4, null); 126 fDuplicateButton.setToolTipText(DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_5); 127 fDuplicateButton.setEnabled(false); 128 fDuplicateButton.addSelectionListener(new SelectionListener() { 129 public void widgetDefaultSelected(SelectionEvent e) {} 130 public void widgetSelected(SelectionEvent e) { 131 handleCopy(); 132 } 133 }); 134 fEditButton = SWTFactory.createPushButton(buttonComp, DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_6, null); 135 fEditButton.setToolTipText(DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_7); 136 fEditButton.setEnabled(false); 137 fEditButton.addSelectionListener(new SelectionListener() { 138 public void widgetDefaultSelected(SelectionEvent e) {} 139 public void widgetSelected(SelectionEvent e) { 140 handleEdit(); 141 } 142 }); 143 fDeleteButton = SWTFactory.createPushButton(buttonComp, DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_8, null); 144 fDeleteButton.setToolTipText(DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_9); 145 fDeleteButton.setEnabled(false); 146 fDeleteButton.addSelectionListener(new SelectionListener() { 147 public void widgetDefaultSelected(SelectionEvent e) {} 148 public void widgetSelected(SelectionEvent e) { 149 handleDelete(); 150 } 151 }); 152 153 fViewer.setSelection(new StructuredSelection()); 154 applyDialogFont(topComposite); 155 return topComposite; 156 } 157 158 164 protected TableViewer createViewer(Composite parent){ 165 TableViewer viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER); 166 viewer.setLabelProvider(new DefaultLabelProvider()); 167 viewer.setContentProvider(new ArrayContentProvider()); 168 viewer.setComparator(new LaunchConfigurationComparator()); 169 Table builderTable = viewer.getTable(); 170 GridData tableGridData = new GridData(GridData.FILL_BOTH); 171 tableGridData.heightHint = 300; 172 builderTable.setLayoutData(tableGridData); 173 IResource resource = getResource(); 174 viewer.setInput(collectConfigCandidates(resource)); 175 viewer.addSelectionChangedListener(new ISelectionChangedListener() { 176 public void selectionChanged(SelectionChangedEvent event) { 177 ISelection sel = event.getSelection(); 178 if(sel instanceof IStructuredSelection) { 179 IStructuredSelection ss = (IStructuredSelection) sel; 180 boolean empty = ss.isEmpty(); 181 int size = ss.size(); 182 fEditButton.setEnabled(!empty && size == 1); 183 fDuplicateButton.setEnabled(!empty && size == 1); 184 fDeleteButton.setEnabled(!empty); 185 } 186 } 187 }); 188 return viewer; 189 } 190 191 194 public void dispose() { 195 if(fOriginalCandidates != null) { 196 fOriginalCandidates.clear(); 197 fOriginalCandidates = null; 198 } 199 if(fChangedConfigurations != null) { 200 fChangedConfigurations.clear(); 201 fChangedConfigurations = null; 202 } 203 super.dispose(); 204 } 205 206 211 protected TableViewer getViewer() { 212 return fViewer; 213 } 214 215 219 protected LaunchManager getLaunchManager() { 220 return (LaunchManager) DebugPlugin.getDefault().getLaunchManager(); 221 } 222 223 229 protected List collectTypeCandidates() { 230 if(fTypeCandidates == null) { 231 fTypeCandidates = DebugUIPlugin.getDefault().getLaunchConfigurationManager().getApplicableConfigurationTypes(getResource()); 232 Collections.sort(fTypeCandidates, new Comparator () { 233 public int compare(Object o1, Object o2) { 234 ILaunchConfigurationType t1 = (ILaunchConfigurationType) o1; 235 ILaunchConfigurationType t2 = (ILaunchConfigurationType) o2; 236 return t1.getName().compareTo(t2.getName()); 237 } 238 }); 239 } 240 return fTypeCandidates; 241 } 242 243 250 protected Set collectConfigCandidates(IResource resource) { 251 if(fOriginalCandidates == null) { 252 fOriginalCandidates = new HashSet (); 253 try { 254 List configs = DebugUIPlugin.getDefault().getLaunchConfigurationManager().getApplicableLaunchConfigurations(resource); 255 for(Iterator iter = configs.iterator(); iter.hasNext();) { 256 fOriginalCandidates.add(((ILaunchConfiguration)iter.next()).getWorkingCopy()); 257 } 258 } 259 catch(CoreException ce) {DebugUIPlugin.log(ce);} 260 } 261 return fOriginalCandidates; 262 } 263 264 265 266 271 protected IResource getResource() { 272 Object element = getElement(); 273 IResource resource = null; 274 if (element instanceof IResource) { 275 resource = (IResource) element; 276 } else if (element instanceof IAdaptable) { 277 resource = (IResource) ((IAdaptable)element).getAdapter(IResource.class); 278 } 279 return resource; 280 } 281 282 285 public boolean performOk() { 286 Iterator iter = fDeletedConfigurations.iterator(); 288 while (iter.hasNext()) { 289 ILaunchConfigurationWorkingCopy currentConfig = (ILaunchConfigurationWorkingCopy) iter.next(); 290 try{ 291 if (currentConfig.getOriginal() != null){ 292 currentConfig.getOriginal().delete(); 293 } 294 } catch (CoreException e) { 295 DebugPlugin.logMessage("Problem deleting configuration " + currentConfig.getName(), e); } 297 } 298 iter = fChangedConfigurations.iterator(); 300 while (iter.hasNext()) { 301 ILaunchConfigurationWorkingCopy currentConfig = (ILaunchConfigurationWorkingCopy) iter.next(); 302 try{ 303 currentConfig.doSave(); 304 } catch (CoreException e) { 305 DebugPlugin.logMessage("Problem saving changes to configuration " + currentConfig.getName(), e); } 307 } 308 309 return super.performOk(); 310 } 311 312 315 protected void performDefaults() { 316 setErrorMessage(null); 317 setValid(true); 318 fOriginalCandidates.clear(); 319 fOriginalCandidates = null; 320 getViewer().setInput(collectConfigCandidates(getResource())); 321 fChangedConfigurations.clear(); 322 fDeletedConfigurations.clear(); 323 fViewer.refresh(true, true); 324 super.performDefaults(); 325 } 326 327 331 private Set getConfigurationNames() { 332 Set names = new HashSet (); 333 Iterator iter = fOriginalCandidates.iterator(); 334 while (iter.hasNext()) { 335 names.add(((ILaunchConfiguration)iter.next()).getName()); 336 } 337 iter = fChangedConfigurations.iterator(); 338 while (iter.hasNext()) { 339 names.add(((ILaunchConfiguration)iter.next()).getName()); 340 } 341 return names; 342 } 343 344 349 private ILaunchConfigurationWorkingCopy[] getSelectedConfigurations() { 350 IStructuredSelection ss = (IStructuredSelection) fViewer.getSelection(); 351 return (ILaunchConfigurationWorkingCopy[]) ss.toList().toArray(new ILaunchConfigurationWorkingCopy[ss.size()]); 352 } 353 354 357 private void handleCopy() { 358 ILaunchConfigurationWorkingCopy configuration = getSelectedConfigurations()[0]; 359 try { 360 ILaunchConfigurationWorkingCopy copy = configuration.copy( 361 ((LaunchManager)DebugPlugin.getDefault().getLaunchManager()).generateUniqueLaunchConfigurationNameFrom(configuration.getName(), getConfigurationNames())); 362 copy.setAttributes(configuration.getAttributes()); 363 fChangedConfigurations.add(copy); 364 fViewer.add(copy); 365 fViewer.setSelection(new StructuredSelection(copy)); 366 } catch (CoreException e) { 367 setErrorMessage(e.getMessage()); 368 } 369 } 370 371 374 private void handleDelete() { 375 Table table = fViewer.getTable(); 376 int[] indices = table.getSelectionIndices(); 377 Arrays.sort(indices); 378 ILaunchConfiguration[] configurations = getSelectedConfigurations(); 379 for (int i = 0; i < configurations.length; i++) { 380 fDeletedConfigurations.add(configurations[i]); 381 fChangedConfigurations.remove(configurations[i]); 382 fViewer.remove(configurations[i]); 383 } 384 if (indices[0] < table.getItemCount()) { 385 fViewer.setSelection(new StructuredSelection(table.getItem(indices[0]).getData())); 386 } else if (table.getItemCount() > 0) { 387 fViewer.setSelection(new StructuredSelection(table.getItem(table.getItemCount() - 1).getData())); 388 } 389 } 390 391 394 private void handleEdit() { 395 ILaunchConfigurationWorkingCopy config = getSelectedConfigurations()[0]; 396 if(edit(config, false) == IDialogConstants.OK_ID) { 397 fChangedConfigurations.add(config); 398 fViewer.refresh(config, true, true); 399 } 400 } 401 402 410 private int edit(ILaunchConfigurationWorkingCopy configuration, boolean setDefaults) { 411 return DebugUIPlugin.openLaunchConfigurationPropertiesDialog(getShell(), configuration, IDebugUIConstants.ID_RUN_LAUNCH_GROUP, getConfigurationNames(), null, setDefaults); 412 } 413 414 417 private void handleNew() { 418 419 final List typeCandidates = collectTypeCandidates(); 420 421 SelectionDialog dialog = new AbstractDebugListSelectionDialog(getShell()){ 422 423 426 protected String getDialogSettingsId() { 427 return DebugUIPlugin.getUniqueIdentifier() + ".SELECT_CONFIGURATION_TYPE_DIALOG"; } 429 430 433 protected Object getViewerInput() { 434 return typeCandidates; 435 } 436 437 440 protected String getHelpContextId() { 441 return IDebugHelpContextIds.SELECT_CONFIGURATION_TYPE_DIALOG; 442 } 443 444 447 protected String getViewerLabel() { 448 return DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_12; 449 } 450 451 }; 452 dialog.setTitle(DebugPreferencesMessages.DefaultLaunchConfigurationsPropertiesPage_11); 453 454 if (dialog.open() == Window.OK) { 455 Object [] result = dialog.getResult(); 456 if (result.length == 1) { 457 ILaunchConfigurationType type = (ILaunchConfigurationType) result[0]; 458 try { 459 ILaunchConfigurationWorkingCopy wc = type.newInstance(null, 460 ((LaunchManager)DebugPlugin.getDefault().getLaunchManager()). 461 generateUniqueLaunchConfigurationNameFrom("New_configuration", getConfigurationNames())); if (edit(wc, true) == Window.OK) { 463 fChangedConfigurations.add(wc); 464 fViewer.add(wc); 465 fViewer.setSelection(new StructuredSelection(wc)); 466 } 467 } catch (CoreException e) { 468 setErrorMessage(e.getMessage()); 469 } 470 } 471 } 472 } 473 } 474 | Popular Tags |