KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > tools > UpdateClasspathJob


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.tools;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IProjectDescription;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.IWorkspaceRunnable;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.core.runtime.jobs.Job;
23 import org.eclipse.jdt.core.JavaCore;
24 import org.eclipse.pde.core.plugin.IPluginModelBase;
25 import org.eclipse.pde.internal.core.builders.PDEMarkerFactory;
26 import org.eclipse.pde.internal.ui.IPDEUIConstants;
27 import org.eclipse.pde.internal.ui.PDEPlugin;
28 import org.eclipse.pde.internal.ui.PDEUIMessages;
29 import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer;
30
31 public class UpdateClasspathJob extends Job {
32     IPluginModelBase[] fModels;
33
34     /**
35      * @param name
36      */

37     public UpdateClasspathJob(IPluginModelBase[] models) {
38         super(PDEUIMessages.UpdateClasspathJob_title);
39         setPriority(Job.LONG);
40         fModels = models;
41     }
42     /*
43      * return canceled
44      */

45     public boolean doUpdateClasspath(IProgressMonitor monitor,
46             IPluginModelBase[] models) throws CoreException {
47         monitor.beginTask(PDEUIMessages.UpdateClasspathJob_task,
48                 models.length);
49         try {
50             for (int i = 0; i < models.length; i++) {
51                 IPluginModelBase model = models[i];
52                 monitor.subTask(models[i].getPluginBase().getId());
53                 // no reason to compile classpath for a non-Java model
54
IProject project = model.getUnderlyingResource().getProject();
55                 if (!project.hasNature(JavaCore.NATURE_ID)) {
56                     monitor.worked(1);
57                     continue;
58                 }
59                 IProjectDescription projDesc = project.getDescription();
60                 if (projDesc == null) continue;
61                 projDesc.setReferencedProjects(new IProject[0]);
62                 project.setDescription(projDesc, null);
63                 IFile file = project.getFile(".project"); //$NON-NLS-1$
64
if (file.exists())
65                     file.deleteMarkers(PDEMarkerFactory.MARKER_ID, true, IResource.DEPTH_ZERO);
66                 ClasspathComputer.setClasspath(project, model);
67                 monitor.worked(1);
68                 if (monitor.isCanceled())
69                     return false;
70             }
71         } finally {
72             monitor.done();
73         }
74         return true;
75     }
76
77     class UpdateClasspathWorkspaceRunnable implements IWorkspaceRunnable {
78         boolean fCanceled = false;
79         public void run(IProgressMonitor monitor)
80                 throws CoreException {
81             fCanceled = doUpdateClasspath(monitor, fModels);
82         }
83         public boolean isCanceled(){
84             return fCanceled;
85         }
86     }
87     /* (non-Javadoc)
88      * @see org.eclipse.core.internal.jobs.InternalJob#run(org.eclipse.core.runtime.IProgressMonitor)
89      */

90     protected IStatus run(IProgressMonitor monitor) {
91         try {
92             UpdateClasspathWorkspaceRunnable runnable = new UpdateClasspathWorkspaceRunnable();
93             PDEPlugin.getWorkspace().run(runnable, monitor);
94             if(runnable.isCanceled()){
95                 return new Status(IStatus.CANCEL, IPDEUIConstants.PLUGIN_ID, IStatus.CANCEL, "",null); //$NON-NLS-1$
96
}
97
98         } catch (CoreException e) {
99             String JavaDoc title = PDEUIMessages.UpdateClasspathJob_error_title;
100             String JavaDoc message = PDEUIMessages.UpdateClasspathJob_error_message;
101             PDEPlugin.logException(e, title, message);
102             return new Status(IStatus.ERROR, IPDEUIConstants.PLUGIN_ID, IStatus.OK, message, e);
103         }
104         return new Status(IStatus.OK, IPDEUIConstants.PLUGIN_ID, IStatus.OK, "",null); //$NON-NLS-1$
105
}
106
107 }
108
Popular Tags