Lines 11-18
Link Here
|
11 |
*******************************************************************************/ |
11 |
*******************************************************************************/ |
12 |
package org.eclipse.compare.internal; |
12 |
package org.eclipse.compare.internal; |
13 |
|
13 |
|
|
|
14 |
import java.io.BufferedReader; |
15 |
import java.io.ByteArrayInputStream; |
14 |
import java.io.FileOutputStream; |
16 |
import java.io.FileOutputStream; |
15 |
import java.io.IOException; |
17 |
import java.io.IOException; |
|
|
18 |
import java.io.InputStream; |
19 |
import java.io.InputStreamReader; |
16 |
|
20 |
|
17 |
import org.eclipse.compare.CompareConfiguration; |
21 |
import org.eclipse.compare.CompareConfiguration; |
18 |
import org.eclipse.compare.CompareUI; |
22 |
import org.eclipse.compare.CompareUI; |
Lines 33-38
Link Here
|
33 |
import org.eclipse.jface.viewers.ISelection; |
37 |
import org.eclipse.jface.viewers.ISelection; |
34 |
import org.eclipse.jface.viewers.StructuredSelection; |
38 |
import org.eclipse.jface.viewers.StructuredSelection; |
35 |
import org.eclipse.swt.SWT; |
39 |
import org.eclipse.swt.SWT; |
|
|
40 |
import org.eclipse.swt.dnd.Clipboard; |
36 |
import org.eclipse.swt.dnd.DND; |
41 |
import org.eclipse.swt.dnd.DND; |
37 |
import org.eclipse.swt.dnd.DragSource; |
42 |
import org.eclipse.swt.dnd.DragSource; |
38 |
import org.eclipse.swt.dnd.DragSourceEvent; |
43 |
import org.eclipse.swt.dnd.DragSourceEvent; |
Lines 52-66
Link Here
|
52 |
import org.eclipse.swt.widgets.Composite; |
57 |
import org.eclipse.swt.widgets.Composite; |
53 |
import org.eclipse.swt.widgets.Control; |
58 |
import org.eclipse.swt.widgets.Control; |
54 |
import org.eclipse.swt.widgets.DirectoryDialog; |
59 |
import org.eclipse.swt.widgets.DirectoryDialog; |
|
|
60 |
import org.eclipse.swt.widgets.Display; |
55 |
import org.eclipse.swt.widgets.Event; |
61 |
import org.eclipse.swt.widgets.Event; |
56 |
import org.eclipse.swt.widgets.FileDialog; |
62 |
import org.eclipse.swt.widgets.FileDialog; |
57 |
import org.eclipse.swt.widgets.Group; |
63 |
import org.eclipse.swt.widgets.Group; |
58 |
import org.eclipse.swt.widgets.Listener; |
64 |
import org.eclipse.swt.widgets.Listener; |
59 |
import org.eclipse.swt.widgets.Shell; |
65 |
import org.eclipse.swt.widgets.Shell; |
60 |
import org.eclipse.swt.widgets.Text; |
66 |
import org.eclipse.swt.widgets.Text; |
|
|
67 |
import org.eclipse.ui.IEditorDescriptor; |
68 |
import org.eclipse.ui.IEditorReference; |
69 |
import org.eclipse.ui.IWorkbenchPage; |
70 |
import org.eclipse.ui.PartInitException; |
61 |
import org.eclipse.ui.forms.events.ExpansionAdapter; |
71 |
import org.eclipse.ui.forms.events.ExpansionAdapter; |
62 |
import org.eclipse.ui.forms.events.ExpansionEvent; |
72 |
import org.eclipse.ui.forms.events.ExpansionEvent; |
63 |
import org.eclipse.ui.forms.widgets.ExpandableComposite; |
73 |
import org.eclipse.ui.forms.widgets.ExpandableComposite; |
|
|
74 |
import org.eclipse.ui.part.FileEditorInput; |
64 |
import org.eclipse.ui.part.ResourceTransfer; |
75 |
import org.eclipse.ui.part.ResourceTransfer; |
65 |
|
76 |
|
66 |
/** |
77 |
/** |
Lines 363-368
Link Here
|
363 |
} |
374 |
} |
364 |
|
375 |
|
365 |
} |
376 |
} |
|
|
377 |
|
378 |
private class ClipboardContent extends ContentTypeElement { |
379 |
|
380 |
protected Button showInEditorButton; |
381 |
private Clipboard clipboard; |
382 |
|
383 |
public ClipboardContent(Composite parent, InternalSection section) { |
384 |
super(parent, CompareMessages.CompareWithOther_clipboardRadioButton, section); |
385 |
clipboard = new Clipboard(Display.getDefault()); |
386 |
} |
387 |
|
388 |
protected void createText(Composite parent) { |
389 |
text = new Text(parent, SWT.BORDER | SWT.MULTI); |
390 |
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); |
391 |
text.setEditable(false); |
392 |
} |
393 |
|
394 |
protected void createMainButton(Composite parent) { |
395 |
|
396 |
Composite buttonComposite = new Composite(parent, SWT.NONE); |
397 |
buttonComposite.setLayout(new GridLayout(1, false)); |
398 |
|
399 |
createRefreshButton(buttonComposite); |
400 |
createShowInEditorButton(buttonComposite); |
401 |
} |
402 |
|
403 |
protected void setEnabled(boolean enabled) { |
404 |
super.setEnabled(enabled); |
405 |
showInEditorButton.setEnabled(enabled); |
406 |
if (enabled) { |
407 |
String fileContent = clipboard.getContents(TextTransfer.getInstance()).toString(); |
408 |
text.setText(fileContent); |
409 |
setResource(createClipboardFile(fileContent, section.clipboardFileName)); |
410 |
} |
411 |
} |
412 |
|
413 |
private void createShowInEditorButton(Composite parent) { |
414 |
showInEditorButton = new Button(parent, SWT.PUSH); |
415 |
showInEditorButton.setText(CompareMessages.CompareWithOther_showInEditorButton); |
416 |
showInEditorButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); |
417 |
showInEditorButton.setEnabled(false); |
418 |
showInEditorButton.addSelectionListener(new SelectionListener() { |
419 |
public void widgetDefaultSelected(SelectionEvent e) { |
420 |
widgetSelected(e); |
421 |
} |
422 |
public void widgetSelected(SelectionEvent e) { |
423 |
IWorkbenchPage page = getWorkbenchPage(); |
424 |
String id = getEditorId(page); |
425 |
try { |
426 |
FileEditorInput input = new FileEditorInput((IFile)getResource()); |
427 |
page.openEditor(input, id); |
428 |
} catch (PartInitException e1) { |
429 |
CompareUIPlugin.log(e1); |
430 |
} |
431 |
} |
432 |
}); |
433 |
} |
434 |
|
435 |
private String getEditorId(IWorkbenchPage page) { |
436 |
IFile file = createClipboardFile("test content", "testFile"); //$NON-NLS-1$ //$NON-NLS-2$ // create empty clipboard file to get it's default editor |
437 |
IEditorDescriptor descriptor = CompareUI.getPlugin().getWorkbench().getEditorRegistry().getDefaultEditor(file.getName()); |
438 |
if (descriptor != null) |
439 |
return descriptor.getId(); |
440 |
return null; |
441 |
} |
442 |
|
443 |
private void createRefreshButton(Composite parent) { |
444 |
mainButton = new Button(parent, SWT.PUSH); |
445 |
mainButton.setText(CompareMessages.CompareWithOther_refreshButton); |
446 |
mainButton.setEnabled(false); |
447 |
mainButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false)); |
448 |
mainButton.addSelectionListener(new SelectionListener() { |
449 |
public void widgetDefaultSelected(SelectionEvent e) { |
450 |
widgetSelected(e); |
451 |
} |
452 |
public void widgetSelected(SelectionEvent e) { |
453 |
String newFileContent = clipboard.getContents(TextTransfer.getInstance()).toString(); |
454 |
IFile currentClipboardFile = section.getMyClipboardFile(); |
455 |
String contents = getFilesContent(currentClipboardFile); |
456 |
if (contents == null || !contents.equals(newFileContent)) { |
457 |
text.setText(newFileContent); |
458 |
setResource(createClipboardFile(newFileContent, section.clipboardFileName)); |
459 |
} |
460 |
} |
461 |
}); |
462 |
} |
463 |
|
464 |
private IFile createClipboardFile(String fileContent, String fileName) { |
465 |
IFile file = null; |
466 |
try { |
467 |
file = tmpProject.getClipboardFileHandler(fileName); |
468 |
if (file.exists()) |
469 |
file.delete(true, null); |
470 |
InputStream source = new ByteArrayInputStream(fileContent.getBytes()); |
471 |
file.create(source, IResource.NONE, null); |
472 |
} catch (CoreException e) { |
473 |
CompareUIPlugin.log(e); |
474 |
MessageDialog.openError(getShell(), |
475 |
CompareMessages.CompareWithOther_createTmpFile_title, |
476 |
CompareMessages.CompareWithOther_createTmpFile_message); |
477 |
return file; |
478 |
} |
479 |
return file; |
480 |
} |
481 |
|
482 |
private String getFilesContent(IFile file) { |
483 |
|
484 |
if (!file.exists()) |
485 |
return null; |
486 |
|
487 |
InputStream stream = null; |
488 |
|
489 |
try { |
490 |
stream = file.getContents(); |
491 |
} catch (CoreException e1) { |
492 |
CompareUIPlugin.log(e1); |
493 |
return null; |
494 |
} |
495 |
|
496 |
BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); |
497 |
StringBuffer buffer = new StringBuffer(); |
498 |
int c; |
499 |
|
500 |
try { |
501 |
while ((c = reader.read()) != -1) |
502 |
buffer.append((char)c); |
503 |
} catch (IOException e) { |
504 |
CompareUIPlugin.log(e); |
505 |
return null; // we don't need only a part of a content |
506 |
} finally { |
507 |
try { |
508 |
stream.close(); |
509 |
} catch (IOException e) { |
510 |
CompareUIPlugin.log(e); |
511 |
return null; |
512 |
} |
513 |
} |
514 |
return buffer.toString(); |
515 |
} |
516 |
} |
366 |
|
517 |
|
367 |
private abstract class InternalSection { |
518 |
private abstract class InternalSection { |
368 |
|
519 |
|
Lines 373-382
Link Here
|
373 |
|
524 |
|
374 |
protected Group group; |
525 |
protected Group group; |
375 |
private IResource resource; |
526 |
private IResource resource; |
|
|
527 |
private String clipboardFileName; |
376 |
|
528 |
|
377 |
ExternalFileContent externalFileContent; |
529 |
ExternalFileContent externalFileContent; |
378 |
ExternalFolderContent externalFolderContent; |
530 |
ExternalFolderContent externalFolderContent; |
379 |
WorkspaceContent workspaceContent; |
531 |
WorkspaceContent workspaceContent; |
|
|
532 |
ClipboardContent clipboardContent; |
380 |
|
533 |
|
381 |
public InternalSection(Composite parent) { |
534 |
public InternalSection(Composite parent) { |
382 |
createContents(parent); |
535 |
createContents(parent); |
Lines 385-390
Link Here
|
385 |
private InternalSection() { |
538 |
private InternalSection() { |
386 |
// not to instantiate |
539 |
// not to instantiate |
387 |
} |
540 |
} |
|
|
541 |
|
542 |
public IFile getMyClipboardFile() { |
543 |
try { |
544 |
IFile file = tmpProject.getClipboardFileHandler(clipboardFileName); |
545 |
return file; |
546 |
} catch (CoreException e) { |
547 |
return null; |
548 |
} |
549 |
} |
388 |
|
550 |
|
389 |
protected void createContents(Composite parent) { |
551 |
protected void createContents(Composite parent) { |
390 |
|
552 |
|
Lines 395-407
Link Here
|
395 |
workspaceContent = new WorkspaceContent(group, this); |
557 |
workspaceContent = new WorkspaceContent(group, this); |
396 |
externalFileContent = new ExternalFileContent(group, this); |
558 |
externalFileContent = new ExternalFileContent(group, this); |
397 |
externalFolderContent = new ExternalFolderContent(group, this); |
559 |
externalFolderContent = new ExternalFolderContent(group, this); |
|
|
560 |
clipboardContent = new ClipboardContent(group, this); |
398 |
|
561 |
|
399 |
addListenersToRadioButtons(); |
562 |
addListenersToRadioButtons(); |
400 |
} |
563 |
} |
401 |
|
564 |
|
402 |
private void addListenersToRadioButtons() { |
565 |
private void addListenersToRadioButtons() { |
403 |
final ContentTypeElement[] elements = new ContentTypeElement[] { workspaceContent, |
566 |
final ContentTypeElement[] elements = new ContentTypeElement[] { workspaceContent, |
404 |
externalFileContent, externalFolderContent }; |
567 |
externalFileContent, externalFolderContent, clipboardContent }; |
405 |
for (int i = 0; i < elements.length; i++) |
568 |
for (int i = 0; i < elements.length; i++) |
406 |
elements[i].getRadioButton().addListener(SWT.Selection, new Listener() { |
569 |
elements[i].getRadioButton().addListener(SWT.Selection, new Listener() { |
407 |
public void handleEvent(Event event) { |
570 |
public void handleEvent(Event event) { |
Lines 461-466
Link Here
|
461 |
externalFolderContent.setEnabled(true); |
624 |
externalFolderContent.setEnabled(true); |
462 |
} |
625 |
} |
463 |
} |
626 |
} |
|
|
627 |
|
628 |
protected void setClipboardFileName(String name) { |
629 |
this.clipboardFileName = name; |
630 |
} |
464 |
} |
631 |
} |
465 |
|
632 |
|
466 |
private class InternalGroup extends InternalSection { |
633 |
private class InternalGroup extends InternalSection { |
Lines 525-531
Link Here
|
525 |
} |
692 |
} |
526 |
} |
693 |
} |
527 |
|
694 |
|
528 |
private class ExternalResourcesProject { |
695 |
private class ExternalResourcesAndClipboardProject { |
529 |
|
696 |
|
530 |
// Implementation based on org.eclipse.jdt.internal.core.ExternalFoldersManager |
697 |
// Implementation based on org.eclipse.jdt.internal.core.ExternalFoldersManager |
531 |
|
698 |
|
Lines 544-552
Link Here
|
544 |
+ "\t<natures>\n" + "\t</natures>\n" //$NON-NLS-1$//$NON-NLS-2$ |
711 |
+ "\t<natures>\n" + "\t</natures>\n" //$NON-NLS-1$//$NON-NLS-2$ |
545 |
+ "</projectDescription>"; //$NON-NLS-1$ |
712 |
+ "</projectDescription>"; //$NON-NLS-1$ |
546 |
|
713 |
|
547 |
private final static String TMP_FOLDER_NAME = "tmpFolder"; //$NON-NLS-1$ |
714 |
private final static String EXTERNAL_FILES_FOLDER_NAME = "externalsFolder"; //$NON-NLS-1$ |
|
|
715 |
private final static String CLIPBOARD_FILES_FOLDER_NAME = "ClipboardFolder"; //$NON-NLS-1$ |
548 |
|
716 |
|
549 |
private ExternalResourcesProject() { |
717 |
private ExternalResourcesAndClipboardProject() { |
550 |
// nothing to do here |
718 |
// nothing to do here |
551 |
} |
719 |
} |
552 |
|
720 |
|
Lines 587-593
Link Here
|
587 |
} |
755 |
} |
588 |
|
756 |
|
589 |
private IFolder getTmpFolder(IProject project) throws CoreException { |
757 |
private IFolder getTmpFolder(IProject project) throws CoreException { |
590 |
IFolder folder = project.getFolder(TMP_FOLDER_NAME); |
758 |
IFolder folder = project.getFolder(EXTERNAL_FILES_FOLDER_NAME); |
591 |
if (!folder.exists()) |
759 |
if (!folder.exists()) |
592 |
folder.create(IResource.NONE, true, null); |
760 |
folder.create(IResource.NONE, true, null); |
593 |
return folder; |
761 |
return folder; |
Lines 666-678
Link Here
|
666 |
return ResourcesPlugin.getWorkspace().getRoot().getProject( |
834 |
return ResourcesPlugin.getWorkspace().getRoot().getProject( |
667 |
TMP_PROJECT_NAME); |
835 |
TMP_PROJECT_NAME); |
668 |
} |
836 |
} |
|
|
837 |
|
838 |
private IFile getClipboardFileHandler(String fileName) throws CoreException { |
839 |
IFile file = null; |
840 |
IProject project = createTmpProject(); |
841 |
if (!project.isOpen()) |
842 |
project.open(null); |
843 |
IFolder folder = project.getFolder(CLIPBOARD_FILES_FOLDER_NAME); |
844 |
if (!folder.exists()) |
845 |
folder.create(IResource.NONE, true, null); |
846 |
file = folder.getFile(fileName + ".txt"); //$NON-NLS-1$ |
847 |
return file; |
848 |
} |
669 |
} |
849 |
} |
670 |
|
850 |
|
671 |
private Button okButton; |
851 |
private Button okButton; |
672 |
private InternalGroup rightPanel, leftPanel; |
852 |
private InternalGroup rightPanel, leftPanel; |
673 |
private InternalExpandable ancestorPanel; |
853 |
private InternalExpandable ancestorPanel; |
674 |
private ISelection selection; |
854 |
private ISelection selection; |
675 |
private ExternalResourcesProject tmpProject = new ExternalResourcesProject(); |
855 |
private ExternalResourcesAndClipboardProject tmpProject = new ExternalResourcesAndClipboardProject(); |
676 |
|
856 |
|
677 |
/** |
857 |
/** |
678 |
* Creates the dialog. |
858 |
* Creates the dialog. |
Lines 705-720
Link Here
|
705 |
|
885 |
|
706 |
ancestorPanel = new InternalExpandable(mainPanel); |
886 |
ancestorPanel = new InternalExpandable(mainPanel); |
707 |
ancestorPanel.setText(CompareMessages.CompareWithOther_ancestor); |
887 |
ancestorPanel.setText(CompareMessages.CompareWithOther_ancestor); |
|
|
888 |
ancestorPanel.setClipboardFileName(CompareMessages.CompareWithOther_fileName_0); |
708 |
GridData ancestorGD = new GridData(SWT.FILL, SWT.FILL, true, false); |
889 |
GridData ancestorGD = new GridData(SWT.FILL, SWT.FILL, true, false); |
709 |
ancestorGD.horizontalSpan = 2; |
890 |
ancestorGD.horizontalSpan = 2; |
710 |
ancestorPanel.setLayoutData(ancestorGD); |
891 |
ancestorPanel.setLayoutData(ancestorGD); |
711 |
|
892 |
|
712 |
leftPanel = new InternalGroup(mainPanel); |
893 |
leftPanel = new InternalGroup(mainPanel); |
713 |
leftPanel.setText(CompareMessages.CompareWithOther_leftPanel); |
894 |
leftPanel.setText(CompareMessages.CompareWithOther_leftPanel); |
|
|
895 |
leftPanel.setClipboardFileName(CompareMessages.CompareWithOther_fileName_1); |
714 |
leftPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
896 |
leftPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
715 |
|
897 |
|
716 |
rightPanel = new InternalGroup(mainPanel); |
898 |
rightPanel = new InternalGroup(mainPanel); |
717 |
rightPanel.setText(CompareMessages.CompareWithOther_rightPanel); |
899 |
rightPanel.setText(CompareMessages.CompareWithOther_rightPanel); |
|
|
900 |
rightPanel.setClipboardFileName(CompareMessages.CompareWithOther_fileName_2); |
718 |
rightPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
901 |
rightPanel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false)); |
719 |
|
902 |
|
720 |
setSelection(selection); |
903 |
setSelection(selection); |
Lines 819-822
Link Here
|
819 |
rightResource }; |
1002 |
rightResource }; |
820 |
return resources; |
1003 |
return resources; |
821 |
} |
1004 |
} |
|
|
1005 |
|
1006 |
/** |
1007 |
* Removes temporary files created from clipboard's content or imported to |
1008 |
* workspace only for comparing. |
1009 |
*/ |
1010 |
public static void cleanup() { |
1011 |
try { |
1012 |
IProject project = ResourcesPlugin.getWorkspace().getRoot() |
1013 |
.getProject(ExternalResourcesAndClipboardProject.TMP_PROJECT_NAME); |
1014 |
project.delete(true, true, null); |
1015 |
} catch (CoreException e) { |
1016 |
CompareUIPlugin.log(e); |
1017 |
} |
1018 |
} |
1019 |
|
1020 |
private IWorkbenchPage getWorkbenchPage() { |
1021 |
return CompareUI.getPlugin().getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
1022 |
} |
1023 |
|
1024 |
private void closeEditorWithClipboard(IWorkbenchPage page) { |
1025 |
IEditorReference[] editors = page.getEditorReferences(); |
1026 |
String[] names = new String[] { CompareMessages.CompareWithOther_fileName_0, |
1027 |
CompareMessages.CompareWithOther_fileName_1, |
1028 |
CompareMessages.CompareWithOther_fileName_2 }; |
1029 |
for (int i = 0; i < editors.length; i++) |
1030 |
for (int j = 0; j < 3; j++) |
1031 |
try { |
1032 |
if (editors[i].getEditorInput().getName().equals(names[j] + ".txt")) //$NON-NLS-1$ |
1033 |
page.closeEditor(editors[i].getEditor(false), false); |
1034 |
} catch (PartInitException e) { |
1035 |
CompareUIPlugin.log(e); |
1036 |
} |
1037 |
} |
1038 |
|
1039 |
/** |
1040 |
* Closes all editor pages containing clipboard files before closing the dialog |
1041 |
* and setting its return code to OK. |
1042 |
*/ |
1043 |
public void okPressed() { |
1044 |
IWorkbenchPage page = getWorkbenchPage(); |
1045 |
closeEditorWithClipboard(page); |
1046 |
super.okPressed(); |
1047 |
} |
1048 |
|
1049 |
/** |
1050 |
* Closes all editor pages containing clipboard files before closing the dialog |
1051 |
* and setting its return code to CANCEL. |
1052 |
*/ |
1053 |
public void cancelPressed() { |
1054 |
IWorkbenchPage page = getWorkbenchPage(); |
1055 |
closeEditorWithClipboard(page); |
1056 |
super.cancelPressed(); |
1057 |
} |
1058 |
|
822 |
} |
1059 |
} |