17 * Failures and causes: |
17 * Failures and causes: |
18 */ |
18 */ |
19 package org.symbian.tools.wrttools.util; |
19 package org.symbian.tools.wrttools.util; |
20 |
20 |
21 import java.io.File; |
21 import java.io.File; |
|
22 import java.io.FileInputStream; |
22 import java.io.IOException; |
23 import java.io.IOException; |
23 import java.net.URI; |
24 import java.net.URI; |
|
25 import java.text.MessageFormat; |
|
26 import java.util.zip.ZipEntry; |
24 import java.util.zip.ZipInputStream; |
27 import java.util.zip.ZipInputStream; |
25 |
28 |
26 import javax.swing.filechooser.FileSystemView; |
29 import javax.swing.filechooser.FileSystemView; |
27 |
30 |
|
31 import org.eclipse.core.resources.IContainer; |
28 import org.eclipse.core.resources.IFile; |
32 import org.eclipse.core.resources.IFile; |
|
33 import org.eclipse.core.resources.IFolder; |
29 import org.eclipse.core.resources.IProject; |
34 import org.eclipse.core.resources.IProject; |
30 import org.eclipse.core.resources.IProjectDescription; |
35 import org.eclipse.core.resources.IProjectDescription; |
|
36 import org.eclipse.core.resources.IResource; |
31 import org.eclipse.core.resources.IWorkspace; |
37 import org.eclipse.core.resources.IWorkspace; |
32 import org.eclipse.core.resources.ResourcesPlugin; |
38 import org.eclipse.core.resources.ResourcesPlugin; |
33 import org.eclipse.core.runtime.CoreException; |
39 import org.eclipse.core.runtime.CoreException; |
|
40 import org.eclipse.core.runtime.IPath; |
34 import org.eclipse.core.runtime.IProgressMonitor; |
41 import org.eclipse.core.runtime.IProgressMonitor; |
35 import org.eclipse.core.runtime.NullProgressMonitor; |
42 import org.eclipse.core.runtime.NullProgressMonitor; |
|
43 import org.eclipse.core.runtime.Path; |
36 import org.eclipse.core.runtime.SubProgressMonitor; |
44 import org.eclipse.core.runtime.SubProgressMonitor; |
37 import org.eclipse.wst.jsdt.internal.ui.wizards.buildpaths.BuildPathsBlock; |
45 import org.eclipse.wst.jsdt.internal.ui.wizards.buildpaths.BuildPathsBlock; |
38 import org.eclipse.wst.validation.ValidationFramework; |
46 import org.eclipse.wst.validation.ValidationFramework; |
39 import org.symbian.tools.wrttools.Activator; |
47 import org.symbian.tools.wrttools.Activator; |
40 import org.symbian.tools.wrttools.WidgetProjectNature; |
48 import org.symbian.tools.wrttools.WidgetProjectNature; |
43 private static final String DEFAULT_APTANA_WORKSPACE = "Aptana Studio Workspace"; |
51 private static final String DEFAULT_APTANA_WORKSPACE = "Aptana Studio Workspace"; |
44 public static final String PREVIEW_FOLDER = "preview"; |
52 public static final String PREVIEW_FOLDER = "preview"; |
45 public static final String PREVIEW_FRAME_FILE = "wrt_preview_frame.html"; |
53 public static final String PREVIEW_FRAME_FILE = "wrt_preview_frame.html"; |
46 public static final String PREVIEW_MAIN_FILE = "wrt_preview_main.html"; |
54 public static final String PREVIEW_MAIN_FILE = "wrt_preview_main.html"; |
47 |
55 |
|
56 private static boolean isDefaultProjectLocation(URI uri) { |
|
57 File file = new File(uri); |
|
58 IPath project = new Path(file.getAbsolutePath()); |
|
59 IPath workspace = ResourcesPlugin.getWorkspace().getRoot() |
|
60 .getLocation(); |
|
61 return workspace.isPrefixOf(project); |
|
62 } |
|
63 |
48 @SuppressWarnings("restriction") |
64 @SuppressWarnings("restriction") |
49 public static IProject createWrtProject(String name, URI uri, |
65 public static IProject createWrtProject(String name, URI uri, |
50 IProgressMonitor monitor) throws CoreException { |
66 IProgressMonitor monitor) throws CoreException { |
|
67 uri = isDefaultProjectLocation(uri) ? null |
|
68 : uri; |
51 monitor.beginTask("Create project resources", 20); |
69 monitor.beginTask("Create project resources", 20); |
52 IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
70 IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
53 IProject project = workspace.getRoot().getProject(name); |
71 IProject project = workspace.getRoot().getProject(name); |
54 BuildPathsBlock.createProject(project, uri, new SubProgressMonitor( |
72 BuildPathsBlock.createProject(project, uri, new SubProgressMonitor( |
55 monitor, 10)); |
73 monitor, 10)); |
127 |
145 |
128 public static File isAptanaProject(File[] contents) { |
146 public static File isAptanaProject(File[] contents) { |
129 File dotProjectFile = null; |
147 File dotProjectFile = null; |
130 boolean hasPreviewer = false; |
148 boolean hasPreviewer = false; |
131 boolean hasFrame = false; |
149 boolean hasFrame = false; |
132 |
150 |
133 for (int i = 0; i < contents.length; i++) { |
151 for (int i = 0; i < contents.length; i++) { |
134 File file = contents[i]; |
152 File file = contents[i]; |
135 if (file.isFile() && file.getName().equals(IProjectDescription.DESCRIPTION_FILE_NAME)) { |
153 if (file.isFile() |
|
154 && file.getName().equals( |
|
155 IProjectDescription.DESCRIPTION_FILE_NAME)) { |
136 dotProjectFile = file; |
156 dotProjectFile = file; |
137 } |
157 } |
138 if (file.isFile() && PREVIEW_FRAME_FILE.equalsIgnoreCase(file.getName())) { |
158 if (file.isFile() |
|
159 && PREVIEW_FRAME_FILE.equalsIgnoreCase(file.getName())) { |
139 hasFrame = true; |
160 hasFrame = true; |
140 } |
161 } |
141 if (file.isDirectory() && PREVIEW_FOLDER.equalsIgnoreCase(file.getName())) { |
162 if (file.isDirectory() |
|
163 && PREVIEW_FOLDER.equalsIgnoreCase(file.getName())) { |
142 hasPreviewer = true; |
164 hasPreviewer = true; |
143 } |
165 } |
144 } |
166 } |
145 if (!(hasFrame && hasPreviewer)) { |
167 if (!(hasFrame && hasPreviewer)) { |
146 dotProjectFile = null; |
168 dotProjectFile = null; |
147 } |
169 } |
148 return dotProjectFile; |
170 return dotProjectFile; |
149 } |
171 } |
|
172 |
|
173 public static void unzip(String archiveFile, IContainer location, int trimSegments, |
|
174 IProgressMonitor progressMonitor) throws IOException, CoreException { |
|
175 progressMonitor.beginTask(MessageFormat.format("Unpacking {0}", |
|
176 archiveFile), IProgressMonitor.UNKNOWN); |
|
177 ZipInputStream stream = new ZipInputStream(new FileInputStream( |
|
178 archiveFile)); |
|
179 |
|
180 try { |
|
181 ZipEntry nextEntry; |
|
182 while ((nextEntry = stream.getNextEntry()) != null) { |
|
183 IPath p = new Path(nextEntry.getName()).removeFirstSegments(trimSegments); |
|
184 if (!nextEntry.isDirectory()) { |
|
185 IFile file = location.getFile(p); |
|
186 checkParent(file.getParent()); |
|
187 file.create(new NonClosingStream(stream), false, |
|
188 new SubProgressMonitor(progressMonitor, 1)); |
|
189 } |
|
190 } |
|
191 } finally { |
|
192 stream.close(); |
|
193 } |
|
194 progressMonitor.done(); |
|
195 } |
|
196 |
|
197 private static void checkParent(IContainer parent) throws CoreException { |
|
198 if (parent.getType() == IResource.FOLDER && !parent.exists()) { |
|
199 checkParent(parent.getParent()); |
|
200 ((IFolder) parent).create(false, true, new NullProgressMonitor()); |
|
201 } |
|
202 } |
150 } |
203 } |