KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > patch > ApplyPatchOperation


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.patch;
12
13 import java.io.BufferedReader JavaDoc;
14 import java.io.IOException JavaDoc;
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 /**
27  * An operation that provides an interface to the Apply Patch Wizard. Users specify
28  * the input in terms of an <code>IStorage</code> (note: input must be in unified diff
29  * format), an <code>IResource</code> target to apply the patch to and can provide <code>CompareConfiguration</code>
30  * elements to supply the label and images used on the preview page and hunk merge page. Finally, the
31  * user can also supply a title and image to override the default ones provided by the Apply Patch Wizard.
32  * Note that the Apply Patch Wizard does not require any particular set of inputs, and in the absence of
33  * any user supplied values, it will work in default mode.
34  *
35  * @since 3.3
36  *
37  */

38 public class ApplyPatchOperation implements Runnable JavaDoc {
39
40     private IWorkbenchPart part;
41     
42     /**
43      * Used for the Preview Patch page.
44      */

45     private CompareConfiguration configuration;
46     
47     /**
48      * The patch to use as an input into the Apply Patch wizard
49      */

50     private IStorage patch;
51     
52     /**
53      * Specific <code>IResource</code> target to patch.
54      */

55     private IResource target;
56     
57     /**
58      * An optional image for the patch wizard
59      */

60     private ImageDescriptor patchWizardImage;
61     
62     
63     /**
64      * An optional title for the patchWizard
65      */

66     private String JavaDoc patchWizardTitle;
67     
68     /**
69      * Return whether the given storage contains a patch.
70      * @param storage the storage
71      * @return whether the given storage contains a patch
72      * @throws CoreException if an error occurs reading the contents from the storage
73      */

74     public static boolean isPatch(IStorage storage) throws CoreException {
75         return parsePatch(storage).length > 0;
76     }
77     
78     /**
79      * Parse the given patch and return the set of file patches that it contains.
80      * @param storage the storage that contains the patch
81      * @return the set of file patches that the storage contains
82      * @throws CoreException if an error occurs reading the contents from the storage
83      */

84     public static IFilePatch[] parsePatch(IStorage storage) throws CoreException {
85         BufferedReader JavaDoc reader = Patcher.createReader(storage);
86         try {
87             PatchReader patchReader= new PatchReader();
88             patchReader.parse(reader);
89             return patchReader.getAdjustedDiffs();
90         } catch (IOException JavaDoc 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 JavaDoc e) { //ignored
96
}
97         }
98     }
99     
100     /**
101      * Creates a new ApplyPatchOperation with the supplied compare configuration, patch and target.
102      * The behaviour of the Apply Patch wizard is controlled by the number of parameters supplied:
103      * <ul>
104      * <li>If a patch is supplied, the initial input page is skipped. If a patch is not supplied the wizard
105      * will open on the input page.</li>
106      * <li>If the patch is a workspace patch, the target selection page is skipped and the preview page is
107      * displayed.</li>
108      * <li>If the patch is not a workspace patch and the target is specified, the target page is still
109      * shown with the target selected.</li>
110      * </ul>
111      *
112      * @param part an IWorkbenchPart or <code>null</code>
113      * @param patch an IStorage containing a patch in unified diff format or <code>null</code>
114      * @param target an IResource which the patch is to be applied to or <code>null</code>
115      * @param configuration a CompareConfiguration supplying the labels and images for the preview patch page
116      */

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     /**
126      * Create an operation for the given part and resource. This method is a convenience
127      * method that calls {@link #ApplyPatchOperation(IWorkbenchPart, IStorage, IResource, CompareConfiguration)}
128      * with appropriate defaults for the other parameters.
129      * @param targetPart an IResource which the patch is to be applied to or <code>null</code>
130      * @param resource an IResource which the patch is to be applied to or <code>null</code>
131      * @see #ApplyPatchOperation(IWorkbenchPart, IStorage, IResource, CompareConfiguration)
132      */

133     public ApplyPatchOperation(IWorkbenchPart targetPart, IResource resource) {
134         this(targetPart, null, resource, new CompareConfiguration());
135     }
136
137     /**
138      * Open the Apply Patch wizard using the values associated with this operation.
139      * This method must be called from the UI thread.
140      */

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     /**
158      * Return the parent shell to be used when the wizard is opened.
159      * By default, the site of the part is used to get the shell.
160      * Subclasses may override.
161      * @return the parent shell to be used when the wizard is opened
162      */

163     protected Shell getShell() {
164         if (part == null)
165             return CompareUIPlugin.getShell();
166         return part.getSite().getShell();
167     }
168     
169     /**
170      * This method will save all dirty editors. It will prompt the user if the Compare preference to save
171      * dirty editors before viewing a patch is <code>false</code>. Clients can use this or provide their own
172      * implementation.
173      */

174     protected void saveAllEditors(){
175         IDE.saveAllEditors(new IResource[]{ResourcesPlugin.getWorkspace().getRoot()}, !ComparePreferencePage.getSaveAllEditors());
176     }
177     
178     /**
179      * Sets the title of the patch wizard. Needs to be set before {@link #openWizard()} is called.
180      * @param title a string to display in the title bar
181      */

182     public void setPatchWizardTitle(String JavaDoc title){
183         this.patchWizardTitle = title;
184     }
185     
186     /**
187      * Sets the image descriptor to use in the patch wizard. Needs to be set before {@link #openWizard()} is called.
188      * @param descriptor an image descriptor
189      */

190     public void setPatchWizardImageDescriptor(ImageDescriptor descriptor){
191         this.patchWizardImage = descriptor;
192     }
193     
194     /* (non-Javadoc)
195      * @see java.lang.Runnable#run()
196      */

197     public void run() {
198         openWizard();
199     }
200     
201 }
202
Popular Tags