001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.wizards.tools;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.core.resources.IProject;
014: import org.eclipse.core.resources.IProjectDescription;
015: import org.eclipse.core.resources.IResource;
016: import org.eclipse.core.resources.IWorkspaceRunnable;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.Status;
021: import org.eclipse.core.runtime.jobs.Job;
022: import org.eclipse.jdt.core.JavaCore;
023: import org.eclipse.pde.core.plugin.IPluginModelBase;
024: import org.eclipse.pde.internal.core.builders.PDEMarkerFactory;
025: import org.eclipse.pde.internal.ui.IPDEUIConstants;
026: import org.eclipse.pde.internal.ui.PDEPlugin;
027: import org.eclipse.pde.internal.ui.PDEUIMessages;
028: import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer;
029:
030: public class UpdateClasspathJob extends Job {
031: IPluginModelBase[] fModels;
032:
033: /**
034: * @param name
035: */
036: public UpdateClasspathJob(IPluginModelBase[] models) {
037: super (PDEUIMessages.UpdateClasspathJob_title);
038: setPriority(Job.LONG);
039: fModels = models;
040: }
041:
042: /*
043: * return canceled
044: */
045: public boolean doUpdateClasspath(IProgressMonitor monitor,
046: IPluginModelBase[] models) throws CoreException {
047: monitor.beginTask(PDEUIMessages.UpdateClasspathJob_task,
048: models.length);
049: try {
050: for (int i = 0; i < models.length; i++) {
051: IPluginModelBase model = models[i];
052: monitor.subTask(models[i].getPluginBase().getId());
053: // no reason to compile classpath for a non-Java model
054: IProject project = model.getUnderlyingResource()
055: .getProject();
056: if (!project.hasNature(JavaCore.NATURE_ID)) {
057: monitor.worked(1);
058: continue;
059: }
060: IProjectDescription projDesc = project.getDescription();
061: if (projDesc == null)
062: continue;
063: projDesc.setReferencedProjects(new IProject[0]);
064: project.setDescription(projDesc, null);
065: IFile file = project.getFile(".project"); //$NON-NLS-1$
066: if (file.exists())
067: file.deleteMarkers(PDEMarkerFactory.MARKER_ID,
068: true, IResource.DEPTH_ZERO);
069: ClasspathComputer.setClasspath(project, model);
070: monitor.worked(1);
071: if (monitor.isCanceled())
072: return false;
073: }
074: } finally {
075: monitor.done();
076: }
077: return true;
078: }
079:
080: class UpdateClasspathWorkspaceRunnable implements
081: IWorkspaceRunnable {
082: boolean fCanceled = false;
083:
084: public void run(IProgressMonitor monitor) throws CoreException {
085: fCanceled = doUpdateClasspath(monitor, fModels);
086: }
087:
088: public boolean isCanceled() {
089: return fCanceled;
090: }
091: }
092:
093: /* (non-Javadoc)
094: * @see org.eclipse.core.internal.jobs.InternalJob#run(org.eclipse.core.runtime.IProgressMonitor)
095: */
096: protected IStatus run(IProgressMonitor monitor) {
097: try {
098: UpdateClasspathWorkspaceRunnable runnable = new UpdateClasspathWorkspaceRunnable();
099: PDEPlugin.getWorkspace().run(runnable, monitor);
100: if (runnable.isCanceled()) {
101: return new Status(IStatus.CANCEL,
102: IPDEUIConstants.PLUGIN_ID, IStatus.CANCEL,
103: "", null); //$NON-NLS-1$
104: }
105:
106: } catch (CoreException e) {
107: String title = PDEUIMessages.UpdateClasspathJob_error_title;
108: String message = PDEUIMessages.UpdateClasspathJob_error_message;
109: PDEPlugin.logException(e, title, message);
110: return new Status(IStatus.ERROR, IPDEUIConstants.PLUGIN_ID,
111: IStatus.OK, message, e);
112: }
113: return new Status(IStatus.OK, IPDEUIConstants.PLUGIN_ID,
114: IStatus.OK, "", null); //$NON-NLS-1$
115: }
116:
117: }
|