1 12 package org.eclipse.debug.internal.ui.sourcelookup; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.debug.core.ILaunchConfiguration; 20 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; 21 import org.eclipse.debug.core.model.ISourceLocator; 22 import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector; 23 import org.eclipse.debug.core.sourcelookup.ISourceContainer; 24 import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector; 25 import org.eclipse.debug.core.sourcelookup.containers.DefaultSourceContainer; 26 import org.eclipse.debug.internal.ui.DebugUIPlugin; 27 import org.eclipse.debug.ui.AbstractLaunchConfigurationTab; 28 import org.eclipse.debug.ui.sourcelookup.WorkingSetSourceContainer; 29 import org.eclipse.jface.dialogs.Dialog; 30 import org.eclipse.jface.dialogs.IDialogConstants; 31 import org.eclipse.jface.util.IPropertyChangeListener; 32 import org.eclipse.jface.util.PropertyChangeEvent; 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.events.SelectionAdapter; 35 import org.eclipse.swt.events.SelectionEvent; 36 import org.eclipse.swt.graphics.Font; 37 import org.eclipse.swt.graphics.FontMetrics; 38 import org.eclipse.swt.graphics.GC; 39 import org.eclipse.swt.layout.GridData; 40 import org.eclipse.swt.layout.GridLayout; 41 import org.eclipse.swt.widgets.Button; 42 import org.eclipse.swt.widgets.Composite; 43 import org.eclipse.swt.widgets.Label; 44 import org.eclipse.ui.IWorkingSet; 45 import org.eclipse.ui.IWorkingSetManager; 46 47 52 public class SourceLookupPanel extends AbstractLaunchConfigurationTab implements IPropertyChangeListener { 53 protected ILaunchConfiguration fConfig; 55 protected SourceContainerViewer fPathViewer; 57 protected Button fDuplicatesButton; 59 protected List fActions = new ArrayList (6); 61 protected ISourceLookupDirector fLocator; 63 64 protected AddContainerAction fAddAction; 65 protected EditContainerAction fEditAction; 66 protected RestoreDefaultAction fRestoreDefaultAction; 67 68 73 public void createControl(Composite parent) { 74 Font font = parent.getFont(); 75 76 Composite comp = new Composite(parent, SWT.NONE); 77 GridLayout topLayout = new GridLayout(); 78 topLayout.numColumns = 2; 79 comp.setLayout(topLayout); 80 GridData gd = new GridData(GridData.FILL_BOTH); 81 comp.setLayoutData(gd); 82 83 Label viewerLabel = new Label(comp, SWT.LEFT); 84 viewerLabel.setText( 85 SourceLookupUIMessages.sourceTab_lookupLabel); 86 gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL); 87 gd.horizontalSpan = 2; 88 viewerLabel.setLayoutData(gd); 89 viewerLabel.setFont(font); 90 91 fPathViewer = new SourceContainerViewer(comp, this); 92 93 gd = new GridData(GridData.FILL_BOTH); 94 fPathViewer.getControl().setLayoutData(gd); 95 fPathViewer.getControl().setFont(font); 96 97 IWorkingSetManager workingSetMgr =DebugUIPlugin.getDefault().getWorkbench().getWorkingSetManager(); 98 workingSetMgr.addPropertyChangeListener(this); 101 102 Composite pathButtonComp = new Composite(comp, SWT.NONE); 103 GridLayout pathButtonLayout = new GridLayout(); 104 pathButtonLayout.marginHeight = 0; 105 pathButtonLayout.marginWidth = 0; 106 pathButtonComp.setLayout(pathButtonLayout); 107 gd = 108 new GridData( 109 GridData.VERTICAL_ALIGN_BEGINNING 110 | GridData.HORIZONTAL_ALIGN_FILL); 111 pathButtonComp.setLayoutData(gd); 112 pathButtonComp.setFont(font); 113 114 createVerticalSpacer(comp, 2); 115 116 fDuplicatesButton = new Button(comp, SWT.CHECK); 117 fDuplicatesButton.setText( 118 SourceLookupUIMessages.sourceTab_searchDuplicateLabel); 119 gd = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING); 120 gd.horizontalSpan = 2; 121 fDuplicatesButton.setLayoutData(gd); 122 fDuplicatesButton.setFont(font); 123 fDuplicatesButton.addSelectionListener(new SelectionAdapter() { 124 public void widgetSelected(SelectionEvent evt) { 125 setDirty(true); 126 updateLaunchConfigurationDialog(); 127 } 128 }); 129 130 GC gc = new GC(parent); 131 gc.setFont(parent.getFont()); 132 FontMetrics fontMetrics = gc.getFontMetrics(); 133 gc.dispose(); 134 135 fAddAction = new AddContainerAction(); 136 Button button = 137 createPushButton(pathButtonComp, fAddAction.getText(), fontMetrics); 138 fAddAction.setButton(button); 139 addAction(fAddAction); 140 141 fEditAction = new EditContainerAction(); 142 button = 143 createPushButton(pathButtonComp, fEditAction.getText(), fontMetrics); 144 fEditAction.setButton(button); 145 addAction(fEditAction); 146 147 SourceContainerAction action = new RemoveAction(); 148 button = 149 createPushButton(pathButtonComp, action.getText(), fontMetrics); 150 action.setButton(button); 151 addAction(action); 152 153 action = new UpAction(); 154 button = 155 createPushButton(pathButtonComp, action.getText(), fontMetrics); 156 action.setButton(button); 157 addAction(action); 158 159 action = new DownAction(); 160 button = 161 createPushButton(pathButtonComp, action.getText(), fontMetrics); 162 action.setButton(button); 163 addAction(action); 164 165 fRestoreDefaultAction = new RestoreDefaultAction(); 166 button = createPushButton(pathButtonComp, fRestoreDefaultAction.getText(), fontMetrics); 167 fRestoreDefaultAction.setButton(button); 168 addAction(fRestoreDefaultAction); 169 170 retargetActions(fPathViewer); 171 172 Dialog.applyDialogFont(comp); 173 setControl(comp); 174 } 175 176 183 protected Button createPushButton( 184 Composite parent, 185 String label, 186 FontMetrics fontMetrics) { 187 Button button = new Button(parent, SWT.PUSH); 188 button.setFont(parent.getFont()); 189 button.setText(label); 190 GridData gd = getButtonGridData(button, fontMetrics); 191 button.setLayoutData(gd); 192 return button; 193 } 194 195 private GridData getButtonGridData( 196 Button button, 197 FontMetrics fontMetrics) { 198 GridData gd = 199 new GridData( 200 GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING); 201 202 int widthHint = 203 Dialog.convertHorizontalDLUsToPixels( 204 fontMetrics, 205 IDialogConstants.BUTTON_WIDTH); 206 gd.widthHint = 207 Math.max( 208 widthHint, 209 button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); 210 211 return gd; 212 } 213 214 217 protected void addAction(SourceContainerAction action) { 218 fActions.add(action); 219 } 220 221 224 protected void retargetActions(SourceContainerViewer viewer) { 225 Iterator actions = fActions.iterator(); 226 while (actions.hasNext()) { 227 SourceContainerAction action = (SourceContainerAction) actions.next(); 228 action.setViewer(viewer); 229 } 230 } 231 232 236 public void initializeFrom(ILaunchConfiguration configuration) { 237 if (fLocator != null) { 238 fLocator.dispose(); 239 fLocator = null; 240 } 241 setErrorMessage(null); 242 setMessage(null); 243 String memento = null; 244 String type = null; 245 try{ 246 memento = configuration.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, (String )null); 247 type = configuration.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, (String )null); 248 if (type == null) { 249 type = configuration.getType().getSourceLocatorId(); 250 } 251 } catch(CoreException e){ 252 setErrorMessage(e.getMessage()); 253 return; 254 } 255 256 if(type == null) { 257 setErrorMessage(SourceLookupUIMessages.sourceLookupPanel_2); 258 return; 259 } 260 261 boolean migration = false; 262 try { 263 ISourceLocator locator = getLaunchManager().newSourceLocator(type); 264 if(!(locator instanceof AbstractSourceLookupDirector)) { 265 memento = null; type = configuration.getType().getSourceLocatorId(); 268 if(type == null) { 269 setErrorMessage(SourceLookupUIMessages.sourceLookupPanel_2); 270 return; 271 } 272 locator = getLaunchManager().newSourceLocator(type); 273 if (!(locator instanceof AbstractSourceLookupDirector)) { 274 setErrorMessage(SourceLookupUIMessages.sourceLookupPanel_2); 275 return; 276 } 277 migration = true; 278 } 279 fLocator = (AbstractSourceLookupDirector)locator; 280 if (memento == null) { 281 fLocator.initializeDefaults(configuration); 282 } else { 283 fLocator.initializeFromMemento(memento, configuration); 284 } 285 } catch (CoreException e) { 286 setErrorMessage(e.getMessage()); 287 return; 288 } 289 initializeFrom(fLocator); 290 if (migration && configuration.isWorkingCopy()) { 291 setDirty(true); 293 performApply((ILaunchConfigurationWorkingCopy)configuration); 294 } 295 } 296 297 301 public void initializeFrom(ISourceLookupDirector locator) { 302 if(fConfig == null) { 303 fConfig = locator.getLaunchConfiguration(); 304 } 305 fPathViewer.setEntries(locator.getSourceContainers()); 306 fDuplicatesButton.setSelection(locator.isFindDuplicates()); 307 fLocator = locator; 308 fAddAction.setSourceLookupDirector(locator); 309 fEditAction.setSourceLookupDirector(locator); 310 fRestoreDefaultAction.setSourceLookupDirector(locator); 311 setDirty(false); 312 } 313 314 322 public void performApply(ILaunchConfigurationWorkingCopy configuration) { 323 if (isDirty()) { 324 if (fLocator == null) { 325 configuration.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, (String )null); 326 configuration.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, (String )null); 327 return; 328 } 329 ILaunchConfigurationWorkingCopy workingCopy = configuration; 330 if(configuration == null) { 331 try { 332 ILaunchConfiguration config = fLocator.getLaunchConfiguration(); 333 if(config != null) { 334 workingCopy = config.getWorkingCopy(); 335 } 336 } 337 catch(CoreException e) { 338 DebugUIPlugin.log(e); 339 setErrorMessage(SourceLookupUIMessages.sourceLookupPanel_1); 340 return; 341 } 342 } 343 if(workingCopy == null) { 344 DebugUIPlugin.logErrorMessage("Error occurred - a working copy could not be acquired, therefore source lookup path changes will not be applied."); return; 346 } 347 fLocator.setSourceContainers(fPathViewer.getEntries()); 349 fLocator.setFindDuplicates(fDuplicatesButton.getSelection()); 350 351 try { 353 if (isDefault()) { 354 workingCopy.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, (String )null); 355 workingCopy.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, (String )null); 356 } else { 357 workingCopy.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, fLocator.getMemento()); 358 workingCopy.setAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, fLocator.getId()); 359 } 360 } 361 catch(CoreException e) { 362 DebugUIPlugin.log(e); 363 setErrorMessage(SourceLookupUIMessages.sourceLookupPanel_1); 364 } 365 366 } 367 } 368 369 374 protected boolean isDefault() { 375 ISourceContainer[] current = getEntries(); 376 return !fDuplicatesButton.getSelection() && current.length == 1 && current[0] instanceof DefaultSourceContainer; 377 } 378 379 382 public ISourceContainer[] getEntries() { 383 return fPathViewer.getEntries(); 384 } 385 388 public void setDirty(boolean dirty) { 389 super.setDirty(dirty); 390 391 } 392 393 396 public String getName() { 397 return SourceLookupUIMessages.sourceTab_tabTitle; 398 } 399 400 403 public void setDefaults(ILaunchConfigurationWorkingCopy configuration) { 404 405 } 406 407 410 protected void updateLaunchConfigurationDialog() { 411 if (getLaunchConfigurationDialog() != null) { 412 super.updateLaunchConfigurationDialog(); 413 } 414 } 415 416 422 private void validateWorkingSetSourceContainers(IWorkingSet affectedWorkingSet) { 423 List sourceContainers = (List ) fPathViewer.getInput(); 424 425 if (sourceContainers != null) { 426 for (int i = 0; i < sourceContainers.size(); i++) { 427 if (sourceContainers.get(i) 428 instanceof WorkingSetSourceContainer) { 429 WorkingSetSourceContainer wsSrcContainer = 430 (WorkingSetSourceContainer) sourceContainers.get(i); 431 if (wsSrcContainer 432 .getName() 433 .equals(affectedWorkingSet.getName())) { 434 sourceContainers.remove(i); 435 } 436 } 437 } 438 } 439 } 440 441 445 public void propertyChange(PropertyChangeEvent event) { 446 if (event.getProperty().equals(IWorkingSetManager.CHANGE_WORKING_SET_REMOVE)) 449 validateWorkingSetSourceContainers((IWorkingSet) event.getOldValue()); 450 451 } 455 456 457 460 public void activated(ILaunchConfigurationWorkingCopy workingCopy) { 461 initializeFrom(workingCopy); 462 } 463 464 471 public ISourceLookupDirector getDirector() { 472 return fLocator; 473 } 474 475 478 public void dispose() { 479 super.dispose(); 480 IWorkingSetManager workingSetMgr =DebugUIPlugin.getDefault().getWorkbench().getWorkingSetManager(); 481 workingSetMgr.removePropertyChangeListener(this); 484 } 485 } 486 | Popular Tags |