1 11 12 package org.eclipse.ui.internal; 13 14 import java.io.OutputStream ; 15 import java.util.Collection ; 16 import java.util.HashSet ; 17 import java.util.Locale ; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IConfigurationElement; 21 import org.eclipse.core.runtime.IExtensionPoint; 22 import org.eclipse.core.runtime.IPath; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.Platform; 25 import org.eclipse.core.runtime.Status; 26 import org.eclipse.jface.preference.IPreferenceStore; 27 import org.eclipse.jface.preference.PreferenceManager; 28 import org.eclipse.jface.resource.ImageDescriptor; 29 import org.eclipse.jface.resource.ImageRegistry; 30 import org.eclipse.jface.window.Window; 31 import org.eclipse.swt.SWT; 32 import org.eclipse.swt.custom.BusyIndicator; 33 import org.eclipse.ui.IEditorRegistry; 34 import org.eclipse.ui.IElementFactory; 35 import org.eclipse.ui.IPerspectiveRegistry; 36 import org.eclipse.ui.ISharedImages; 37 import org.eclipse.ui.IWorkbench; 38 import org.eclipse.ui.IWorkingSetManager; 39 import org.eclipse.ui.PlatformUI; 40 import org.eclipse.ui.internal.StartupThreading.StartupRunnable; 41 import org.eclipse.ui.internal.decorators.DecoratorManager; 42 import org.eclipse.ui.internal.dialogs.WorkbenchPreferenceManager; 43 import org.eclipse.ui.internal.intro.IIntroRegistry; 44 import org.eclipse.ui.internal.intro.IntroRegistry; 45 import org.eclipse.ui.internal.misc.StatusUtil; 46 import org.eclipse.ui.internal.operations.WorkbenchOperationSupport; 47 import org.eclipse.ui.internal.progress.ProgressManager; 48 import org.eclipse.ui.internal.registry.ActionSetRegistry; 49 import org.eclipse.ui.internal.registry.EditorRegistry; 50 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 51 import org.eclipse.ui.internal.registry.PerspectiveRegistry; 52 import org.eclipse.ui.internal.registry.PreferencePageRegistryReader; 53 import org.eclipse.ui.internal.registry.ViewRegistry; 54 import org.eclipse.ui.internal.registry.WorkingSetRegistry; 55 import org.eclipse.ui.internal.themes.IThemeRegistry; 56 import org.eclipse.ui.internal.themes.ThemeRegistry; 57 import org.eclipse.ui.internal.themes.ThemeRegistryReader; 58 import org.eclipse.ui.internal.util.BundleUtility; 59 import org.eclipse.ui.internal.util.SWTResourceUtil; 60 import org.eclipse.ui.internal.wizards.ExportWizardRegistry; 61 import org.eclipse.ui.internal.wizards.ImportWizardRegistry; 62 import org.eclipse.ui.internal.wizards.NewWizardRegistry; 63 import org.eclipse.ui.operations.IWorkbenchOperationSupport; 64 import org.eclipse.ui.plugin.AbstractUIPlugin; 65 import org.eclipse.ui.presentations.AbstractPresentationFactory; 66 import org.eclipse.ui.views.IViewRegistry; 67 import org.eclipse.ui.wizards.IWizardRegistry; 68 import org.osgi.framework.Bundle; 69 import org.osgi.framework.BundleContext; 70 import org.osgi.framework.BundleEvent; 71 import org.osgi.framework.BundleListener; 72 import org.osgi.framework.InvalidSyntaxException; 73 import org.osgi.framework.ServiceReference; 74 import org.osgi.framework.SynchronousBundleListener; 75 76 import com.ibm.icu.text.MessageFormat; 77 78 97 public class WorkbenchPlugin extends AbstractUIPlugin { 98 99 private static final String LEFT_TO_RIGHT = "ltr"; private static final String RIGHT_TO_LEFT = "rtl"; private static final String ORIENTATION_COMMAND_LINE = "-dir"; private static final String ORIENTATION_PROPERTY = "eclipse.orientation"; private static final String NL_USER_PROPERTY = "osgi.nl.user"; private static final String UI_BUNDLE_ACTIVATOR = "org.eclipse.ui.internal.UIPlugin"; 106 private static WorkbenchPlugin inst; 108 109 private EditorRegistry editorRegistry; 111 112 private DecoratorManager decoratorManager; 114 115 private ThemeRegistry themeRegistry; 117 118 private WorkingSetManager workingSetManager; 120 121 private WorkingSetRegistry workingSetRegistry; 123 124 private BundleContext bundleContext; 126 127 private Collection startingBundles = new HashSet (); 129 130 134 public static boolean DEBUG = false; 135 136 141 public static String PI_WORKBENCH = PlatformUI.PLUGIN_ID; 142 143 146 public static char PREFERENCE_PAGE_CATEGORY_SEPARATOR = '/'; 147 148 private WorkbenchPreferenceManager preferenceManager; 150 151 private ViewRegistry viewRegistry; 152 153 private PerspectiveRegistry perspRegistry; 154 155 private ActionSetRegistry actionSetRegistry; 156 157 private SharedImages sharedImages; 158 159 164 private ProductInfo productInfo = null; 165 166 private IntroRegistry introRegistry; 167 168 private WorkbenchOperationSupport operationSupport; 169 private BundleListener bundleListener; 170 171 172 177 public WorkbenchPlugin() { 178 super(); 179 inst = this; 180 } 181 182 186 void reset() { 187 editorRegistry = null; 188 189 if (decoratorManager != null) { 190 decoratorManager.dispose(); 191 decoratorManager = null; 192 } 193 194 ProgressManager.shutdownProgressManager(); 195 196 themeRegistry = null; 197 if (workingSetManager != null) { 198 workingSetManager.dispose(); 199 workingSetManager = null; 200 } 201 workingSetRegistry = null; 202 203 preferenceManager = null; 204 if (viewRegistry != null) { 205 viewRegistry.dispose(); 206 viewRegistry = null; 207 } 208 if (perspRegistry != null) { 209 perspRegistry.dispose(); 210 perspRegistry = null; 211 } 212 actionSetRegistry = null; 213 sharedImages = null; 214 215 productInfo = null; 216 introRegistry = null; 217 218 if (operationSupport != null) { 219 operationSupport.dispose(); 220 operationSupport = null; 221 } 222 223 DEBUG = false; 224 225 } 226 227 237 public static Object createExtension(final IConfigurationElement element, 238 final String classAttribute) throws CoreException { 239 try { 240 if (BundleUtility.isActivated(element.getDeclaringExtension() 243 .getNamespace())) { 244 return element.createExecutableExtension(classAttribute); 245 } 246 final Object [] ret = new Object [1]; 247 final CoreException[] exc = new CoreException[1]; 248 BusyIndicator.showWhile(null, new Runnable () { 249 public void run() { 250 try { 251 ret[0] = element 252 .createExecutableExtension(classAttribute); 253 } catch (CoreException e) { 254 exc[0] = e; 255 } 256 } 257 }); 258 if (exc[0] != null) { 259 throw exc[0]; 260 } 261 return ret[0]; 262 263 } catch (CoreException core) { 264 throw core; 265 } catch (Exception e) { 266 throw new CoreException(new Status(IStatus.ERROR, PI_WORKBENCH, 267 IStatus.ERROR, WorkbenchMessages.WorkbenchPlugin_extension,e)); 268 } 269 } 270 271 283 public static boolean hasExecutableExtension(IConfigurationElement element, 284 String extensionName) { 285 286 if (element.getAttribute(extensionName) != null) 287 return true; 288 String elementText = element.getValue(); 289 if (elementText != null && !elementText.equals("")) return true; 291 IConfigurationElement [] children = element.getChildren(extensionName); 292 if (children.length == 1) { 293 if (children[0].getAttribute(IWorkbenchRegistryConstants.ATT_CLASS) != null) 294 return true; 295 } 296 return false; 297 } 298 299 319 public static boolean isBundleLoadedForExecutableExtension( 320 IConfigurationElement element, String extensionName) { 321 Bundle bundle = getBundleForExecutableExtension(element, extensionName); 322 323 if (bundle == null) 324 return true; 325 return bundle.getState() == Bundle.ACTIVE; 326 } 327 328 347 public static Bundle getBundleForExecutableExtension(IConfigurationElement element, String extensionName) { 348 String prop = null; 351 String executable; 352 String contributorName = null; 353 int i; 354 355 if (extensionName != null) 356 prop = element.getAttribute(extensionName); 357 else { 358 prop = element.getValue(); 360 if (prop != null) { 361 prop = prop.trim(); 362 if (prop.equals("")) prop = null; 364 } 365 } 366 367 if (prop == null) { 368 IConfigurationElement[] exec = element.getChildren(extensionName); 370 if (exec.length != 0) 371 contributorName = exec[0].getAttribute("plugin"); } else { 373 i = prop.indexOf(':'); 375 if (i != -1) 376 executable = prop.substring(0, i).trim(); 377 else 378 executable = prop; 379 380 i = executable.indexOf('/'); 381 if (i != -1) 382 contributorName = executable.substring(0, i).trim(); 383 384 } 385 386 if (contributorName == null) 387 contributorName = element.getContributor().getName(); 388 389 return Platform.getBundle(contributorName); 390 } 391 392 405 protected ImageRegistry createImageRegistry() { 406 return WorkbenchImages.getImageRegistry(); 407 } 408 409 414 public ActionSetRegistry getActionSetRegistry() { 415 if (actionSetRegistry == null) { 416 actionSetRegistry = new ActionSetRegistry(); 417 } 418 return actionSetRegistry; 419 } 420 421 426 public static WorkbenchPlugin getDefault() { 427 return inst; 428 } 429 430 436 437 public IEditorRegistry getEditorRegistry() { 438 if (editorRegistry == null) { 439 editorRegistry = new EditorRegistry(); 440 } 441 return editorRegistry; 442 } 443 444 449 public IElementFactory getElementFactory(String targetID) { 450 451 IExtensionPoint extensionPoint; 453 extensionPoint = Platform.getExtensionRegistry().getExtensionPoint( 454 PI_WORKBENCH, IWorkbenchRegistryConstants.PL_ELEMENT_FACTORY); 455 456 if (extensionPoint == null) { 457 WorkbenchPlugin 458 .log("Unable to find element factory. Extension point: " + IWorkbenchRegistryConstants.PL_ELEMENT_FACTORY + " not found"); return null; 460 } 461 462 IConfigurationElement targetElement = null; 464 IConfigurationElement[] configElements = extensionPoint 465 .getConfigurationElements(); 466 for (int j = 0; j < configElements.length; j++) { 467 String strID = configElements[j].getAttribute("id"); if (targetID.equals(strID)) { 469 targetElement = configElements[j]; 470 break; 471 } 472 } 473 if (targetElement == null) { 474 WorkbenchPlugin.log("Unable to find element factory: " + targetID); return null; 477 } 478 479 IElementFactory factory = null; 481 try { 482 factory = (IElementFactory) createExtension(targetElement, "class"); } catch (CoreException e) { 484 WorkbenchPlugin.log( 486 "Unable to create element factory.", e.getStatus()); factory = null; 488 } 489 return factory; 490 } 491 492 498 public AbstractPresentationFactory getPresentationFactory(String targetID) { 499 Object o = createExtension( 500 IWorkbenchRegistryConstants.PL_PRESENTATION_FACTORIES, 501 "factory", targetID); if (o instanceof AbstractPresentationFactory) { 503 return (AbstractPresentationFactory) o; 504 } 505 WorkbenchPlugin 506 .log("Error creating presentation factory: " + targetID + " -- class is not an AbstractPresentationFactory"); return null; 508 } 509 510 520 private Object createExtension(String extensionPointId, String elementName, 521 String targetID) { 522 IExtensionPoint extensionPoint = Platform.getExtensionRegistry() 523 .getExtensionPoint(PI_WORKBENCH, extensionPointId); 524 if (extensionPoint == null) { 525 WorkbenchPlugin 526 .log("Unable to find extension. Extension point: " + extensionPointId + " not found"); return null; 528 } 529 530 IConfigurationElement targetElement = null; 532 IConfigurationElement[] elements = extensionPoint 533 .getConfigurationElements(); 534 for (int j = 0; j < elements.length; j++) { 535 IConfigurationElement element = elements[j]; 536 if (elementName == null || elementName.equals(element.getName())) { 537 String strID = element.getAttribute("id"); if (targetID.equals(strID)) { 539 targetElement = element; 540 break; 541 } 542 } 543 } 544 if (targetElement == null) { 545 WorkbenchPlugin.log("Unable to find extension: " + targetID + " in extension point: " + extensionPointId); return null; 549 } 550 551 try { 553 return createExtension(targetElement, "class"); } catch (CoreException e) { 555 WorkbenchPlugin.log("Unable to create extension: " + targetID + " in extension point: " + extensionPointId + ", status: ", e.getStatus()); } 560 return null; 561 } 562 563 567 public IPerspectiveRegistry getPerspectiveRegistry() { 568 if (perspRegistry == null) { 569 perspRegistry = new PerspectiveRegistry(); 570 StartupThreading.runWithoutExceptions(new StartupRunnable() { 576 public void runWithException() throws Throwable { 577 perspRegistry.load(); 578 } 579 }); 580 581 } 582 return perspRegistry; 583 } 584 585 591 public IWorkingSetManager getWorkingSetManager() { 592 if (workingSetManager == null) { 593 workingSetManager = new WorkingSetManager(bundleContext); 594 workingSetManager.restoreState(); 595 } 596 return workingSetManager; 597 } 598 599 605 public WorkingSetRegistry getWorkingSetRegistry() { 606 if (workingSetRegistry == null) { 607 workingSetRegistry = new WorkingSetRegistry(); 608 workingSetRegistry.load(); 609 } 610 return workingSetRegistry; 611 } 612 613 619 public IIntroRegistry getIntroRegistry() { 620 if (introRegistry == null) { 621 introRegistry = new IntroRegistry(); 622 } 623 return introRegistry; 624 } 625 626 632 public IWorkbenchOperationSupport getOperationSupport() { 633 if (operationSupport == null) { 634 operationSupport = new WorkbenchOperationSupport(); 635 } 636 return operationSupport; 637 } 638 639 640 645 public PreferenceManager getPreferenceManager() { 646 if (preferenceManager == null) { 647 preferenceManager = new WorkbenchPreferenceManager( 648 PREFERENCE_PAGE_CATEGORY_SEPARATOR); 649 650 PreferencePageRegistryReader registryReader = new PreferencePageRegistryReader( 652 getWorkbench()); 653 registryReader 654 .loadFromRegistry(Platform.getExtensionRegistry()); 655 preferenceManager.addPages(registryReader.getTopLevelNodes()); 656 657 } 658 return preferenceManager; 659 } 660 661 666 public ISharedImages getSharedImages() { 667 if (sharedImages == null) { 668 sharedImages = new SharedImages(); 669 } 670 return sharedImages; 671 } 672 673 678 public IThemeRegistry getThemeRegistry() { 679 if (themeRegistry == null) { 680 themeRegistry = new ThemeRegistry(); 681 ThemeRegistryReader reader = new ThemeRegistryReader(); 682 reader.readThemes(Platform.getExtensionRegistry(), 683 themeRegistry); 684 } 685 return themeRegistry; 686 } 687 688 693 public IViewRegistry getViewRegistry() { 694 if (viewRegistry == null) { 695 viewRegistry = new ViewRegistry(); 696 } 697 return viewRegistry; 698 } 699 700 704 public IWorkbench getWorkbench() { 705 return PlatformUI.getWorkbench(); 706 } 707 708 713 protected void initializeDefaultPreferences(IPreferenceStore store) { 714 } 717 718 731 public static void log(String message) { 732 getDefault().getLog().log( 733 StatusUtil.newStatus(IStatus.ERROR, message, null)); 734 } 735 736 740 public static void log(Throwable t) { 741 getDefault().getLog().log(getStatus(t)); 742 } 743 744 749 public static IStatus getStatus(Throwable t) { 750 String message = StatusUtil.getLocalizedMessage(t); 751 752 return newError(message, t); 753 } 754 755 762 public static IStatus newError(String message, Throwable t) { 763 String pluginId = "org.eclipse.ui.workbench"; int errorCode = IStatus.OK; 765 766 if (t instanceof CoreException) { 769 CoreException ce = (CoreException) t; 770 pluginId = ce.getStatus().getPlugin(); 771 errorCode = ce.getStatus().getCode(); 772 } 773 774 return new Status(IStatus.ERROR, pluginId, errorCode, message, 775 StatusUtil.getCause(t)); 776 } 777 778 791 public static void log(String message, Throwable t) { 792 IStatus status = StatusUtil.newStatus(IStatus.ERROR, message, t); 793 log(message, status); 794 } 795 796 811 public static void log(Class clazz, String methodName, Throwable t) { 812 String msg = MessageFormat.format("Exception in {0}.{1}: {2}", new Object [] { clazz.getName(), methodName, t }); 814 log(msg, t); 815 } 816 817 829 public static void log(String message, IStatus status) { 830 831 833 if (message != null) { 834 getDefault().getLog().log( 835 StatusUtil.newStatus(IStatus.ERROR, message, null)); 836 } 837 838 getDefault().getLog().log(status); 839 } 840 841 845 public static void log(IStatus status) { 846 getDefault().getLog().log(status); 847 } 848 849 854 public DecoratorManager getDecoratorManager() { 855 if (this.decoratorManager == null) { 856 this.decoratorManager = new DecoratorManager(); 857 } 858 return decoratorManager; 859 } 860 861 865 public void start(BundleContext context) throws Exception { 866 context.addBundleListener(getBundleListener()); 867 super.start(context); 868 bundleContext = context; 869 870 JFaceUtil.initializeJFace(); 871 872 Window.setDefaultOrientation(getDefaultOrientation()); 873 874 Bundle uiBundle = Platform.getBundle(PlatformUI.PLUGIN_ID); 878 try { 879 if(uiBundle != null) 884 uiBundle.loadClass(UI_BUNDLE_ACTIVATOR); 885 } catch (ClassNotFoundException e) { 886 WorkbenchPlugin.log("Unable to load UI activator", e); } 888 893 894 } 895 896 905 private int getDefaultOrientation() { 906 907 String [] commandLineArgs = Platform.getCommandLineArgs(); 908 909 int orientation = getCommandLineOrientation(commandLineArgs); 910 911 if(orientation != SWT.NONE) { 912 return orientation; 913 } 914 915 orientation = getSystemPropertyOrientation(); 916 917 if(orientation != SWT.NONE) { 918 return orientation; 919 } 920 921 return checkCommandLineLocale(); } 923 924 940 private int checkCommandLineLocale() { 941 942 if(System.getProperty(NL_USER_PROPERTY) == null) { 945 return SWT.NONE; 946 } 947 948 Locale locale = Locale.getDefault(); 949 String lang = locale.getLanguage(); 950 951 if ("iw".equals(lang) || "he".equals(lang) || "ar".equals(lang) || "fa".equals(lang) || "ur".equals(lang)) { return SWT.RIGHT_TO_LEFT; 954 } 955 956 return SWT.NONE; 957 } 958 959 968 private int getSystemPropertyOrientation() { 969 String orientation = System.getProperty(ORIENTATION_PROPERTY); 970 if(RIGHT_TO_LEFT.equals(orientation)) { 971 return SWT.RIGHT_TO_LEFT; 972 } 973 if(LEFT_TO_RIGHT.equals(orientation)) { 974 return SWT.LEFT_TO_RIGHT; 975 } 976 return SWT.NONE; 977 } 978 979 988 private int getCommandLineOrientation(String [] commandLineArgs) { 989 for (int i = 0; i < commandLineArgs.length - 1; i++) { 991 if(commandLineArgs[i].equalsIgnoreCase(ORIENTATION_COMMAND_LINE)){ 992 String orientation = commandLineArgs[i+1]; 993 if(orientation.equals(RIGHT_TO_LEFT)){ 994 System.setProperty(ORIENTATION_PROPERTY,RIGHT_TO_LEFT); 995 return SWT.RIGHT_TO_LEFT; 996 } 997 if(orientation.equals(LEFT_TO_RIGHT)){ 998 System.setProperty(ORIENTATION_PROPERTY,LEFT_TO_RIGHT); 999 return SWT.LEFT_TO_RIGHT; 1000 } 1001 } 1002 } 1003 1004 return SWT.NONE; 1005 } 1006 1007 1013 public Bundle[] getBundles() { 1014 return bundleContext == null ? new Bundle[0] : bundleContext 1015 .getBundles(); 1016 } 1017 1018 1024 public BundleContext getBundleContext() { 1025 return bundleContext; 1026 } 1027 1028 1041 public String getAppName() { 1042 return getProductInfo().getAppName(); 1043 } 1044 1045 1051 public String getProductName() { 1052 return getProductInfo().getProductName(); 1053 } 1054 1055 1062 public ImageDescriptor[] getWindowImages() { 1063 return getProductInfo().getWindowImages(); 1064 } 1065 1066 1071 private ProductInfo getProductInfo() { 1072 if (productInfo == null) { 1073 productInfo = new ProductInfo(Platform.getProduct()); 1074 } 1075 return productInfo; 1076 } 1077 1078 1081 public void stop(BundleContext context) throws Exception { 1082 if (bundleListener!=null) { 1083 context.removeBundleListener(bundleListener); 1084 bundleListener = null; 1085 } 1086 super.stop(context); 1089 if (workingSetManager != null) { 1090 workingSetManager.dispose(); 1091 workingSetManager= null; 1092 } 1093 SWTResourceUtil.shutdown(); 1094 } 1095 1096 1102 public IWizardRegistry getNewWizardRegistry() { 1103 return NewWizardRegistry.getInstance(); 1104 } 1105 1106 1112 public IWizardRegistry getImportWizardRegistry() { 1113 return ImportWizardRegistry.getInstance(); 1114 } 1115 1116 1122 public IWizardRegistry getExportWizardRegistry() { 1123 return ExportWizardRegistry.getInstance(); 1124 } 1125 1126 1139 public IPath getDataLocation() { 1140 try { 1141 return getStateLocation(); 1142 } catch (IllegalStateException e) { 1143 return null; 1146 } 1147 } 1148 1149 void addBundleListener(BundleListener bundleListener) { 1150 bundleContext.addBundleListener(bundleListener); 1151 } 1152 1153 void removeBundleListener(BundleListener bundleListener) { 1154 bundleContext.removeBundleListener(bundleListener); 1155 } 1156 1157 int getBundleCount() { 1158 return bundleContext.getBundles().length; 1159 } 1160 1161 OutputStream getSplashStream() { 1162 ServiceReference[] ref; 1165 try { 1166 ref = bundleContext.getServiceReferences(OutputStream .class.getName(), null); 1167 } catch (InvalidSyntaxException e) { 1168 return null; 1169 } 1170 if(ref==null) { 1171 return null; 1172 } 1173 for (int i = 0; i < ref.length; i++) { 1174 String name = (String ) ref[i].getProperty("name"); if (name != null && name.equals("splashstream")) { Object result = bundleContext.getService(ref[i]); 1177 bundleContext.ungetService(ref[i]); 1178 return (OutputStream ) result; 1179 } 1180 } 1181 return null; 1182 } 1183 1184 1187 private BundleListener getBundleListener() { 1188 if (bundleListener == null) { 1189 bundleListener = new SynchronousBundleListener() { 1190 public void bundleChanged(BundleEvent event) { 1191 WorkbenchPlugin.this.bundleChanged(event); 1192 } 1193 }; 1194 } 1195 return bundleListener; 1196 } 1197 1198 private void bundleChanged(BundleEvent event) { 1199 synchronized (startingBundles) { 1202 switch (event.getType()) { 1203 case BundleEvent.STARTING : 1204 startingBundles.add(event.getBundle()); 1205 break; 1206 case BundleEvent.STARTED : 1207 case BundleEvent.STOPPED : 1208 startingBundles.remove(event.getBundle()); 1209 break; 1210 default : 1211 break; 1212 } 1213 } 1214 } 1215 1216 public boolean isStarting(Bundle bundle) { 1217 synchronized (startingBundles) { 1218 return startingBundles.contains(bundle); 1219 } 1220 } 1221 1222} 1223 | Popular Tags |