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.tests.macro;
011:
012: import java.io.PrintWriter;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.core.runtime.IProgressMonitor;
016: import org.eclipse.core.runtime.Path;
017: import org.eclipse.core.runtime.Platform;
018: import org.eclipse.core.runtime.jobs.IJobChangeEvent;
019: import org.eclipse.core.runtime.jobs.IJobManager;
020: import org.eclipse.core.runtime.jobs.Job;
021: import org.eclipse.core.runtime.jobs.JobChangeAdapter;
022: import org.eclipse.swt.widgets.Composite;
023: import org.eclipse.swt.widgets.Display;
024: import org.eclipse.swt.widgets.Event;
025:
026: public class WaitCommand extends MacroCommand {
027: public static final String TYPE = "wait";
028: private static final WidgetIdentifier nullIdentifier = new WidgetIdentifier(
029: new Path(""), new Path(""));
030:
031: private static class JobListener extends JobChangeAdapter {
032: private int counter = 0;
033: private IProgressMonitor monitor;
034: private Thread t;
035:
036: public JobListener(IProgressMonitor monitor, Thread t,
037: int number) {
038: this .counter = number;
039: this .monitor = monitor;
040: this .t = t;
041: }
042:
043: private synchronized void change(int increment) {
044: this .counter += increment;
045: if (counter == 0) {
046: monitor.subTask("");
047: synchronized (t) {
048: t.interrupt();
049: }
050: }
051: }
052:
053: public void running(IJobChangeEvent event) {
054: Job job = event.getJob();
055: if (!job.isSystem())
056: change(1);
057: }
058:
059: public void done(IJobChangeEvent event) {
060: Job job = event.getJob();
061: if (!job.isSystem())
062: change(-1);
063: }
064: }
065:
066: public WaitCommand() {
067: super (nullIdentifier);
068: }
069:
070: /*
071: * (non-Javadoc)
072: *
073: * @see org.eclipse.ui.macro.MacroCommand#getType()
074: */
075: public String getType() {
076: return TYPE;
077: }
078:
079: /*
080: * (non-Javadoc)
081: *
082: * @see org.eclipse.ui.macro.MacroCommand#processEvent(org.eclipse.swt.widgets.Event)
083: */
084: public void processEvent(Event e) {
085: }
086:
087: /*
088: * (non-Javadoc)
089: *
090: * @see org.eclipse.ui.macro.IWritable#write(java.lang.String,
091: * java.io.PrintWriter)
092: */
093: public void write(String indent, PrintWriter writer) {
094: writer.print(indent);
095: writer.print("<command type=\"");
096: writer.print(getType());
097: writer.print("\"");
098: writer.println("/>");
099: }
100:
101: /*
102: * (non-Javadoc)
103: *
104: * @see org.eclipse.ui.macro.IPlayable#playback(org.eclipse.swt.widgets.Composite)
105: */
106: public boolean playback(Display display, Composite parent,
107: IProgressMonitor monitor) throws CoreException {
108: if (parent.isDisposed())
109: return false;
110: IJobManager jobManager = Platform.getJobManager();
111: int nrunning = getNumberOfRunningJobs(jobManager);
112: if (nrunning == 0)
113: return true;
114: String message = "Waiting for the background jobs...";
115: JobListener listener = new JobListener(monitor, Thread
116: .currentThread(), nrunning);
117: jobManager.addJobChangeListener(listener);
118: monitor.subTask(message);
119: try {
120: Thread.sleep(30000);
121: } catch (InterruptedException e) {
122: }
123: jobManager.removeJobChangeListener(listener);
124: return true;
125: }
126:
127: private int getNumberOfRunningJobs(IJobManager manager) {
128: int count = 0;
129: Job[] jobs = manager.find(null);
130: for (int i = 0; i < jobs.length; i++) {
131: if (!jobs[i].isSystem()
132: && jobs[i].getState() == Job.RUNNING)
133: count++;
134: }
135: return count;
136: }
137: }
|