1 11 package org.eclipse.compare.internal; 12 13 import java.io.*; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.net.URL ; 16 import java.util.*; 17 import java.util.List ; 18 19 import org.eclipse.compare.*; 20 import org.eclipse.compare.structuremergeviewer.*; 21 import org.eclipse.core.runtime.*; 22 import org.eclipse.core.runtime.content.IContentType; 23 import org.eclipse.core.runtime.content.IContentTypeManager; 24 import org.eclipse.jface.dialogs.MessageDialog; 25 import org.eclipse.jface.operation.IRunnableContext; 26 import org.eclipse.jface.preference.IPreferenceStore; 27 import org.eclipse.jface.resource.ImageDescriptor; 28 import org.eclipse.jface.util.IPropertyChangeListener; 29 import org.eclipse.jface.util.PropertyChangeEvent; 30 import org.eclipse.jface.viewers.Viewer; 31 import org.eclipse.swt.graphics.Image; 32 import org.eclipse.swt.widgets.*; 33 import org.eclipse.ui.*; 34 import org.eclipse.ui.model.IWorkbenchAdapter; 35 import org.eclipse.ui.plugin.AbstractUIPlugin; 36 import org.osgi.framework.BundleContext; 37 38 39 53 public final class CompareUIPlugin extends AbstractUIPlugin { 54 55 static class CompareRegistry { 56 57 private final static String ID_ATTRIBUTE= "id"; private final static String EXTENSIONS_ATTRIBUTE= "extensions"; private final static String CONTENT_TYPE_ID_ATTRIBUTE= "contentTypeId"; 61 private HashMap fIdMap; private HashMap fExtensionMap; private HashMap fContentTypeBindings; 65 66 void register(IConfigurationElement element, Object data) { 67 String id= element.getAttribute(ID_ATTRIBUTE); 68 if (id != null) { 69 if (fIdMap == null) 70 fIdMap= new HashMap(); 71 fIdMap.put(id, data); 72 } 73 74 String types= element.getAttribute(EXTENSIONS_ATTRIBUTE); 75 if (types != null) { 76 if (fExtensionMap == null) 77 fExtensionMap= new HashMap(); 78 StringTokenizer tokenizer= new StringTokenizer(types, ","); while (tokenizer.hasMoreElements()) { 80 String extension= tokenizer.nextToken().trim(); 81 fExtensionMap.put(normalizeCase(extension), data); 82 } 83 } 84 } 85 86 void createBinding(IConfigurationElement element, String idAttributeName) { 87 String type= element.getAttribute(CONTENT_TYPE_ID_ATTRIBUTE); 88 String id= element.getAttribute(idAttributeName); 89 if (id == null) 90 logErrorMessage(Utilities.getFormattedString("CompareUIPlugin.targetIdAttributeMissing", idAttributeName)); if (type != null && id != null && fIdMap != null) { 92 Object o= fIdMap.get(id); 93 if (o != null) { 94 IContentType ct= fgContentTypeManager.getContentType(type); 95 if (ct != null) { 96 if (fContentTypeBindings == null) 97 fContentTypeBindings= new HashMap(); 98 fContentTypeBindings.put(ct, o); 99 } else { 100 logErrorMessage(Utilities.getFormattedString("CompareUIPlugin.contentTypeNotFound", type)); } 102 } else { 103 logErrorMessage(Utilities.getFormattedString("CompareUIPlugin.targetNotFound", id)); } 105 } 106 } 107 108 Object search(IContentType type) { 109 if (fContentTypeBindings != null) { 110 for (; type != null; type= type.getBaseType()) { 111 Object data= fContentTypeBindings.get(type); 112 if (data != null) 113 return data; 114 } 115 } 116 return null; 117 } 118 119 Object search(String extension) { 120 if (fExtensionMap != null) 121 return fExtensionMap.get(normalizeCase(extension)); 122 return null; 123 } 124 } 125 126 127 public static final int INTERNAL_ERROR= 1; 128 129 private static boolean NORMALIZE_CASE= true; 130 131 public static final String PLUGIN_ID= "org.eclipse.compare"; 133 private static final String BINARY_TYPE= "binary"; 135 private static final String STREAM_MERGER_EXTENSION_POINT= "streamMergers"; private static final String STREAM_MERGER= "streamMerger"; private static final String STREAM_MERGER_ID_ATTRIBUTE= "streamMergerId"; private static final String STRUCTURE_CREATOR_EXTENSION_POINT= "structureCreators"; private static final String STRUCTURE_CREATOR= "structureCreator"; private static final String STRUCTURE_CREATOR_ID_ATTRIBUTE= "structureCreatorId"; 142 private static final String VIEWER_TAG= "viewer"; private static final String STRUCTURE_MERGE_VIEWER_EXTENSION_POINT= "structureMergeViewers"; private static final String STRUCTURE_MERGE_VIEWER_ID_ATTRIBUTE= "structureMergeViewerId"; private static final String CONTENT_MERGE_VIEWER_EXTENSION_POINT= "contentMergeViewers"; private static final String CONTENT_MERGE_VIEWER_ID_ATTRIBUTE= "contentMergeViewerId"; private static final String CONTENT_VIEWER_EXTENSION_POINT= "contentViewers"; private static final String CONTENT_VIEWER_ID_ATTRIBUTE= "contentViewerId"; 150 private static final String CONTENT_TYPE_BINDING= "contentTypeBinding"; 152 153 private static final String COMPARE_EDITOR= PLUGIN_ID + ".CompareEditor"; 155 private static final String STRUCTUREVIEWER_ALIASES_PREFERENCE_NAME= "StructureViewerAliases"; 157 private static final IContentTypeManager fgContentTypeManager= Platform.getContentTypeManager(); 159 160 public static final int NO_DIFFERENCE = 10000; 161 162 165 private static CompareUIPlugin fgComparePlugin; 166 167 168 private static Map fgImages= new Hashtable(10); 169 170 private static Map fgImageDescriptors= new Hashtable(10); 171 172 private static Map fgImages2= new Hashtable(10); 173 174 private static List fgDisposeOnShutdownImages= new ArrayList(); 175 176 private ResourceBundle fResourceBundle; 177 178 private boolean fRegistriesInitialized; 179 private CompareRegistry fStreamMergers= new CompareRegistry(); 180 private CompareRegistry fStructureCreators= new CompareRegistry(); 181 private CompareRegistry fStructureMergeViewers= new CompareRegistry(); 182 private CompareRegistry fContentViewers= new CompareRegistry(); 183 private CompareRegistry fContentMergeViewers= new CompareRegistry(); 184 185 private Map fStructureViewerAliases; 186 private CompareFilter fFilter; 187 private IPropertyChangeListener fPropertyChangeListener; 188 private boolean useOldDifferencer = false; 189 190 198 public CompareUIPlugin() { 199 super(); 200 Assert.isTrue(fgComparePlugin == null); 201 fgComparePlugin= this; 202 } 203 204 public void start(BundleContext context) throws Exception { 205 super.start(context); 206 } 207 208 public void stop(BundleContext context) throws Exception { 209 210 IPreferenceStore ps= getPreferenceStore(); 211 rememberAliases(ps); 212 if (fPropertyChangeListener != null) { 213 ps.removePropertyChangeListener(fPropertyChangeListener); 214 fPropertyChangeListener= null; 215 } 216 217 super.stop(context); 218 219 if (fgDisposeOnShutdownImages != null) { 220 Iterator i= fgDisposeOnShutdownImages.iterator(); 221 while (i.hasNext()) { 222 Image img= (Image) i.next(); 223 if (!img.isDisposed()) 224 img.dispose(); 225 } 226 fgImages= null; 227 } 228 } 229 230 235 public static CompareUIPlugin getDefault() { 236 return fgComparePlugin; 237 } 238 239 244 public ResourceBundle getResourceBundle() { 245 if (fResourceBundle == null) 246 fResourceBundle= Platform.getResourceBundle(getBundle()); 247 return fResourceBundle; 248 } 249 250 255 public static String getPluginId() { 256 return getDefault().getBundle().getSymbolicName(); 257 } 258 259 private void initializeRegistries() { 260 if (!fRegistriesInitialized) { 261 registerExtensions(); 262 fRegistriesInitialized= true; 263 } 264 } 265 266 270 private void registerExtensions() { 271 IExtensionRegistry registry= Platform.getExtensionRegistry(); 272 273 IConfigurationElement[] elements= registry.getConfigurationElementsFor(PLUGIN_ID, STREAM_MERGER_EXTENSION_POINT); 275 for (int i= 0; i < elements.length; i++) { 276 IConfigurationElement element= elements[i]; 277 if (STREAM_MERGER.equals(element.getName())) 278 fStreamMergers.register(element, new StreamMergerDescriptor(element)); 279 } 280 for (int i= 0; i < elements.length; i++) { 281 IConfigurationElement element= elements[i]; 282 if (CONTENT_TYPE_BINDING.equals(element.getName())) 283 fStreamMergers.createBinding(element, STREAM_MERGER_ID_ATTRIBUTE); 284 } 285 286 elements= registry.getConfigurationElementsFor(PLUGIN_ID, STRUCTURE_CREATOR_EXTENSION_POINT); 288 for (int i= 0; i < elements.length; i++) { 289 IConfigurationElement element= elements[i]; 290 String name= element.getName(); 291 if (!CONTENT_TYPE_BINDING.equals(name)) { 292 if (!STRUCTURE_CREATOR.equals(name)) 293 logErrorMessage(Utilities.getFormattedString("CompareUIPlugin.unexpectedTag", name, STRUCTURE_CREATOR)); fStructureCreators.register(element, new StructureCreatorDescriptor(element)); 295 } 296 } 297 for (int i= 0; i < elements.length; i++) { 298 IConfigurationElement element= elements[i]; 299 if (CONTENT_TYPE_BINDING.equals(element.getName())) 300 fStructureCreators.createBinding(element, STRUCTURE_CREATOR_ID_ATTRIBUTE); 301 } 302 303 elements= registry.getConfigurationElementsFor(PLUGIN_ID, STRUCTURE_MERGE_VIEWER_EXTENSION_POINT); 305 for (int i= 0; i < elements.length; i++) { 306 IConfigurationElement element= elements[i]; 307 String name= element.getName(); 308 if (!CONTENT_TYPE_BINDING.equals(name)) { 309 if (!VIEWER_TAG.equals(name)) 310 logErrorMessage(Utilities.getFormattedString("CompareUIPlugin.unexpectedTag", name, VIEWER_TAG)); fStructureMergeViewers.register(element, new ViewerDescriptor(element)); 312 } 313 } 314 for (int i= 0; i < elements.length; i++) { 315 IConfigurationElement element= elements[i]; 316 if (CONTENT_TYPE_BINDING.equals(element.getName())) 317 fStructureMergeViewers.createBinding(element, STRUCTURE_MERGE_VIEWER_ID_ATTRIBUTE); 318 } 319 320 elements= registry.getConfigurationElementsFor(PLUGIN_ID, CONTENT_MERGE_VIEWER_EXTENSION_POINT); 322 for (int i= 0; i < elements.length; i++) { 323 IConfigurationElement element= elements[i]; 324 String name= element.getName(); 325 if (!CONTENT_TYPE_BINDING.equals(name)) { 326 if (!VIEWER_TAG.equals(name)) 327 logErrorMessage(Utilities.getFormattedString("CompareUIPlugin.unexpectedTag", name, VIEWER_TAG)); fContentMergeViewers.register(element, new ViewerDescriptor(element)); 329 } 330 } 331 for (int i= 0; i < elements.length; i++) { 332 IConfigurationElement element= elements[i]; 333 if (CONTENT_TYPE_BINDING.equals(element.getName())) 334 fContentMergeViewers.createBinding(element, CONTENT_MERGE_VIEWER_ID_ATTRIBUTE); 335 } 336 337 elements= registry.getConfigurationElementsFor(PLUGIN_ID, CONTENT_VIEWER_EXTENSION_POINT); 339 for (int i= 0; i < elements.length; i++) { 340 IConfigurationElement element= elements[i]; 341 String name= element.getName(); 342 if (!CONTENT_TYPE_BINDING.equals(name)) { 343 if (!VIEWER_TAG.equals(name)) 344 logErrorMessage(Utilities.getFormattedString("CompareUIPlugin.unexpectedTag", name, VIEWER_TAG)); fContentViewers.register(element, new ViewerDescriptor(element)); 346 } 347 } 348 for (int i= 0; i < elements.length; i++) { 349 IConfigurationElement element= elements[i]; 350 if (CONTENT_TYPE_BINDING.equals(element.getName())) 351 fContentViewers.createBinding(element, CONTENT_VIEWER_ID_ATTRIBUTE); 352 } 353 } 354 355 public static IWorkbench getActiveWorkbench() { 356 CompareUIPlugin plugin= getDefault(); 357 if (plugin == null) 358 return null; 359 return plugin.getWorkbench(); 360 } 361 362 public static IWorkbenchWindow getActiveWorkbenchWindow() { 363 IWorkbench workbench= getActiveWorkbench(); 364 if (workbench == null) 365 return null; 366 return workbench.getActiveWorkbenchWindow(); 367 } 368 369 376 private static IWorkbenchPage getActivePage() { 377 IWorkbenchWindow window= getActiveWorkbenchWindow(); 378 if (window == null) 379 return null; 380 return window.getActivePage(); 381 } 382 383 390 public static Shell getShell() { 391 IWorkbenchWindow window= getActiveWorkbenchWindow(); 392 if (window == null) 393 return null; 394 return window.getShell(); 395 } 396 397 402 public static void disposeOnShutdown(Image image) { 403 if (image != null) 404 fgDisposeOnShutdownImages.add(image); 405 } 406 407 416 public void openCompareEditor(final CompareEditorInput input, final IWorkbenchPage page, final IReusableEditor editor) { 417 CompareConfiguration configuration = input.getCompareConfiguration(); 418 if (configuration != null) { 419 IPreferenceStore ps= configuration.getPreferenceStore(); 420 if (ps != null) 421 configuration.setProperty( 422 CompareConfiguration.USE_OUTLINE_VIEW, 423 Boolean.valueOf(ps.getBoolean(ComparePreferencePage.USE_OUTLINE_VIEW))); 424 } 425 if (input.canRunAsJob()) { 426 openEditorInBackground(input, page, editor); 427 } else { 428 if (compareResultOK(input, null)) { 429 internalOpenEditor(input, page, editor); 430 } 431 } 432 } 433 434 private void openEditorInBackground(final CompareEditorInput input, 435 final IWorkbenchPage page, final IReusableEditor editor) { 436 internalOpenEditor(input, page, editor); 437 } 438 439 private void internalOpenEditor(final CompareEditorInput input, 440 final IWorkbenchPage wp, final IReusableEditor editor) { 441 Runnable runnable = new Runnable () { 442 public void run() { 443 if (editor != null && !editor.getSite().getShell().isDisposed()) { editor.setInput(input); 445 return; 446 } 447 448 IWorkbenchPage page = wp; 449 if (page == null) 450 page= getActivePage(); 451 if (page != null) { 452 try { 454 page.openEditor(input, COMPARE_EDITOR); 455 } catch (PartInitException e) { 456 MessageDialog.openError(getShell(), Utilities.getString("CompareUIPlugin.openEditorError"), e.getMessage()); } 458 } else { 459 MessageDialog.openError(getShell(), 460 Utilities.getString("CompareUIPlugin.openEditorError"), Utilities.getString("CompareUIPlugin.noActiveWorkbenchPage")); } 463 } 464 }; 465 syncExec(runnable); 466 } 467 468 475 public void openCompareDialog(final CompareEditorInput input) { 476 if (compareResultOK(input, null)) { 478 internalOpenDialog(input); 479 } 480 } 481 482 public IStatus prepareInput(CompareEditorInput input, IProgressMonitor monitor) { 483 try { 484 input.run(monitor); 485 String message= input.getMessage(); 486 if (message != null) { 487 return new Status(IStatus.ERROR, CompareUIPlugin.PLUGIN_ID, 0, message, null); 488 } 489 if (input.getCompareResult() == null) { 490 return new Status(IStatus.ERROR, CompareUIPlugin.PLUGIN_ID, NO_DIFFERENCE, Utilities.getString("CompareUIPlugin.noDifferences"), null); } 492 return Status.OK_STATUS; 493 } catch (InterruptedException e) { 494 throw new OperationCanceledException(); 495 } catch (InvocationTargetException e) { 496 return new Status(IStatus.ERROR, CompareUIPlugin.PLUGIN_ID, 0, Utilities.getString("CompareUIPlugin.compareFailed"), e.getTargetException()); } 498 } 499 500 503 public boolean compareResultOK(CompareEditorInput input, IRunnableContext context) { 504 final Shell shell= getShell(); 505 try { 506 507 if (context == null) 509 context = PlatformUI.getWorkbench().getProgressService(); 510 context.run(true, true, input); 511 512 String message= input.getMessage(); 513 if (message != null) { 514 MessageDialog.openError(shell, Utilities.getString("CompareUIPlugin.compareFailed"), message); return false; 516 } 517 518 if (input.getCompareResult() == null) { 519 MessageDialog.openInformation(shell, Utilities.getString("CompareUIPlugin.dialogTitle"), Utilities.getString("CompareUIPlugin.noDifferences")); return false; 521 } 522 523 return true; 524 525 } catch (InterruptedException x) { 526 } catch (InvocationTargetException x) { 528 MessageDialog.openError(shell, Utilities.getString("CompareUIPlugin.compareFailed"), x.getTargetException().getMessage()); } 530 return false; 531 } 532 533 536 private static void registerImage(String type, Image image, boolean dispose) { 537 fgImages.put(normalizeCase(type), image); 538 if (image != null && dispose) { 539 fgDisposeOnShutdownImages.add(image); 540 } 541 } 542 543 549 public static void registerImageDescriptor(String type, ImageDescriptor descriptor) { 550 fgImageDescriptors.put(normalizeCase(type), descriptor); 551 } 552 553 public static ImageDescriptor getImageDescriptor(String relativePath) { 554 if (fgComparePlugin == null) 555 return null; 556 IPath path= Utilities.getIconPath(null).append(relativePath); 557 URL url= FileLocator.find(fgComparePlugin.getBundle(), path, null); 558 if (url == null) 559 return null; 560 return ImageDescriptor.createFromURL(url); 561 } 562 563 575 public static Image getImage(String type) { 576 577 type= normalizeCase(type); 578 579 boolean dispose= false; 580 Image image= null; 581 if (type != null) 582 image= (Image) fgImages.get(type); 583 if (image == null) { 584 ImageDescriptor id= (ImageDescriptor) fgImageDescriptors.get(type); 585 if (id != null) { 586 image= id.createImage(); 587 dispose= true; 588 } 589 590 if (image == null) { 591 if (fgComparePlugin != null) { 592 if (ITypedElement.FOLDER_TYPE.equals(type)) { 593 image= getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER); 594 } else { 596 image= createWorkbenchImage(type); 597 dispose= true; 598 } 599 } else { 600 id= (ImageDescriptor) fgImageDescriptors.get(normalizeCase("file")); image= id.createImage(); 602 dispose= true; 603 } 604 } 605 if (image != null) 606 registerImage(type, image, dispose); 607 } 608 return image; 609 } 610 611 625 public static Image getImage(IAdaptable adaptable) { 626 if (adaptable != null) { 627 Object o= adaptable.getAdapter(IWorkbenchAdapter.class); 628 if (o instanceof IWorkbenchAdapter) { 629 ImageDescriptor id= ((IWorkbenchAdapter) o).getImageDescriptor(adaptable); 630 if (id != null) { 631 Image image= (Image)fgImages2.get(id); 632 if (image == null) { 633 image= id.createImage(); 634 try { 635 fgImages2.put(id, image); 636 } catch (NullPointerException ex) { 637 } 639 fgDisposeOnShutdownImages.add(image); 640 641 } 642 return image; 643 } 644 } 645 } 646 return null; 647 } 648 649 private static Image createWorkbenchImage(String type) { 650 IEditorRegistry er= getDefault().getWorkbench().getEditorRegistry(); 651 ImageDescriptor id= er.getImageDescriptor("foo." + type); return id.createImage(); 653 } 654 655 662 public StructureCreatorDescriptor getStructureCreator(String type) { 663 initializeRegistries(); 664 return (StructureCreatorDescriptor) fStructureCreators.search(type); 665 } 666 667 674 public IStreamMerger createStreamMerger(String type) { 675 initializeRegistries(); 676 StreamMergerDescriptor descriptor= (StreamMergerDescriptor) fStreamMergers.search(type); 677 if (descriptor != null) 678 return descriptor.createStreamMerger(); 679 return null; 680 } 681 682 689 public IStreamMerger createStreamMerger(IContentType type) { 690 initializeRegistries(); 691 StreamMergerDescriptor descriptor= (StreamMergerDescriptor) fStreamMergers.search(type); 692 if (descriptor != null) 693 return descriptor.createStreamMerger(); 694 return null; 695 } 696 697 710 public Viewer findStructureViewer(Viewer oldViewer, ICompareInput input, Composite parent, 711 CompareConfiguration configuration) { 712 713 if (input.getLeft() == null || input.getRight() == null) return null; 715 716 IContentType ctype= getCommonType(input); 718 if (ctype != null) { 719 initializeRegistries(); 720 Viewer viewer= getViewer(fStructureMergeViewers.search(ctype), oldViewer, parent, configuration); 721 if (viewer != null) 722 return viewer; 723 } 724 725 String [] types= getTypes(input); 727 String type= null; 728 if (isHomogenous(types)) { 729 type= normalizeCase(types[0]); 730 initializeRegistries(); 731 IViewerDescriptor vd= (IViewerDescriptor) fStructureMergeViewers.search(type); 732 if (vd == null) { 733 String alias= getStructureViewerAlias(type); 734 if (alias != null) 735 vd= (IViewerDescriptor) fStructureMergeViewers.search(alias); 736 } 737 if (vd != null) 738 return vd.createViewer(oldViewer, parent, configuration); 739 } 740 741 744 StructureCreatorDescriptor scc= null; 745 initializeRegistries(); 746 Object desc= fStructureCreators.search(ctype); if (desc instanceof StructureCreatorDescriptor) 748 scc= (StructureCreatorDescriptor) desc; 749 if (scc == null && type != null) 750 scc= getStructureCreator(type); if (scc != null) { 752 IStructureCreator sc= scc.createStructureCreator(); 753 if (sc != null) { 754 StructureDiffViewer sdv= new StructureDiffViewer(parent, configuration); 755 sdv.setStructureCreator(sc); 756 return sdv; 757 } 758 } 759 return null; 760 } 761 762 775 public Viewer findContentViewer(Viewer oldViewer, Object in, Composite parent, CompareConfiguration cc) { 776 777 if (in instanceof IStreamContentAccessor) { 778 String type= ITypedElement.TEXT_TYPE; 779 780 if (in instanceof ITypedElement) { 781 ITypedElement tin= (ITypedElement) in; 782 783 IContentType ct= getContentType(tin); 784 if (ct != null) { 785 initializeRegistries(); 786 Viewer viewer= getViewer(fContentViewers.search(ct), oldViewer, parent, cc); 787 if (viewer != null) 788 return viewer; 789 } 790 791 String ty= tin.getType(); 792 if (ty != null) 793 type= ty; 794 } 795 796 initializeRegistries(); 797 Viewer viewer= getViewer(fContentViewers.search(type), oldViewer, parent, cc); 798 if (viewer != null) 799 return viewer; 800 return new SimpleTextViewer(parent); 802 } 803 804 if (!(in instanceof ICompareInput)) 805 return null; 806 807 ICompareInput input= (ICompareInput) in; 808 809 810 IContentType ctype= getCommonType(input); 811 if (isCompareAsText(input, cc)) { 812 ctype = Platform.getContentTypeManager().getContentType(IContentTypeManager.CT_TEXT); 813 } 814 if (ctype != null) { 815 initializeRegistries(); 816 Viewer viewer= getViewer(fContentMergeViewers.search(ctype), oldViewer, parent, cc); 817 if (viewer != null) 818 return viewer; 819 } 820 821 String [] types= getTypes(input); 822 String type= null; 823 if (isHomogenous(types)) 824 type= types[0]; 825 826 if (ITypedElement.FOLDER_TYPE.equals(type)) 827 return null; 828 829 if (type == null) { 830 int n= 0; 831 for (int i= 0; i < types.length; i++) 832 if (!ITypedElement.UNKNOWN_TYPE.equals(types[i])) { 833 n++; 834 if (type == null) 835 type= types[i]; } 837 if (n > 1) type= null; 839 } 840 841 if (type != null) { 842 initializeRegistries(); 843 Viewer viewer= getViewer(fContentMergeViewers.search(type), oldViewer, parent, cc); 844 if (viewer != null) 845 return viewer; 846 } 847 848 String leftType= guessType(input.getLeft()); 850 String rightType= guessType(input.getRight()); 851 852 if (leftType != null || rightType != null) { 853 boolean right_text= rightType != null && ITypedElement.TEXT_TYPE.equals(rightType); 854 boolean left_text= leftType != null && ITypedElement.TEXT_TYPE.equals(leftType); 855 if ((leftType == null && right_text) || (left_text && rightType == null) || (left_text && right_text)) 856 type= ITypedElement.TEXT_TYPE; 857 else 858 type= BINARY_TYPE; 859 860 initializeRegistries(); 861 IViewerDescriptor vd= (IViewerDescriptor) fContentMergeViewers.search(type); 862 if (vd != null) 863 return vd.createViewer(oldViewer, parent, cc); 864 } 865 return null; 866 } 867 868 private boolean isCompareAsText(ICompareInput input, CompareConfiguration cc) { 869 Set set = (Set)cc.getProperty(ICompareAsText.PROP_TEXT_INPUTS); 870 if (set == null) 871 return false; 872 return set.contains(input); 873 } 874 875 private static Viewer getViewer(Object descriptor, Viewer oldViewer, Composite parent, CompareConfiguration cc) { 876 if (descriptor instanceof IViewerDescriptor) 877 return ((IViewerDescriptor)descriptor).createViewer(oldViewer, parent, cc); 878 return null; 879 } 880 881 private static String [] getTypes(ICompareInput input) { 882 ITypedElement ancestor= input.getAncestor(); 883 ITypedElement left= input.getLeft(); 884 ITypedElement right= input.getRight(); 885 886 ArrayList tmp= new ArrayList(); 887 if (ancestor != null) { 888 String type= ancestor.getType(); 889 if (type != null) 890 tmp.add(normalizeCase(type)); 891 } 892 if (left != null) { 893 String type= left.getType(); 894 if (type != null) 895 tmp.add(normalizeCase(type)); 896 } 897 if (right != null) { 898 String type= right.getType(); 899 if (type != null) 900 tmp.add(normalizeCase(type)); 901 } 902 return (String []) tmp.toArray(new String [tmp.size()]); 903 } 904 905 private static IContentType getContentType(ITypedElement element) { 906 if (element == null) 907 return null; 908 String name= element.getName(); 909 IContentType ct= null; 910 if (element instanceof IStreamContentAccessor) { 911 IStreamContentAccessor isa= (IStreamContentAccessor) element; 912 try { 913 InputStream is= isa.getContents(); 914 if (is != null) { 915 InputStream bis= new BufferedInputStream(is); 916 try { 917 ct= fgContentTypeManager.findContentTypeFor(is, name); 918 } catch (IOException e) { 919 } finally { 921 try { 922 bis.close(); 923 } catch (IOException e2) { 924 } 926 } 927 } 928 } catch (CoreException e1) { 929 } 931 } 932 if (ct == null) 933 ct= fgContentTypeManager.findContentTypeFor(name); 934 return ct; 935 } 936 937 940 private static boolean isHomogenous(String [] types) { 941 switch (types.length) { 942 case 1: 943 return true; 944 case 2: 945 return types[0].equals(types[1]); 946 case 3: 947 return types[0].equals(types[1]) && types[1].equals(types[2]); 948 } 949 return false; 950 } 951 952 955 private static IContentType getCommonType(ICompareInput input) { 956 957 ITypedElement ancestor= input.getAncestor(); 958 ITypedElement left= input.getLeft(); 959 ITypedElement right= input.getRight(); 960 961 int n= 0; 962 IContentType[] types= new IContentType[3]; 963 IContentType type= null; 964 965 if (ancestor != null) { 966 type= getContentType(ancestor); 967 if (type != null) 968 types[n++]= type; 969 } 970 type= getContentType(left); 971 if (type != null) 972 types[n++]= type; 973 else 974 return null; 975 type= getContentType(right); 976 if (type != null) 977 types[n++]= type; 978 else 979 return null; 980 981 IContentType result= null; 982 IContentType[] s0, s1, s2; 983 switch (n) { 984 case 0: 985 return null; 986 case 1: 987 return types[0]; 988 case 2: 989 if (types[0].equals(types[1])) 990 return types[0]; 991 s0= toFullPath(types[0]); 992 s1= toFullPath(types[1]); 993 for (int i= 0; i < Math.min(s0.length, s1.length); i++) { 994 if (!s0[i].equals(s1[i])) 995 break; 996 result= s0[i]; 997 } 998 return result; 999 case 3: 1000 if (types[0].equals(types[1]) && types[1].equals(types[2])) 1001 return types[0]; 1002 s0= toFullPath(types[0]); 1003 s1= toFullPath(types[1]); 1004 s2= toFullPath(types[2]); 1005 for (int i= 0; i < Math.min(Math.min(s0.length, s1.length), s2.length); i++) { 1006 if (!s0[i].equals(s1[i]) || !s1[i].equals(s2[i])) 1007 break; 1008 result= s0[i]; 1009 } 1010 return result; 1011 } 1012 return null; 1013 } 1014 1015 private static IContentType[] toFullPath(IContentType ct) { 1016 List l= new ArrayList(); 1017 for (; ct != null; ct= ct.getBaseType()) 1018 l.add(0, ct); 1019 return (IContentType[]) l.toArray(new IContentType[l.size()]); 1020 } 1021 1022 1028 private static String guessType(ITypedElement input) { 1029 if (input instanceof IStreamContentAccessor) { 1030 IStreamContentAccessor sca= (IStreamContentAccessor) input; 1031 InputStream is= null; 1032 try { 1033 is= sca.getContents(); 1034 if (is == null) 1035 return null; 1036 int lineLength= 0; 1037 int lines= 0; 1038 while (lines < 10) { 1039 int c= is.read(); 1040 if (c == -1) break; 1042 if (c == '\n' || c == '\r') { lineLength= 0; 1044 lines++; 1045 } else 1046 lineLength++; 1047 if (lineLength > 1000) 1048 return ITypedElement.UNKNOWN_TYPE; 1049 } 1050 return ITypedElement.TEXT_TYPE; 1051 } catch (CoreException ex) { 1052 } catch (IOException ex) { 1054 } finally { 1056 if (is != null) { 1057 try { 1058 is.close(); 1059 } catch (IOException ex) { 1060 } 1062 } 1063 } 1064 return ITypedElement.UNKNOWN_TYPE; 1065 } 1066 return null; 1067 } 1068 1069 private static String normalizeCase(String s) { 1070 if (NORMALIZE_CASE && s != null) 1071 return s.toUpperCase(); 1072 return s; 1073 } 1074 1075 1077 private String getStructureViewerAlias(String type) { 1078 return (String ) getStructureViewerAliases().get(type); 1079 } 1080 1081 public void addStructureViewerAlias(String type, String alias) { 1082 getStructureViewerAliases().put(normalizeCase(alias), normalizeCase(type)); 1083 } 1084 1085 private Map getStructureViewerAliases() { 1086 if (fStructureViewerAliases == null) { 1087 fStructureViewerAliases= new Hashtable(10); 1088 String aliases= getPreferenceStore().getString(STRUCTUREVIEWER_ALIASES_PREFERENCE_NAME); 1089 if (aliases != null && aliases.length() > 0) { 1090 StringTokenizer st= new StringTokenizer(aliases, " "); while (st.hasMoreTokens()) { 1092 String pair= st.nextToken(); 1093 int pos= pair.indexOf('.'); 1094 if (pos > 0) { 1095 String key= pair.substring(0, pos); 1096 String alias= pair.substring(pos+1); 1097 fStructureViewerAliases.put(key, alias); 1098 } 1099 } 1100 } 1101 } 1102 return fStructureViewerAliases; 1103 } 1104 1105 public void removeAllStructureViewerAliases(String type) { 1106 if (fStructureViewerAliases == null) 1107 return; 1108 String t= normalizeCase(type); 1109 Set entrySet= fStructureViewerAliases.entrySet(); 1110 for (Iterator iter= entrySet.iterator(); iter.hasNext(); ) { 1111 Map.Entry entry= (Map.Entry)iter.next(); 1112 if (entry.getValue().equals(t)) 1113 iter.remove(); 1114 } 1115 } 1116 1117 1123 private void rememberAliases(IPreferenceStore ps) { 1124 if (fStructureViewerAliases == null) 1125 return; 1126 StringBuffer buffer= new StringBuffer (); 1127 Iterator iter= fStructureViewerAliases.keySet().iterator(); 1128 while (iter.hasNext()) { 1129 String key= (String ) iter.next(); 1130 String alias= (String ) fStructureViewerAliases.get(key); 1131 buffer.append(key); 1132 buffer.append('.'); 1133 buffer.append(alias); 1134 buffer.append(' '); 1135 } 1136 ps.setValue(STRUCTUREVIEWER_ALIASES_PREFERENCE_NAME, buffer.toString()); 1137 } 1138 1139 1141 public boolean filter(String name, boolean isFolder, boolean isArchive) { 1142 if (fFilter == null) { 1143 fFilter= new CompareFilter(); 1144 final IPreferenceStore ps= getPreferenceStore(); 1145 fFilter.setFilters(ps.getString(ComparePreferencePage.PATH_FILTER)); 1146 fPropertyChangeListener= new IPropertyChangeListener() { 1147 public void propertyChange(PropertyChangeEvent event) { 1148 if (ComparePreferencePage.PATH_FILTER.equals(event.getProperty())) 1149 fFilter.setFilters(ps.getString(ComparePreferencePage.PATH_FILTER)); 1150 } 1151 }; 1152 ps.addPropertyChangeListener(fPropertyChangeListener); 1153 } 1154 return fFilter.filter(name, isFolder, isArchive); 1155 } 1156 1157 private void internalOpenDialog(final CompareEditorInput input) { 1158 Runnable runnable = new Runnable () { 1159 public void run() { 1160 CompareDialog dialog= new CompareDialog(getShell(), input); 1161 dialog.open(); 1162 } 1163 }; 1164 syncExec(runnable); 1165 } 1166 1167 private void syncExec(Runnable runnable) { 1168 if (Display.getCurrent() == null) { 1169 Display.getDefault().syncExec(runnable); 1170 } else { 1171 runnable.run(); 1172 } 1173 } 1174 1175 1177 protected void handleNoDifference() { 1178 Runnable runnable = new Runnable () { 1179 public void run() { 1180 MessageDialog.openInformation(getShell(), Utilities.getString("CompareUIPlugin.dialogTitle"), Utilities.getString("CompareUIPlugin.noDifferences")); } 1182 }; 1183 syncExec(runnable); 1184 } 1185 1186 1192 public static IEditorPart[] getDirtyEditors() { 1193 Set inputs= new HashSet(); 1194 List result= new ArrayList(0); 1195 IWorkbench workbench= getDefault().getWorkbench(); 1196 IWorkbenchWindow[] windows= workbench.getWorkbenchWindows(); 1197 for (int i= 0; i < windows.length; i++) { 1198 IWorkbenchPage[] pages= windows[i].getPages(); 1199 for (int x= 0; x < pages.length; x++) { 1200 IEditorPart[] editors= pages[x].getDirtyEditors(); 1201 for (int z= 0; z < editors.length; z++) { 1202 IEditorPart ep= editors[z]; 1203 IEditorInput input= ep.getEditorInput(); 1204 if (!inputs.contains(input)) { 1205 inputs.add(input); 1206 result.add(ep); 1207 } 1208 } 1209 } 1210 } 1211 return (IEditorPart[])result.toArray(new IEditorPart[result.size()]); 1212 } 1213 1214 public static void logErrorMessage(String message) { 1215 if (message == null) 1216 message= ""; log(new Status(IStatus.ERROR, getPluginId(), INTERNAL_ERROR, message, null)); 1218 } 1219 1220 public static void log(Throwable e) { 1221 log(new Status(IStatus.ERROR, getPluginId(), INTERNAL_ERROR, CompareMessages.ComparePlugin_internal_error, e)); 1222 } 1223 1224 public static void log(IStatus status) { 1225 getDefault().getLog().log(status); 1226 } 1227 1228 public boolean isUseOldDifferencer() { 1229 return useOldDifferencer; 1230 } 1231 1232 public void setUseOldDifferencer(boolean useOldDifferencer) { 1233 this.useOldDifferencer = useOldDifferencer; 1234 } 1235} 1236 | Popular Tags |