package tool.builder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import tool.ToolBuilderActivator;
import tool.ToolProjectSupport;
public class FscriptBuilder extends IncrementalProjectBuilder {
class DeltaVisitor implements IResourceDeltaVisitor {
/*
* (non-Javadoc)
*
* @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta)
*/
public boolean visit(IResourceDelta delta) throws CoreException {
IResource resource = delta.getResource();
if (resource.getFullPath().toPortableString().contains(ToolProjectSupport.LIBRARY_FOLDER_NAME))
return false;
switch (delta.getKind()) {
case IResourceDelta.ADDED:
// handle added resource
compileToolComponent(resource);
break;
case IResourceDelta.REMOVED:
// TODO handle removed resource in the Builder
break;
case IResourceDelta.CHANGED:
// handle changed resource
compileToolComponent(resource);
break;
}
//return true to continue visiting children.
return true;
}
}
public static final String BUILDER_ID = "ToolBuilder.fscriptBuilder";
//public static final String MARKER_TYPE = "ToolBuilder.xmlProblem";
public static final String MARKER_TYPE = "ToolBuilder.toolProblem";
private void addMarker(IFile file, String message, int lineNumber,
int severity) {
try {
IMarker marker = file.createMarker(MARKER_TYPE);
marker.setAttribute(IMarker.MESSAGE, message);
marker.setAttribute(IMarker.SEVERITY, severity);
if (lineNumber == -1) {
lineNumber = 1;
}
marker.setAttribute(IMarker.LINE_NUMBER, lineNumber);
} catch (CoreException e) {
}
}
/*
* (non-Javadoc)
*
* @see org.eclipse.core.internal.events.InternalBuilder#build(int,
* java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
*/
protected IProject[] build(int kind, Map args, IProgressMonitor monitor)
throws CoreException {
if (kind == FULL_BUILD) {
fullBuild(monitor);
} else {
IResourceDelta delta = getDelta(getProject());
if (delta == null) {
fullBuild(monitor);
} else {
incrementalBuild(delta, monitor);
}
}
return null;
}
void compileToolComponent(IResource resource) {
if (isPlanComponent(resource)) {
IFile file = (IFile) resource;
deleteMarkers(file);
FscriptErrorHandler reporter = new FscriptErrorHandler(file);
try {
FScript.getFScript(file).compilePlanComponent(file.getParent().getName(), file, reporter);
} catch (CoreException e) {
ToolBuilderActivator.showError("FScript error", e);
} catch (InvalidToolRepositoryException e) {
ToolBuilderActivator.showError("FScript error", e);
} catch (ToolSystemException e) {
ToolBuilderActivator.showError("FScript error", e);
}
}
}
private boolean isPlanComponent(IResource resource){
return (resource instanceof IFile &&
(resource.getName().endsWith(".CLA")
||resource.getName().endsWith(".CON")
||resource.getName().endsWith(".IFC")
||resource.getName().endsWith(".WCL")
));
}
private void deleteMarkers(IFile file) {
try {
file.deleteMarkers(MARKER_TYPE, false, IResource.DEPTH_ZERO);
} catch (CoreException ce) {
}
}
protected void fullBuild(final IProgressMonitor monitor)
throws CoreException {
try {
List<IFile> componentsToCompile = new ArrayList<IFile>();
getProject().accept(new FullBuildResourceVisitor(componentsToCompile));
monitor.beginTask("Compiling Tool Components", componentsToCompile.size());
for (IFile comp : componentsToCompile){
if (monitor.isCanceled())
break;
monitor.subTask(comp.getName());
//TODO compileToolComponent(comp);
monitor.worked(1);
}
} catch (CoreException e) {
} finally {
monitor.done();
}
}
class FullBuildResourceVisitor implements IResourceVisitor {
List<IFile> componentsToBuild;
public FullBuildResourceVisitor(List<IFile> componentsToBuild){
super();
this.componentsToBuild = componentsToBuild;
}
public boolean visit(IResource resource) {
if (isPlanComponent(resource)){
componentsToBuild.add((IFile) resource);
}
return true;
}
}
protected void incrementalBuild(IResourceDelta delta,
IProgressMonitor monitor) throws CoreException {
// the visitor does the work.
delta.accept(new DeltaVisitor());
}
}