org.symbian.tools.mtw.ui/src/org/symbian/tools/mtw/internal/deployment/DeploymentTargetProviderDescriptor.java
changeset 456 12b549765c34
parent 455 5da55957c779
child 457 f1087591ff71
equal deleted inserted replaced
455:5da55957c779 456:12b549765c34
     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.mtw.internal.deployment;
       
    20 
       
    21 import java.util.Map;
       
    22 import java.util.WeakHashMap;
       
    23 
       
    24 import org.eclipse.core.runtime.CoreException;
       
    25 import org.eclipse.core.runtime.IConfigurationElement;
       
    26 import org.eclipse.core.runtime.IProgressMonitor;
       
    27 import org.eclipse.core.runtime.jobs.ISchedulingRule;
       
    28 import org.eclipse.jface.resource.ImageDescriptor;
       
    29 import org.symbian.tools.mtw.core.projects.IMTWProject;
       
    30 import org.symbian.tools.mtw.ui.MTWCoreUI;
       
    31 import org.symbian.tools.mtw.ui.deployment.IDeploymentTarget;
       
    32 import org.symbian.tools.mtw.ui.deployment.IDeploymentTargetProvider;
       
    33 
       
    34 public final class DeploymentTargetProviderDescriptor implements IDeploymentTargetProvider {
       
    35     public class NullProvider implements IDeploymentTargetProvider {
       
    36         public IDeploymentTarget[] getTargets(IMTWProject project) {
       
    37             return null;
       
    38         }
       
    39 
       
    40         public void discoverTargets(IProgressMonitor monitor) throws CoreException {
       
    41             // Do nothing
       
    42         }
       
    43 
       
    44         public IDeploymentTarget findTarget(IMTWProject project, String id) {
       
    45             return null;
       
    46         }
       
    47 
       
    48         public boolean targetsDiscovered() {
       
    49             return true;
       
    50         }
       
    51 
       
    52         public ISchedulingRule getSchedulingRule(IDeploymentTarget target) {
       
    53             return null; // No scheduling
       
    54         }
       
    55     }
       
    56 
       
    57     private static final DeploymentTargetWrapper[] NO_TARGETS = new DeploymentTargetWrapper[0];
       
    58     private final IConfigurationElement element;
       
    59     private IDeploymentTargetProvider provider;
       
    60     private final Map<IDeploymentTarget, DeploymentTargetWrapper> wrappers = new WeakHashMap<IDeploymentTarget, DeploymentTargetWrapper>();
       
    61     private ImageDescriptor imageDescriptor = null;
       
    62 
       
    63     public DeploymentTargetProviderDescriptor(IConfigurationElement element) {
       
    64         this.element = element;
       
    65     }
       
    66 
       
    67     public boolean supports(IMTWProject project) {
       
    68         // We will support more declarative filtering later
       
    69         return true;
       
    70     }
       
    71 
       
    72     public DeploymentTargetWrapper[] getTargets(IMTWProject project) {
       
    73         final DeploymentTargetWrapper[] targets = wrap(getProvider().getTargets(project));
       
    74         return targets != null ? targets : NO_TARGETS;
       
    75     }
       
    76 
       
    77     private DeploymentTargetWrapper[] wrap(IDeploymentTarget[] targets) {
       
    78         final DeploymentTargetWrapper[] w = new DeploymentTargetWrapper[targets.length];
       
    79         for (int i = 0; i < targets.length; i++) {
       
    80             final IDeploymentTarget target = targets[i];
       
    81             DeploymentTargetWrapper wrapper = wrappers.get(target);
       
    82             if (wrapper == null) {
       
    83                 wrapper = new DeploymentTargetWrapper(target, this);
       
    84                 wrappers.put(target, wrapper);
       
    85             }
       
    86             w[i] = wrapper;
       
    87         }
       
    88         return w;
       
    89     }
       
    90 
       
    91     public void discoverTargets(IProgressMonitor monitor) throws CoreException {
       
    92         getProvider().discoverTargets(monitor);
       
    93     }
       
    94 
       
    95     public IDeploymentTarget findTarget(IMTWProject project, String id) {
       
    96         return getProvider().findTarget(project, id);
       
    97     }
       
    98 
       
    99     private synchronized IDeploymentTargetProvider getProvider() {
       
   100         if (provider == null) {
       
   101             try {
       
   102                 provider = (IDeploymentTargetProvider) element.createExecutableExtension("class");
       
   103             } catch (CoreException e) {
       
   104                 MTWCoreUI.log("Cannot instantiate provider " + getId(), e);
       
   105                 provider = new NullProvider();
       
   106             }
       
   107         }
       
   108         return provider;
       
   109     }
       
   110 
       
   111     public String getId() {
       
   112         return element.getAttribute("id");
       
   113     }
       
   114 
       
   115     public boolean targetsDiscovered() {
       
   116         return getProvider().targetsDiscovered();
       
   117     }
       
   118 
       
   119     public int getPriority() {
       
   120         final String attribute = element.getAttribute("priority");
       
   121         if (attribute != null && attribute.trim().length() > 0) {
       
   122             try {
       
   123                 return Integer.parseInt(attribute);
       
   124             } catch (NumberFormatException e) {
       
   125                 MTWCoreUI.log(String.format("%s is not a valid priority value for %s provider", attribute, getId()),
       
   126                         null);
       
   127             }
       
   128         }
       
   129         return 0;
       
   130     }
       
   131 
       
   132     public ImageDescriptor getImageDescriptor() {
       
   133         if (imageDescriptor == null) {
       
   134             final String image = element.getAttribute("icon");
       
   135             if (image == null) {
       
   136                 imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
       
   137             } else {
       
   138                 imageDescriptor = MTWCoreUI.imageDescriptorFromPlugin(element.getNamespaceIdentifier(), image);
       
   139             }
       
   140         }
       
   141         return imageDescriptor;
       
   142     }
       
   143 
       
   144     public ISchedulingRule getSchedulingRule(IDeploymentTarget target) {
       
   145         return provider.getSchedulingRule(target);
       
   146     }
       
   147 
       
   148 }