KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > imports > FeatureImportOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.wizards.imports;
12
13 import java.io.File JavaDoc;
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IFolder;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IProjectDescription;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.IWorkspaceRoot;
23 import org.eclipse.core.resources.IWorkspaceRunnable;
24 import org.eclipse.core.resources.ResourcesPlugin;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IPath;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.core.runtime.IStatus;
29 import org.eclipse.core.runtime.MultiStatus;
30 import org.eclipse.core.runtime.NullProgressMonitor;
31 import org.eclipse.core.runtime.OperationCanceledException;
32 import org.eclipse.core.runtime.Path;
33 import org.eclipse.core.runtime.Status;
34 import org.eclipse.core.runtime.SubProgressMonitor;
35 import org.eclipse.jdt.core.IClasspathEntry;
36 import org.eclipse.jdt.core.IJavaProject;
37 import org.eclipse.jdt.core.JavaCore;
38 import org.eclipse.jdt.core.JavaModelException;
39 import org.eclipse.osgi.util.NLS;
40 import org.eclipse.pde.core.build.IBuildEntry;
41 import org.eclipse.pde.internal.core.PDECore;
42 import org.eclipse.pde.internal.core.build.WorkspaceBuildModel;
43 import org.eclipse.pde.internal.core.ifeature.IFeatureInstallHandler;
44 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
45 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
46 import org.eclipse.pde.internal.core.natures.PDE;
47 import org.eclipse.pde.internal.ui.PDEPlugin;
48 import org.eclipse.pde.internal.ui.PDEUIMessages;
49 import org.eclipse.team.core.RepositoryProvider;
50 import org.eclipse.team.core.TeamException;
51 import org.eclipse.ui.dialogs.IOverwriteQuery;
52 import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider;
53 import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;
54 import org.eclipse.ui.wizards.datatransfer.ImportOperation;
55
56 public class FeatureImportOperation implements IWorkspaceRunnable {
57     public interface IReplaceQuery {
58
59         // return codes
60
public static final int CANCEL = 0;
61         public static final int NO = 1;
62         public static final int YES = 2;
63
64         /**
65          * Do the callback. Returns YES, NO or CANCEL
66          */

67         int doQuery(IProject project);
68     }
69
70     private IFeatureModel[] fModels;
71     private boolean fBinary;
72     private IPath fTargetPath;
73
74     private IWorkspaceRoot fRoot;
75     private IReplaceQuery fReplaceQuery;
76
77     /**
78      *
79      * @param models
80      * @param targetPath a parent of external project or null
81      * @param replaceQuery
82      */

83     public FeatureImportOperation(
84         IFeatureModel[] models,
85         boolean binary,
86         IPath targetPath,
87         IReplaceQuery replaceQuery) {
88         fModels = models;
89         fBinary = binary;
90         fTargetPath = targetPath;
91         fRoot = ResourcesPlugin.getWorkspace().getRoot();
92         fReplaceQuery = replaceQuery;
93     }
94
95     /*
96      * @see IWorkspaceRunnable#run(IProgressMonitor)
97      */

98     public void run(IProgressMonitor monitor)
99         throws CoreException, OperationCanceledException {
100         if (monitor == null) {
101             monitor = new NullProgressMonitor();
102         }
103         monitor.beginTask(
104             PDEUIMessages.FeatureImportWizard_operation_creating,
105             fModels.length);
106         try {
107             MultiStatus multiStatus =
108                 new MultiStatus(
109                     PDEPlugin.getPluginId(),
110                     IStatus.OK,
111                     PDEUIMessages.FeatureImportWizard_operation_multiProblem,
112                     null);
113             for (int i = 0; i < fModels.length; i++) {
114                 try {
115                     createProject(fModels[i], new SubProgressMonitor(monitor, 1));
116                 } catch (CoreException e) {
117                     multiStatus.merge(e.getStatus());
118                 }
119                 if (monitor.isCanceled()) {
120                     throw new OperationCanceledException();
121                 }
122             }
123             if (!multiStatus.isOK()) {
124                 throw new CoreException(multiStatus);
125             }
126         } finally {
127             monitor.done();
128         }
129     }
130
131     private void createProject(IFeatureModel model, IProgressMonitor monitor)
132         throws CoreException {
133         String JavaDoc name = model.getFeature().getId();
134
135         IFeaturePlugin[] plugins = model.getFeature().getPlugins();
136         for (int i = 0; i < plugins.length; i++) {
137             if (name.equals(plugins[i].getId())) {
138                 name += "-feature"; //$NON-NLS-1$
139
break;
140             }
141
142         }
143         
144         String JavaDoc task =
145             NLS.bind(PDEUIMessages.FeatureImportWizard_operation_creating2, name);
146         monitor.beginTask(task, 9);
147         try {
148             IProject project = fRoot.getProject(name);
149
150             if (project.exists() || new File JavaDoc(project.getParent().getLocation().toFile(), name).exists()) {
151                 if (queryReplace(project)) {
152                     if (!project.exists())
153                         project.create(new SubProgressMonitor(monitor, 1));
154                     project.delete(true, true, new SubProgressMonitor(monitor, 1));
155                     try {
156                         RepositoryProvider.unmap(project);
157                     } catch (TeamException e) {
158                     }
159                 } else {
160                     return;
161                 }
162             } else {
163                 monitor.worked(1);
164             }
165
166             IProjectDescription description =
167                 PDEPlugin.getWorkspace().newProjectDescription(name);
168             if (fTargetPath != null)
169                 description.setLocation(fTargetPath.append(name));
170
171             project.create(description, new SubProgressMonitor(monitor, 1));
172             if (!project.isOpen()) {
173                 project.open(null);
174             }
175             File JavaDoc featureDir = new File JavaDoc(model.getInstallLocation());
176
177             importContent(
178                 featureDir,
179                 project.getFullPath(),
180                 FileSystemStructureProvider.INSTANCE,
181                 null,
182                 new SubProgressMonitor(monitor, 1));
183             IFolder folder = project.getFolder("META-INF"); //$NON-NLS-1$
184
if (folder.exists()) {
185                 folder.delete(true, null);
186             }
187             if (fBinary) {
188                 // Mark this project so that we can show image overlay
189
// using the label decorator
190
project.setPersistentProperty(
191                         PDECore.EXTERNAL_PROJECT_PROPERTY,
192                         PDECore.BINARY_PROJECT_VALUE);
193             }
194             createBuildProperties(project);
195             setProjectNatures(project, model, monitor);
196             if (project.hasNature(JavaCore.NATURE_ID))
197                 setClasspath(project, model, monitor);
198
199         } finally {
200             monitor.done();
201         }
202     }
203
204     private void importContent(
205         Object JavaDoc source,
206         IPath destPath,
207         IImportStructureProvider provider,
208         List JavaDoc filesToImport,
209         IProgressMonitor monitor)
210         throws CoreException {
211         IOverwriteQuery query = new IOverwriteQuery() {
212             public String JavaDoc queryOverwrite(String JavaDoc file) {
213                 return ALL;
214             }
215         };
216         ImportOperation op = new ImportOperation(destPath, source, provider, query);
217         op.setCreateContainerStructure(false);
218         if (filesToImport != null) {
219             op.setFilesToImport(filesToImport);
220         }
221
222         try {
223             op.run(monitor);
224         } catch (InvocationTargetException JavaDoc e) {
225             Throwable JavaDoc th = e.getTargetException();
226             if (th instanceof CoreException) {
227                 throw (CoreException) th;
228             }
229             IStatus status =
230                 new Status(
231                     IStatus.ERROR,
232                     PDEPlugin.getPluginId(),
233                     IStatus.ERROR,
234                     e.getMessage(),
235                     e);
236             throw new CoreException(status);
237         } catch (InterruptedException JavaDoc e) {
238             throw new OperationCanceledException(e.getMessage());
239         }
240     }
241
242     private boolean queryReplace(IProject project) throws OperationCanceledException {
243         switch (fReplaceQuery.doQuery(project)) {
244             case IReplaceQuery.CANCEL :
245                 throw new OperationCanceledException();
246             case IReplaceQuery.NO :
247                 return false;
248         }
249         return true;
250     }
251
252     private void setProjectNatures(
253         IProject project,
254         IFeatureModel model,
255         IProgressMonitor monitor)
256         throws CoreException {
257         IProjectDescription desc = project.getDescription();
258         if (needsJavaNature(model)) {
259             desc.setNatureIds(new String JavaDoc[] { JavaCore.NATURE_ID,
260                     PDE.FEATURE_NATURE });
261         } else {
262             desc.setNatureIds(new String JavaDoc[] { PDE.FEATURE_NATURE });
263         }
264         project.setDescription(desc, new SubProgressMonitor(monitor, 1));
265     }
266
267     private void setClasspath(IProject project, IFeatureModel model,
268             IProgressMonitor monitor) throws JavaModelException {
269         IJavaProject jProject = JavaCore.create(project);
270
271         IClasspathEntry jreCPEntry = JavaCore.newContainerEntry(new Path(
272                 "org.eclipse.jdt.launching.JRE_CONTAINER")); //$NON-NLS-1$
273

274         String JavaDoc libName = model.getFeature().getInstallHandler().getLibrary();
275         IClasspathEntry handlerCPEntry = JavaCore.newLibraryEntry(project
276                 .getFullPath().append(libName), null, null);
277
278         jProject.setRawClasspath(new IClasspathEntry[] { jreCPEntry,
279                 handlerCPEntry }, monitor);
280     }
281
282     private boolean needsJavaNature(IFeatureModel model) {
283         IFeatureInstallHandler handler = model.getFeature().getInstallHandler();
284         if (handler == null) {
285             return false;
286         }
287         String JavaDoc libName = handler.getLibrary();
288         if (libName == null || libName.length() <= 0) {
289             return false;
290         }
291         File JavaDoc lib = new File JavaDoc(model.getInstallLocation(), libName);
292         return lib.exists();
293     }
294     
295     private void createBuildProperties(IProject project) {
296         IFile file = project.getFile("build.properties"); //$NON-NLS-1$
297
if (!file.exists()) {
298             WorkspaceBuildModel model = new WorkspaceBuildModel(file);
299             IBuildEntry ientry = model.getFactory().createEntry("bin.includes"); //$NON-NLS-1$
300
try {
301                 IResource[] res = project.members();
302                 for (int i = 0; i < res.length; i++) {
303                     String JavaDoc path = res[i].getProjectRelativePath().toString();
304                     if (!path.equals(".project")) //$NON-NLS-1$
305
ientry.addToken(path);
306                 }
307                 model.getBuild().add(ientry);
308                 model.save();
309             } catch (CoreException e) {
310             }
311         }
312     }
313
314 }
315
Popular Tags