1 11 package org.eclipse.compare.internal.patch; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Iterator ; 15 16 import org.eclipse.compare.CompareConfiguration; 17 import org.eclipse.compare.internal.CompareUIPlugin; 18 import org.eclipse.compare.internal.ICompareUIConstants; 19 import org.eclipse.core.runtime.Assert; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.NullProgressMonitor; 23 import org.eclipse.jface.action.Action; 24 import org.eclipse.jface.action.IMenuManager; 25 import org.eclipse.jface.action.Separator; 26 import org.eclipse.jface.dialogs.IDialogConstants; 27 import org.eclipse.jface.operation.IRunnableWithProgress; 28 import org.eclipse.jface.util.IPropertyChangeListener; 29 import org.eclipse.jface.util.PropertyChangeEvent; 30 import org.eclipse.jface.viewers.ISelection; 31 import org.eclipse.jface.viewers.ISelectionChangedListener; 32 import org.eclipse.jface.viewers.IStructuredSelection; 33 import org.eclipse.jface.viewers.SelectionChangedEvent; 34 import org.eclipse.jface.viewers.TreeSelection; 35 import org.eclipse.jface.window.Window; 36 import org.eclipse.jface.wizard.WizardPage; 37 import org.eclipse.swt.SWT; 38 import org.eclipse.swt.events.ModifyEvent; 39 import org.eclipse.swt.events.ModifyListener; 40 import org.eclipse.swt.events.SelectionAdapter; 41 import org.eclipse.swt.events.SelectionEvent; 42 import org.eclipse.swt.graphics.Point; 43 import org.eclipse.swt.layout.GridData; 44 import org.eclipse.swt.layout.GridLayout; 45 import org.eclipse.swt.widgets.Button; 46 import org.eclipse.swt.widgets.Combo; 47 import org.eclipse.swt.widgets.Composite; 48 import org.eclipse.swt.widgets.Control; 49 import org.eclipse.swt.widgets.Display; 50 import org.eclipse.swt.widgets.Group; 51 import org.eclipse.swt.widgets.Label; 52 import org.eclipse.swt.widgets.Shell; 53 import org.eclipse.swt.widgets.Text; 54 import org.eclipse.ui.PlatformUI; 55 56 57 public class PreviewPatchPage2 extends WizardPage { 58 59 protected final static String PREVIEWPATCHPAGE_NAME= "PreviewPatchPage"; 61 final WorkspacePatcher fPatcher; 62 private final CompareConfiguration fConfiguration; 63 private PatchCompareEditorInput fInput; 64 65 private Combo fStripPrefixSegments; 66 private Text fFuzzField; 67 68 private Action fExcludeAction; 69 private Action fIncludeAction; 70 private Action fIgnoreWhiteSpace; 71 private Action fReversePatch; 72 private Action fMoveAction; 73 74 protected boolean pageRecalculate= true; 75 76 public PreviewPatchPage2(WorkspacePatcher patcher, CompareConfiguration configuration) { 77 super(PREVIEWPATCHPAGE_NAME, PatchMessages.PreviewPatchPage_title, null); 78 setDescription(PatchMessages.PreviewPatchPage2_8); 79 Assert.isNotNull(patcher); 80 Assert.isNotNull(configuration); 81 this.fPatcher = patcher; 82 this.fConfiguration = configuration; 83 this.fConfiguration.addPropertyChangeListener(new IPropertyChangeListener() { 84 public void propertyChange(PropertyChangeEvent event) { 85 if (event.getProperty().equals(CompareConfiguration.IGNORE_WHITESPACE)){ 86 rebuildTree(); 87 } 88 } 89 }); 90 } 91 92 public void createControl(Composite parent) { 93 Composite composite = new Composite(parent, SWT.NONE); 94 composite.setLayout(new GridLayout()); 95 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 96 initializeDialogUnits(parent); 97 98 fInput = new PatchCompareEditorInput(getPatcher(), getCompareConfiguration()) { 99 protected void fillContextMenu(IMenuManager manager) { 100 if (isShowAll()) { 101 manager.add(fIncludeAction); 102 } 103 manager.add(fExcludeAction); 104 manager.add(new Separator()); 105 manager.add(fMoveAction); 106 } 107 }; 108 109 buildPatchOptionsGroup(composite); 110 111 try { 113 fInput.run(null); 114 } catch (InterruptedException e) { } catch (InvocationTargetException e) { } 117 118 Control c = fInput.createContents(composite); 119 initializeActions(); 120 fInput.contributeDiffViewerToolbarItems(getContributedActions(), getPatcher().isWorkspacePatch()); 121 122 c.setLayoutData(new GridData(GridData.FILL_BOTH)); 123 124 setControl(composite); 125 } 126 127 131 private void updateEnablements() { 132 boolean atLeastOneIsEnabled = false; 133 if (fInput != null) 134 atLeastOneIsEnabled = fInput.hasResultToApply(); 135 setPageComplete(atLeastOneIsEnabled); 136 } 137 138 private Action[] getContributedActions() { 139 return new Action[]{ fIgnoreWhiteSpace }; 140 } 141 142 private void initializeActions() { 143 144 fMoveAction = new Action(PatchMessages.PreviewPatchPage2_RetargetAction, null) { 145 public void run() { 146 Shell shell = getShell(); 147 ISelection selection = fInput.getViewer().getSelection(); 148 PatchDiffNode node = null; 149 if (selection instanceof IStructuredSelection) { 150 IStructuredSelection ss = (IStructuredSelection) selection; 151 if (ss.getFirstElement() instanceof PatchDiffNode) { 152 node = (PatchDiffNode) ss.getFirstElement(); 153 } 154 } 155 if (node == null) 156 return; 157 final RetargetPatchElementDialog dialog = new RetargetPatchElementDialog(shell, fPatcher, node); 158 int returnCode = dialog.open(); 159 if (returnCode == Window.OK) { 160 rebuildTree(); 162 } 163 } 164 }; 165 fMoveAction .setToolTipText(PatchMessages.PreviewPatchPage2_RetargetTooltip); 166 fMoveAction.setEnabled(true); 167 fInput.getViewer().addSelectionChangedListener(new ISelectionChangedListener() { 168 public void selectionChanged(SelectionChangedEvent event) { 169 IStructuredSelection sel= (IStructuredSelection) event.getSelection(); 170 Object obj= sel.getFirstElement(); 171 boolean enable = false; 172 if (obj instanceof PatchProjectDiffNode) { 173 enable = true; 174 } else if (obj instanceof PatchFileDiffNode) { 175 PatchFileDiffNode node = (PatchFileDiffNode) obj; 176 enable = node.getDiffResult().getDiffProblem(); 177 } else if (obj instanceof HunkDiffNode) { 178 enable = true; 179 } 180 fMoveAction.setEnabled(enable); 181 } 182 }); 183 184 fExcludeAction = new Action(PatchMessages.PreviewPatchPage2_0) { 185 public void run() { 186 ISelection selection = fInput.getViewer().getSelection(); 187 if (selection instanceof TreeSelection){ 188 Iterator iter = ((TreeSelection) selection).iterator(); 189 while (iter.hasNext()){ 190 Object obj = iter.next(); 191 if (obj instanceof PatchDiffNode){ 192 PatchDiffNode node = ((PatchDiffNode) obj); 193 node.setEnabled(false); 194 } 196 } 197 } 198 fInput.getViewer().refresh(); 199 } 200 }; 201 fExcludeAction.setEnabled(true); 202 203 fIncludeAction = new Action(PatchMessages.PreviewPatchPage2_1) { 204 public void run() { 205 ISelection selection = fInput.getViewer().getSelection(); 206 if (selection instanceof TreeSelection){ 207 Iterator iter = ((TreeSelection) selection).iterator(); 208 while (iter.hasNext()){ 209 Object obj = iter.next(); 210 if (obj instanceof PatchDiffNode){ 211 PatchDiffNode node = ((PatchDiffNode) obj); 212 node.setEnabled(true); 213 } 215 } 216 } 217 fInput.getViewer().refresh(); 218 } 219 }; 220 fIncludeAction.setEnabled(true); 221 222 fIgnoreWhiteSpace = new Action(PatchMessages.PreviewPatchPage2_IgnoreWSAction, CompareUIPlugin.getImageDescriptor(ICompareUIConstants.IGNORE_WHITESPACE_ENABLED)){ 223 public void run(){ 224 try { 225 getContainer().run(false, true, new IRunnableWithProgress() { 226 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 227 monitor.beginTask(PatchMessages.PreviewPatchPage2_IgnoreWhitespace, IProgressMonitor.UNKNOWN); 228 if (isChecked() != getPatcher().isIgnoreWhitespace()) { 229 if (promptToRebuild(PatchMessages.PreviewPatchPage2_2)) { 230 if (getPatcher().setIgnoreWhitespace(isChecked())){ 231 getCompareConfiguration().setProperty(CompareConfiguration.IGNORE_WHITESPACE, new Boolean (isChecked())); 232 } 233 } else { 234 fIgnoreWhiteSpace.setChecked(!isChecked()); 235 } 236 } 237 monitor.done(); 238 } 239 }); 240 } catch (InvocationTargetException e) { } catch (InterruptedException e) { } 243 } 244 }; 245 fIgnoreWhiteSpace.setChecked(false); 246 fIgnoreWhiteSpace.setToolTipText(PatchMessages.PreviewPatchPage2_IgnoreWSTooltip); 247 fIgnoreWhiteSpace.setDisabledImageDescriptor(CompareUIPlugin.getImageDescriptor(ICompareUIConstants.IGNORE_WHITESPACE_DISABLED)); 248 249 fReversePatch = new Action(PatchMessages.PreviewPatchPage_ReversePatch_text){ 250 public void run(){ 251 try { 252 getContainer().run(true, true, new IRunnableWithProgress() { 253 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 254 monitor.beginTask(PatchMessages.PreviewPatchPage2_CalculateReverse, IProgressMonitor.UNKNOWN); 255 if (isChecked() != getPatcher().isReversed()) { 256 if (promptToRebuild(PatchMessages.PreviewPatchPage2_3)) { 257 if (getPatcher().setReversed(isChecked())){ 258 rebuildTree(); 259 } 260 } else { 261 fReversePatch.setChecked(!isChecked()); 262 } 263 } 264 monitor.done(); 265 } 266 }); 267 } catch (InvocationTargetException e) { } catch (InterruptedException e) { } 270 271 } 272 273 }; 274 fReversePatch.setChecked(false); 275 fReversePatch.setToolTipText(PatchMessages.PreviewPatchPage_ReversePatch_text); 276 } 277 278 public void setVisible(boolean visible) { 279 super.setVisible(visible); 280 if (visible){ 282 fillSegmentCombo(); 283 rebuildTree(); 285 updateEnablements(); 286 } 287 } 288 289 private boolean promptToRebuild(final String promptToConfirm){ 290 final Control ctrl = getControl(); 291 final boolean[] result = new boolean[] { false }; 292 if (ctrl != null && !ctrl.isDisposed()){ 293 Runnable runnable = new Runnable () { 294 public void run() { 295 if (!ctrl.isDisposed()) { 296 try { 298 fInput.saveChanges(null); 299 } catch (CoreException e) { 300 CompareUIPlugin.log(e); 301 } 302 result[0] = fInput.confirmRebuild(promptToConfirm); 303 } 304 } 305 }; 306 if (Display.getCurrent() == null) 307 ctrl.getDisplay().syncExec(runnable); 308 else 309 runnable.run(); 310 } 311 return result[0]; 312 } 313 314 private void rebuildTree(){ 315 final Control ctrl = getControl(); 316 if (ctrl != null && !ctrl.isDisposed()){ 317 Runnable runnable = new Runnable () { 318 public void run() { 319 if (!ctrl.isDisposed()) { 320 fInput.buildTree(); 321 updateEnablements(); 322 } 323 } 324 }; 325 if (Display.getCurrent() == null) 326 ctrl.getDisplay().syncExec(runnable); 327 else 328 runnable.run(); 329 } 330 } 331 332 private void fillSegmentCombo() { 333 if (getPatcher().isWorkspacePatch()) { 334 fStripPrefixSegments.setEnabled(false); 335 } else { 336 fStripPrefixSegments.setEnabled(true); 337 int length= 99; 338 if (fStripPrefixSegments!=null && pageRecalculate) { 339 length= getPatcher().calculatePrefixSegmentCount(); 340 if (length!=99) { 341 for (int k= 1; k<length; k++) 342 fStripPrefixSegments.add(Integer.toString(k)); 343 pageRecalculate= false; 344 } 345 } 346 } 347 } 348 351 private void buildPatchOptionsGroup(Composite parent) { 352 Group group= new Group(parent, SWT.NONE); 353 group.setText(PatchMessages.PreviewPatchPage_PatchOptions_title); 354 GridLayout gl= new GridLayout(); gl.numColumns= 3; 355 group.setLayout(gl); 356 group.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL|GridData.GRAB_HORIZONTAL)); 357 358 createStripSegmentCombo(group); 360 addSpacer(group); 361 createFuzzFactorChooser(group); 362 363 createReversePatchToggle(group); 365 createShowRemovedToggle(group); 366 createGenerateRejectsToggle(group); 367 368 final WorkspacePatcher patcher= getPatcher(); 370 if (fStripPrefixSegments!=null) 371 fStripPrefixSegments.addSelectionListener( 372 new SelectionAdapter() { 373 public void widgetSelected(SelectionEvent e) { 374 if (patcher.getStripPrefixSegments() != getStripPrefixSegments()) { 375 if (promptToRebuild(PatchMessages.PreviewPatchPage2_4)) { 376 if (patcher.setStripPrefixSegments(getStripPrefixSegments())) 377 rebuildTree(); 378 } 379 } 380 } 381 } 382 ); 383 384 385 fFuzzField.addModifyListener( 386 new ModifyListener() { 387 public void modifyText(ModifyEvent e) { 388 if (patcher.getFuzz() != getFuzzFactor()) { 389 if (promptToRebuild(PatchMessages.PreviewPatchPage2_5)) { 390 if (patcher.setFuzz(getFuzzFactor())) 391 rebuildTree(); 392 } else { 393 fFuzzField.setText(Integer.toString(patcher.getFuzz())); 394 } 395 } 396 } 397 }); 398 } 399 400 private void createFuzzFactorChooser(Group group) { 401 final WorkspacePatcher patcher= getPatcher(); 402 Composite pair= new Composite(group, SWT.NONE); 403 GridLayout gl= new GridLayout(); gl.numColumns= 3; gl.marginHeight= gl.marginWidth= 0; 404 pair.setLayout(gl); 405 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 406 pair.setLayoutData(gd); 407 408 Label l= new Label(pair, SWT.NONE); 409 l.setText(PatchMessages.PreviewPatchPage_FuzzFactor_text); 410 l.setToolTipText(PatchMessages.PreviewPatchPage_FuzzFactor_tooltip); 411 gd= new GridData(GridData.VERTICAL_ALIGN_CENTER|GridData.HORIZONTAL_ALIGN_BEGINNING|GridData.GRAB_HORIZONTAL); 412 l.setLayoutData(gd); 413 414 fFuzzField= new Text(pair, SWT.BORDER); 415 fFuzzField.setText("2"); gd= new GridData(GridData.VERTICAL_ALIGN_CENTER | GridData.HORIZONTAL_ALIGN_END); gd.widthHint= 30; 417 fFuzzField.setLayoutData(gd); 418 419 Button b= new Button(pair, SWT.PUSH); 420 b.setText(PatchMessages.PreviewPatchPage_GuessFuzz_text); 421 b.addSelectionListener(new SelectionAdapter() { 422 public void widgetSelected(SelectionEvent e) { 423 if (promptToRebuild(PatchMessages.PreviewPatchPage2_6)) { 424 int fuzz= guessFuzzFactor(patcher); 425 if (fuzz>=0) 426 fFuzzField.setText(Integer.toString(fuzz)); 427 } 428 } 429 } 430 ); 431 gd= new GridData(GridData.VERTICAL_ALIGN_CENTER); 432 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 433 Point minSize = b.computeSize(SWT.DEFAULT, SWT.DEFAULT, true); 434 gd.widthHint = Math.max(widthHint, minSize.x); 435 b.setLayoutData(gd); 436 } 437 438 private void createGenerateRejectsToggle(Composite pair) { 439 final Button generateRejects = new Button(pair, SWT.CHECK); 440 generateRejects.setText(PatchMessages.HunkMergePage_GenerateRejectFile); 441 GridData gd = new GridData(GridData.VERTICAL_ALIGN_CENTER 442 | GridData.HORIZONTAL_ALIGN_BEGINNING 443 | GridData.GRAB_HORIZONTAL); 444 generateRejects.addSelectionListener(new SelectionAdapter() { 445 public void widgetSelected(SelectionEvent e) { 446 getPatcher().setGenerateRejectFile( 447 generateRejects.getSelection()); 448 } 449 }); 450 generateRejects.setSelection(false); 451 generateRejects.setLayoutData(gd); 452 } 453 454 private void createShowRemovedToggle(Composite pair) { 455 final Button showRemoved = new Button(pair, SWT.CHECK); 456 showRemoved.setText(PatchMessages.PreviewPatchPage2_7); 457 GridData gd = new GridData(GridData.VERTICAL_ALIGN_CENTER 458 | GridData.HORIZONTAL_ALIGN_BEGINNING 459 | GridData.GRAB_HORIZONTAL); 460 showRemoved.addSelectionListener(new SelectionAdapter() { 461 public void widgetSelected(SelectionEvent e) { 462 fInput.setShowAll(showRemoved.getSelection()); 463 fInput.updateTree(); 464 } 465 }); 466 showRemoved.setSelection(fInput.isShowAll()); 467 showRemoved.setLayoutData(gd); 468 } 469 470 private void createReversePatchToggle(Composite pair) { 471 final Button reversePatch = new Button(pair, SWT.CHECK); 472 reversePatch.setText(PatchMessages.PreviewPatchPage_ReversePatch_text); 473 GridData gd = new GridData(GridData.VERTICAL_ALIGN_CENTER 474 | GridData.HORIZONTAL_ALIGN_BEGINNING 475 | GridData.GRAB_HORIZONTAL); 476 reversePatch.addSelectionListener(new SelectionAdapter() { 477 public void widgetSelected(SelectionEvent e) { 478 if (fReversePatch != null) { 479 fReversePatch.setChecked(reversePatch.getSelection()); 480 fReversePatch.run(); 481 if (fReversePatch.isChecked() != reversePatch.getSelection()) { 482 reversePatch.setSelection(fReversePatch.isChecked()); 483 } 484 } 485 } 486 }); 487 reversePatch.setSelection(getPatcher().isReversed()); 488 reversePatch.setLayoutData(gd); 489 } 490 491 private void createStripSegmentCombo(Group group) { 492 final WorkspacePatcher patcher= getPatcher(); 493 494 Composite pair= new Composite(group, SWT.NONE); 495 GridLayout gl= new GridLayout(); gl.numColumns= 2; gl.marginHeight= gl.marginWidth= 0; 496 pair.setLayout(gl); 497 GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_FILL); 498 pair.setLayoutData(gd); 499 500 Label l= new Label(pair, SWT.NONE); 501 l.setText(PatchMessages.PreviewPatchPage_IgnoreSegments_text); 502 gd= new GridData(GridData.VERTICAL_ALIGN_CENTER|GridData.HORIZONTAL_ALIGN_BEGINNING); 503 l.setLayoutData(gd); 504 505 fStripPrefixSegments= new Combo(pair, SWT.DROP_DOWN|SWT.READ_ONLY|SWT.SIMPLE); 506 int prefixCnt= patcher.getStripPrefixSegments(); 507 String prefix= Integer.toString(prefixCnt); 508 fStripPrefixSegments.add(prefix); 509 fStripPrefixSegments.setText(prefix); 510 gd= new GridData(GridData.VERTICAL_ALIGN_CENTER|GridData.HORIZONTAL_ALIGN_BEGINNING | GridData.GRAB_HORIZONTAL); 511 fStripPrefixSegments.setLayoutData(gd); 512 } 513 514 private void addSpacer(Composite parent) { 515 Label label= new Label(parent, SWT.NONE); 516 GridData gd= new GridData(GridData.FILL_HORIZONTAL); 517 gd.widthHint= 10; 518 label.setLayoutData(gd); 519 } 520 521 public int getFuzzFactor() { 522 int fuzzFactor= 0; 523 if (fFuzzField!=null) { 524 String s= fFuzzField.getText(); 525 try { 526 fuzzFactor= Integer.parseInt(s); 527 } catch (NumberFormatException ex) { 528 } 530 } 531 return fuzzFactor; 532 } 533 534 public int getStripPrefixSegments() { 535 int stripPrefixSegments= 0; 536 if (fStripPrefixSegments!=null) { 537 String s= fStripPrefixSegments.getText(); 538 try { 539 stripPrefixSegments= Integer.parseInt(s); 540 } catch (NumberFormatException ex) { 541 } 543 } 544 return stripPrefixSegments; 545 } 546 547 private int guessFuzzFactor(final WorkspacePatcher patcher) { 548 final int[] result= new int[] { -1 }; 549 try { 550 PlatformUI.getWorkbench().getProgressService().run(true, true, 551 new IRunnableWithProgress() { 552 public void run(IProgressMonitor monitor) { 553 result[0]= patcher.guessFuzzFactor(monitor); 554 } 555 } 556 ); 557 } catch (InvocationTargetException ex) { 558 } catch (InterruptedException ex) { 560 } 562 return result[0]; 563 } 564 565 public void ensureContentsSaved() { 566 try { 567 fInput.saveChanges(new NullProgressMonitor()); 568 } catch (CoreException e) { 569 } 571 } 572 573 public WorkspacePatcher getPatcher() { 574 return fPatcher; 575 } 576 577 public CompareConfiguration getCompareConfiguration() { 578 return fConfiguration; 579 } 580 581 582 } 583 | Popular Tags |