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

import java.io.*;
import java.io.ByteArrayInputStream;
import java.text.MessageFormat;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;

import org.eclipse.core.runtime.*;
import org.eclipse.core.resources.*;

import org.eclipse.jface.viewers.*;

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


/**
 * A PatchCompareInput uses a Patcher to 
 * patch selected workspace resources.
 */
/* package */ class PatchCompareInput extends CompareEditorInput {
	
	static class Rejected extends DiffNode implements IStreamContentAccessor {
		Diff fDiff;
		String fName;
		Rejected(IDiffContainer parent, String name, Diff diff) {
			super(parent, Differencer.NO_CHANGE);
			fName= name;
			fDiff= diff;
		}
		public String getName() {
			return fName + " *"; //$NON-NLS-1$
		}
		public String getType() {
			return "txt"; //$NON-NLS-1$
		}
		public Image getImage() {
			return CompareUI.getImage("file"); //$NON-NLS-1$
		}
		public InputStream getContents() {
			return new ByteArrayInputStream(fDiff.fRejected.getBytes());
		}
	}
		
	private DiffNode fRoot;
	private IResource fTarget;
	private Patcher fPatcher;
	
	/**
	 * Creates an compare editor input for the given selection.
	 */
	/* package */ PatchCompareInput(CompareConfiguration config, Patcher patcher, ISelection selection) {
		super(config);
		fPatcher= patcher;
		IResource[] s= Utilities.getResources(selection);
		if (s.length == 1)
			fTarget= s[0];	
	}
	
	/**
	 * Performs a two-way or three-way diff on the current selection.
	 */
	public Object prepareInput(IProgressMonitor pm) throws InvocationTargetException {
						
		CompareConfiguration cc= (CompareConfiguration) getCompareConfiguration();
		
		try {				
			Diff[] diffs= fPatcher.getDiffs();
			
			pm.beginTask(Utilities.getString("ResourceCompare.taskName"), diffs.length); //$NON-NLS-1$
		
			fRoot= new DiffNode(0);
			IContainer rootFolder= null;
			if (fTarget instanceof IContainer)
				rootFolder= (IContainer) fTarget;
				
			for (int i= 0; i < diffs.length; i++) {
				Diff diff= diffs[i];
				if (diff.isEnabled()) {
					IPath path= fPatcher.getPath(diff);
					createPath(fRoot, rootFolder, path, diff, false);
					
					String rej= diff.fRejected;
					if (rej != null) {
						IPath pp= path.removeLastSegments(1);
						pp= pp.append(path.lastSegment() + ".rej"); //$NON-NLS-1$
						createPath(fRoot, rootFolder, pp, diff, true);
					}
				}
				pm.worked(1);
			}
						
			fTarget.refreshLocal(IResource.DEPTH_INFINITE, pm);
			
			String leftLabel= fTarget.getName();
			cc.setLeftLabel(leftLabel);
			cc.setLeftImage(CompareUIPlugin.getImage(fTarget));
			
			String rformat= PatchMessages.getString("PatchCompareInput.RightTitle.format");	//$NON-NLS-1$
			String rightLabel= MessageFormat.format(rformat, new String[] { fPatcher.getName() } );
			cc.setRightLabel(rightLabel);
			//cc.setRightImage(CompareUIPlugin.getImage(fRightResource));
			
			String format= Utilities.getString("ResourceCompare.twoWay.title"); //$NON-NLS-1$
			String title= MessageFormat.format(format, new String[] {leftLabel, rightLabel} );
			setTitle(title);

			return fRoot;
			
		} catch (CoreException ex) {
			throw new InvocationTargetException(ex);
		} finally {
			pm.done();
		}
	}
	
	public void saveChanges(IProgressMonitor pm) throws CoreException {
		if (fRoot instanceof DiffNode) {
			try {
				commit(pm, (DiffNode) fRoot);
			} finally {	
				setDirty(false);
			}
		}
	}
	
	/*
	 * Recursively walks the diff tree and commits all changes.
	 */
	private static void commit(IProgressMonitor pm, DiffNode node) throws CoreException {
		
		ITypedElement left= node.getLeft();
		if (left instanceof BufferedResourceNode)
			((BufferedResourceNode) left).commit(pm);
			
		ITypedElement right= node.getRight();
		if (right instanceof BufferedResourceNode)
			((BufferedResourceNode) right).commit(pm);

		IDiffElement[] children= node.getChildren();
		if (children != null) {
			for (int i= 0; i < children.length; i++) {
				IDiffElement element= children[i];
				if (element instanceof DiffNode)
					commit(pm, (DiffNode) element);
			}
		}
	}
	
	private void createPath(DiffContainer root, IContainer folder, IPath path, Diff diff, boolean reject) {
		if (path.segmentCount() > 1) {
			IFolder f= folder.getFolder(path.uptoSegment(1));
			IDiffElement child= root.findChild(path.segment(0));
			if (child == null) {
				ResourceNode rn= new ResourceNode(f);
				child= new DiffNode(root, Differencer.CHANGE, null, rn, rn);
			}
			if (child instanceof DiffContainer)
				createPath((DiffContainer)child, f, path.removeFirstSegments(1), diff, reject);
		} else {
			// a leaf
			BufferedResourceNode rn= new BufferedResourceNode(folder.getFile(path));
			if (reject) {
				new Rejected(root, path.segment(0), diff);
			} else {
				new DiffNode(root, diff.getType(), null, rn, new PatchedResource(rn, diff, path, fPatcher));
			}					
		}
	}
}


Back to the top