org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/project/ProjectTemplateImpl.java
changeset 463 aea4c83725d8
child 464 0b02f3d6f52c
equal deleted inserted replaced
462:cdc4995b1677 463:aea4c83725d8
       
     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.internal.ui.project;
       
    20 
       
    21 import org.eclipse.core.resources.IProject;
       
    22 import org.eclipse.core.runtime.CoreException;
       
    23 import org.eclipse.core.runtime.IConfigurationElement;
       
    24 import org.eclipse.core.runtime.IPath;
       
    25 import org.eclipse.core.runtime.IProgressMonitor;
       
    26 import org.eclipse.core.runtime.SubProgressMonitor;
       
    27 import org.eclipse.jface.resource.ImageDescriptor;
       
    28 import org.eclipse.swt.graphics.Image;
       
    29 import org.eclipse.wst.common.project.facet.core.IProjectFacet;
       
    30 import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
       
    31 import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
       
    32 import org.symbian.tools.tmw.core.TMWCore;
       
    33 import org.symbian.tools.tmw.core.runtimes.IMobileWebRuntime;
       
    34 import org.symbian.tools.tmw.ui.TMWCoreUI;
       
    35 import org.symbian.tools.tmw.ui.project.IProjectTemplate;
       
    36 import org.symbian.tools.tmw.ui.project.IProjectTemplateContext;
       
    37 import org.symbian.tools.tmw.ui.project.ITemplateInstaller;
       
    38 
       
    39 public class ProjectTemplateImpl implements IProjectTemplate {
       
    40     private final IConfigurationElement element;
       
    41     private Image image;
       
    42     private IMobileWebRuntime[] runtimes;
       
    43     private IProjectFacetVersion[] facetVersions;
       
    44     private ITemplateInstaller installer;
       
    45 
       
    46     public ProjectTemplateImpl(IConfigurationElement element) {
       
    47         this.element = element;
       
    48     }
       
    49 
       
    50     public int getWeight() {
       
    51         final String weight = element.getAttribute("weight");
       
    52         try {
       
    53             return Integer.valueOf(weight).intValue();
       
    54         } catch (NumberFormatException e) {
       
    55             TMWCoreUI.log("Invalid weight: %s in project template %s", weight, getName());
       
    56             return Integer.MAX_VALUE;
       
    57         }
       
    58     }
       
    59 
       
    60     public Image getIcon() {
       
    61         if (image == null) {
       
    62             final ImageDescriptor imageDescriptor = TMWCoreUI.imageDescriptorFromPlugin(element.getContributor()
       
    63                     .getName(), element.getAttribute("icon"));
       
    64             final String key = "Template#" + getName();
       
    65             TMWCoreUI.getDefault().getImageRegistry().put(key, imageDescriptor);
       
    66             image = TMWCoreUI.getDefault().getImageRegistry().get(key);
       
    67         }
       
    68         return image;
       
    69     }
       
    70 
       
    71     public String getName() {
       
    72         return element.getAttribute("name");
       
    73     }
       
    74 
       
    75     public String getDescription() {
       
    76         IConfigurationElement[] children = element.getChildren("description");
       
    77         if (children.length > 0) {
       
    78             return children[0].getValue();
       
    79         }
       
    80         return "";
       
    81     }
       
    82 
       
    83     public IMobileWebRuntime[] getSupportedRuntimes() {
       
    84         if (runtimes == null) {
       
    85             final IConfigurationElement[] children = element.getChildren("supported-runtime");
       
    86             runtimes = new IMobileWebRuntime[children.length];
       
    87             for (int i = 0; i < children.length; i++) {
       
    88                 final IConfigurationElement element = children[i];
       
    89                 final String id = element.getAttribute("id");
       
    90                 final String version = element.getAttribute("version");
       
    91                 runtimes[i] = TMWCore.getRuntimesManager().getRuntime(id, version);
       
    92                 if (runtimes[i] == null) {
       
    93                     TMWCoreUI.log("Runtime %s@%s was not found. It is needed for project template %s.", id, version,
       
    94                             getName());
       
    95                 }
       
    96             }
       
    97         }
       
    98         return runtimes;
       
    99     }
       
   100 
       
   101     public IProjectFacetVersion[] getRequiredFacets() {
       
   102         if (facetVersions == null) {
       
   103             final IConfigurationElement[] children = element.getChildren("required-facet");
       
   104             facetVersions = new IProjectFacetVersion[children.length];
       
   105             for (int i = 0; i < children.length; i++) {
       
   106                 final IConfigurationElement element = children[i];
       
   107                 final IProjectFacet projectFacet = ProjectFacetsManager.getProjectFacet(element.getAttribute("id"));
       
   108                 if (projectFacet != null) {
       
   109                     facetVersions[i] = projectFacet.getVersion(element.getAttribute("version"));
       
   110                 }
       
   111                 if (facetVersions[i] == null) {
       
   112                     TMWCoreUI.log("Facet %s@%s was not found. It is required by project template %s.",
       
   113                             element.getAttribute("id"), element.getAttribute("version"));
       
   114                 }
       
   115             }
       
   116         }
       
   117         return facetVersions;
       
   118     }
       
   119 
       
   120     public void init(IProject project, IProjectTemplateContext context, IProgressMonitor monitor) {
       
   121         if (installer == null) {
       
   122             installer = CompoundInstaller.combine(null, element.getChildren());
       
   123         }
       
   124         final ITemplateInstaller templateInstaller;
       
   125         final IMobileWebRuntime runtime = context.getRuntime();
       
   126         if (runtime == null) {
       
   127             templateInstaller = installer;
       
   128         } else {
       
   129             templateInstaller = CompoundInstaller.combine(installer, TMWCoreUI.getProjectTemplateManager()
       
   130                     .getEmptyProjectTemplate(runtime));
       
   131         }
       
   132         templateInstaller.prepare(project, context);
       
   133         try {
       
   134             final IPath[] files = templateInstaller.getFiles();
       
   135             monitor.beginTask("Copying project files", files.length * 10);
       
   136             templateInstaller.copyFiles(files, new SubProgressMonitor(monitor, files.length * 10));
       
   137         } catch (CoreException e) {
       
   138             TMWCoreUI.log(e);
       
   139         } finally {
       
   140             templateInstaller.cleanup();
       
   141         }
       
   142         monitor.done();
       
   143     }
       
   144 
       
   145 }