1313 *******************************************************************************/
1414package org .eclipse .compare .internal ;
1515
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 .IEncodedStreamContentAccessor ;
26- import org .eclipse .compare .ITypedElement ;
27- import org .eclipse .compare .structuremergeviewer .DiffNode ;
2816import org .eclipse .core .resources .IFile ;
29- import org .eclipse .core .runtime .CoreException ;
30- import org .eclipse .core .runtime .IProgressMonitor ;
31- import org .eclipse .jface .action .IAction ;
3217import org .eclipse .jface .dialogs .MessageDialog ;
33- import org .eclipse .jface .text .ITextSelection ;
3418import org .eclipse .jface .viewers .ISelection ;
35- import org .eclipse .osgi .util .NLS ;
36- import org .eclipse .swt .dnd .Clipboard ;
37- import org .eclipse .swt .dnd .TextTransfer ;
38- import org .eclipse .swt .graphics .Image ;
39- import org .eclipse .swt .widgets .Display ;
4019import org .eclipse .swt .widgets .Shell ;
41- import org .eclipse .ui .IEditorPart ;
42- import org .eclipse .ui .IObjectActionDelegate ;
43- import org .eclipse .ui .IViewPart ;
44- import org .eclipse .ui .IWorkbenchPage ;
45- import org .eclipse .ui .IWorkbenchPart ;
46- import org .eclipse .ui .PlatformUI ;
47- import org .eclipse .ui .forms .editor .FormEditor ;
48- import org .eclipse .ui .texteditor .ITextEditor ;
49-
50- public class ClipboardCompare extends BaseCompareAction implements IObjectActionDelegate {
5120
52- private final String clipboard = "Clipboard" ; //$NON-NLS-1$
53- private final String compareFailed = "Comparision Failed" ; //$NON-NLS-1$
5421
55- private IWorkbenchPart activePart ;
22+ public class ClipboardCompare extends BaseCompareAction {
5623
5724 @ Override
5825 protected void run (ISelection selection ) {
5926 IFile [] files = Utilities .getFiles (selection );
6027 Shell parentShell = CompareUIPlugin .getShell ();
6128 for (IFile file : files ) {
6229 try {
63- processComparison (file , parentShell );
30+ ClipboardCompareProcess pro = new ClipboardCompareProcess ();
31+ pro .processComparison (parentShell , file );
6432 } catch (Exception e ) {
65- MessageDialog .openError (parentShell , compareFailed , e .getMessage ());
33+ MessageDialog .openError (parentShell , "Comparison Failed" , e .getMessage ()); //$NON-NLS-1$
6634 }
6735 }
6836 }
69- @ Override
70- protected boolean isEnabled (ISelection selection ) {
71- return Utilities .getFiles (selection ).length == 1 && getClipboard () != null ;
72- }
73-
74- /**
75- * Process comparison with selection or entire editor contents with contents in
76- * clipboard
77- *
78- * @param file Editor file
79- * @param parentShell The shell containing this window's controls
80- * @throws IOException, CoreException
81- */
82- private void processComparison (IFile file , Shell parentShell ) throws IOException , CoreException {
83- String cb = getClipboard ().toString ();
84- String fileName = file .getName ();
85- IWorkbenchPage page = PlatformUI .getWorkbench ().getActiveWorkbenchWindow ().getActivePage ();
86- IEditorPart editor = page .getActiveEditor ();
87- if (activePart instanceof IViewPart ) {
88- String fileContents = new String (file .getContents ().readAllBytes (), file .getCharset ());
89- showComparison (fileContents , fileName , cb , parentShell );
90- return ;
91- }
92- final String selectionContents ;
93- if (editor instanceof FormEditor fromEditor ) {
94- editor = fromEditor .getActiveEditor ();
95- }
96- if (editor instanceof ITextEditor txtEditor ) {
97- ISelection selection = txtEditor .getSelectionProvider ().getSelection ();
98- if (selection instanceof ITextSelection textSelection ) {
99- selectionContents = textSelection .getText ();
100- if (selectionContents .isEmpty ()) {
101- String fileContents = new String (file .getContents ().readAllBytes (), file .getCharset ());
102- showComparison (fileContents , fileName , cb , parentShell );
103- } else {
104- showComparison (selectionContents , fileName , cb , parentShell );
105- }
106- return ;
107- }
108- }
109- if (editor instanceof CompareEditor existingCompare ) { // if selection is from compare editor itself
110- ISelection selection = existingCompare .getSite ().getSelectionProvider ().getSelection ();
111- if (selection instanceof ITextSelection textSelection ) {
112- String selectedText = textSelection .getText ();
113- String fileContents = new String (file .getContents ().readAllBytes (), file .getCharset ());
114- showComparison (fileContents , fileName , selectedText , parentShell );
115- }
116- }
117- }
118-
119- /**
120- * Shows comparison result
121- *
122- * @param source Either selection from current editor or entire
123- * editor if no selection
124- * @param fileName Editor file name
125- * @param clipboardContents Contents in clipboard
126- * @param parentShell The shell containing this window's controls
127- */
128- private void showComparison (String source , String fileName , String clipboardContents , Shell parentShell ) {
129- class ClipboardTypedElement implements ITypedElement , IEncodedStreamContentAccessor {
130- private final String name ;
131- private final String content ;
132-
133- public ClipboardTypedElement (String name , String content ) {
134- this .name = name ;
135- this .content = content ;
136- }
137-
138- @ Override
139- public String getName () {
140- return name ;
141- }
142-
143- @ Override
144- public Image getImage () {
145- return null ;
146- }
147-
148- @ Override
149- public String getType () {
150- return null ;
151- }
152-
153- @ Override
154- public String getCharset () throws CoreException {
155- return "UTF-8" ; //$NON-NLS-1$
156- }
157-
158- @ Override
159- public InputStream getContents () throws CoreException {
160- return new ByteArrayInputStream (content .getBytes (StandardCharsets .UTF_8 ));
161- }
162-
163- }
164- if (source == null ) {
165- MessageDialog .openInformation (parentShell , compareFailed , "Failed to process selected file" ); //$NON-NLS-1$
166- return ;
167- }
168- CompareConfiguration config = new CompareConfiguration ();
169- config .setLeftLabel (fileName );
170- config .setRightLabel (clipboard );
171- config .setLeftEditable (true );
172- config .setRightEditable (true );
173- CompareEditorInput compareInput = new CompareEditorInput (config ) {
174- @ Override
175- protected Object prepareInput (IProgressMonitor monitor )
176- throws InvocationTargetException , InterruptedException {
177- return new DiffNode (new ClipboardTypedElement (fileName , source ),
178- new ClipboardTypedElement (clipboard , clipboardContents ));
179-
180- }
181- };
182- compareInput .setTitle (NLS .bind (CompareMessages .CompareWithClipboardTitle , fileName ));
183- CompareUI .openCompareEditor (compareInput );
184- }
185-
186- /**
187- * Returns Clipboard Object or null if there is nothing in clipboard
188- *
189- * @returns Clipboard Object or null
190- */
191- private Object getClipboard () {
192- Clipboard clip = new Clipboard (Display .getDefault ());
193- try {
194- return clip .getContents (TextTransfer .getInstance ());
195- } finally {
196- clip .dispose ();
197- }
198- }
19937
20038 @ Override
201- public void setActivePart ( IAction action , IWorkbenchPart targetPart ) {
202- this . activePart = targetPart ;
39+ protected boolean isEnabled ( ISelection selection ) {
40+ return Utilities . getFiles ( selection ). length == 1 && ClipboardCompareProcess . getClipboard () != null ;
20341 }
204-
205- }
42+ }
0 commit comments