Project creation from the templates was implemented
authorEugene Ostroukhov <eugeneo@symbian.org>
Tue, 17 Aug 2010 13:40:28 -0700
changeset 466 129c94e78375
parent 465 87920e15f8eb
child 467 5a2901872fcf
Project creation from the templates was implemented
org.symbian.tools.mtw.ui/schema/projectTemplate.exsd
org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/project/CompoundInstaller.java
org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/project/LazyInstaller.java
org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/project/ProjectTemplateImpl.java
org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/project/ZipInstaller.java
org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/wizard/NewApplicationWizard.java
org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/wizard/WizardContext.java
org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/util/DelegateRunnable.java
org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/util/OpenFilesRunnable.java
org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/util/Util.java
org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/ui/project/IProjectTemplateContext.java
org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/ui/project/ITemplateInstaller.java
org.symbian.tools.wrttools/plugin.xml
--- a/org.symbian.tools.mtw.ui/schema/projectTemplate.exsd	Mon Aug 16 16:23:25 2010 -0700
+++ b/org.symbian.tools.mtw.ui/schema/projectTemplate.exsd	Tue Aug 17 13:40:28 2010 -0700
@@ -137,6 +137,13 @@
                </appinfo>
             </annotation>
          </attribute>
+         <attribute name="open-files" type="string">
+            <annotation>
+               <documentation>
+                  Allows the template to specify files that will be opened in the editor after the project was created. This is a semicolon-separated list. This string will be expanded using Velocity so it may contain template keys.
+               </documentation>
+            </annotation>
+         </attribute>
       </complexType>
    </element>
 
--- a/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/project/CompoundInstaller.java	Mon Aug 16 16:23:25 2010 -0700
+++ b/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/project/CompoundInstaller.java	Tue Aug 17 13:40:28 2010 -0700
@@ -31,6 +31,8 @@
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.symbian.tools.tmw.internal.util.DelegateRunnable;
 import org.symbian.tools.tmw.ui.project.IProjectTemplateContext;
 import org.symbian.tools.tmw.ui.project.ITemplateInstaller;
 
@@ -129,4 +131,15 @@
             installer.prepare(project, context);
         }
     }
+
+    public IRunnableWithProgress getPostCreateAction() {
+        final Collection<IRunnableWithProgress> runnables = new LinkedList<IRunnableWithProgress>();
+        for (ITemplateInstaller installer : installers) {
+            final IRunnableWithProgress action = installer.getPostCreateAction();
+            if (action != null) {
+                runnables.add(action);
+            }
+        }
+        return runnables.size() > 0 ? new DelegateRunnable(runnables) : null;
+    }
 }
--- a/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/project/LazyInstaller.java	Mon Aug 16 16:23:25 2010 -0700
+++ b/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/project/LazyInstaller.java	Tue Aug 17 13:40:28 2010 -0700
@@ -23,6 +23,7 @@
 import org.eclipse.core.runtime.IConfigurationElement;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.symbian.tools.tmw.ui.TMWCoreUI;
 import org.symbian.tools.tmw.ui.project.IProjectTemplateContext;
 import org.symbian.tools.tmw.ui.project.ITemplateInstaller;
@@ -44,6 +45,10 @@
         public void prepare(IProject project, IProjectTemplateContext context) {
             // Do nothing
         }
+
+        public IRunnableWithProgress getPostCreateAction() {
+            return null;
+        }
     }
     private final IConfigurationElement element;
     private ITemplateInstaller installer;
@@ -79,4 +84,8 @@
     public void prepare(IProject project, IProjectTemplateContext context) {
         getInstaller().prepare(project, context);
     }
+
+    public IRunnableWithProgress getPostCreateAction() {
+        return getInstaller().getPostCreateAction();
+    }
 }
--- a/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/project/ProjectTemplateImpl.java	Mon Aug 16 16:23:25 2010 -0700
+++ b/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/project/ProjectTemplateImpl.java	Tue Aug 17 13:40:28 2010 -0700
@@ -18,6 +18,7 @@
  */
 package org.symbian.tools.tmw.internal.ui.project;
 
+import java.lang.reflect.InvocationTargetException;
 import java.util.Map;
 import java.util.TreeMap;
 
@@ -27,8 +28,12 @@
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.ui.PlatformUI;
 import org.eclipse.wst.common.project.facet.core.IProjectFacet;
 import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
 import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
@@ -138,6 +143,22 @@
             final IPath[] files = templateInstaller.getFiles();
             monitor.beginTask("Copying project files", files.length * 10);
             templateInstaller.copyFiles(files, new SubProgressMonitor(monitor, files.length * 10));
+            final IRunnableWithProgress action = templateInstaller.getPostCreateAction();
+            if (action != null) {
+                Display.getDefault().asyncExec(new Runnable() {
+                    public void run() {
+                        ProgressMonitorDialog dialog = new ProgressMonitorDialog(PlatformUI.getWorkbench()
+                                .getActiveWorkbenchWindow().getShell());
+                        try {
+                            dialog.run(false, true, action);
+                        } catch (InvocationTargetException e) {
+                            TMWCoreUI.log(e);
+                        } catch (InterruptedException e) {
+                            TMWCoreUI.log(e);
+                        }
+                    }
+                });
+            }
         } catch (CoreException e) {
             TMWCoreUI.log(e);
         } finally {
--- a/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/project/ZipInstaller.java	Mon Aug 16 16:23:25 2010 -0700
+++ b/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/project/ZipInstaller.java	Tue Aug 17 13:40:28 2010 -0700
@@ -32,12 +32,14 @@
 import java.util.Collection;
 import java.util.HashSet;
 import java.util.LinkedList;
+import java.util.Set;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.Velocity;
 import org.apache.velocity.context.AbstractContext;
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.FileLocator;
@@ -50,6 +52,9 @@
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.symbian.tools.tmw.internal.util.OpenFilesRunnable;
+import org.symbian.tools.tmw.internal.util.Util;
 import org.symbian.tools.tmw.ui.TMWCoreUI;
 import org.symbian.tools.tmw.ui.project.IProjectTemplateContext;
 import org.symbian.tools.tmw.ui.project.ITemplateInstaller;
@@ -65,6 +70,7 @@
     private IProject project;
     private IProjectTemplateContext context;
     private String[] paths;
+    private final Set<IFile> openFiles = new HashSet<IFile>();
 
     public ZipInstaller(IConfigurationElement element) {
         this.element = element;
@@ -83,6 +89,7 @@
     public void cleanup() {
         project = null;
         context = null;
+        openFiles.clear();
     }
 
     public IPath[] getFiles() throws CoreException {
@@ -147,6 +154,7 @@
     }
 
     public void copyFiles(IPath[] files, IProgressMonitor monitor) throws CoreException {
+        final String filesToOpen = Util.neverNull(element.getAttribute("open-files"));
         HashSet<IPath> fls = new HashSet<IPath>(Arrays.asList(files));
         ZipInputStream stream = null;
         try {
@@ -156,15 +164,20 @@
             ZipEntry entry;
             while ((entry = stream.getNextEntry()) != null && !monitor.isCanceled()) {
                 String nm = entry.getName();
+                boolean open = filesToOpen.contains(nm.endsWith(TEMPLATE_FILE_EXTENSION) ? nm.substring(0, nm.length()
+                        - TEMPLATE_FILE_EXTENSION.length()) : nm);
                 IPath name = new Path(filter(nm));
                 if (fls.contains(name)) {
-                final InputStream contents;
-                if (nm.endsWith(TEMPLATE_FILE_EXTENSION)) {
-                    contents = copyTemplate(project, name, stream, (int) entry.getSize(), ctx, monitor);
-                } else {
-                    contents = new NonClosingStream(stream);
-                }
-                    context.addFile(project, name, contents, new SubProgressMonitor(monitor, 10));
+                    final InputStream contents;
+                    if (nm.endsWith(TEMPLATE_FILE_EXTENSION)) {
+                        contents = copyTemplate(project, name, stream, (int) entry.getSize(), ctx, monitor);
+                    } else {
+                        contents = new NonClosingStream(stream);
+                    }
+                    IFile file = context.addFile(project, name, contents, new SubProgressMonitor(monitor, 10));
+                    if (open) {
+                        openFiles.add(file);
+                    }
                 }
                 stream.closeEntry();
             }
@@ -183,8 +196,7 @@
     }
 
     private InputStream copyTemplate(IProject project, IPath name, ZipInputStream stream, int size,
-            VelocityContext ctx,
-            IProgressMonitor monitor) throws IOException, CoreException {
+            VelocityContext ctx, IProgressMonitor monitor) throws IOException, CoreException {
         // Templates will not be more then a few megs - we can afford the memory
         ByteArrayOutputStream file = new ByteArrayOutputStream();
 
@@ -248,4 +260,11 @@
         }
 
     }
+
+    public IRunnableWithProgress getPostCreateAction() {
+        if (openFiles.size() > 0) {
+            return new OpenFilesRunnable(openFiles);
+        }
+        return null;
+    }
 }
--- a/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/wizard/NewApplicationWizard.java	Mon Aug 16 16:23:25 2010 -0700
+++ b/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/wizard/NewApplicationWizard.java	Tue Aug 17 13:40:28 2010 -0700
@@ -46,6 +46,8 @@
     private NewApplicationFacetsWizardPage facetsPage;
     private NewApplicationDetailsWizardPage firstPage;
     private IStructuredSelection selection;
+    private IProjectTemplate template = null;
+    private INewApplicationWizardPage[] templatePages = new INewApplicationWizardPage[0];
     private NewApplicationTemplateWizardPage templatesPage;
     private final WizardContext wizardContext = new WizardContext();
     private IWorkbench workbench;
@@ -79,7 +81,6 @@
         firstPage = new NewApplicationDetailsWizardPage(wizardContext, databindingContext);
         return firstPage;
     }
-
     @Override
     public IWizardPage getNextPage(final IWizardPage page) {
         if (page == this.firstPage) {
@@ -99,9 +100,6 @@
         return nextPage;
     }
 
-    private IProjectTemplate template = null;
-    private INewApplicationWizardPage[] templatePages = new INewApplicationWizardPage[0];
-
     public IWizardPage[] getPages() {
         final IProjectTemplate current = wizardContext.getTemplate();
         if (template != current) {
@@ -167,9 +165,4 @@
         this.workbench = workbench;
         this.selection = selection;
     }
-
-    public boolean performFinish() {
-        super.performFinish();
-        return true;
-    }
 }
--- a/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/wizard/WizardContext.java	Mon Aug 16 16:23:25 2010 -0700
+++ b/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/ui/wizard/WizardContext.java	Tue Aug 17 13:40:28 2010 -0700
@@ -66,7 +66,7 @@
         }
     }
 
-    public void addFile(IProject project, IPath name, InputStream contents, IProgressMonitor monitor)
+    public IFile addFile(IProject project, IPath name, InputStream contents, IProgressMonitor monitor)
             throws CoreException {
         monitor.beginTask(name.toOSString(), 100);
         final IFile file = project.getFile(name);
@@ -75,6 +75,7 @@
         }
         file.create(contents, false, new SubProgressMonitor(monitor, 100));
         monitor.done();
+        return file;
     }
 
     public void addPropertyChangeListener(PropertyChangeListener arg0) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/util/DelegateRunnable.java	Tue Aug 17 13:40:28 2010 -0700
@@ -0,0 +1,43 @@
+/**
+ * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
+ * All rights reserved.
+ * This component and the accompanying materials are made available
+ * under the terms of the License "Eclipse Public License v1.0"
+ * which accompanies this distribution, and is available
+ * at the URL "http://www.eclipse.org/legal/epl-v10.html".
+ *
+ * Initial Contributors:
+ * Symbian Foundation - initial contribution.
+ * Contributors:
+ * Description:
+ * Overview:
+ * Details:
+ * Platforms/Drives/Compatibility:
+ * Assumptions/Requirement/Pre-requisites:
+ * Failures and causes:
+ */
+package org.symbian.tools.tmw.internal.util;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.Collection;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+
+public class DelegateRunnable implements IRunnableWithProgress {
+    private final Collection<IRunnableWithProgress> runnables;
+
+    public DelegateRunnable(Collection<IRunnableWithProgress> runnables) {
+        this.runnables = runnables;
+    }
+
+    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
+        monitor.beginTask("Preparing project", runnables.size() * 20);
+        for (IRunnableWithProgress runnable : runnables) {
+            runnable.run(new SubProgressMonitor(monitor, 20));
+        }
+        monitor.done();
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/util/OpenFilesRunnable.java	Tue Aug 17 13:40:28 2010 -0700
@@ -0,0 +1,53 @@
+/**
+ * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies).
+ * All rights reserved.
+ * This component and the accompanying materials are made available
+ * under the terms of the License "Eclipse Public License v1.0"
+ * which accompanies this distribution, and is available
+ * at the URL "http://www.eclipse.org/legal/epl-v10.html".
+ *
+ * Initial Contributors:
+ * Symbian Foundation - initial contribution.
+ * Contributors:
+ * Description:
+ * Overview:
+ * Details:
+ * Platforms/Drives/Compatibility:
+ * Assumptions/Requirement/Pre-requisites:
+ * Failures and causes:
+ */
+package org.symbian.tools.tmw.internal.util;
+
+import java.lang.reflect.InvocationTargetException;
+import java.util.Set;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ide.IDE;
+import org.symbian.tools.tmw.ui.TMWCoreUI;
+
+public class OpenFilesRunnable implements IRunnableWithProgress {
+    private final IFile[] openFiles;
+
+    public OpenFilesRunnable(Set<IFile> openFiles) {
+        this.openFiles = openFiles.toArray(new IFile[openFiles.size()]);
+    }
+
+    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
+        monitor.beginTask("Opening files in editors", openFiles.length);
+        for (IFile file : openFiles) {
+            IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
+            try {
+                IDE.openEditor(page, file);
+            } catch (PartInitException e) {
+                TMWCoreUI.log(e);
+            }
+            monitor.worked(1);
+        }
+        monitor.done();
+    }
+}
--- a/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/util/Util.java	Mon Aug 16 16:23:25 2010 -0700
+++ b/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/internal/util/Util.java	Tue Aug 17 13:40:28 2010 -0700
@@ -24,4 +24,8 @@
         return value != null ? value.trim().replace(" ", "") : "";
     }
 
+    public static String neverNull(String string) {
+        return string == null ? "" : string.trim();
+    }
+
 }
--- a/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/ui/project/IProjectTemplateContext.java	Mon Aug 16 16:23:25 2010 -0700
+++ b/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/ui/project/IProjectTemplateContext.java	Tue Aug 17 13:40:28 2010 -0700
@@ -21,6 +21,7 @@
 import java.io.InputStream;
 
 import org.eclipse.core.databinding.observable.value.IObservableValue;
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
@@ -64,7 +65,7 @@
      * @param monitor progress monitor
      * @throws CoreException
      */
-    void addFile(IProject project, IPath name, InputStream contents, IProgressMonitor monitor) throws CoreException;
+    IFile addFile(IProject project, IPath name, InputStream contents, IProgressMonitor monitor) throws CoreException;
 
     /**
      * Allows binding to parameter value from UI.
--- a/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/ui/project/ITemplateInstaller.java	Mon Aug 16 16:23:25 2010 -0700
+++ b/org.symbian.tools.mtw.ui/src/org/symbian/tools/tmw/ui/project/ITemplateInstaller.java	Tue Aug 17 13:40:28 2010 -0700
@@ -22,6 +22,7 @@
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.operation.IRunnableWithProgress;
 
 public interface ITemplateInstaller {
     /**
@@ -45,4 +46,10 @@
      */
     void copyFiles(IPath[] files, IProgressMonitor monitor) throws CoreException;
 
+    /**
+     * These actions will be ran after the new project setup completes. They will be
+     * ran in SWT thread.
+     * @return runnable that will be ran after the project create finishes. Can be null.
+     */
+    IRunnableWithProgress getPostCreateAction();
 }
--- a/org.symbian.tools.wrttools/plugin.xml	Mon Aug 16 16:23:25 2010 -0700
+++ b/org.symbian.tools.wrttools/plugin.xml	Tue Aug 17 13:40:28 2010 -0700
@@ -840,7 +840,8 @@
             name="Flickr Project"
             weight="3">
          <archive
-               file="projecttemplates/flickr.zip">
+               file="projecttemplates/flickr.zip"
+               open-files="${mainHtml}.html">
          </archive>
          <description>
             This wizard generates an Flickr project with a minimal Info.plist, html,css and js and WRTKit.