Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2008 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 |
* Krzysztof Poglodzinski (intuicje@gmail.com) - initial API and implementation |
10 |
* Mariusz Tanski (mariusztanski@gmail.com) - initial API and implementation |
11 |
* Kacper Zdanowicz (kacper.zdanowicz@gmail.com) - initial API and implementation |
12 |
*******************************************************************************/ |
13 |
package org.eclipse.compare.internal; |
14 |
|
15 |
|
16 |
import java.io.InputStream; |
17 |
|
18 |
import org.eclipse.compare.CompareConfiguration; |
19 |
import org.eclipse.compare.contentmergeviewer.ITokenComparator; |
20 |
import org.eclipse.compare.contentmergeviewer.TokenComparator; |
21 |
import org.eclipse.compare.internal.merge.DocumentMerger; |
22 |
import org.eclipse.compare.internal.merge.DocumentMerger.IDocumentMergerInput; |
23 |
import org.eclipse.core.resources.IFile; |
24 |
import org.eclipse.core.resources.IResource; |
25 |
import org.eclipse.jface.action.IAction; |
26 |
import org.eclipse.jface.text.Document; |
27 |
import org.eclipse.jface.text.IDocument; |
28 |
import org.eclipse.jface.text.Position; |
29 |
import org.eclipse.jface.viewers.ISelection; |
30 |
import org.eclipse.swt.widgets.Shell; |
31 |
import org.eclipse.ui.IObjectActionDelegate; |
32 |
import org.eclipse.ui.IWorkbenchPage; |
33 |
import org.eclipse.ui.IWorkbenchPart; |
34 |
|
35 |
|
36 |
/* |
37 |
* The "Generate diff" action |
38 |
*/ |
39 |
public class GenerateDiffAction extends BaseCompareAction implements IObjectActionDelegate { |
40 |
|
41 |
protected ResourceCompareInput fInput; |
42 |
protected IWorkbenchPage fWorkbenchPage; |
43 |
protected boolean showSelectAncestorDialog = true; |
44 |
|
45 |
public void run(ISelection selection) { |
46 |
|
47 |
IResource[] ir = Utilities.getResources(selection); |
48 |
Shell shell = fWorkbenchPage.getWorkbenchWindow().getShell(); |
49 |
boolean ok = fInput.setSelection(selection,shell, showSelectAncestorDialog); |
50 |
if (!ok) return; |
51 |
|
52 |
DocumentMerger merger = null; |
53 |
if(ir[0] instanceof IFile && ir[1] instanceof IFile) { |
54 |
merger = createDocumentMerger((IFile)ir[0], (IFile)ir[1]); |
55 |
merger.getCompareConfiguration().setLeftLabel(ir[0].getFullPath().toString()); |
56 |
merger.getCompareConfiguration().setRightLabel(ir[1].getFullPath().toString()); |
57 |
GenerateDiffFileWizard.run(merger, shell, true); |
58 |
} |
59 |
|
60 |
|
61 |
} |
62 |
|
63 |
protected boolean isEnabled(ISelection selection) { |
64 |
if (fInput == null) { |
65 |
CompareConfiguration cc= new CompareConfiguration(); |
66 |
cc.setProperty(CompareEditor.CONFIRM_SAVE_PROPERTY, new Boolean(false)); |
67 |
fInput= new ResourceCompareInput(cc); |
68 |
} |
69 |
return fInput.isEnabled(selection); |
70 |
} |
71 |
|
72 |
public void setActivePart(IAction action, IWorkbenchPart targetPart) { |
73 |
fWorkbenchPage= targetPart.getSite().getPage(); |
74 |
} |
75 |
|
76 |
private DocumentMerger createDocumentMerger(IFile leftFile, IFile rightFile){ |
77 |
IDocument leftDoc = convertFileToDocument(leftFile); |
78 |
IDocument rightDoc = convertFileToDocument(rightFile); |
79 |
if (leftDoc == null) { |
80 |
leftDoc = new Document(); |
81 |
} |
82 |
if (rightDoc == null) { |
83 |
rightDoc = new Document(); |
84 |
} |
85 |
DocumentMerger docMerger; |
86 |
docMerger = new DocumentMerger(new LocalDiffMergerInput(leftDoc, rightDoc)); |
87 |
try { |
88 |
docMerger.doDiff(); |
89 |
} catch (Exception e) { |
90 |
throw new RuntimeException(e); |
91 |
} |
92 |
return docMerger; |
93 |
} |
94 |
|
95 |
private IDocument convertFileToDocument(IFile file) { |
96 |
if(file == null) |
97 |
return null; |
98 |
StringBuffer str = new StringBuffer(); |
99 |
int c; |
100 |
try{ |
101 |
InputStream inputStream = file.getContents(); |
102 |
while ((c = inputStream.read())!=-1){ |
103 |
str.append((char)c); |
104 |
} |
105 |
inputStream.close(); |
106 |
return new Document(str.toString()); |
107 |
}catch(Exception ex){ |
108 |
CompareUIPlugin.log(ex); |
109 |
} |
110 |
|
111 |
return null; |
112 |
} |
113 |
|
114 |
private class LocalDiffMergerInput implements IDocumentMergerInput { |
115 |
|
116 |
private IDocument leftDoc; |
117 |
private IDocument rightDoc; |
118 |
|
119 |
public LocalDiffMergerInput(IDocument leftDoc, IDocument rightDoc) { |
120 |
this.leftDoc = leftDoc ; |
121 |
this.rightDoc= rightDoc; |
122 |
} |
123 |
|
124 |
public ITokenComparator createTokenComparator(String line) { |
125 |
return new TokenComparator(line); |
126 |
} |
127 |
public CompareConfiguration getCompareConfiguration() { |
128 |
return fInput.getCompareConfiguration(); |
129 |
} |
130 |
public IDocument getDocument(char contributor) { |
131 |
switch (contributor) { |
132 |
case MergeViewerContentProvider.LEFT_CONTRIBUTOR: |
133 |
return leftDoc; |
134 |
case MergeViewerContentProvider.RIGHT_CONTRIBUTOR: |
135 |
return rightDoc; |
136 |
case MergeViewerContentProvider.ANCESTOR_CONTRIBUTOR: |
137 |
return null; |
138 |
} |
139 |
return null; |
140 |
} |
141 |
public int getHunkStart() { |
142 |
return 0; |
143 |
} |
144 |
public Position getRegion(char contributor) { |
145 |
switch (contributor) { |
146 |
case MergeViewerContentProvider.LEFT_CONTRIBUTOR: |
147 |
return new Position(0, leftDoc.getLength()); |
148 |
case MergeViewerContentProvider.RIGHT_CONTRIBUTOR: |
149 |
return new Position(0, rightDoc.getLength()); |
150 |
case MergeViewerContentProvider.ANCESTOR_CONTRIBUTOR: |
151 |
return new Position(0, 0); |
152 |
} |
153 |
return null; |
154 |
} |
155 |
public boolean isHunkOnLeft() { |
156 |
return false; |
157 |
} |
158 |
public boolean isIgnoreAncestor() { |
159 |
return true; |
160 |
} |
161 |
public boolean isPatchHunk() { |
162 |
return false; |
163 |
} |
164 |
|
165 |
public boolean isShowPseudoConflicts() { |
166 |
return false; |
167 |
} |
168 |
public boolean isThreeWay() { |
169 |
return false; |
170 |
} |
171 |
public boolean isPatchHunkOk() { |
172 |
return false; |
173 |
} |
174 |
|
175 |
} |
176 |
|
177 |
} |
178 |
|