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