Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 31efd37955ae7efacf771c96791487cf06cb00e0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*******************************************************************************
 * Copyright (c) 2000, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare.internal.patch;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.MultiRule;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.resources.ResourcesPlugin;

import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.Wizard;

import org.eclipse.ui.actions.WorkspaceModifyOperation;

import org.eclipse.compare.CompareConfiguration;
import org.eclipse.compare.internal.CompareUIPlugin;
import org.eclipse.compare.internal.ExceptionHandler;
import org.eclipse.compare.internal.Utilities;

public class PatchWizard extends Wizard {

	// dialog store id constants
	private final static String DIALOG_SETTINGS_KEY= "PatchWizard"; //$NON-NLS-1$

	private boolean fHasNewDialogSettings;

	protected InputPatchPage fPatchWizardPage;
	protected PatchTargetPage fPatchTargetPage;
	protected PreviewPatchPage2 fPreviewPage2;

	private final WorkspacePatcher fPatcher;

	private CompareConfiguration fConfiguration;
	private IStorage patch;

	private boolean patchReadIn = false;

	public PatchWizard(IStorage patch, IResource target, CompareConfiguration configuration) {
		Assert.isNotNull(configuration);
		this.fConfiguration = configuration;
		setDefaultPageImageDescriptor(CompareUIPlugin.getImageDescriptor("wizban/applypatch_wizban.png")); //$NON-NLS-1$
		setWindowTitle(PatchMessages.PatchWizard_title);
		initializeDialogSettings();
		fPatcher= new WorkspacePatcher(target);
		if (patch != null) {
			try {
				fPatcher.parse(patch);
				this.patch = patch;
				patchReadIn = true;
			} catch (IOException e) {
				MessageDialog.openError(null,
						PatchMessages.InputPatchPage_PatchErrorDialog_title,
						PatchMessages.InputPatchPage_ParseError_message);
			} catch (CoreException e) {
				ErrorDialog.openError(getShell(),
						PatchMessages.InputPatchPage_PatchErrorDialog_title,
						PatchMessages.InputPatchPage_PatchFileNotFound_message, e.getStatus());
			}
		}
	}

	private void initializeDialogSettings() {
		IDialogSettings workbenchSettings= CompareUIPlugin.getDefault().getDialogSettings();
		IDialogSettings section= workbenchSettings.getSection(DIALOG_SETTINGS_KEY);
		if (section == null) {
			fHasNewDialogSettings= true;
		} else {
			fHasNewDialogSettings= false;
			setDialogSettings(section);
		}
	}

	protected WorkspacePatcher getPatcher() {
		return fPatcher;
	}

	protected IStorage getPatch() {
		return patch;
	}

	IResource getTarget() {
		return fPatcher.getTarget();
	}

	/* (non-Javadoc)
	 * Method declared on IWizard.
	 */
	public void addPages() {
		if (patch == null)
			addPage(fPatchWizardPage = new InputPatchPage(this));
		if (patch == null || (!fPatcher.isWorkspacePatch() && !fPatcher.isGitPatch()))
			addPage(fPatchTargetPage = new PatchTargetPage(fPatcher));
		else if (fPatcher.isGitPatch())
			fPatcher.setTarget(ResourcesPlugin.getWorkspace().getRoot());
		fPreviewPage2 = new PreviewPatchPage2(fPatcher, fConfiguration);
		addPage(fPreviewPage2);
	}

	/* (non-Javadoc)
	 * Method declared on IWizard.
	 */
	public boolean performFinish() {

		IWizardPage currentPage = getContainer().getCurrentPage();
		if (currentPage.getName().equals(PreviewPatchPage2.PREVIEWPATCHPAGE_NAME)){
			PreviewPatchPage2 previewPage = (PreviewPatchPage2) currentPage;
			previewPage.ensureContentsSaved();
		}

		if (fPatchWizardPage != null){
			// make sure that the patch has been read
			if (!fPatchWizardPage.isPatchRead())
				fPatchWizardPage.readInPatch();
			fPatcher.refresh();

			// make sure that the patch is not invalid
			if (!fPatchWizardPage.checkPageComplete())
				return false;
		} else {
			//either we have a patch from the patch input page or one has
			//been specified; double check this
			Assert.isNotNull(patch);
			//make sure that the patch has been read in
			Assert.isTrue(patchReadIn);
		}

		if (!currentPage.getName().equals(PreviewPatchPage2.PREVIEWPATCHPAGE_NAME) && fPatcher.hasRejects()){
			if (!MessageDialog.openConfirm(getShell(), PatchMessages.PatchWizard_0, PatchMessages.PatchWizard_1)) {
				return false;
			}
		}

		try {
			// create scheduling rule based on the type of patch - single or workspace
			ISchedulingRule scheduleRule = null;
			if (fPatcher.isWorkspacePatch()) {
				// workspace patch
				ISchedulingRule[] projectRules = fPatcher.getTargetProjects();
				scheduleRule = new MultiRule(projectRules);
			} else {
				// single patch
				IResource resource = getTarget();
				if (resource.getType() == IResource.FILE) {
					// For a file, use the modify rule for the parent since we may need to include a reject file
					resource = resource.getParent();
				}
				scheduleRule = ResourcesPlugin.getWorkspace().getRuleFactory().modifyRule(resource);
			}

			WorkspaceModifyOperation op = new WorkspaceModifyOperation(scheduleRule) {
				protected void execute(IProgressMonitor monitor) throws InvocationTargetException {
					try {
						fPatcher.applyAll(monitor, new Patcher.IFileValidator() {
							public boolean validateResources(IFile[] resoures) {
								return Utilities.validateResources(resoures, getShell(), PatchMessages.PatchWizard_title);
							}
						});
					} catch (CoreException e) {
						throw new InvocationTargetException(e);
					}
				}
			};
			getContainer().run(true, false, op);

		} catch (InvocationTargetException e) {
			ExceptionHandler.handle(e, PatchMessages.PatchWizard_title, PatchMessages.PatchWizard_unexpectedException_message);
		} catch (InterruptedException e) {
			// cannot happen
			// NeedWork: use assert!
		}

		// Save the dialog settings
		if (fHasNewDialogSettings) {
			IDialogSettings workbenchSettings = CompareUIPlugin.getDefault().getDialogSettings();
			IDialogSettings section = workbenchSettings.getSection(DIALOG_SETTINGS_KEY);
			section = workbenchSettings.addNewSection(DIALOG_SETTINGS_KEY);
			setDialogSettings(section);
		}

		if (fPatchWizardPage != null)
			fPatchWizardPage.saveWidgetValues();
		fPreviewPage2.saveWidgetValues();
		return true;
	}

	public void showPage(IWizardPage page) {
		getContainer().showPage(page);
	}

	public IWizardPage getNextPage(IWizardPage page) {
		//no patch has been read in yet, input patch page
		if (!patchReadIn)
			return fPatchWizardPage;

		//Check to see if we're already on the patch target page and if
		//a target has been set - if it has return the next page in sequence (the preview patch page)
		if (page instanceof PatchTargetPage && getTarget() != null) {
			return super.getNextPage(page);
		} else if (page instanceof InputPatchPage && !fPatcher.isWorkspacePatch()) {
			//Check to see if we need a target
			return fPatchTargetPage;
		}
		return super.getNextPage(page);
	}

	/**
	 * Used to report that the patch has
	 *
	 */
	protected void patchReadIn() {
		patchReadIn = true;
	}

	public CompareConfiguration getCompareConfiguration() {
		return fConfiguration;
	}

	public boolean canFinish() {
		IWizardPage currentPage = getContainer().getCurrentPage();
		if (currentPage.getName().equals(PreviewPatchPage2.PREVIEWPATCHPAGE_NAME)){
			return currentPage.isPageComplete();
		}
		return super.canFinish();
	}

}

Back to the top