22
22
import org .eclipse .compare .CompareConfiguration ;
23
23
import org .eclipse .compare .CompareEditorInput ;
24
24
import org .eclipse .compare .CompareUI ;
25
+ import org .eclipse .compare .IEditableContent ;
25
26
import org .eclipse .compare .IStreamContentAccessor ;
26
27
import org .eclipse .compare .ITypedElement ;
28
+ import org .eclipse .compare .ResourceNode ;
27
29
import org .eclipse .compare .structuremergeviewer .DiffNode ;
28
30
import org .eclipse .core .resources .IFile ;
31
+ import org .eclipse .core .resources .IResource ;
29
32
import org .eclipse .core .runtime .CoreException ;
30
33
import org .eclipse .core .runtime .IProgressMonitor ;
31
34
import org .eclipse .jface .action .IAction ;
@@ -50,15 +53,18 @@ public class ClipboardCompare extends BaseCompareAction implements IObjectAction
50
53
private String clipboard = "Clipboard" ; //$NON-NLS-1$
51
54
private String compareFailed = "Comparision Failed" ; //$NON-NLS-1$
52
55
56
+ private IFile currentResouce ;
57
+
53
58
private IWorkbenchPart activePart ;
54
59
55
60
@ Override
56
61
protected void run (ISelection selection ) {
57
62
IFile [] files = Utilities .getFiles (selection );
58
63
Shell parentShell = CompareUIPlugin .getShell ();
59
64
for (IFile file : files ) {
65
+ currentResouce = file ;
60
66
try {
61
- processComparison (file , parentShell );
67
+ processComparison (parentShell );
62
68
} catch (Exception e ) {
63
69
MessageDialog .openError (parentShell , compareFailed , e .getMessage ());
64
70
}
@@ -73,17 +79,16 @@ protected boolean isEnabled(ISelection selection) {
73
79
* Process comparison with selection or entire editor contents with contents in
74
80
* clipboard
75
81
*
76
- * @param file Editor file
77
82
* @param parentShell The shell containing this window's controls
78
83
* @throws IOException, CoreException
79
84
*/
80
- private void processComparison (IFile file , Shell parentShell ) throws IOException , CoreException {
85
+ private void processComparison (Shell parentShell ) throws IOException , CoreException {
81
86
String cb = getClipboard ().toString ();
82
- String fileName = file .getName ();
87
+ String fileName = currentResouce .getName ();
83
88
IWorkbenchPage page = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getActivePage ();
84
89
IEditorPart editor = page .getActiveEditor ();
85
90
if (activePart instanceof IViewPart ) {
86
- String fileContents = new String (file .getContents ().readAllBytes (), file .getCharset ());
91
+ String fileContents = new String (currentResouce .getContents ().readAllBytes (), currentResouce .getCharset ());
87
92
showComparison (fileContents , fileName , cb , parentShell );
88
93
return ;
89
94
}
@@ -93,7 +98,8 @@ private void processComparison(IFile file, Shell parentShell) throws IOException
93
98
if (selection instanceof ITextSelection textSelection ) {
94
99
selectionContents = textSelection .getText ();
95
100
if (selectionContents .isEmpty ()) {
96
- String fileContents = new String (file .getContents ().readAllBytes (), file .getCharset ());
101
+ String fileContents = new String (currentResouce .getContents ().readAllBytes (),
102
+ currentResouce .getCharset ());
97
103
showComparison (fileContents , fileName , cb , parentShell );
98
104
} else {
99
105
showComparison (selectionContents , fileName , cb , parentShell );
@@ -105,7 +111,8 @@ private void processComparison(IFile file, Shell parentShell) throws IOException
105
111
ISelection selection = existingCompare .getSite ().getSelectionProvider ().getSelection ();
106
112
if (selection instanceof ITextSelection textSelection ) {
107
113
String selectedText = textSelection .getText ();
108
- String fileContents = new String (file .getContents ().readAllBytes (), file .getCharset ());
114
+ String fileContents = new String (currentResouce .getContents ().readAllBytes (),
115
+ currentResouce .getCharset ());
109
116
showComparison (fileContents , fileName , selectedText , parentShell );
110
117
}
111
118
}
@@ -151,6 +158,26 @@ public InputStream getContents() throws CoreException {
151
158
}
152
159
153
160
}
161
+ class EditableFileNode extends ResourceNode implements IEditableContent {
162
+ public EditableFileNode (IFile file ) {
163
+ super (file );
164
+ }
165
+
166
+ @ Override
167
+ public void setContent (byte [] newContent ) {
168
+ try {
169
+ ((IFile ) getResource ()).setContents (new ByteArrayInputStream (newContent ),
170
+ IResource .FORCE | IResource .KEEP_HISTORY , null );
171
+ } catch (CoreException e ) {
172
+ MessageDialog .openError (parentShell , compareFailed , e .getMessage ());
173
+ }
174
+ }
175
+
176
+ @ Override
177
+ public boolean isEditable () {
178
+ return true ;
179
+ }
180
+ }
154
181
if (source == null ) {
155
182
MessageDialog .openInformation (parentShell , compareFailed , "Failed to process selected file" ); //$NON-NLS-1$
156
183
return ;
@@ -164,11 +191,13 @@ public InputStream getContents() throws CoreException {
164
191
@ Override
165
192
protected Object prepareInput (IProgressMonitor monitor )
166
193
throws InvocationTargetException , InterruptedException {
167
- return new DiffNode (new ClipboardTypedElement (fileName , source ),
168
- new ClipboardTypedElement (clipboard , clipboardContents ));
194
+ ITypedElement left = new EditableFileNode (currentResouce );
195
+ ITypedElement right = new ClipboardTypedElement (clipboard , clipboardContents );
196
+ return new DiffNode (left , right );
169
197
170
198
}
171
199
};
200
+ compareInput .setTitle (currentResouce .getName ());
172
201
CompareUI .openCompareEditor (compareInput );
173
202
}
174
203
0 commit comments