1 11 package org.eclipse.pde.internal.ui.samples; 12 13 import java.io.ByteArrayInputStream ; 14 import java.io.ByteArrayOutputStream ; 15 import java.io.IOException ; 16 import java.io.UnsupportedEncodingException ; 17 import java.lang.reflect.InvocationTargetException ; 18 import java.net.URL ; 19 import java.util.Properties ; 20 import java.util.zip.ZipFile ; 21 22 import org.eclipse.core.resources.IFile; 23 import org.eclipse.core.resources.IProject; 24 import org.eclipse.core.resources.IWorkspace; 25 import org.eclipse.core.resources.IWorkspaceRoot; 26 import org.eclipse.core.resources.IWorkspaceRunnable; 27 import org.eclipse.core.runtime.CoreException; 28 import org.eclipse.core.runtime.FileLocator; 29 import org.eclipse.core.runtime.IConfigurationElement; 30 import org.eclipse.core.runtime.IPath; 31 import org.eclipse.core.runtime.IProgressMonitor; 32 import org.eclipse.core.runtime.IStatus; 33 import org.eclipse.core.runtime.NullProgressMonitor; 34 import org.eclipse.core.runtime.OperationCanceledException; 35 import org.eclipse.core.runtime.Platform; 36 import org.eclipse.core.runtime.Status; 37 import org.eclipse.core.runtime.SubProgressMonitor; 38 import org.eclipse.jface.operation.IRunnableWithProgress; 39 import org.eclipse.pde.internal.ui.IPDEUIConstants; 40 import org.eclipse.pde.internal.ui.PDEPlugin; 41 import org.eclipse.pde.internal.ui.PDEUIMessages; 42 import org.eclipse.ui.dialogs.IOverwriteQuery; 43 import org.eclipse.ui.wizards.datatransfer.ImportOperation; 44 import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider; 45 import org.osgi.framework.Bundle; 46 47 public class SampleOperation implements IRunnableWithProgress { 48 private static final String SAMPLE_PROPERTIES = "sample.properties"; 50 private IConfigurationElement sample; 51 52 private String [] projectNames; 53 54 private IFile sampleManifest; 55 56 private IOverwriteQuery query; 57 58 private boolean yesToAll; 59 60 private boolean cancel; 61 62 private IProject[] createdProjects; 63 64 67 public SampleOperation(IConfigurationElement sample, String [] projectNames, 68 IOverwriteQuery query) { 69 this.sample = sample; 70 this.query = query; 71 this.projectNames = projectNames; 72 } 73 74 public IFile getSampleManifest() { 75 return sampleManifest; 76 } 77 78 public IProject[] getCreatedProjects() { 79 return createdProjects; 80 } 81 82 87 public void run(IProgressMonitor monitor) throws InvocationTargetException , 88 InterruptedException { 89 try { 90 IWorkspaceRunnable op = new IWorkspaceRunnable() { 91 public void run(IProgressMonitor monitor) throws CoreException { 92 IConfigurationElement[] projects = sample 93 .getChildren("project"); monitor 95 .beginTask( 96 PDEUIMessages.SampleOperation_creating, 4 * projects.length); 97 createdProjects = new IProject[projects.length]; 98 try { 99 for (int i = 0; i < projects.length; i++) { 100 IFile file = importProject(projectNames[i], 101 projects[i], new SubProgressMonitor(monitor, 4)); 102 if (file != null && sampleManifest == null) 103 sampleManifest = file; 104 if (file != null) { 105 createdProjects[i] = file.getProject(); 106 } 107 if(cancel) 108 break; 110 } 111 } 112 catch (InterruptedException e) { 113 throw new OperationCanceledException(); 114 } 115 catch (InvocationTargetException e) { 116 throwCoreException(e); 117 } 118 } 119 }; 120 PDEPlugin.getWorkspace().run(op, monitor); 121 } catch (CoreException e) { 122 throw new InvocationTargetException (e); 123 } catch (OperationCanceledException e) { 124 throw e; 125 } finally { 126 monitor.done(); 127 } 128 } 129 130 private void throwCoreException(InvocationTargetException e) throws CoreException { 131 Throwable t = e.getCause(); 132 Status status= new Status(IStatus.ERROR, 133 IPDEUIConstants.PLUGIN_ID, 134 IStatus.OK, 135 e.getMessage(), 136 t); 137 throw new CoreException(status); 138 } 139 140 private IFile importProject(String name, IConfigurationElement config, 141 IProgressMonitor monitor) throws CoreException, 142 InvocationTargetException , InterruptedException { 143 String path = config.getAttribute("archive"); if (name == null || path == null) 145 return null; 146 IWorkspace workspace = PDEPlugin.getWorkspace(); 147 IWorkspaceRoot root = workspace.getRoot(); 148 IProject project = root.getProject(name); 149 boolean skip = false; 150 if (project.exists()) { 151 if (!yesToAll) { 152 String returnId = query.queryOverwrite(project.getFullPath() 153 .toString()); 154 if (returnId.equals(IOverwriteQuery.ALL)) { 155 yesToAll = true; 156 skip = false; 157 } else if (returnId.equals(IOverwriteQuery.YES)) { 158 skip = false; 159 } else if (returnId.equals(IOverwriteQuery.NO)) { 160 skip = true; 161 } else if (returnId.equals(IOverwriteQuery.CANCEL)) { 162 skip = true; 163 cancel = true; 164 } 165 } 166 if (!skip) { 167 project.delete(true, true, new SubProgressMonitor(monitor, 1)); 168 project = root.getProject(name); 169 } else 170 monitor.worked(1); 171 } 172 if (skip) { 173 monitor.worked(3); 174 IFile manifest = project.getFile(SAMPLE_PROPERTIES); 175 return manifest; 176 } 177 178 project.create(new SubProgressMonitor(monitor, 1)); 179 project.open(new NullProgressMonitor()); 180 Bundle bundle = Platform.getBundle(sample.getNamespaceIdentifier()); 181 ZipFile zipFile = getZipFileFromPluginDir(path, bundle); 182 importFilesFromZip(zipFile, project.getFullPath(), 183 new SubProgressMonitor(monitor, 1)); 184 return createSampleManifest(project, config, new SubProgressMonitor( 185 monitor, 1)); 186 } 187 188 private IFile createSampleManifest(IProject project, 189 IConfigurationElement config, IProgressMonitor monitor) 190 throws CoreException { 191 IFile file = project.getFile(SAMPLE_PROPERTIES); 192 if (!file.exists()) { 193 try { 194 ByteArrayOutputStream out = new ByteArrayOutputStream (); 195 Properties properties = new Properties (); 196 createSampleManifestContent( 197 config.getAttribute("name"), properties); properties.store(out, ""); out.flush(); 200 String contents = out.toString(); 201 out.close(); 202 ByteArrayInputStream stream = new ByteArrayInputStream (contents 203 .getBytes("UTF8")); file.create(stream, true, monitor); 205 stream.close(); 206 } catch (UnsupportedEncodingException e) { 207 } catch (IOException e) { 208 } 209 } 210 return file; 211 } 212 213 private void createSampleManifestContent(String projectName, 214 Properties properties) { 215 writeProperty(properties, "id", sample.getAttribute("id")); writeProperty(properties, "name", sample.getAttribute("name")); writeProperty(properties, "projectName", projectName); writeProperty(properties, "launcher", sample.getAttribute("launcher")); IConfigurationElement desc[] = sample.getChildren("description"); if (desc.length == 1) { 221 writeProperty(properties, "helpHref", desc[0] .getAttribute("helpHref")); writeProperty(properties, "description", desc[0].getValue()); } 225 } 226 227 private void writeProperty(Properties properties, String name, String value) { 228 if (value == null) 229 return; 230 properties.setProperty(name, value); 231 } 232 233 private ZipFile getZipFileFromPluginDir(String pluginRelativePath, 234 Bundle bundle) throws CoreException { 235 try { 236 URL starterURL = FileLocator.resolve(bundle.getEntry(pluginRelativePath)); 237 return new ZipFile (FileLocator.toFileURL(starterURL).getFile()); 238 } catch (IOException e) { 239 String message = pluginRelativePath + ": " + e.getMessage(); Status status = new Status(IStatus.ERROR, PDEPlugin.getPluginId(), 241 IStatus.ERROR, message, e); 242 throw new CoreException(status); 243 } 244 } 245 246 private void importFilesFromZip(ZipFile srcZipFile, IPath destPath, 247 IProgressMonitor monitor) throws InvocationTargetException , 248 InterruptedException { 249 ZipFileStructureProvider structureProvider = new ZipFileStructureProvider( 250 srcZipFile); 251 ImportOperation op = new ImportOperation(destPath, structureProvider 252 .getRoot(), structureProvider, query); 253 op.run(monitor); 254 } 255 } 256 | Popular Tags |