|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 IBM Corporation and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * IBM Corporation - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.compare.internal; |
| 15 | + |
| 16 | +import java.io.ByteArrayInputStream; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStream; |
| 19 | +import java.lang.reflect.InvocationTargetException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | + |
| 22 | +import org.eclipse.compare.CompareConfiguration; |
| 23 | +import org.eclipse.compare.CompareEditorInput; |
| 24 | +import org.eclipse.compare.CompareUI; |
| 25 | +import org.eclipse.compare.IStreamContentAccessor; |
| 26 | +import org.eclipse.compare.ITypedElement; |
| 27 | +import org.eclipse.compare.structuremergeviewer.DiffNode; |
| 28 | +import org.eclipse.core.resources.IFile; |
| 29 | +import org.eclipse.core.runtime.CoreException; |
| 30 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 31 | +import org.eclipse.jface.dialogs.MessageDialog; |
| 32 | +import org.eclipse.jface.text.ITextSelection; |
| 33 | +import org.eclipse.jface.viewers.ISelection; |
| 34 | +import org.eclipse.swt.dnd.Clipboard; |
| 35 | +import org.eclipse.swt.dnd.TextTransfer; |
| 36 | +import org.eclipse.swt.graphics.Image; |
| 37 | +import org.eclipse.swt.widgets.Display; |
| 38 | +import org.eclipse.swt.widgets.Shell; |
| 39 | +import org.eclipse.ui.IEditorInput; |
| 40 | +import org.eclipse.ui.IEditorPart; |
| 41 | +import org.eclipse.ui.IFileEditorInput; |
| 42 | +import org.eclipse.ui.IWorkbenchPage; |
| 43 | +import org.eclipse.ui.PlatformUI; |
| 44 | +import org.eclipse.ui.texteditor.ITextEditor; |
| 45 | + |
| 46 | +public class ClipboardCompare extends BaseCompareAction{ |
| 47 | + |
| 48 | + private String clipboard = "Clipboard"; //$NON-NLS-1$ |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void run(ISelection selection) { |
| 52 | + IFile[] files = Utilities.getFiles(selection); |
| 53 | + for (IFile file : files) { |
| 54 | + try { |
| 55 | + processComparison(file); |
| 56 | + } catch (Exception e) { |
| 57 | + Shell parentShell = CompareUIPlugin.getShell(); |
| 58 | + MessageDialog.openError(parentShell, "Comparision Failed", e.getMessage()); //$NON-NLS-1$ |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + @Override |
| 63 | + protected boolean isEnabled(ISelection selection) { |
| 64 | + return Utilities.getFiles(selection).length == 1 && getClipboard() != null; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Process comparison with selection or entire editor contents with contents in |
| 69 | + * clipboard |
| 70 | + * |
| 71 | + * @param file Editor file |
| 72 | + * @throws IOException, CoreException |
| 73 | + */ |
| 74 | + private void processComparison(IFile file) throws IOException, CoreException { |
| 75 | + String cb = getClipboard().toString(); |
| 76 | + String fileName = file.getName(); |
| 77 | + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
| 78 | + IEditorPart editor = page.getActiveEditor(); |
| 79 | + IEditorInput input = editor.getEditorInput(); |
| 80 | + if (input instanceof IFileEditorInput ed) { |
| 81 | + IFile file2 = ed.getFile(); |
| 82 | + String fileName2 = file2.getName(); |
| 83 | + if (!file.getName().equals(fileName2)) { |
| 84 | + String fileContents = new String(file.getContents().readAllBytes(), file.getCharset()); |
| 85 | + showComparison(fileContents, fileName, cb); |
| 86 | + return; |
| 87 | + } |
| 88 | + } |
| 89 | + final String selectionContents; |
| 90 | + if (editor instanceof ITextEditor txtEditor) { |
| 91 | + ISelection selection = txtEditor.getSelectionProvider().getSelection(); |
| 92 | + if (selection instanceof ITextSelection textSelection) { |
| 93 | + selectionContents = textSelection.getText(); |
| 94 | + if (selectionContents.isEmpty()) { |
| 95 | + String fileContents = new String(file.getContents().readAllBytes(), file.getCharset()); |
| 96 | + showComparison(fileContents, fileName, cb); |
| 97 | + } else { |
| 98 | + showComparison(selectionContents, fileName, cb); |
| 99 | + } |
| 100 | + return; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Shows comparison result |
| 107 | + * |
| 108 | + * @param source Either selection from current editor or entire |
| 109 | + * editor if no selection |
| 110 | + * @param fileName Editor file name |
| 111 | + * @param clipboardContents Contents in clipboard |
| 112 | + */ |
| 113 | + private void showComparison(String source, String fileName, String clipboardContents) { |
| 114 | + class ClipboardTypedElement implements ITypedElement, IStreamContentAccessor { |
| 115 | + private final String name; |
| 116 | + private final String content; |
| 117 | + |
| 118 | + public ClipboardTypedElement(String name, String content) { |
| 119 | + this.name = name; |
| 120 | + this.content = content; |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public String getName() { |
| 125 | + return name; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public Image getImage() { |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public String getType() { |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public InputStream getContents() throws CoreException { |
| 140 | + return new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)); |
| 141 | + } |
| 142 | + |
| 143 | + } |
| 144 | + CompareConfiguration config = new CompareConfiguration(); |
| 145 | + config.setLeftLabel(fileName); |
| 146 | + config.setRightLabel(clipboard); |
| 147 | + config.setLeftEditable(true); |
| 148 | + config.setRightEditable(true); |
| 149 | + CompareEditorInput compareInput = new CompareEditorInput(config) { |
| 150 | + @Override |
| 151 | + protected Object prepareInput(IProgressMonitor monitor) |
| 152 | + throws InvocationTargetException, InterruptedException { |
| 153 | + return new DiffNode(new ClipboardTypedElement(fileName, source), |
| 154 | + new ClipboardTypedElement(clipboard, clipboardContents)); |
| 155 | + |
| 156 | + } |
| 157 | + }; |
| 158 | + CompareUI.openCompareEditor(compareInput); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Returns Clipboard Object or null if there is nothing in clipboard |
| 163 | + * |
| 164 | + * @returns Clipboard Object or null |
| 165 | + */ |
| 166 | + private Object getClipboard() { |
| 167 | + Clipboard clip = new Clipboard(Display.getDefault()); |
| 168 | + return clip.getContents(TextTransfer.getInstance()); |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments