001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.build;
011:
012: import org.eclipse.core.resources.IResource;
013: import org.eclipse.core.resources.ResourcesPlugin;
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IProgressMonitor;
016: import org.eclipse.core.runtime.IStatus;
017: import org.eclipse.core.runtime.MultiStatus;
018: import org.eclipse.core.runtime.Status;
019: import org.eclipse.core.runtime.jobs.ISchedulingRule;
020: import org.eclipse.core.runtime.jobs.Job;
021: import org.eclipse.jface.dialogs.ErrorDialog;
022: import org.eclipse.jface.dialogs.MessageDialog;
023: import org.eclipse.osgi.util.NLS;
024: import org.eclipse.pde.internal.core.exports.FeatureExportInfo;
025: import org.eclipse.pde.internal.core.exports.FeatureExportOperation;
026: import org.eclipse.pde.internal.ui.PDEPlugin;
027: import org.eclipse.pde.internal.ui.PDEUIMessages;
028: import org.eclipse.swt.widgets.Display;
029:
030: public class FeatureExportJob extends Job {
031:
032: class SchedulingRule implements ISchedulingRule {
033: public boolean contains(ISchedulingRule rule) {
034: return rule instanceof SchedulingRule
035: || rule instanceof IResource;
036: }
037:
038: public boolean isConflicting(ISchedulingRule rule) {
039: return rule instanceof SchedulingRule;
040: }
041: }
042:
043: protected FeatureExportInfo fInfo;
044:
045: public FeatureExportJob(FeatureExportInfo info) {
046: this (info, PDEUIMessages.FeatureExportJob_name);
047: }
048:
049: protected FeatureExportJob(FeatureExportInfo info, String name) {
050: super (name);
051: fInfo = info;
052: setRule(ResourcesPlugin.getWorkspace().getRoot());
053: }
054:
055: protected IStatus run(IProgressMonitor monitor) {
056: String errorMessage = null;
057: FeatureExportOperation op = createOperation();
058: try {
059: op.run(monitor);
060: } catch (final CoreException e) {
061: final Display display = getStandardDisplay();
062: display.asyncExec(new Runnable() {
063: public void run() {
064: MultiStatus status = new MultiStatus(e.getStatus()
065: .getPlugin(), e.getStatus().getCode(),
066: new IStatus[] { e.getStatus() },
067: PDEUIMessages.FeatureExportJob_problems, e);
068: ErrorDialog.openError(display.getActiveShell(),
069: PDEUIMessages.FeatureExportJob_error,
070: PDEUIMessages.FeatureExportJob_error,
071: status); //
072: done(new Status(IStatus.OK,
073: PDEPlugin.getPluginId(), IStatus.OK,
074: "", null)); //$NON-NLS-1$
075: }
076: });
077: return Job.ASYNC_FINISH;
078: }
079:
080: if (errorMessage == null && op.hasErrors())
081: errorMessage = getLogFoundMessage();
082:
083: if (errorMessage != null) {
084: final String em = errorMessage;
085: getStandardDisplay().asyncExec(new Runnable() {
086: public void run() {
087: asyncNotifyExportException(em);
088: }
089: });
090: return Job.ASYNC_FINISH;
091: }
092: return new Status(IStatus.OK, PDEPlugin.getPluginId(),
093: IStatus.OK, "", null); //$NON-NLS-1$
094: }
095:
096: protected FeatureExportOperation createOperation() {
097: return new FeatureExportOperation(fInfo);
098: }
099:
100: /**
101: * Returns the standard display to be used. The method first checks, if the
102: * thread calling this method has an associated disaply. If so, this display
103: * is returned. Otherwise the method returns the default display.
104: */
105: public static Display getStandardDisplay() {
106: Display display = Display.getCurrent();
107: if (display == null)
108: display = Display.getDefault();
109: return display;
110: }
111:
112: private void asyncNotifyExportException(String errorMessage) {
113: getStandardDisplay().beep();
114: MessageDialog.openError(PDEPlugin.getActiveWorkbenchShell(),
115: PDEUIMessages.FeatureExportJob_error, errorMessage);
116: done(new Status(IStatus.OK, PDEPlugin.getPluginId(),
117: IStatus.OK, "", null)); //$NON-NLS-1$
118: }
119:
120: protected String getLogFoundMessage() {
121: return NLS.bind(PDEUIMessages.ExportJob_error_message,
122: fInfo.destinationDirectory);
123: }
124:
125: }
|