Skip to content

Commit ac391fe

Browse files
committed
Support for modifying source in compare clipboard editor
1 parent 2fe0e8d commit ac391fe

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

team/bundles/org.eclipse.compare/compare/org/eclipse/compare/internal/ClipboardCompare.java

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222
import org.eclipse.compare.CompareConfiguration;
2323
import org.eclipse.compare.CompareEditorInput;
2424
import org.eclipse.compare.CompareUI;
25+
import org.eclipse.compare.IEditableContent;
2526
import org.eclipse.compare.IStreamContentAccessor;
2627
import org.eclipse.compare.ITypedElement;
28+
import org.eclipse.compare.ResourceNode;
2729
import org.eclipse.compare.structuremergeviewer.DiffNode;
2830
import org.eclipse.core.resources.IFile;
31+
import org.eclipse.core.resources.IResource;
2932
import org.eclipse.core.runtime.CoreException;
3033
import org.eclipse.core.runtime.IProgressMonitor;
3134
import org.eclipse.jface.action.IAction;
@@ -50,15 +53,18 @@ public class ClipboardCompare extends BaseCompareAction implements IObjectAction
5053
private String clipboard = "Clipboard"; //$NON-NLS-1$
5154
private String compareFailed = "Comparision Failed"; //$NON-NLS-1$
5255

56+
private IFile currentResouce;
57+
5358
private IWorkbenchPart activePart;
5459

5560
@Override
5661
protected void run(ISelection selection) {
5762
IFile[] files = Utilities.getFiles(selection);
5863
Shell parentShell = CompareUIPlugin.getShell();
5964
for (IFile file : files) {
65+
currentResouce = file;
6066
try {
61-
processComparison(file, parentShell);
67+
processComparison(parentShell);
6268
} catch (Exception e) {
6369
MessageDialog.openError(parentShell, compareFailed, e.getMessage());
6470
}
@@ -73,17 +79,16 @@ protected boolean isEnabled(ISelection selection) {
7379
* Process comparison with selection or entire editor contents with contents in
7480
* clipboard
7581
*
76-
* @param file Editor file
7782
* @param parentShell The shell containing this window's controls
7883
* @throws IOException, CoreException
7984
*/
80-
private void processComparison(IFile file, Shell parentShell) throws IOException, CoreException {
85+
private void processComparison(Shell parentShell) throws IOException, CoreException {
8186
String cb = getClipboard().toString();
82-
String fileName = file.getName();
87+
String fileName = currentResouce.getName();
8388
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
8489
IEditorPart editor = page.getActiveEditor();
8590
if (activePart instanceof IViewPart) {
86-
String fileContents = new String(file.getContents().readAllBytes(), file.getCharset());
91+
String fileContents = new String(currentResouce.getContents().readAllBytes(), currentResouce.getCharset());
8792
showComparison(fileContents, fileName, cb, parentShell);
8893
return;
8994
}
@@ -93,7 +98,8 @@ private void processComparison(IFile file, Shell parentShell) throws IOException
9398
if (selection instanceof ITextSelection textSelection) {
9499
selectionContents = textSelection.getText();
95100
if (selectionContents.isEmpty()) {
96-
String fileContents = new String(file.getContents().readAllBytes(), file.getCharset());
101+
String fileContents = new String(currentResouce.getContents().readAllBytes(),
102+
currentResouce.getCharset());
97103
showComparison(fileContents, fileName, cb, parentShell);
98104
} else {
99105
showComparison(selectionContents, fileName, cb, parentShell);
@@ -105,7 +111,8 @@ private void processComparison(IFile file, Shell parentShell) throws IOException
105111
ISelection selection = existingCompare.getSite().getSelectionProvider().getSelection();
106112
if (selection instanceof ITextSelection textSelection) {
107113
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());
109116
showComparison(fileContents, fileName, selectedText, parentShell);
110117
}
111118
}
@@ -151,6 +158,26 @@ public InputStream getContents() throws CoreException {
151158
}
152159

153160
}
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+
}
154181
if (source == null) {
155182
MessageDialog.openInformation(parentShell, compareFailed, "Failed to process selected file"); //$NON-NLS-1$
156183
return;
@@ -164,11 +191,13 @@ public InputStream getContents() throws CoreException {
164191
@Override
165192
protected Object prepareInput(IProgressMonitor monitor)
166193
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);
169197

170198
}
171199
};
200+
compareInput.setTitle(currentResouce.getName());
172201
CompareUI.openCompareEditor(compareInput);
173202
}
174203

0 commit comments

Comments
 (0)