KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > patch > PreviewPatchPage2


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.compare.internal.patch;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.Iterator JavaDoc;
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 JavaDoc PREVIEWPATCHPAGE_NAME= "PreviewPatchPage"; //$NON-NLS-1$
60

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         // Initialize the input
112
try {
113             fInput.run(null);
114         } catch (InterruptedException JavaDoc e) {//ignore
115
} catch (InvocationTargetException JavaDoc e) {//ignore
116
}
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     /**
128      * Makes sure that at least one hunk is checked off in the tree before
129      * allowing the patch to be applied.
130      */

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                     // TODO: This could be a problem. We should only rebuild the affected nodes
161
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 JavaDoc 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 JavaDoc iter = ((TreeSelection) selection).iterator();
189                     while (iter.hasNext()){
190                         Object JavaDoc obj = iter.next();
191                         if (obj instanceof PatchDiffNode){
192                             PatchDiffNode node = ((PatchDiffNode) obj);
193                             node.setEnabled(false);
194                             // TODO: This may require a rebuild if matched hunks are shown
195
}
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 JavaDoc iter = ((TreeSelection) selection).iterator();
208                     while (iter.hasNext()){
209                         Object JavaDoc obj = iter.next();
210                         if (obj instanceof PatchDiffNode){
211                             PatchDiffNode node = ((PatchDiffNode) obj);
212                             node.setEnabled(true);
213                             // TODO: This may require a rebuild if matched hunks are shown
214
}
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 JavaDoc, InterruptedException JavaDoc {
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 JavaDoc(isChecked()));
232                                     }
233                                 } else {
234                                     fIgnoreWhiteSpace.setChecked(!isChecked());
235                                 }
236                             }
237                             monitor.done();
238                         }
239                     });
240                 } catch (InvocationTargetException JavaDoc e) { //ignore
241
} catch (InterruptedException JavaDoc e) { //ignore
242
}
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 JavaDoc, InterruptedException JavaDoc {
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 JavaDoc e) { //ignore
268
} catch (InterruptedException JavaDoc e) { //ignore
269
}
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         //Need to handle input and rebuild tree only when becoming visible
281
if (visible){
282             fillSegmentCombo();
283             // TODO: We should only do this if the tree needs to be rebuilt
284
rebuildTree();
285             updateEnablements();
286         }
287     }
288     
289     private boolean promptToRebuild(final String JavaDoc promptToConfirm){
290         final Control ctrl = getControl();
291         final boolean[] result = new boolean[] { false };
292         if (ctrl != null && !ctrl.isDisposed()){
293             Runnable JavaDoc runnable = new Runnable JavaDoc() {
294                 public void run() {
295                     if (!ctrl.isDisposed()) {
296                         // flush any viewers before prompting
297
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 JavaDoc runnable = new Runnable JavaDoc() {
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     /*
349      * Create the group for setting various patch options
350      */

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         // 1st row
359
createStripSegmentCombo(group);
360         addSpacer(group);
361         createFuzzFactorChooser(group);
362         
363         // 2nd row
364
createReversePatchToggle(group);
365         createShowRemovedToggle(group);
366         createGenerateRejectsToggle(group);
367         
368         // register listeners
369
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"); //$NON-NLS-1$
416
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 JavaDoc 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 JavaDoc s= fFuzzField.getText();
525             try {
526                 fuzzFactor= Integer.parseInt(s);
527             } catch (NumberFormatException JavaDoc ex) {
528                 // silently ignored
529
}
530         }
531         return fuzzFactor;
532     }
533     
534     public int getStripPrefixSegments() {
535         int stripPrefixSegments= 0;
536         if (fStripPrefixSegments!=null) {
537             String JavaDoc s= fStripPrefixSegments.getText();
538             try {
539                 stripPrefixSegments= Integer.parseInt(s);
540             } catch (NumberFormatException JavaDoc ex) {
541                 // silently ignored
542
}
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 JavaDoc ex) {
558             // NeedWork
559
} catch (InterruptedException JavaDoc ex) {
560             // NeedWork
561
}
562         return result[0];
563     }
564     
565     public void ensureContentsSaved() {
566         try {
567             fInput.saveChanges(new NullProgressMonitor());
568         } catch (CoreException e) {
569             //ignore
570
}
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