1 11 package org.eclipse.debug.ui; 12 13 14 import java.io.IOException ; 15 import java.io.StringReader ; 16 import java.io.StringWriter ; 17 import com.ibm.icu.text.MessageFormat; 18 import org.eclipse.core.resources.IResource; 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IAdaptable; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.MultiStatus; 25 import org.eclipse.core.runtime.NullProgressMonitor; 26 import org.eclipse.core.runtime.Path; 27 import org.eclipse.core.runtime.Status; 28 import org.eclipse.debug.core.DebugPlugin; 29 import org.eclipse.debug.core.ILaunchConfiguration; 30 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 31 import org.eclipse.debug.internal.ui.DebugPluginImages; 32 import org.eclipse.debug.internal.ui.DebugUIPlugin; 33 import org.eclipse.debug.internal.ui.IDebugHelpContextIds; 34 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants; 35 import org.eclipse.debug.internal.ui.launchConfigurations.LaunchConfigurationsDialog; 36 import org.eclipse.debug.internal.ui.stringsubstitution.SelectedResourceManager; 37 import org.eclipse.debug.internal.ui.stringsubstitution.StringSubstitutionMessages; 38 import org.eclipse.jface.window.Window; 39 import org.eclipse.jface.wizard.WizardDialog; 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.events.SelectionAdapter; 42 import org.eclipse.swt.events.SelectionEvent; 43 import org.eclipse.swt.graphics.Image; 44 import org.eclipse.swt.layout.GridData; 45 import org.eclipse.swt.layout.GridLayout; 46 import org.eclipse.swt.widgets.Button; 47 import org.eclipse.swt.widgets.Composite; 48 import org.eclipse.swt.widgets.Group; 49 import org.eclipse.ui.IWorkingSet; 50 import org.eclipse.ui.IWorkingSetManager; 51 import org.eclipse.ui.PlatformUI; 52 import org.eclipse.ui.WorkbenchException; 53 import org.eclipse.ui.XMLMemento; 54 import org.eclipse.ui.dialogs.IWorkingSetEditWizard; 55 56 66 public class RefreshTab extends AbstractLaunchConfigurationTab { 67 68 72 public static final String ATTR_REFRESH_RECURSIVE = DebugPlugin.getUniqueIdentifier() + ".ATTR_REFRESH_RECURSIVE"; 74 79 public static final String ATTR_REFRESH_SCOPE = DebugPlugin.getUniqueIdentifier() + ".ATTR_REFRESH_SCOPE"; 81 private static final String NO_WORKING_SET = "NONE"; 84 private Button fRefreshButton; 86 private Button fRecursiveButton; 87 88 private Group fGroup; 90 91 private Button fContainerButton; 93 private Button fProjectButton; 94 private Button fResourceButton; 95 private Button fWorkingSetButton; 96 private Button fWorkspaceButton; 97 98 private Button fSelectButton; 100 101 private IWorkingSet fWorkingSet; 103 104 107 private static final String TAG_LAUNCH_CONFIGURATION_WORKING_SET= "launchConfigurationWorkingSet"; 109 113 private static final String TAG_FACTORY_ID = "factoryID"; 115 118 public void createControl(Composite parent) { 119 Composite mainComposite = new Composite(parent, SWT.NONE); 120 setControl(mainComposite); 121 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IDebugHelpContextIds.LAUNCH_CONFIGURATION_DIALOG_REFRESH_TAB); 122 123 GridLayout layout = new GridLayout(); 124 GridData gd = new GridData(GridData.FILL_HORIZONTAL); 125 mainComposite.setLayout(layout); 126 mainComposite.setLayoutData(gd); 127 mainComposite.setFont(parent.getFont()); 128 129 fRefreshButton = createCheckButton(mainComposite, StringSubstitutionMessages.RefreshTab_31); 130 fRefreshButton.addSelectionListener(new SelectionAdapter() { 131 public void widgetSelected(SelectionEvent e) { 132 updateEnabledState(); 133 updateLaunchConfigurationDialog(); 134 } 135 }); 136 137 fGroup = new Group(mainComposite, SWT.NONE); 138 fGroup.setFont(mainComposite.getFont()); 139 layout = new GridLayout(); 140 layout.numColumns = 2; 141 layout.makeColumnsEqualWidth = false; 142 fGroup.setLayout(layout); 143 gd = new GridData(GridData.FILL_HORIZONTAL); 144 gd.horizontalSpan = 2; 145 fGroup.setLayoutData(gd); 146 147 SelectionAdapter adapter = new SelectionAdapter() { 148 public void widgetSelected(SelectionEvent e) { 149 if (((Button)e.getSource()).getSelection()) { 150 updateEnabledState(); 151 updateLaunchConfigurationDialog(); 152 } 153 } 154 }; 155 156 fWorkspaceButton = createRadioButton(fGroup, StringSubstitutionMessages.RefreshTab_32); 157 gd = new GridData(GridData.FILL_HORIZONTAL); 158 gd.horizontalSpan = 2; 159 fWorkspaceButton.setLayoutData(gd); 160 fWorkspaceButton.addSelectionListener(adapter); 161 162 fResourceButton = createRadioButton(fGroup, StringSubstitutionMessages.RefreshTab_33); 163 gd = new GridData(GridData.FILL_HORIZONTAL); 164 gd.horizontalSpan = 2; 165 fResourceButton.setLayoutData(gd); 166 fResourceButton.addSelectionListener(adapter); 167 168 fProjectButton = createRadioButton(fGroup, StringSubstitutionMessages.RefreshTab_34); 169 gd = new GridData(GridData.FILL_HORIZONTAL); 170 gd.horizontalSpan = 2; 171 fProjectButton.setLayoutData(gd); 172 fProjectButton.addSelectionListener(adapter); 173 174 fContainerButton = createRadioButton(fGroup, StringSubstitutionMessages.RefreshTab_35); 175 gd = new GridData(GridData.FILL_HORIZONTAL); 176 gd.horizontalSpan = 2; 177 fContainerButton.setLayoutData(gd); 178 fContainerButton.addSelectionListener(adapter); 179 180 fWorkingSetButton = createRadioButton(fGroup, StringSubstitutionMessages.RefreshTab_36); 181 gd = new GridData(GridData.FILL_HORIZONTAL); 182 gd.horizontalSpan = 1; 183 fWorkingSetButton.setLayoutData(gd); 184 fWorkingSetButton.addSelectionListener(adapter); 185 186 fSelectButton = createPushButton(fGroup, StringSubstitutionMessages.RefreshTab_37, null); 187 gd = (GridData)fSelectButton.getLayoutData(); 188 gd.horizontalAlignment = GridData.HORIZONTAL_ALIGN_END; 189 fSelectButton.addSelectionListener(new SelectionAdapter() { 190 public void widgetSelected(SelectionEvent e) { 191 selectResources(); 192 } 193 }); 194 195 createVerticalSpacer(fGroup, 2); 196 createRecursiveComponent(fGroup); 197 } 198 199 202 private void selectResources() { 203 IWorkingSetManager workingSetManager = PlatformUI.getWorkbench().getWorkingSetManager(); 204 205 if (fWorkingSet == null){ 206 fWorkingSet = workingSetManager.createWorkingSet(StringSubstitutionMessages.RefreshTab_40, new IAdaptable[0]); 207 } 208 IWorkingSetEditWizard wizard = workingSetManager.createWorkingSetEditWizard(fWorkingSet); 209 WizardDialog dialog = new WizardDialog(((LaunchConfigurationsDialog)LaunchConfigurationsDialog.getCurrentlyVisibleLaunchConfigurationDialog()).getShell(), wizard); 210 dialog.create(); 211 212 if (dialog.open() == Window.CANCEL) { 213 return; 214 } 215 fWorkingSet = wizard.getSelection(); 216 updateLaunchConfigurationDialog(); 217 } 218 219 225 private void createRecursiveComponent(Composite parent) { 226 fRecursiveButton = createCheckButton(parent, StringSubstitutionMessages.RefreshTab_0); 227 GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 228 data.horizontalSpan = 2; 229 fRecursiveButton.setLayoutData(data); 230 fRecursiveButton.addSelectionListener(new SelectionAdapter() { 231 public void widgetSelected(SelectionEvent e) { 232 updateLaunchConfigurationDialog(); 233 } 234 }); 235 } 236 237 240 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) { 241 } 242 243 246 public void initializeFrom(ILaunchConfiguration configuration) { 247 updateRefresh(configuration); 248 updateRecursive(configuration); 249 updateScope(configuration); 250 updateEnabledState(); 251 } 252 253 256 private void updateScope(ILaunchConfiguration configuration) { 257 String scope = null; 258 try { 259 scope= configuration.getAttribute(ATTR_REFRESH_SCOPE, (String )null); 260 } catch (CoreException ce) { 261 DebugUIPlugin.log(DebugUIPlugin.newErrorStatus("Exception reading launch configuration", ce)); } 263 fWorkspaceButton.setSelection(false); 264 fResourceButton.setSelection(false); 265 fContainerButton.setSelection(false); 266 fProjectButton.setSelection(false); 267 fWorkingSetButton.setSelection(false); 268 if (scope == null) { 269 fWorkspaceButton.setSelection(true); 271 } else { 272 if (scope.equals("${workspace}")) { fWorkspaceButton.setSelection(true); 274 } else if (scope.equals("${resource}")) { fResourceButton.setSelection(true); 276 } else if (scope.equals("${container}")) { fContainerButton.setSelection(true); 278 } else if (scope.equals("${project}")) { fProjectButton.setSelection(true); 280 } else if (scope.startsWith("${resource:")) { fWorkingSetButton.setSelection(true); 282 try { 283 IResource[] resources = getRefreshResources(scope); 284 IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager(); 285 fWorkingSet = workingSetManager.createWorkingSet(StringSubstitutionMessages.RefreshTab_40, resources); 286 } catch (CoreException e) { 287 fWorkingSet = null; 288 } 289 } else if (scope.startsWith("${working_set:")) { fWorkingSetButton.setSelection(true); 291 String memento = scope.substring(14, scope.length() - 1); 292 fWorkingSet = restoreWorkingSet(memento); 293 } 294 } 295 } 296 300 private void updateRecursive(ILaunchConfiguration configuration) { 301 boolean recursive= true; 302 try { 303 recursive= configuration.getAttribute(ATTR_REFRESH_RECURSIVE, true); 304 } catch (CoreException ce) { 305 DebugUIPlugin.log(DebugUIPlugin.newErrorStatus("Exception reading launch configuration", ce)); } 307 fRecursiveButton.setSelection(recursive); 308 } 309 313 private void updateRefresh(ILaunchConfiguration configuration) { 314 String scope= null; 315 try { 316 scope= configuration.getAttribute(ATTR_REFRESH_SCOPE, (String )null); 317 } catch (CoreException ce) { 318 DebugUIPlugin.log(DebugUIPlugin.newErrorStatus("Exception reading launch configuration", ce)); } 320 fRefreshButton.setSelection(scope != null); 321 } 322 323 326 public void performApply(ILaunchConfigurationWorkingCopy configuration) { 327 if (fRefreshButton.getSelection()) { 328 String scope = generateScopeMemento(); 329 configuration.setAttribute(ATTR_REFRESH_SCOPE, scope); 330 setAttribute(ATTR_REFRESH_RECURSIVE, configuration, fRecursiveButton.getSelection(), true); 331 } else { 332 configuration.setAttribute(ATTR_REFRESH_SCOPE, (String )null); 334 setAttribute(ATTR_REFRESH_RECURSIVE, configuration, true, true); 335 } 336 } 337 338 344 private String generateScopeMemento() { 345 if (fWorkspaceButton.getSelection()) { 346 return "${workspace}"; } 348 if (fResourceButton.getSelection()) { 349 return "${resource}"; } 351 if (fContainerButton.getSelection()) { 352 return "${container}"; } 354 if (fProjectButton.getSelection()) { 355 return "${project}"; } 357 if (fWorkingSetButton.getSelection()) { 358 return getRefreshAttribute(fWorkingSet); 359 } 360 return null; 361 } 362 363 366 public String getName() { 367 return StringSubstitutionMessages.RefreshTab_6; 368 } 369 370 373 private void updateEnabledState() { 374 boolean enabled= fRefreshButton.getSelection(); 375 fRecursiveButton.setEnabled(enabled); 376 fGroup.setEnabled(enabled); 377 fWorkspaceButton.setEnabled(enabled); 378 fResourceButton.setEnabled(enabled); 379 fContainerButton.setEnabled(enabled); 380 fProjectButton.setEnabled(enabled); 381 fWorkingSetButton.setEnabled(enabled); 382 fSelectButton.setEnabled(enabled && fWorkingSetButton.getSelection()); 383 if (!enabled) { 384 super.setErrorMessage(null); 385 } 386 } 387 388 391 public Image getImage() { 392 return DebugPluginImages.getImage(IInternalDebugUIConstants.IMG_OBJS_REFRESH_TAB); 393 } 394 395 public boolean isValid(ILaunchConfiguration launchConfig) { 396 setErrorMessage(null); 397 setMessage(null); 398 if (fRefreshButton.getSelection() && (fWorkingSetButton.getSelection() && (fWorkingSet == null || fWorkingSet.getElements().length == 0))) { 399 setErrorMessage(StringSubstitutionMessages.RefreshTab_42); 400 return false; 401 } 402 return true; 403 } 404 405 412 public static void refreshResources(ILaunchConfiguration configuration, IProgressMonitor monitor) throws CoreException { 413 if (monitor == null) { 414 monitor = new NullProgressMonitor(); 415 } 416 String scope = getRefreshScope(configuration); 417 IResource[] resources= null; 418 if (scope != null) { 419 resources = getRefreshResources(scope); 420 } 421 if (resources == null || resources.length == 0){ 422 return; 423 } 424 int depth = IResource.DEPTH_ONE; 425 if (isRefreshRecursive(configuration)) 426 depth = IResource.DEPTH_INFINITE; 427 428 if (monitor.isCanceled()) { 429 return; 430 } 431 432 monitor.beginTask(StringSubstitutionMessages.RefreshTab_7, 433 resources.length); 434 435 MultiStatus status = new MultiStatus(DebugUIPlugin.getUniqueIdentifier(), 0, StringSubstitutionMessages.RefreshTab_8, null); 436 for (int i = 0; i < resources.length; i++) { 437 if (monitor.isCanceled()) 438 break; 439 if (resources[i] != null && resources[i].isAccessible()) { 440 try { 441 resources[i].refreshLocal(depth, null); 442 } catch (CoreException e) { 443 status.merge(e.getStatus()); 444 } 445 } 446 monitor.worked(1); 447 } 448 449 monitor.done(); 450 if (!status.isOK()) { 451 throw new CoreException(status); 452 } 453 } 454 455 462 public static IResource[] getRefreshResources(String scope) throws CoreException { 463 if (scope.startsWith("${resource:")) { String pathString = scope.substring(11, scope.length() - 1); 466 Path path = new Path(pathString); 467 IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path); 468 if (resource == null) { 469 throw new CoreException(new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.INTERNAL_ERROR, MessageFormat.format(StringSubstitutionMessages.RefreshTab_38, new String []{pathString}), null)); 470 } 471 return new IResource[]{resource}; 472 } else if (scope.startsWith("${working_set:")) { IWorkingSet workingSet = getWorkingSet(scope); 474 if (workingSet == null) { 475 throw new CoreException(new Status(IStatus.ERROR, DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.INTERNAL_ERROR, StringSubstitutionMessages.RefreshTab_39, null)); 476 } 477 IAdaptable[] elements = workingSet.getElements(); 478 IResource[] resources = new IResource[elements.length]; 479 for (int i = 0; i < elements.length; i++) { 480 IAdaptable adaptable = elements[i]; 481 if (adaptable instanceof IResource) { 482 resources[i] = (IResource) adaptable; 483 } else { 484 resources[i] = (IResource) adaptable.getAdapter(IResource.class); 485 } 486 } 487 return resources; 488 } else if(scope.equals("${workspace}")) { return new IResource[]{ResourcesPlugin.getWorkspace().getRoot()}; 490 } else { 491 IResource resource = SelectedResourceManager.getDefault().getSelectedResource(); 492 if (resource == null) { 493 return new IResource[]{}; 495 } 496 if (scope.equals("${resource}")) { } else if (scope.equals("${container}")) { resource = resource.getParent(); 500 } else if (scope.equals("${project}")) { resource = resource.getProject(); 502 } 503 return new IResource[]{resource}; 504 } 505 } 506 507 515 private static IWorkingSet restoreWorkingSet(String mementoString) { 516 if (NO_WORKING_SET.equals(mementoString)) { 517 return null; 518 } 519 StringReader reader= new StringReader (mementoString); 520 XMLMemento memento= null; 521 try { 522 memento = XMLMemento.createReadRoot(reader); 523 } catch (WorkbenchException e) { 524 DebugUIPlugin.log(e); 525 return null; 526 } 527 528 IWorkingSetManager workingSetManager= PlatformUI.getWorkbench().getWorkingSetManager(); 529 return workingSetManager.createWorkingSet(memento); 530 } 531 532 540 public static String getRefreshScope(ILaunchConfiguration configuration) throws CoreException { 541 return configuration.getAttribute(ATTR_REFRESH_SCOPE, (String ) null); 542 } 543 544 552 public static boolean isRefreshRecursive(ILaunchConfiguration configuration) throws CoreException { 553 return configuration.getAttribute(ATTR_REFRESH_RECURSIVE, true); 554 } 555 556 563 public static String getRefreshAttribute(IWorkingSet workingSet) { 564 String set = null; 565 if (workingSet == null || workingSet.getElements().length == 0) { 566 set = NO_WORKING_SET; 567 } else { 568 XMLMemento workingSetMemento = XMLMemento.createWriteRoot(TAG_LAUNCH_CONFIGURATION_WORKING_SET); 569 workingSetMemento.putString(RefreshTab.TAG_FACTORY_ID, workingSet.getFactoryId()); 570 workingSet.saveState(workingSetMemento); 571 StringWriter writer= new StringWriter (); 572 try { 573 workingSetMemento.save(writer); 574 } catch (IOException e) { 575 DebugUIPlugin.log(e); 576 } 577 set = writer.toString(); 578 } 579 if (set != null) { 580 StringBuffer memento = new StringBuffer (); 581 memento.append("${working_set:"); memento.append(set); 583 memento.append("}"); return memento.toString(); 585 } 586 return null; 587 } 588 589 597 public static IWorkingSet getWorkingSet(String refreshAttribute) { 598 if (refreshAttribute.startsWith("${working_set:")) { String memento = refreshAttribute.substring(14, refreshAttribute.length() - 1); 600 return restoreWorkingSet(memento); 601 } 602 return null; 603 } 604 607 public void activated(ILaunchConfigurationWorkingCopy workingCopy) { 608 } 610 611 614 public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) { 615 } 617 } 618 | Popular Tags |