KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.io.IOException JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15
16 import org.eclipse.compare.CompareConfiguration;
17 import org.eclipse.compare.internal.CompareUIPlugin;
18 import org.eclipse.compare.internal.ExceptionHandler;
19 import org.eclipse.core.resources.*;
20 import org.eclipse.core.runtime.*;
21 import org.eclipse.core.runtime.jobs.ISchedulingRule;
22 import org.eclipse.core.runtime.jobs.MultiRule;
23 import org.eclipse.jface.dialogs.*;
24 import org.eclipse.jface.wizard.IWizardPage;
25 import org.eclipse.jface.wizard.Wizard;
26 import org.eclipse.ui.actions.WorkspaceModifyOperation;
27
28 public class PatchWizard extends Wizard {
29
30     // dialog store id constants
31
private final static String JavaDoc DIALOG_SETTINGS_KEY= "PatchWizard"; //$NON-NLS-1$
32

33     private boolean fHasNewDialogSettings;
34     
35     private InputPatchPage fPatchWizardPage;
36     private PatchTargetPage fPatchTargetPage;
37     private PreviewPatchPage2 fPreviewPage2;
38     
39     private final WorkspacePatcher fPatcher;
40     private PatchWizardDialog fDialog;
41     
42     private CompareConfiguration fConfiguration;
43     private IStorage patch;
44
45     private boolean patchReadIn = false;
46     
47     public PatchWizard(IStorage patch, IResource target, CompareConfiguration configuration) {
48         Assert.isNotNull(configuration);
49         this.fConfiguration = configuration;
50         setDefaultPageImageDescriptor(CompareUIPlugin.getImageDescriptor("wizban/applypatch_wizban.png")); //$NON-NLS-1$
51
setWindowTitle(PatchMessages.PatchWizard_title);
52         initializeDialogSettings();
53         fPatcher= new WorkspacePatcher(target);
54         if (patch != null) {
55             try {
56                 fPatcher.parse(patch);
57                 this.patch = patch;
58                 patchReadIn = true;
59             } catch (IOException JavaDoc e) {
60                 MessageDialog.openError(null,
61                         PatchMessages.InputPatchPage_PatchErrorDialog_title,
62                         PatchMessages.InputPatchPage_ParseError_message);
63             } catch (CoreException e) {
64                 ErrorDialog.openError(getShell(),
65                         PatchMessages.InputPatchPage_PatchErrorDialog_title,
66                         PatchMessages.InputPatchPage_PatchFileNotFound_message, e.getStatus());
67             }
68         }
69     }
70     
71     private void initializeDialogSettings() {
72         IDialogSettings workbenchSettings= CompareUIPlugin.getDefault().getDialogSettings();
73         IDialogSettings section= workbenchSettings.getSection(DIALOG_SETTINGS_KEY);
74         if (section == null) {
75             fHasNewDialogSettings= true;
76         } else {
77             fHasNewDialogSettings= false;
78             setDialogSettings(section);
79         }
80     }
81
82     WorkspacePatcher getPatcher() {
83         return fPatcher;
84     }
85     
86     IResource getTarget() {
87         return fPatcher.getTarget();
88     }
89     
90     
91     /* (non-Javadoc)
92      * Method declared on IWizard.
93      */

94     public void addPages() {
95         if (patch == null)
96             addPage(fPatchWizardPage = new InputPatchPage(this));
97         if (patch == null || !fPatcher.isWorkspacePatch())
98             addPage(fPatchTargetPage = new PatchTargetPage(fPatcher));
99         fPreviewPage2 = new PreviewPatchPage2(fPatcher, fConfiguration);
100         addPage(fPreviewPage2);
101     }
102     
103     /* (non-Javadoc)
104      * Method declared on IWizard.
105      */

106     public boolean performFinish() {
107         
108         IWizardPage currentPage = fDialog.getCurrentPage();
109         if (currentPage.getName().equals(PreviewPatchPage2.PREVIEWPATCHPAGE_NAME)){
110             PreviewPatchPage2 previewPage = (PreviewPatchPage2) currentPage;
111             previewPage.ensureContentsSaved();
112         }
113         
114         if (fPatchWizardPage != null){
115             // make sure that the patch has been read
116
if (!fPatchWizardPage.isPatchRead())
117                 fPatchWizardPage.readInPatch();
118             fPatcher.refresh();
119         } else {
120             //either we have a patch from the patch input page or one has
121
//been specified; double check this
122
Assert.isNotNull(patch);
123             //make sure that the patch has been read in
124
Assert.isTrue(patchReadIn);
125         }
126         
127         if (!currentPage.getName().equals(PreviewPatchPage2.PREVIEWPATCHPAGE_NAME) && fPatcher.hasRejects()){
128             if (!MessageDialog.openConfirm(getShell(), PatchMessages.PatchWizard_0, PatchMessages.PatchWizard_1)) {
129                 return false;
130             }
131         }
132         
133         try {
134             // create scheduling rule based on the type of patch - single or workspace
135
ISchedulingRule scheduleRule = null;
136             if (fPatcher.isWorkspacePatch()) {
137                 // workspace patch
138
ISchedulingRule[] projectRules = fPatcher.getTargetProjects();
139                 scheduleRule = new MultiRule(projectRules);
140             } else {
141                 // single patch
142
IResource resource = getTarget();
143                 if (resource.getType() == IResource.FILE) {
144                     // For a file, use the modify rule for the parent since we may need to include a reject file
145
resource = resource.getParent();
146                 }
147                 scheduleRule = ResourcesPlugin.getWorkspace().getRuleFactory().modifyRule(resource);
148             }
149
150             WorkspaceModifyOperation op = new WorkspaceModifyOperation(scheduleRule) {
151                 protected void execute(IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
152                     try {
153                         fPatcher.applyAll(monitor, getShell(), PatchMessages.PatchWizard_title);
154                     } catch (CoreException e) {
155                         throw new InvocationTargetException JavaDoc(e);
156                     }
157                 }
158             };
159             getContainer().run(true, false, op);
160
161         } catch (InvocationTargetException JavaDoc e) {
162             ExceptionHandler.handle(e, PatchMessages.PatchWizard_title, PatchMessages.PatchWizard_unexpectedException_message);
163         } catch (InterruptedException JavaDoc e) {
164             // cannot happen
165
// NeedWork: use assert!
166
}
167
168         // Save the dialog settings
169
if (fHasNewDialogSettings) {
170             IDialogSettings workbenchSettings = CompareUIPlugin.getDefault().getDialogSettings();
171             IDialogSettings section = workbenchSettings.getSection(DIALOG_SETTINGS_KEY);
172             section = workbenchSettings.addNewSection(DIALOG_SETTINGS_KEY);
173             setDialogSettings(section);
174         }
175
176         if (fPatchWizardPage != null)
177             fPatchWizardPage.saveWidgetValues();
178             //fPreviewPatchPage.saveWidgetValues();
179
return true;
180     }
181
182     public void setDialog(PatchWizardDialog dialog) {
183         fDialog= dialog;
184     }
185     
186     public void showPage(IWizardPage page) {
187         fDialog.showPage(page);
188     }
189     
190     public IWizardPage getNextPage(IWizardPage page) {
191         //no patch has been read in yet, input patch page
192
if (!patchReadIn)
193             return fPatchWizardPage;
194
195         //Check to see if we're already on the patch target page and if
196
//a target has been set - if it has return the next page in sequence (the preview patch page)
197
if (page instanceof PatchTargetPage && getTarget() != null) {
198             return super.getNextPage(page);
199         } else if (page instanceof InputPatchPage && !fPatcher.isWorkspacePatch()) {
200             //Check to see if we need a target
201
return fPatchTargetPage;
202         }
203         return super.getNextPage(page);
204     }
205
206     /**
207      * Used to report that the patch has
208      *
209      */

210     protected void patchReadIn() {
211         patchReadIn = true;
212     }
213
214     public CompareConfiguration getCompareConfiguration() {
215         return fConfiguration;
216     }
217     
218     public boolean canFinish() {
219         IWizardPage currentPage = fDialog.getCurrentPage();
220         if (currentPage.getName().equals(PreviewPatchPage2.PREVIEWPATCHPAGE_NAME)){
221             return currentPage.isPageComplete();
222         }
223         return super.canFinish();
224     }
225     
226 }
227
Popular Tags