plugins/org.symbian.tools.tmw.ui/src/org/symbian/tools/tmw/internal/ui/project/CompoundInstaller.java
changeset 470 d4809db37847
parent 466 129c94e78375
child 483 109da596fa9d
equal deleted inserted replaced
469:4d198a32ac7d 470:d4809db37847
       
     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 java.util.ArrayList;
       
    22 import java.util.Arrays;
       
    23 import java.util.Collection;
       
    24 import java.util.HashSet;
       
    25 import java.util.LinkedList;
       
    26 import java.util.List;
       
    27 
       
    28 import org.eclipse.core.resources.IProject;
       
    29 import org.eclipse.core.runtime.CoreException;
       
    30 import org.eclipse.core.runtime.IConfigurationElement;
       
    31 import org.eclipse.core.runtime.IPath;
       
    32 import org.eclipse.core.runtime.IProgressMonitor;
       
    33 import org.eclipse.core.runtime.SubProgressMonitor;
       
    34 import org.eclipse.jface.operation.IRunnableWithProgress;
       
    35 import org.symbian.tools.tmw.internal.util.DelegateRunnable;
       
    36 import org.symbian.tools.tmw.ui.project.IProjectTemplateContext;
       
    37 import org.symbian.tools.tmw.ui.project.ITemplateInstaller;
       
    38 
       
    39 public class CompoundInstaller implements ITemplateInstaller {
       
    40     private final class InstallerFiles {
       
    41         private final ITemplateInstaller installer;
       
    42         private final Collection<IPath> paths;
       
    43 
       
    44         public InstallerFiles(Collection<IPath> paths, ITemplateInstaller installer) {
       
    45             this.paths = paths;
       
    46             this.installer = installer;
       
    47         }
       
    48     }
       
    49 
       
    50     public static ITemplateInstaller combine(ITemplateInstaller installer, IConfigurationElement[] elements) {
       
    51         final ITemplateInstaller[] installers = getChildren(installer);
       
    52         final Collection<ITemplateInstaller> installerCollection = new ArrayList<ITemplateInstaller>(installers.length
       
    53                 + elements.length);
       
    54         installerCollection.addAll(Arrays.asList(installers));
       
    55         installerCollection.addAll(fromExtensions(elements));
       
    56         return new CompoundInstaller(installerCollection.toArray(new ITemplateInstaller[installerCollection.size()]));
       
    57     }
       
    58 
       
    59     public static ITemplateInstaller combine(ITemplateInstaller installer1, ITemplateInstaller installer2) {
       
    60         final ITemplateInstaller[] children1 = getChildren(installer1);
       
    61         final ITemplateInstaller[] children2 = getChildren(installer2);
       
    62         final ITemplateInstaller[] result = new ITemplateInstaller[children1.length + children2.length];
       
    63         System.arraycopy(children1, 0, result, 0, children1.length);
       
    64         System.arraycopy(children2, 0, result, children1.length, children2.length);
       
    65         return new CompoundInstaller(result);
       
    66     }
       
    67 
       
    68     private static Collection<? extends ITemplateInstaller> fromExtensions(IConfigurationElement[] elements) {
       
    69         final Collection<ITemplateInstaller> result = new LinkedList<ITemplateInstaller>();
       
    70         for (IConfigurationElement element : elements) {
       
    71             if ("installer".equals(element.getName())) {
       
    72                 result.add(new LazyInstaller(element));
       
    73             } else if ("archive".equals(element.getName())) {
       
    74                 result.add(new ZipInstaller(element));
       
    75             }
       
    76         }
       
    77         return result;
       
    78     }
       
    79 
       
    80     private static ITemplateInstaller[] getChildren(ITemplateInstaller installer) {
       
    81         final ITemplateInstaller[] installers;
       
    82         if (installer == null) {
       
    83             installers = new ITemplateInstaller[0];
       
    84         } else if (installer instanceof CompoundInstaller) {
       
    85             installers = ((CompoundInstaller) installer).installers;
       
    86         } else {
       
    87             installers = new ITemplateInstaller[] { installer };
       
    88         }
       
    89         return installers;
       
    90     }
       
    91 
       
    92     private final Collection<InstallerFiles> files = new LinkedList<CompoundInstaller.InstallerFiles>();
       
    93     private final ITemplateInstaller[] installers;
       
    94 
       
    95     private CompoundInstaller(ITemplateInstaller[] installers) {
       
    96         this.installers = installers;
       
    97     }
       
    98 
       
    99     public void cleanup() {
       
   100         for (ITemplateInstaller installer : installers) {
       
   101             installer.cleanup();
       
   102         }
       
   103     }
       
   104 
       
   105     public void copyFiles(IPath[] files, IProgressMonitor monitor) throws CoreException {
       
   106         monitor.beginTask("Copying files", files.length * 10);
       
   107         final List<IPath> list = Arrays.asList(files);
       
   108         for (InstallerFiles fs : this.files) {
       
   109             final ITemplateInstaller installer = fs.installer;
       
   110             fs.paths.retainAll(list);
       
   111             IPath[] array = fs.paths.toArray(new IPath[fs.paths.size()]);
       
   112             installer.copyFiles(array, new SubProgressMonitor(monitor, array.length * 10));
       
   113         }
       
   114         monitor.done();
       
   115     }
       
   116 
       
   117     public IPath[] getFiles() throws CoreException {
       
   118         final Collection<IPath> paths = new HashSet<IPath>();
       
   119         for (ITemplateInstaller installer : installers) {
       
   120             final IPath[] f = installer.getFiles();
       
   121             final Collection<IPath> c = new HashSet<IPath>(Arrays.asList(f));
       
   122             c.removeAll(paths);
       
   123             files.add(new InstallerFiles(c, installer));
       
   124             paths.addAll(c);
       
   125         }
       
   126         return paths.toArray(new IPath[paths.size()]);
       
   127     }
       
   128 
       
   129     public void prepare(IProject project, IProjectTemplateContext context) {
       
   130         for (ITemplateInstaller installer : installers) {
       
   131             installer.prepare(project, context);
       
   132         }
       
   133     }
       
   134 
       
   135     public IRunnableWithProgress getPostCreateAction() {
       
   136         final Collection<IRunnableWithProgress> runnables = new LinkedList<IRunnableWithProgress>();
       
   137         for (ITemplateInstaller installer : installers) {
       
   138             final IRunnableWithProgress action = installer.getPostCreateAction();
       
   139             if (action != null) {
       
   140                 runnables.add(action);
       
   141             }
       
   142         }
       
   143         return runnables.size() > 0 ? new DelegateRunnable(runnables) : null;
       
   144     }
       
   145 }