30 import java.io.Writer; |
30 import java.io.Writer; |
31 import java.util.Arrays; |
31 import java.util.Arrays; |
32 import java.util.Collection; |
32 import java.util.Collection; |
33 import java.util.HashSet; |
33 import java.util.HashSet; |
34 import java.util.LinkedList; |
34 import java.util.LinkedList; |
|
35 import java.util.Set; |
35 import java.util.zip.ZipEntry; |
36 import java.util.zip.ZipEntry; |
36 import java.util.zip.ZipInputStream; |
37 import java.util.zip.ZipInputStream; |
37 |
38 |
38 import org.apache.velocity.VelocityContext; |
39 import org.apache.velocity.VelocityContext; |
39 import org.apache.velocity.app.Velocity; |
40 import org.apache.velocity.app.Velocity; |
40 import org.apache.velocity.context.AbstractContext; |
41 import org.apache.velocity.context.AbstractContext; |
|
42 import org.eclipse.core.resources.IFile; |
41 import org.eclipse.core.resources.IProject; |
43 import org.eclipse.core.resources.IProject; |
42 import org.eclipse.core.runtime.CoreException; |
44 import org.eclipse.core.runtime.CoreException; |
43 import org.eclipse.core.runtime.FileLocator; |
45 import org.eclipse.core.runtime.FileLocator; |
44 import org.eclipse.core.runtime.IConfigurationElement; |
46 import org.eclipse.core.runtime.IConfigurationElement; |
45 import org.eclipse.core.runtime.IPath; |
47 import org.eclipse.core.runtime.IPath; |
48 import org.eclipse.core.runtime.InvalidRegistryObjectException; |
50 import org.eclipse.core.runtime.InvalidRegistryObjectException; |
49 import org.eclipse.core.runtime.Path; |
51 import org.eclipse.core.runtime.Path; |
50 import org.eclipse.core.runtime.Platform; |
52 import org.eclipse.core.runtime.Platform; |
51 import org.eclipse.core.runtime.Status; |
53 import org.eclipse.core.runtime.Status; |
52 import org.eclipse.core.runtime.SubProgressMonitor; |
54 import org.eclipse.core.runtime.SubProgressMonitor; |
|
55 import org.eclipse.jface.operation.IRunnableWithProgress; |
|
56 import org.symbian.tools.tmw.internal.util.OpenFilesRunnable; |
|
57 import org.symbian.tools.tmw.internal.util.Util; |
53 import org.symbian.tools.tmw.ui.TMWCoreUI; |
58 import org.symbian.tools.tmw.ui.TMWCoreUI; |
54 import org.symbian.tools.tmw.ui.project.IProjectTemplateContext; |
59 import org.symbian.tools.tmw.ui.project.IProjectTemplateContext; |
55 import org.symbian.tools.tmw.ui.project.ITemplateInstaller; |
60 import org.symbian.tools.tmw.ui.project.ITemplateInstaller; |
56 |
61 |
57 /** |
62 /** |
63 private static final String TEMPLATE_FILE_EXTENSION = ".velocitytemplate"; |
68 private static final String TEMPLATE_FILE_EXTENSION = ".velocitytemplate"; |
64 private final IConfigurationElement element; |
69 private final IConfigurationElement element; |
65 private IProject project; |
70 private IProject project; |
66 private IProjectTemplateContext context; |
71 private IProjectTemplateContext context; |
67 private String[] paths; |
72 private String[] paths; |
|
73 private final Set<IFile> openFiles = new HashSet<IFile>(); |
68 |
74 |
69 public ZipInstaller(IConfigurationElement element) { |
75 public ZipInstaller(IConfigurationElement element) { |
70 this.element = element; |
76 this.element = element; |
71 try { |
77 try { |
72 Velocity.init(); |
78 Velocity.init(); |
145 final ZipInputStream inputStream = new ZipInputStream(stream); |
152 final ZipInputStream inputStream = new ZipInputStream(stream); |
146 return inputStream; |
153 return inputStream; |
147 } |
154 } |
148 |
155 |
149 public void copyFiles(IPath[] files, IProgressMonitor monitor) throws CoreException { |
156 public void copyFiles(IPath[] files, IProgressMonitor monitor) throws CoreException { |
|
157 final String filesToOpen = Util.neverNull(element.getAttribute("open-files")); |
150 HashSet<IPath> fls = new HashSet<IPath>(Arrays.asList(files)); |
158 HashSet<IPath> fls = new HashSet<IPath>(Arrays.asList(files)); |
151 ZipInputStream stream = null; |
159 ZipInputStream stream = null; |
152 try { |
160 try { |
153 VelocityContext ctx = new VelocityContext(new TemplateContext(context)); |
161 VelocityContext ctx = new VelocityContext(new TemplateContext(context)); |
154 stream = openArchive(); |
162 stream = openArchive(); |
155 monitor.beginTask("Generating project contents", IProgressMonitor.UNKNOWN); |
163 monitor.beginTask("Generating project contents", IProgressMonitor.UNKNOWN); |
156 ZipEntry entry; |
164 ZipEntry entry; |
157 while ((entry = stream.getNextEntry()) != null && !monitor.isCanceled()) { |
165 while ((entry = stream.getNextEntry()) != null && !monitor.isCanceled()) { |
158 String nm = entry.getName(); |
166 String nm = entry.getName(); |
|
167 boolean open = filesToOpen.contains(nm.endsWith(TEMPLATE_FILE_EXTENSION) ? nm.substring(0, nm.length() |
|
168 - TEMPLATE_FILE_EXTENSION.length()) : nm); |
159 IPath name = new Path(filter(nm)); |
169 IPath name = new Path(filter(nm)); |
160 if (fls.contains(name)) { |
170 if (fls.contains(name)) { |
161 final InputStream contents; |
171 final InputStream contents; |
162 if (nm.endsWith(TEMPLATE_FILE_EXTENSION)) { |
172 if (nm.endsWith(TEMPLATE_FILE_EXTENSION)) { |
163 contents = copyTemplate(project, name, stream, (int) entry.getSize(), ctx, monitor); |
173 contents = copyTemplate(project, name, stream, (int) entry.getSize(), ctx, monitor); |
164 } else { |
174 } else { |
165 contents = new NonClosingStream(stream); |
175 contents = new NonClosingStream(stream); |
166 } |
176 } |
167 context.addFile(project, name, contents, new SubProgressMonitor(monitor, 10)); |
177 IFile file = context.addFile(project, name, contents, new SubProgressMonitor(monitor, 10)); |
|
178 if (open) { |
|
179 openFiles.add(file); |
|
180 } |
168 } |
181 } |
169 stream.closeEntry(); |
182 stream.closeEntry(); |
170 } |
183 } |
171 monitor.done(); |
184 monitor.done(); |
172 } catch (Exception e) { |
185 } catch (Exception e) { |
181 } |
194 } |
182 } |
195 } |
183 } |
196 } |
184 |
197 |
185 private InputStream copyTemplate(IProject project, IPath name, ZipInputStream stream, int size, |
198 private InputStream copyTemplate(IProject project, IPath name, ZipInputStream stream, int size, |
186 VelocityContext ctx, |
199 VelocityContext ctx, IProgressMonitor monitor) throws IOException, CoreException { |
187 IProgressMonitor monitor) throws IOException, CoreException { |
|
188 // Templates will not be more then a few megs - we can afford the memory |
200 // Templates will not be more then a few megs - we can afford the memory |
189 ByteArrayOutputStream file = new ByteArrayOutputStream(); |
201 ByteArrayOutputStream file = new ByteArrayOutputStream(); |
190 |
202 |
191 Reader reader = new InputStreamReader(new NonClosingStream(stream)); |
203 Reader reader = new InputStreamReader(new NonClosingStream(stream)); |
192 Writer writer = new OutputStreamWriter(file); |
204 Writer writer = new OutputStreamWriter(file); |