1 11 package org.eclipse.compare.patch; 12 13 import java.io.BufferedReader ; 14 import java.io.IOException ; 15 16 import org.eclipse.compare.CompareConfiguration; 17 import org.eclipse.compare.internal.*; 18 import org.eclipse.compare.internal.patch.*; 19 import org.eclipse.core.resources.*; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.jface.resource.ImageDescriptor; 22 import org.eclipse.swt.widgets.Shell; 23 import org.eclipse.ui.IWorkbenchPart; 24 import org.eclipse.ui.ide.IDE; 25 26 38 public class ApplyPatchOperation implements Runnable { 39 40 private IWorkbenchPart part; 41 42 45 private CompareConfiguration configuration; 46 47 50 private IStorage patch; 51 52 55 private IResource target; 56 57 60 private ImageDescriptor patchWizardImage; 61 62 63 66 private String patchWizardTitle; 67 68 74 public static boolean isPatch(IStorage storage) throws CoreException { 75 return parsePatch(storage).length > 0; 76 } 77 78 84 public static IFilePatch[] parsePatch(IStorage storage) throws CoreException { 85 BufferedReader reader = Patcher.createReader(storage); 86 try { 87 PatchReader patchReader= new PatchReader(); 88 patchReader.parse(reader); 89 return patchReader.getAdjustedDiffs(); 90 } catch (IOException e) { 91 throw new CoreException(new Status(IStatus.ERROR, CompareUIPlugin.PLUGIN_ID, 0, e.getMessage(), e)); 92 } finally { 93 try { 94 reader.close(); 95 } catch (IOException e) { } 97 } 98 } 99 100 117 public ApplyPatchOperation(IWorkbenchPart part, IStorage patch, IResource target, CompareConfiguration configuration) { 118 Assert.isNotNull(configuration); 119 this.part = part; 120 this.patch = patch; 121 this.target = target; 122 this.configuration = configuration; 123 } 124 125 133 public ApplyPatchOperation(IWorkbenchPart targetPart, IResource resource) { 134 this(targetPart, null, resource, new CompareConfiguration()); 135 } 136 137 141 public void openWizard() { 142 143 saveAllEditors(); 144 145 PatchWizard wizard = new PatchWizard(patch, target, configuration); 146 if (patchWizardImage != null) 147 wizard.setDefaultPageImageDescriptor(patchWizardImage); 148 if (patchWizardTitle != null) 149 wizard.setWindowTitle(patchWizardTitle); 150 wizard.setNeedsProgressMonitor(true); 151 152 PatchWizardDialog dialog = new PatchWizardDialog(getShell(), wizard); 153 wizard.setDialog(dialog); 154 dialog.open(); 155 } 156 157 163 protected Shell getShell() { 164 if (part == null) 165 return CompareUIPlugin.getShell(); 166 return part.getSite().getShell(); 167 } 168 169 174 protected void saveAllEditors(){ 175 IDE.saveAllEditors(new IResource[]{ResourcesPlugin.getWorkspace().getRoot()}, !ComparePreferencePage.getSaveAllEditors()); 176 } 177 178 182 public void setPatchWizardTitle(String title){ 183 this.patchWizardTitle = title; 184 } 185 186 190 public void setPatchWizardImageDescriptor(ImageDescriptor descriptor){ 191 this.patchWizardImage = descriptor; 192 } 193 194 197 public void run() { 198 openWizard(); 199 } 200 201 } 202 | Popular Tags |