org.symbian.tools.wrttools/src/org/symbian/tools/wrttools/util/ProjectUtils.java
changeset 11 05e53cfc29d8
parent 3 d3477de62514
child 14 e3d48d24826c
equal deleted inserted replaced
7:1a02e8a8be9c 11:05e53cfc29d8
       
     1 /**
       
     2  * Copyright (c) 2009 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.wrttools.util;
       
    20 
       
    21 import java.io.BufferedInputStream;
       
    22 import java.io.File;
       
    23 import java.io.FileInputStream;
       
    24 import java.io.FileNotFoundException;
       
    25 import java.io.FileOutputStream;
       
    26 import java.io.IOException;
       
    27 import java.io.InputStream;
       
    28 import java.io.OutputStream;
       
    29 import java.net.URI;
       
    30 import java.util.zip.ZipEntry;
       
    31 import java.util.zip.ZipInputStream;
       
    32 import java.util.zip.ZipOutputStream;
       
    33 
       
    34 import javax.swing.filechooser.FileSystemView;
       
    35 
       
    36 import org.eclipse.core.resources.IFile;
       
    37 import org.eclipse.core.resources.IFolder;
       
    38 import org.eclipse.core.resources.IProject;
       
    39 import org.eclipse.core.resources.IWorkspace;
       
    40 import org.eclipse.core.resources.ResourcesPlugin;
       
    41 import org.eclipse.core.runtime.CoreException;
       
    42 import org.eclipse.core.runtime.IPath;
       
    43 import org.eclipse.core.runtime.IProgressMonitor;
       
    44 import org.eclipse.core.runtime.IStatus;
       
    45 import org.eclipse.core.runtime.NullProgressMonitor;
       
    46 import org.eclipse.core.runtime.Path;
       
    47 import org.eclipse.core.runtime.Status;
       
    48 import org.eclipse.core.runtime.SubProgressMonitor;
       
    49 import org.eclipse.jface.window.Window;
       
    50 import org.eclipse.swt.widgets.Display;
       
    51 import org.eclipse.ui.PlatformUI;
       
    52 import org.eclipse.ui.statushandlers.StatusManager;
       
    53 import org.eclipse.wst.jsdt.internal.ui.wizards.buildpaths.BuildPathsBlock;
       
    54 import org.eclipse.wst.validation.ValidationFramework;
       
    55 import org.symbian.tools.wrttools.Activator;
       
    56 import org.symbian.tools.wrttools.dialogs.AptanaProjectSelectionDialog;
       
    57 
       
    58 public class ProjectUtils {
       
    59 	public static IProject createWrtProject(String name, URI uri,
       
    60 			IProgressMonitor monitor) throws CoreException {
       
    61 		monitor.beginTask("Create project resources", 20);
       
    62 		IWorkspace workspace = ResourcesPlugin.getWorkspace();
       
    63 		IProject project = workspace.getRoot().getProject(name);
       
    64 		BuildPathsBlock.createProject(project, uri, new SubProgressMonitor(
       
    65 				monitor, 10));
       
    66 
       
    67 		BuildPathsBlock.addJavaNature(project, new SubProgressMonitor(monitor,
       
    68 				10));
       
    69 
       
    70 		// TODO: Build path, super type, etc.
       
    71 		// BuildPathsBlock.flush(classPathEntries, javaScriptProject, superType,
       
    72 		// monitor)
       
    73 
       
    74 		ValidationFramework.getDefault().addValidationBuilder(project);
       
    75 
       
    76 		monitor.done();
       
    77 		return project;
       
    78 	}
       
    79 
       
    80 	public static void addPreviewer(IProject project, IPath mainHtml) {
       
    81 		URI archive = getPreviewerArchive();
       
    82 		try {
       
    83 			if (archive != null) {
       
    84 				ZipInputStream stream = new ZipInputStream(archive.toURL().openStream());
       
    85 				ZipEntry entry;
       
    86 				while ((entry = stream.getNextEntry()) != null) {
       
    87 					if (!entry.isDirectory()) {
       
    88 						copyFile(project, entry.getName(), stream, entry.getSize(), new NullProgressMonitor());
       
    89 					} else {
       
    90 						IFolder folder = project.getFolder(entry.getName());
       
    91 						if (!folder.exists()) {
       
    92 							folder.create(false, true, new NullProgressMonitor());
       
    93 						}
       
    94 					}
       
    95 					stream.closeEntry();
       
    96 				}
       
    97 			}
       
    98 			IFile file = project.getFile(mainHtml + ".html");
       
    99 			if (file.exists()) {
       
   100 				file.copy(project.getFullPath().append("wrt_preview_main.html"), false, new NullProgressMonitor());
       
   101 			}
       
   102 			
       
   103 		} catch (IOException e) {
       
   104 			StatusManager.getManager().handle(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unable to add previewer to project"));
       
   105 		} catch (CoreException e) {
       
   106 			StatusManager.getManager().handle(e, Activator.PLUGIN_ID);
       
   107 		}
       
   108 	}
       
   109 
       
   110 	private static URI getPreviewerArchive() {
       
   111 		File file = getPreviewerZip();
       
   112 		if (file.isFile()) {
       
   113 			return file.toURI();
       
   114 		}
       
   115 		Display display = Display.getDefault();
       
   116 		display.syncExec(new Runnable() {
       
   117 			@Override
       
   118 			public void run() {
       
   119 				importPreviewer();
       
   120 			}
       
   121 		});
       
   122 		if (file.isFile()) {
       
   123 			return file.toURI();
       
   124 		}
       
   125 		return null;
       
   126 	}
       
   127 
       
   128 	private static File getPreviewerZip() {
       
   129 		return Activator.getDefault().getStateLocation()
       
   130 				.append("previewer.zip").toFile();
       
   131 	}
       
   132 
       
   133 	protected static void importPreviewer() {
       
   134 		AptanaProjectSelectionDialog dialog = new AptanaProjectSelectionDialog(
       
   135 				PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
       
   136 		int open = dialog.open();
       
   137 		if (open == Window.OK) {
       
   138 			File project = dialog.getProject();
       
   139 			try {
       
   140 				zipPreviewer(project);
       
   141 			} catch (IOException e) {
       
   142 				StatusManager
       
   143 						.getManager()
       
   144 						.handle(
       
   145 								new Status(
       
   146 										IStatus.ERROR,
       
   147 										Activator.PLUGIN_ID,
       
   148 										"Failed to create Web Runtime previewer archive.",
       
   149 										e), StatusManager.SHOW);
       
   150 			}
       
   151 		}
       
   152 	}
       
   153 
       
   154 	private static void zipPreviewer(File project) throws IOException {
       
   155 		ZipOutputStream stream = new ZipOutputStream(new FileOutputStream(
       
   156 				getPreviewerZip()));
       
   157 		try {
       
   158 			zip(new File(project, "preview"), stream, "preview/");
       
   159 			zipFile(new File(project, "wrt_preview_frame.html"),
       
   160 					"wrt_preview_frame.html", stream);
       
   161 		} finally {
       
   162 			stream.close();
       
   163 		}
       
   164 	}
       
   165 
       
   166 	private static void zip(File folder, ZipOutputStream stream, String path)
       
   167 			throws IOException {
       
   168 		ZipEntry entry = new ZipEntry(path);
       
   169 		stream.putNextEntry(entry);
       
   170 		stream.closeEntry();
       
   171 		File[] files = folder.listFiles();
       
   172 		for (File file : files) {
       
   173 			if (file.isFile()) {
       
   174 				zipFile(file, path + file.getName(), stream);
       
   175 			} else {
       
   176 				zip(file, stream, path + file.getName() + "/");
       
   177 			}
       
   178 		}
       
   179 	}
       
   180 
       
   181 	private static void zipFile(File file, String zipEntry,
       
   182 			ZipOutputStream stream) throws IOException, FileNotFoundException {
       
   183 		ZipEntry entry = new ZipEntry(zipEntry);
       
   184 		stream.putNextEntry(entry);
       
   185 		BufferedInputStream inputStream = new BufferedInputStream(
       
   186 				new FileInputStream(file));
       
   187 		try {
       
   188 			copy(inputStream, stream);
       
   189 		} finally {
       
   190 			inputStream.close();
       
   191 		}
       
   192 		stream.closeEntry();
       
   193 	}
       
   194 
       
   195 	private static void copy(InputStream in, OutputStream out)
       
   196 			throws IOException {
       
   197 		byte[] buffer = new byte[131072]; // 128k - should be enough for most
       
   198 		// JS/CSS files
       
   199 		int count;
       
   200 		while ((count = in.read(buffer)) > 0) {
       
   201 			out.write(buffer, 0, count);
       
   202 		}
       
   203 	}
       
   204 
       
   205 	public static String getDefaultAptanaLocation() {
       
   206 		File myDocuments = FileSystemView.getFileSystemView()
       
   207 				.getDefaultDirectory();
       
   208 		File file = new File(myDocuments, "Aptana Studio Workspace");
       
   209 		return file.exists() ? file.getAbsolutePath() : "";
       
   210 	}
       
   211 
       
   212 	public static boolean isAptanaProject(File f) {
       
   213 		return new File(f, "preview").isDirectory()
       
   214 				&& new File(f, "wrt_preview_frame.html").isFile();
       
   215 	}
       
   216 
       
   217 	public static void copyFile(IProject project, String name, ZipInputStream stream,
       
   218 			long size, IProgressMonitor monitor) throws CoreException,
       
   219 			IOException {
       
   220 		IFile file = project.getFile(name);
       
   221 		file.create(new NonClosingStream(stream), true,
       
   222 				new SubProgressMonitor(monitor, 1));
       
   223 	}
       
   224 }