KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > omg > lifl > eclipse > plugin > project > OpenCCM > ProjectCreationOperation


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2003 USTL - LIFL - GOAL
5 Contact: openccm-team@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): offroy ________________________.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26 /*
27  * Created on 23 mai 2003 by jerome OFFROY (offroy@lifl.fr)
28  *
29  */

30
31 package org.omg.lifl.eclipse.plugin.project.OpenCCM;
32
33 import java.io.File JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.lang.reflect.InvocationTargetException JavaDoc;
36 import java.net.MalformedURLException JavaDoc;
37 import java.net.URL JavaDoc;
38 import java.util.zip.ZipFile JavaDoc;
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 /**
70  * This class implements the operations to execute
71  * when project configuration is finished
72  *
73  * @author offroy@lifl.fr
74  *
75  * @version 0.1
76  */

77 public class ProjectCreationOperation implements IRunnableWithProgress {
78
79     private IOverwriteQuery fOverwriteQuery;
80     private IResource fElementToOpen;
81     private ProjectCreationWizardPage[] fPages;
82
83     /**
84      * @param pages
85      * @param overwriteQuery
86      */

87     public ProjectCreationOperation(
88         ProjectCreationWizardPage[] pages,
89         IOverwriteQuery overwriteQuery) {
90         fElementToOpen = null;
91         fPages = pages;
92         fOverwriteQuery = overwriteQuery;
93     }
94
95     /* (non-Javadoc)
96      * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
97      */

98     public void run(IProgressMonitor monitor)
99         throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
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     /**
122      * @param root
123      * @param page
124      * @param monitor
125      * @throws InvocationTargetException
126      * @throws InterruptedException
127      */

128     private void createProject(
129         IWorkspaceRoot root,
130         ProjectCreationWizardPage page,
131         IProgressMonitor monitor)
132         throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
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.getName();
148
String JavaDoc name = page.getProjectChooser().get_ProjectName();
149         String JavaDoc[] natureIds = new String JavaDoc[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 JavaDoc 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         // generate the build.properties file for OpenCCM
180
generateFile(
181             proj,
182             page,
183             monitor,
184             desc.getAttribute("BuildPropertiesFileLocation"),
185             desc.getAttribute("BuildPropertiesFileName"),
186             page.getOpenccmConfigChooser().getCheckGroup().isSubGroupVisible());
187
188         // import the OpenCCM src into the project
189
importOpenCCMsrc(page, proj, monitor);
190
191         configureEclipse(page, proj);
192
193         // create Classpath for Eclipse
194
CPManager.setEclipseCP(page, monitor);
195     }
196
197     /**
198      * @param page
199      * @param proj
200      * @param monitor
201      */

202     private void importOpenCCMsrc(
203         ProjectCreationWizardPage page,
204         IProject proj,
205         IProgressMonitor monitor)
206         throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
207         // TODO make a copy not an import (because of date file changes for CVS use)
208
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 JavaDoc(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             //copyFilesFromFS();
224
/*importFilesFromFS(
225                 new File(page.getCvsFSchooser().get_CVSsrcFSPathValue()),
226                 proj.getFullPath(),
227                 new SubProgressMonitor(monitor, 1));*/

228                 try {
229                     proj.refreshLocal(IResource.DEPTH_INFINITE,new SubProgressMonitor(monitor, 1));
230                 } catch (CoreException e) {
231                     // TODO Auto-generated catch block
232
e.printStackTrace();
233                 }
234         }
235
236     }
237
238     /**
239      * @param page
240      */

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 JavaDoc outDirectory =
253                     page
254                         .getEclipseConfigChooser()
255                         .get_ImportWorkbenchPreference()
256                         .get_Name();
257
258                 if (new File JavaDoc(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     /**
275      * @param proj
276      * @param page
277      * @param monitor
278      * @param fileLocation
279      * @param fileName
280      * @param generate
281      */

282     private void generateFile(
283         IProject proj,
284         ProjectCreationWizardPage page,
285         IProgressMonitor monitor,
286         String JavaDoc fileLocation,
287         String JavaDoc fileName,
288         boolean generate) {
289         if (!generate)
290             return;
291
292         BuildPropertySet buildConfig = new BuildPropertySet(page);
293
294         //TODO remove debug
295
//System.out.println(buildConfig.showDebug(generate));
296

297         try {
298             Project antProject = new Project();
299             antProject.init();
300             Copy buildCopy = new Copy();
301             buildCopy.setProject(antProject);
302             File JavaDoc in = null;
303             String JavaDoc inBuildPropertiesFile = fileLocation;
304             String JavaDoc buildPropertiesFileName = fileName;
305             if (inBuildPropertiesFile != null
306                 && buildPropertiesFileName != null) {
307                 in = getFSFromPluginDir(inBuildPropertiesFile);
308                 buildCopy.setFile(in.getAbsoluteFile());
309                 File JavaDoc out =
310                     new File JavaDoc(
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             // TODO add somethig to this catch block
323
e.printStackTrace();
324         }
325
326     }
327
328     /**
329      * @param string
330      */

331     private void backupWorkbenchPreference(IPath exportFile) {
332         // TODO Auto-generated method stub
333
try {
334             Preferences.exportPreferences(exportFile);
335         } catch (CoreException e) {
336             // TODO Auto-generated catch block
337
e.printStackTrace();
338         }
339     }
340
341     /**
342      * import Eclipse Workbench preference from importFile
343      * @param importFile
344      */

345     private void importWorkbenchPreference(IPath importFile) {
346         // import Eclipse Workbench preference from file represented by importFile
347
try {
348             Preferences.importPreferences(importFile);
349         } catch (CoreException e) {
350             // TODO Auto-generated catch block
351
e.printStackTrace();
352         }
353     }
354
355     /**
356      * @param project
357      * @param curr
358      * @param monitor
359      * @throws InvocationTargetException
360      * @throws InterruptedException
361      */

362     private void doImports(
363         IProject project,
364         IConfigurationElement curr,
365         SubProgressMonitor monitor)
366         throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
367         try {
368             IPath destPath;
369             String JavaDoc 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 JavaDoc importPath = curr.getAttribute("src");
381             if (importPath == null) {
382                 importPath = "";
383                 MainPlugin.log("projectsetup descriptor: import missing");
384                 return;
385             }
386             // Zip or FileSystem Import
387
String JavaDoc typeImport = curr.getAttribute("type");
388             if (typeImport == null) {
389                 typeImport = "zip";
390             }
391
392             if (typeImport.compareTo("zip") == 0) {
393                 ZipFile JavaDoc zipFile = getZipFileFromPluginDir(importPath);
394                 importFilesFromZip(
395                     zipFile,
396                     destPath,
397                     new SubProgressMonitor(monitor, 1));
398             } else {
399                 File JavaDoc srcFS = getFSFromPluginDir(importPath);
400                 importFilesFromFS(
401                     srcFS,
402                     destPath,
403                     new SubProgressMonitor(monitor, 1));
404             }
405
406         } catch (CoreException e) {
407             throw new InvocationTargetException JavaDoc(e);
408         }
409     }
410
411     /**
412      * @param srcDir
413      * @param destPath
414      * @param monitor
415      * @throws InvocationTargetException
416      * @throws InterruptedException
417      */

418     private void importFilesFromFS(
419         File JavaDoc srcDir,
420         IPath destPath,
421         IProgressMonitor monitor)
422         throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
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     /**
434      * @param srcZipFile
435      * @param destPath
436      * @param monitor
437      * @throws InvocationTargetException
438      * @throws InterruptedException
439      */

440     private void importFilesFromZip(
441         ZipFile JavaDoc srcZipFile,
442         IPath destPath,
443         IProgressMonitor monitor)
444         throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
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     /**
457      * @param pluginRelativePath
458      * @return ZipFile representing the zip file at pluginRelativePath
459      * @throws CoreException
460      */

461     private ZipFile JavaDoc getZipFileFromPluginDir(String JavaDoc pluginRelativePath)
462         throws CoreException {
463         try {
464             URL JavaDoc starterURL =
465                 new URL JavaDoc(
466                     MainPlugin.getDefault().getDescriptor().getInstallURL(),
467                     pluginRelativePath);
468             return new ZipFile JavaDoc(Platform.asLocalURL(starterURL).getFile());
469         } catch (IOException JavaDoc e) {
470             String JavaDoc 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     /**
483      * @param pluginRelativePath
484      * @return a File corresponding to the file at pluginRelativePath
485      * @throws CoreException
486      */

487     private File JavaDoc getFSFromPluginDir(String JavaDoc pluginRelativePath)
488         throws CoreException {
489         URL JavaDoc starterURL;
490         try {
491             starterURL =
492                 new URL JavaDoc(
493                     MainPlugin.getDefault().getDescriptor().getInstallURL(),
494                     pluginRelativePath);
495
496             return new File JavaDoc(Platform.asLocalURL(starterURL).getPath());
497         } catch (MalformedURLException JavaDoc e) {
498             String JavaDoc 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 JavaDoc e) {
508             String JavaDoc 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     /**
522      * @param root
523      * @param name
524      * @param natureIds
525      * @param referencedProjects
526      * @param monitor
527      * @return
528      * @throws InvocationTargetException
529      */

530     private IProject configNewProject(
531         IWorkspaceRoot root,
532         String JavaDoc name,
533         String JavaDoc[] natureIds,
534         IProject[] referencedProjects,
535         IProgressMonitor monitor)
536         throws InvocationTargetException JavaDoc {
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 JavaDoc(e);
555         }
556     }
557
558     /**
559      * @return
560      */

561     public IResource getElementToOpen() {
562         return fElementToOpen;
563     }
564
565 }
566
Popular Tags