Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9659709885490af8c6f8af0bb4b84dcc66932ba3 (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
/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
package org.eclipse.compare.internal.patch;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import org.eclipse.swt.graphics.Image;

import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.*;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.IProgressMonitor;

import org.eclipse.compare.*;
import org.eclipse.compare.internal.*;
import org.eclipse.compare.structuremergeviewer.Differencer;


/* package */ class PatchWizard extends Wizard {
	
	// dialog store id constants
	private final static String DIALOG_SETTINGS_KEY= "PatchWizard"; //$NON-NLS-1$

	private boolean fHasNewDialogSettings;
	
	private InputPatchPage fPatchWizardPage;
	private PreviewPatchPage fPreviewPatchPage;
	
	private Patcher fPatcher;
	private IResource fTarget;

		
	/**
	 * Creates a wizard for applying a patch file to the workspace.
	 */
	/* package */ PatchWizard(ISelection selection) {
		
		setTargets(getResource(selection));

		fPatcher= new Patcher();
		
		setWindowTitle(PatchMessages.getString("PatchWizard.title")); //$NON-NLS-1$
		
		IDialogSettings workbenchSettings= CompareUIPlugin.getDefault().getDialogSettings();
		IDialogSettings section= workbenchSettings.getSection(DIALOG_SETTINGS_KEY); //$NON-NLS-1$
		if (section == null)
			fHasNewDialogSettings= true;
		else {
			fHasNewDialogSettings= false;
			setDialogSettings(section);
		}	
	}
	
	static IResource[] getResource(ISelection selection) {
		IResource[] rs= Utilities.getResources(selection);
		ArrayList list= null;
		for (int i= 0; i < rs.length; i++) {
			IResource r= rs[i];
			if (r != null && r.isAccessible()) {
				if (list == null)
					list= new ArrayList();
				list.add(r);
			}
		}
		if (list != null && list.size() > 0)
			return (IResource[]) list.toArray(new IResource[list.size()]);
		return null;
	}
		
	Patcher getPatcher() {
		return fPatcher;
	}
	
	IResource getTarget() {
		return fTarget;
	}
	
	void setTargets(IResource[] targets) {
		fTarget= targets[0];	// right now we can only deal with a single selection
	}
	
	/* (non-Javadoc)
	 * Method declared on IWizard.
	 */
	public void addPages() {
		super.addPages();
		
		addPage(fPatchWizardPage= new InputPatchPage(this));
		addPage(fPreviewPatchPage= new PreviewPatchPage(this));
	}
	
	/* (non-Javadoc)
	 * Method declared on IWizard.
	 */
	public boolean performFinish() {
		
		if (false) {
			CompareConfiguration cc= new CompareConfiguration() {
				public Image getImage(int kind) {
					if (kind == Differencer.ADDITION)
						kind= Differencer.DELETION;
					else if (kind == Differencer.DELETION)
						kind= Differencer.ADDITION;
					return super.getImage(kind);
				}
				public Image getImage(Image base, int kind) {
					if (kind == Differencer.ADDITION)
						kind= Differencer.DELETION;
					else if (kind == Differencer.DELETION)
						kind= Differencer.ADDITION;
					return super.getImage(base, kind);
				}
			};
			cc.setProperty(CompareEditor.CONFIRM_SAVE_PROPERTY, new Boolean(false));
	
			fPatcher.setName(fPatchWizardPage.getPatchName());
	
			CompareUI.openCompareEditor(new PatchCompareInput(cc, fPatcher, new StructuredSelection(fTarget)));
		} else {
			fPatcher.setName(fPatchWizardPage.getPatchName());

			try {
				IRunnableWithProgress op= new IRunnableWithProgress() {
					public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
						fPatcher.applyAll(getTarget(), monitor);
					}
				};
				new ProgressMonitorDialog(CompareUIPlugin.getShell()).run(true, true, op);
			} catch (InvocationTargetException e) {
				// handle exception
			} catch (InterruptedException e) {
				// handle cancelation
			}
		}
		
		// 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);
		}
		
		fPatchWizardPage.saveWidgetValues();
		//fPreviewPatchPage.saveWidgetValues();
		
		return true;
	}
}

Back to the top