1 26 30 31 package org.omg.lifl.eclipse.plugin.project.OpenCCM; 32 33 import java.io.File ; 34 import java.io.IOException ; 35 import java.lang.reflect.InvocationTargetException ; 36 import java.net.MalformedURLException ; 37 import java.net.URL ; 38 import java.util.zip.ZipFile ; 39 40 import org.apache.tools.ant.Project; 41 import org.apache.tools.ant.taskdefs.Copy; 42 import org.apache.tools.ant.types.FileSet; 43 import org.apache.tools.ant.types.FilterSet; 44 import org.eclipse.core.resources.IFolder; 45 import org.eclipse.core.resources.IProject; 46 import org.eclipse.core.resources.IProjectDescription; 47 import org.eclipse.core.resources.IResource; 48 import org.eclipse.core.resources.IWorkspaceRoot; 49 import org.eclipse.core.runtime.CoreException; 50 import org.eclipse.core.runtime.IConfigurationElement; 51 import org.eclipse.core.runtime.IPath; 52 import org.eclipse.core.runtime.IProgressMonitor; 53 import org.eclipse.core.runtime.IStatus; 54 import org.eclipse.core.runtime.NullProgressMonitor; 55 import org.eclipse.core.runtime.Path; 56 import org.eclipse.core.runtime.Platform; 57 import org.eclipse.core.runtime.Preferences; 58 import org.eclipse.core.runtime.Status; 59 import org.eclipse.core.runtime.SubProgressMonitor; 60 import org.eclipse.jface.operation.IRunnableWithProgress; 61 import org.eclipse.ui.dialogs.IOverwriteQuery; 62 import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider; 63 import org.eclipse.ui.wizards.datatransfer.ImportOperation; 64 import org.eclipse.ui.wizards.datatransfer.ZipFileStructureProvider; 65 import org.omg.lifl.eclipse.plugin.project.OpenCCM.utils.BuildPropertySet; 66 import org.omg.lifl.eclipse.plugin.project.OpenCCM.utils.CPManager; 67 import org.omg.lifl.eclipse.plugin.project.OpenCCM.utils.ProjectMessages; 68 69 77 public class ProjectCreationOperation implements IRunnableWithProgress { 78 79 private IOverwriteQuery fOverwriteQuery; 80 private IResource fElementToOpen; 81 private ProjectCreationWizardPage[] fPages; 82 83 87 public ProjectCreationOperation( 88 ProjectCreationWizardPage[] pages, 89 IOverwriteQuery overwriteQuery) { 90 fElementToOpen = null; 91 fPages = pages; 92 fOverwriteQuery = overwriteQuery; 93 } 94 95 98 public void run(IProgressMonitor monitor) 99 throws InvocationTargetException , InterruptedException { 100 if (monitor == null) { 101 monitor = new NullProgressMonitor(); 102 } 103 try { 104 monitor.beginTask( 105 ProjectMessages.getString( 106 "ProjectCreationOperation.op_desc.ORBacus"), 107 fPages.length); 108 IWorkspaceRoot root = MainPlugin.getWorkspace().getRoot(); 109 110 for (int i = 0; i < fPages.length; i++) { 111 createProject( 112 root, 113 fPages[i], 114 new SubProgressMonitor(monitor, 1)); 115 } 116 } finally { 117 monitor.done(); 118 } 119 } 120 121 128 private void createProject( 129 IWorkspaceRoot root, 130 ProjectCreationWizardPage page, 131 IProgressMonitor monitor) 132 throws InvocationTargetException , InterruptedException { 133 IConfigurationElement desc = 134 page.getAttributeManager().getConfigurationElement(); 135 136 IConfigurationElement[] imports = desc.getChildren("import"); 137 IConfigurationElement[] natures = desc.getChildren("nature"); 138 IConfigurationElement[] references = desc.getChildren("references"); 139 int nImports = (imports == null) ? 0 : imports.length; 140 int nNatures = (natures == null) ? 0 : natures.length; 141 int nReferences = (references == null) ? 0 : references.length; 142 143 monitor.beginTask( 144 ProjectMessages.getString("ProjectCreationOperation.op_desc_proj"), 145 nImports + 1); 146 147 String name = page.getProjectChooser().get_ProjectName(); 149 String [] natureIds = new String [nNatures]; 150 for (int i = 0; i < nNatures; i++) { 151 natureIds[i] = natures[i].getAttribute("id"); 152 } 153 IProject[] referencedProjects = new IProject[nReferences]; 154 for (int i = 0; i < nReferences; i++) { 155 referencedProjects[i] = 156 root.getProject(references[i].getAttribute("id")); 157 } 158 159 IProject proj = 160 configNewProject( 161 root, 162 name, 163 natureIds, 164 referencedProjects, 165 monitor); 166 167 for (int i = 0; i < nImports; i++) { 168 doImports(proj, imports[i], new SubProgressMonitor(monitor, 1)); 169 } 170 171 String open = desc.getAttribute("open"); 172 if (open != null && open.length() > 0) { 173 IResource fileToOpen = proj.findMember(new Path(open)); 174 if (fileToOpen != null) { 175 fElementToOpen = fileToOpen; 176 } 177 } 178 179 generateFile( 181 proj, 182 page, 183 monitor, 184 desc.getAttribute("BuildPropertiesFileLocation"), 185 desc.getAttribute("BuildPropertiesFileName"), 186 page.getOpenccmConfigChooser().getCheckGroup().isSubGroupVisible()); 187 188 importOpenCCMsrc(page, proj, monitor); 190 191 configureEclipse(page, proj); 192 193 CPManager.setEclipseCP(page, monitor); 195 } 196 197 202 private void importOpenCCMsrc( 203 ProjectCreationWizardPage page, 204 IProject proj, 205 IProgressMonitor monitor) 206 throws InvocationTargetException , InterruptedException { 207 if (page.getCvsFSchooser().getCheckGroup().isSubGroupVisible()) { 209 Project antProject = new Project(); 210 antProject.init(); 211 Copy copy = new Copy(); 212 copy.setProject(antProject); 213 FileSet tmpSRC = new FileSet(); 214 tmpSRC.setDir(new File (page.getCvsFSchooser().get_CVSsrcFSPathValue())); 215 copy.addFileset(tmpSRC); 216 copy.setTodir(proj.getLocation().toFile()); 217 copy.setIncludeEmptyDirs(true); 218 copy.setOverwrite(true); 219 copy.setVerbose(true); 220 copy.setPreserveLastModified(true); 221 copy.perform(); 222 223 228 try { 229 proj.refreshLocal(IResource.DEPTH_INFINITE,new SubProgressMonitor(monitor, 1)); 230 } catch (CoreException e) { 231 e.printStackTrace(); 233 } 234 } 235 236 } 237 238 241 private void configureEclipse( 242 ProjectCreationWizardPage page, 243 IProject proj) { 244 if (page 245 .getEclipseConfigChooser() 246 .getCheckGroup() 247 .isSubGroupVisible()) { 248 if (page 249 .getEclipseConfigChooser() 250 .get_ImportWorkbenchPreference() 251 .isEnable()) { 252 String outDirectory = 253 page 254 .getEclipseConfigChooser() 255 .get_ImportWorkbenchPreference() 256 .get_Name(); 257 258 if (new File (outDirectory).exists()) 259 backupWorkbenchPreference( 260 new Path( 261 new Path(outDirectory) 262 .addTrailingSeparator() 263 .toOSString() 264 + "eclipseBackup.epf")); 265 266 importWorkbenchPreference( 267 new Path( 268 proj.getLocation().addTrailingSeparator().toOSString() 269 + "eclipse_Config.epf")); 270 } 271 } 272 } 273 274 282 private void generateFile( 283 IProject proj, 284 ProjectCreationWizardPage page, 285 IProgressMonitor monitor, 286 String fileLocation, 287 String fileName, 288 boolean generate) { 289 if (!generate) 290 return; 291 292 BuildPropertySet buildConfig = new BuildPropertySet(page); 293 294 297 try { 298 Project antProject = new Project(); 299 antProject.init(); 300 Copy buildCopy = new Copy(); 301 buildCopy.setProject(antProject); 302 File in = null; 303 String inBuildPropertiesFile = fileLocation; 304 String buildPropertiesFileName = fileName; 305 if (inBuildPropertiesFile != null 306 && buildPropertiesFileName != null) { 307 in = getFSFromPluginDir(inBuildPropertiesFile); 308 buildCopy.setFile(in.getAbsoluteFile()); 309 File out = 310 new File ( 311 proj.getLocation().toFile(), 312 buildPropertiesFileName); 313 buildCopy.setTofile(out.getAbsoluteFile()); 314 FilterSet fs = buildCopy.createFilterSet(); 315 buildConfig.createOpenCCMFilter(fs); 316 buildCopy.perform(); 317 proj.refreshLocal( 318 IResource.DEPTH_INFINITE, 319 new SubProgressMonitor(monitor, 1)); 320 } 321 } catch (CoreException e) { 322 e.printStackTrace(); 324 } 325 326 } 327 328 331 private void backupWorkbenchPreference(IPath exportFile) { 332 try { 334 Preferences.exportPreferences(exportFile); 335 } catch (CoreException e) { 336 e.printStackTrace(); 338 } 339 } 340 341 345 private void importWorkbenchPreference(IPath importFile) { 346 try { 348 Preferences.importPreferences(importFile); 349 } catch (CoreException e) { 350 e.printStackTrace(); 352 } 353 } 354 355 362 private void doImports( 363 IProject project, 364 IConfigurationElement curr, 365 SubProgressMonitor monitor) 366 throws InvocationTargetException , InterruptedException { 367 try { 368 IPath destPath; 369 String name = curr.getAttribute("dest"); 370 if (name == null || name.length() == 0) { 371 destPath = project.getFullPath(); 372 } else { 373 IFolder folder = project.getFolder(name); 374 if (!folder.exists()) { 375 folder.create(true, true, null); 376 } 377 destPath = folder.getFullPath(); 378 } 379 380 String importPath = curr.getAttribute("src"); 381 if (importPath == null) { 382 importPath = ""; 383 MainPlugin.log("projectsetup descriptor: import missing"); 384 return; 385 } 386 String typeImport = curr.getAttribute("type"); 388 if (typeImport == null) { 389 typeImport = "zip"; 390 } 391 392 if (typeImport.compareTo("zip") == 0) { 393 ZipFile zipFile = getZipFileFromPluginDir(importPath); 394 importFilesFromZip( 395 zipFile, 396 destPath, 397 new SubProgressMonitor(monitor, 1)); 398 } else { 399 File srcFS = getFSFromPluginDir(importPath); 400 importFilesFromFS( 401 srcFS, 402 destPath, 403 new SubProgressMonitor(monitor, 1)); 404 } 405 406 } catch (CoreException e) { 407 throw new InvocationTargetException (e); 408 } 409 } 410 411 418 private void importFilesFromFS( 419 File srcDir, 420 IPath destPath, 421 IProgressMonitor monitor) 422 throws InvocationTargetException , InterruptedException { 423 ImportOperation op = 424 new ImportOperation( 425 destPath, 426 srcDir, 427 FileSystemStructureProvider.INSTANCE, 428 fOverwriteQuery); 429 op.setCreateContainerStructure(false); 430 op.run(monitor); 431 } 432 433 440 private void importFilesFromZip( 441 ZipFile srcZipFile, 442 IPath destPath, 443 IProgressMonitor monitor) 444 throws InvocationTargetException , InterruptedException { 445 ZipFileStructureProvider structureProvider = 446 new ZipFileStructureProvider(srcZipFile); 447 ImportOperation op = 448 new ImportOperation( 449 destPath, 450 structureProvider.getRoot(), 451 structureProvider, 452 fOverwriteQuery); 453 op.run(monitor); 454 } 455 456 461 private ZipFile getZipFileFromPluginDir(String pluginRelativePath) 462 throws CoreException { 463 try { 464 URL starterURL = 465 new URL ( 466 MainPlugin.getDefault().getDescriptor().getInstallURL(), 467 pluginRelativePath); 468 return new ZipFile (Platform.asLocalURL(starterURL).getFile()); 469 } catch (IOException e) { 470 String message = pluginRelativePath + ": " + e.getMessage(); 471 Status status = 472 new Status( 473 IStatus.ERROR, 474 MainPlugin.getPluginId(), 475 IStatus.ERROR, 476 message, 477 e); 478 throw new CoreException(status); 479 } 480 } 481 482 487 private File getFSFromPluginDir(String pluginRelativePath) 488 throws CoreException { 489 URL starterURL; 490 try { 491 starterURL = 492 new URL ( 493 MainPlugin.getDefault().getDescriptor().getInstallURL(), 494 pluginRelativePath); 495 496 return new File (Platform.asLocalURL(starterURL).getPath()); 497 } catch (MalformedURLException e) { 498 String message = pluginRelativePath + ": " + e.getMessage(); 499 Status status = 500 new Status( 501 IStatus.ERROR, 502 MainPlugin.getPluginId(), 503 IStatus.ERROR, 504 message, 505 e); 506 throw new CoreException(status); 507 } catch (IOException e) { 508 String message = pluginRelativePath + ": " + e.getMessage(); 509 Status status = 510 new Status( 511 IStatus.ERROR, 512 MainPlugin.getPluginId(), 513 IStatus.ERROR, 514 message, 515 e); 516 throw new CoreException(status); 517 } 518 519 } 520 521 530 private IProject configNewProject( 531 IWorkspaceRoot root, 532 String name, 533 String [] natureIds, 534 IProject[] referencedProjects, 535 IProgressMonitor monitor) 536 throws InvocationTargetException { 537 try { 538 IProject project = root.getProject(name); 539 if (!project.exists()) { 540 project.create(null); 541 } 542 if (!project.isOpen()) { 543 project.open(null); 544 } 545 IProjectDescription desc = project.getDescription(); 546 desc.setLocation(null); 547 desc.setNatureIds(natureIds); 548 desc.setReferencedProjects(referencedProjects); 549 550 project.setDescription(desc, new SubProgressMonitor(monitor, 1)); 551 552 return project; 553 } catch (CoreException e) { 554 throw new InvocationTargetException (e); 555 } 556 } 557 558 561 public IResource getElementToOpen() { 562 return fElementToOpen; 563 } 564 565 } 566 | Popular Tags |