|
What this is
This file is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.tasklist.compiler.engine;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.swing.SwingUtilities;
import org.netbeans.modules.tasklist.compiler.StopCompileAction;
import org.netbeans.modules.tasklist.compiler.engine.CompilationEngineListener;
import org.openide.compiler.CompilerGroup;
import org.openide.compiler.CompilerTask;
import org.openide.util.actions.SystemAction;
/**
* Compilation task
*
* @author Tim Lebedkov
*/
public class MyCompilerTask extends CompilerTask {
/** ArrayList */
private static List runningJobs = new ArrayList();
/**
* Stops all currently running compiler jobs
*/
public static void stopAll() {
for (int i = 0; i < runningJobs.size(); i++) {
CompilerTask t = (CompilerTask) runningJobs.get(i);
t.stop();
}
}
/**
* Are any compiler jobs running?
*
* @return true = yes
*/
public static boolean isCompiling() {
return runningJobs.size() != 0;
}
/**
* Collection>
* Levels of CompilerGroups
*/
private Collection levels;
private String name;
private CompilationEngineListener listener;
private boolean success = true;
private boolean uptodate = true;
private Thread[] threads;
/**
* Creates a new instance
*
* @param name localized name of the task
* @param levels levels of CompilerGroups
* Collection>
* @param listener a compilation engine listener
*/
public MyCompilerTask(String name, Collection levels, CompilationEngineListener listener) {
super(new Runnable() {
public void run() {
}
});
this.name = name;
this.levels = levels;
this.listener = listener;
}
public void run() {
runningJobs.add(this);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SystemAction.get(StopCompileAction.class).setEnabled(true);
}
});
try {
notifyRunning();
listener.compilationStarted(this);
listener.progress(0.0f);
// total number of CompilerGroups
int n = 0;
Iterator it = levels.iterator();
while (it.hasNext()) {
n += ((Collection) it.next()).size();
}
int p = 0;
it = levels.iterator();
while (it.hasNext()) {
// CompilerGroup[]
Collection level = (Collection) it.next();
CompilerGroup[] groups = (CompilerGroup[])
level.toArray(new CompilerGroup[level.size()]);
CompilerGroupRunnable[] cgr =
new CompilerGroupRunnable[groups.length];
for (int i = 0; i < cgr.length; i++) {
groups[i].addCompilerListener(listener);
cgr[i] = new CompilerGroupRunnable(groups[i]);
}
threads = new Thread[groups.length];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Thread(cgr[i]);
threads[i].start();
uptodate = false;
}
for (int i = 0; i < threads.length; i++) {
try {
threads[i].join();
if (!cgr[i].getStatus())
success = false;
} catch (InterruptedException e) {
success = false;
}
p++;
listener.progress(((float)p) / n);
}
for (int i = 0; i < cgr.length; i++) {
groups[i].removeCompilerListener(listener);
}
}
} finally {
notifyFinished();
runningJobs.remove(this);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
SystemAction.get(StopCompileAction.class).setEnabled(isCompiling());
}
});
listener.compilationFinished(this, uptodate);
}
}
public void stop() {
if (threads != null) {
for (int i = 0; i < threads.length; i++) {
threads[i].interrupt();
}
}
}
public boolean isSuccessful() {
waitFinished();
return success;
}
/**
* Returns name of this task.
*
* @return localized name of this task
*/
public String getName() {
return name;
}
/**
* Runnable for one CompilerGroups
*/
private static class CompilerGroupRunnable implements Runnable {
private CompilerGroup group;
private boolean status;
/**
* Creates a new runnable.
*
* @param group this compiler group will be run
*/
public CompilerGroupRunnable(CompilerGroup group) {
this.group = group;
}
public void run() {
this.status = group.start();
}
/**
* Returns status of the compilation
*
* @return true = success
*/
public boolean getStatus() {
return status;
}
}
}
|