org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/deployment/DeploymentTargetTypeDescriptor.java
changeset 463 aea4c83725d8
parent 462 cdc4995b1677
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.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.tmw.core.projects.IMTWProject;
       
    30 import org.symbian.tools.tmw.ui.TMWCoreUI;
       
    31 import org.symbian.tools.tmw.ui.deployment.IDeploymentTarget;
       
    32 import org.symbian.tools.tmw.ui.deployment.IDeploymentTargetType;
       
    33 
       
    34 public final class DeploymentTargetTypeDescriptor implements IDeploymentTargetType {
       
    35     public class NullProvider implements IDeploymentTargetType {
       
    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 IDeploymentTargetType type;
       
    60     private final Map<IDeploymentTarget, DeploymentTargetWrapper> wrappers = new WeakHashMap<IDeploymentTarget, DeploymentTargetWrapper>();
       
    61     private ImageDescriptor imageDescriptor = null;
       
    62 
       
    63     public DeploymentTargetTypeDescriptor(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         if (targets == null) {
       
    79             return new DeploymentTargetWrapper[0];
       
    80         } else {
       
    81             final DeploymentTargetWrapper[] w = new DeploymentTargetWrapper[targets.length];
       
    82             for (int i = 0; i < targets.length; i++) {
       
    83                 w[i] = wrap(targets[i]);
       
    84             }
       
    85             return w;
       
    86         }
       
    87     }
       
    88 
       
    89     private DeploymentTargetWrapper wrap(final IDeploymentTarget target) {
       
    90         DeploymentTargetWrapper wrapper = wrappers.get(target);
       
    91         if (wrapper == null) {
       
    92             wrapper = new DeploymentTargetWrapper(target, this);
       
    93             wrappers.put(target, wrapper);
       
    94         }
       
    95         return wrapper;
       
    96     }
       
    97 
       
    98     public void discoverTargets(IProgressMonitor monitor) throws CoreException {
       
    99         getProvider().discoverTargets(monitor);
       
   100     }
       
   101 
       
   102     public IDeploymentTarget findTarget(IMTWProject project, String id) {
       
   103         return wrap(getProvider().findTarget(project, id));
       
   104     }
       
   105 
       
   106     private synchronized IDeploymentTargetType getProvider() {
       
   107         if (type == null) {
       
   108             try {
       
   109                 type = (IDeploymentTargetType) element.createExecutableExtension("class");
       
   110             } catch (CoreException e) {
       
   111                 TMWCoreUI.log("Cannot instantiate provider " + getId(), e);
       
   112                 type = new NullProvider();
       
   113             }
       
   114         }
       
   115         return type;
       
   116     }
       
   117 
       
   118     public String getId() {
       
   119         return element.getAttribute("id");
       
   120     }
       
   121 
       
   122     public boolean targetsDiscovered() {
       
   123         return getProvider().targetsDiscovered();
       
   124     }
       
   125 
       
   126     public int getPriority() {
       
   127         final String attribute = element.getAttribute("priority");
       
   128         if (attribute != null && attribute.trim().length() > 0) {
       
   129             try {
       
   130                 return Integer.parseInt(attribute);
       
   131             } catch (NumberFormatException e) {
       
   132                 TMWCoreUI.log("%s is not a valid priority value for %s provider", attribute, getId());
       
   133             }
       
   134         }
       
   135         return 0;
       
   136     }
       
   137 
       
   138     public ImageDescriptor getImageDescriptor() {
       
   139         if (imageDescriptor == null) {
       
   140             final String image = element.getAttribute("icon");
       
   141             if (image == null) {
       
   142                 imageDescriptor = ImageDescriptor.getMissingImageDescriptor();
       
   143             } else {
       
   144                 imageDescriptor = TMWCoreUI.imageDescriptorFromPlugin(element.getNamespaceIdentifier(), image);
       
   145             }
       
   146         }
       
   147         return imageDescriptor;
       
   148     }
       
   149 
       
   150     public ISchedulingRule getSchedulingRule(IDeploymentTarget target) {
       
   151         return type.getSchedulingRule(target);
       
   152     }
       
   153 
       
   154     @Override
       
   155     public int hashCode() {
       
   156         final int prime = 31;
       
   157         int result = 1;
       
   158         result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
       
   159         return result;
       
   160     }
       
   161 
       
   162     @Override
       
   163     public boolean equals(Object obj) {
       
   164         if (this == obj) {
       
   165             return true;
       
   166         }
       
   167         if (obj == null) {
       
   168             return false;
       
   169         }
       
   170         if (obj instanceof IDeploymentTargetType) {
       
   171             return obj.equals(type);
       
   172         }
       
   173         if (getClass() != obj.getClass()) {
       
   174             return false;
       
   175         }
       
   176         DeploymentTargetTypeDescriptor other = (DeploymentTargetTypeDescriptor) obj;
       
   177         if (getId() == null) {
       
   178             if (other.getId() != null) {
       
   179                 return false;
       
   180             }
       
   181         } else if (!getId().equals(other.getId())) {
       
   182             return false;
       
   183         }
       
   184         return true;
       
   185     }
       
   186 
       
   187     public boolean isLongRunning() {
       
   188         return Boolean.valueOf(element.getAttribute("long-running"));
       
   189     }
       
   190 }