plugins/org.symbian.tools.tmw.previewer/src/org/symbian/tools/tmw/previewer/internal/PreviewerExtensionsManagerImpl.java
changeset 479 518afa7c6d2f
parent 478 6c07c755d0c7
child 480 b6d992b9b998
equal deleted inserted replaced
478:6c07c755d0c7 479:518afa7c6d2f
     1 /**
       
     2  * Copyright (c) 2010 Symbian Foundation 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  * Symbian Foundation - initial contribution.
       
    11  * Contributors:
       
    12  * Description:
       
    13  * Overview:
       
    14  * Details:
       
    15  * Platforms/Drives/Compatibility:
       
    16  * Assumptions/Requirement/Pre-requisites:
       
    17  * Failures and causes:
       
    18  */
       
    19 package org.symbian.tools.tmw.previewer.internal;
       
    20 
       
    21 import java.io.InputStream;
       
    22 import java.util.HashMap;
       
    23 import java.util.Map;
       
    24 
       
    25 import org.eclipse.core.resources.IFile;
       
    26 import org.eclipse.core.resources.IProject;
       
    27 import org.eclipse.core.runtime.CoreException;
       
    28 import org.eclipse.core.runtime.IConfigurationElement;
       
    29 import org.eclipse.core.runtime.IPath;
       
    30 import org.eclipse.core.runtime.Platform;
       
    31 import org.symbian.tools.tmw.core.TMWCore;
       
    32 import org.symbian.tools.tmw.core.projects.ITMWProject;
       
    33 import org.symbian.tools.tmw.core.runtimes.IMobileWebRuntime;
       
    34 import org.symbian.tools.tmw.previewer.PreviewerPlugin;
       
    35 import org.symbian.tools.tmw.previewer.core.IApplicationLayoutProvider;
       
    36 import org.symbian.tools.tmw.previewer.core.IPreviewerExtensionsManager;
       
    37 
       
    38 public class PreviewerExtensionsManagerImpl implements IPreviewerExtensionsManager {
       
    39     public static final class LazyProvider implements IApplicationLayoutProvider {
       
    40         private final IConfigurationElement element;
       
    41         private IApplicationLayoutProvider instance;
       
    42 
       
    43         public LazyProvider(IConfigurationElement element) {
       
    44             this.element = element;
       
    45         }
       
    46 
       
    47         public IPath getResourcePath(IFile file) {
       
    48             return getDelegate().getResourcePath(file);
       
    49         }
       
    50 
       
    51         private IApplicationLayoutProvider getDelegate() {
       
    52             if (instance == null) {
       
    53                 try {
       
    54                     instance = (IApplicationLayoutProvider) element.createExecutableExtension("class");
       
    55                 } catch (CoreException e) {
       
    56                     PreviewerPlugin.log(e);
       
    57                     instance = new IApplicationLayoutProvider() {
       
    58 
       
    59                         public IPath getResourcePath(IFile file) {
       
    60                             return null;
       
    61                         }
       
    62 
       
    63                         public InputStream getResourceFromPath(IProject project, IPath path) {
       
    64                             return null;
       
    65                         }
       
    66 
       
    67                         public IFile getWorkspaceFile(IProject project, IPath applicationPath) {
       
    68                             return null;
       
    69                         }
       
    70                     };
       
    71                 }
       
    72             }
       
    73             return instance;
       
    74         }
       
    75 
       
    76         public InputStream getResourceFromPath(IProject project, IPath path) throws CoreException {
       
    77             return getDelegate().getResourceFromPath(project, path);
       
    78         }
       
    79 
       
    80         public IFile getWorkspaceFile(IProject project, IPath applicationPath) throws CoreException {
       
    81             return getDelegate().getWorkspaceFile(project, applicationPath);
       
    82         }
       
    83     }
       
    84 
       
    85     private Map<IMobileWebRuntime, IApplicationLayoutProvider> providers;
       
    86 
       
    87     public IApplicationLayoutProvider getLayoutProvider(IProject project) {
       
    88         final ITMWProject p = TMWCore.create(project);
       
    89         if (p != null && p.getTargetRuntime() != null) {
       
    90             checkRegistry();
       
    91             return providers.get(p.getTargetRuntime());
       
    92         }
       
    93         return null;
       
    94     }
       
    95 
       
    96     private synchronized void checkRegistry() {
       
    97         if (providers == null) {
       
    98             providers = new HashMap<IMobileWebRuntime, IApplicationLayoutProvider>();
       
    99             final IConfigurationElement[] configuration = Platform.getExtensionRegistry().getConfigurationElementsFor(
       
   100                     PreviewerPlugin.PLUGIN_ID, "layoutProviders");
       
   101             for (IConfigurationElement element : configuration) {
       
   102                 final String runtimeId = element.getAttribute("runtime-id");
       
   103                 final String version = element.getAttribute("runtime-version");
       
   104                 final IMobileWebRuntime runtime = TMWCore.getRuntimesManager().getRuntime(runtimeId, version);
       
   105                 if (runtime != null) {
       
   106                     providers.put(runtime, new LazyProvider(element));
       
   107                 } else {
       
   108                     PreviewerPlugin.log(String.format("Runtime %s:$s referenced from %s was not found", runtimeId,
       
   109                             version, element.getContributor().getName()), null);
       
   110                 }
       
   111             }
       
   112         }
       
   113     }
       
   114 }