uidesigner/com.nokia.sdt.uimodel/src/com/nokia/sdt/component/ComponentSystem.java
changeset 0 fb279309251b
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 /**
       
    18  * 
       
    19  */
       
    20 package com.nokia.sdt.component;
       
    21 
       
    22 import com.nokia.sdt.uimodel.Messages;
       
    23 import com.nokia.sdt.uimodel.UIModelPlugin;
       
    24 import com.nokia.cpp.internal.api.utils.core.Check;
       
    25 import com.nokia.cpp.internal.api.utils.core.Logging;
       
    26 
       
    27 import org.eclipse.core.runtime.*;
       
    28 
       
    29 import java.text.MessageFormat;
       
    30 import java.util.HashMap;
       
    31 import java.util.Map;
       
    32 
       
    33 /**
       
    34  * The interface for the component system.
       
    35  * 
       
    36  * This maintains a collection of component providers. Each provider
       
    37  * represents a specific implementation of components and
       
    38  * can provide a unique set of IComponent instances.
       
    39  * 
       
    40  * @see IComponent
       
    41  * 
       
    42  * 
       
    43  */
       
    44 public class ComponentSystem {
       
    45 	
       
    46 		/**
       
    47 		 * Name of the component provider extension point
       
    48 		 */
       
    49 	public static final String PROVIDER_EXTENSION = "componentProvider"; //$NON-NLS-1$
       
    50 	
       
    51 	static private ComponentSystem instance;
       
    52 	
       
    53         /**
       
    54          * Map of String -> IComponentProvider
       
    55          */
       
    56 	private Map providers = new HashMap();
       
    57 	
       
    58 		/**
       
    59 		 * Returns the singleton instance of the component system
       
    60 		 */
       
    61 	static public ComponentSystem getComponentSystem() {
       
    62 		if (instance == null) {
       
    63 			instance = new ComponentSystem();
       
    64 		}
       
    65 		return instance;
       
    66 	}
       
    67 	
       
    68 	private ComponentSystem() {
       
    69 	}
       
    70 
       
    71 	/**
       
    72 	 * Returns the requested component provider implementation, if loaded.
       
    73 	 * @param id a string representing the provider
       
    74 	 * @return provider or null if not yet loaded
       
    75 	 * @throws CoreException
       
    76 	 */
       
    77 	public synchronized IComponentProvider findProvider(String id) throws CoreException {
       
    78 		return (IComponentProvider) providers.get(id);
       
    79 	}
       
    80 
       
    81 	/**
       
    82 	 * Returns the requested component provider implementation,
       
    83 	 * loading its extension if necessary.
       
    84 	 * @param id a string representing the provider
       
    85 	 * @return provider or null if not found
       
    86 	 * @throws CoreException
       
    87 	 */
       
    88 	public synchronized IComponentProvider getProvider(String id) throws CoreException {
       
    89 			// check if already loaded this provider
       
    90 		IComponentProvider result = (IComponentProvider) providers.get(id);
       
    91 		if (result == null) {
       
    92 	        result = loadExtensions(id);
       
    93 	        providers.put(id, result);
       
    94 		}
       
    95 		return result;
       
    96 	}
       
    97 
       
    98     /**
       
    99      * Set the component provider
       
   100      * <p>
       
   101      * For testing to set a provider without requiring a running workbench.
       
   102      * @param id a string representing the provider
       
   103      * @param provider the provider instance (must not be null)
       
   104      */
       
   105     public synchronized void setProvider(String id, IComponentProvider provider) {
       
   106         Check.checkArg(id);
       
   107         Check.checkArg(provider);
       
   108         providers.put(id, provider);
       
   109     }
       
   110     
       
   111     private IComponentProvider loadExtensions(String id) throws CoreException {
       
   112         IComponentProvider result = null;
       
   113         
       
   114         // Get implementors of the componentProvider extension point
       
   115         IExtensionRegistry er = Platform.getExtensionRegistry();
       
   116         if (er == null)
       
   117             return null;
       
   118         
       
   119         IExtensionPoint ep = er.getExtensionPoint(
       
   120                 UIModelPlugin.PLUGIN_ID, PROVIDER_EXTENSION);
       
   121 
       
   122         IExtension matched = null;
       
   123         
       
   124         // Iterate over all providers looking for the requested one
       
   125         IExtension[] extensions = ep.getExtensions();
       
   126         for (int i = 0; i < extensions.length; i++) {
       
   127 			IExtension extension = extensions[i];
       
   128 			IConfigurationElement[] ces = extension.getConfigurationElements();
       
   129 			if (ces != null && ces.length >= 1) {
       
   130 				IConfigurationElement providerElement = ces[0];
       
   131 				String name = providerElement.getAttribute("name"); //$NON-NLS-1$
       
   132 				if (name != null && name.equals(id)) {
       
   133 					if (providerElement.getAttribute("class") != null) { //$NON-NLS-1$
       
   134                         if (result != null) {
       
   135                             Logging.log(UIModelPlugin.getDefault(), 
       
   136                                     Logging.newStatus(UIModelPlugin.getDefault(),
       
   137                                             IStatus.ERROR,
       
   138                                             MessageFormat.format(Messages.getString("ComponentSystem.1"),
       
   139                                                     new Object[] { id, extension.getUniqueIdentifier(), matched.getUniqueIdentifier() })
       
   140                                                     
       
   141                                             ));
       
   142                         } else {
       
   143                             result = (IComponentProvider) providerElement.createExecutableExtension("class"); //$NON-NLS-1$
       
   144                             matched = extension;
       
   145                         }
       
   146 					}
       
   147 				}
       
   148 			}
       
   149         }
       
   150         return result;
       
   151     }
       
   152 }