1 11 package org.eclipse.compare.internal.patch; 12 13 import java.io.IOException ; 14 import java.lang.reflect.InvocationTargetException ; 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 private final static String DIALOG_SETTINGS_KEY= "PatchWizard"; 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")); 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 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 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 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 if (!fPatchWizardPage.isPatchRead()) 117 fPatchWizardPage.readInPatch(); 118 fPatcher.refresh(); 119 } else { 120 Assert.isNotNull(patch); 123 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 ISchedulingRule scheduleRule = null; 136 if (fPatcher.isWorkspacePatch()) { 137 ISchedulingRule[] projectRules = fPatcher.getTargetProjects(); 139 scheduleRule = new MultiRule(projectRules); 140 } else { 141 IResource resource = getTarget(); 143 if (resource.getType() == IResource.FILE) { 144 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 { 152 try { 153 fPatcher.applyAll(monitor, getShell(), PatchMessages.PatchWizard_title); 154 } catch (CoreException e) { 155 throw new InvocationTargetException (e); 156 } 157 } 158 }; 159 getContainer().run(true, false, op); 160 161 } catch (InvocationTargetException e) { 162 ExceptionHandler.handle(e, PatchMessages.PatchWizard_title, PatchMessages.PatchWizard_unexpectedException_message); 163 } catch (InterruptedException e) { 164 } 167 168 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 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 if (!patchReadIn) 193 return fPatchWizardPage; 194 195 if (page instanceof PatchTargetPage && getTarget() != null) { 198 return super.getNextPage(page); 199 } else if (page instanceof InputPatchPage && !fPatcher.isWorkspacePatch()) { 200 return fPatchTargetPage; 202 } 203 return super.getNextPage(page); 204 } 205 206 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 |