package cbclipse.builder;
import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.IProjectNature;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.LibraryLocation;
import cbclipse.Activator;
import cbclipse.preferences.PreferenceConstants;
public class CBCNature implements IProjectNature {
/**
* ID of this project nature
*/
public static final String NATURE_ID = "cbclipse.builder.cbcNature";
private IProject project;
/*
* (non-Javadoc)
*
* @see org.eclipse.core.resources.IProjectNature#configure()
*/
public void configure() throws CoreException {
IProjectDescription desc = project.getDescription();
ICommand[] commands = desc.getBuildSpec();
for (int i = 0; i < commands.length; ++i) {
if (commands[i].getBuilderName().equals(CBCBuilder.BUILDER_ID)) {
return;
}
}
ICommand[] newCommands = new ICommand[commands.length + 1];
System.arraycopy(commands, 0, newCommands, 0, commands.length);
ICommand command = desc.newCommand();
command.setBuilderName(CBCBuilder.BUILDER_ID);
newCommands[newCommands.length - 1] = command;
desc.setBuildSpec(newCommands);
project.setDescription(desc, null);
IJavaProject javaProject = JavaCore.create(project);
Set<IClasspathEntry> entries = new HashSet<IClasspathEntry>();
entries.addAll(Arrays.asList(javaProject.getRawClasspath()));
String osPath = Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.P_PATH);
IPath path = Path.fromOSString(osPath);
LibraryLocation location = new LibraryLocation(path, Path.EMPTY, Path.EMPTY);
entries.add(JavaCore.newLibraryEntry(location.getSystemLibraryPath(), null, null));
javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), new NullProgressMonitor());
IFile commCBC = project.getFile("comm.cbc");
commCBC.create(new ByteArrayInputStream(new byte[] {}), true, new NullProgressMonitor());
}
/*
* (non-Javadoc)
*
* @see org.eclipse.core.resources.IProjectNature#deconfigure()
*/
public void deconfigure() throws CoreException {
IProjectDescription description = getProject().getDescription();
ICommand[] commands = description.getBuildSpec();
for (int i = 0; i < commands.length; ++i) {
if (commands[i].getBuilderName().equals(CBCBuilder.BUILDER_ID)) {
ICommand[] newCommands = new ICommand[commands.length - 1];
System.arraycopy(commands, 0, newCommands, 0, i);
System.arraycopy(commands, i + 1, newCommands, i,
commands.length - i - 1);
description.setBuildSpec(newCommands);
project.setDescription(description, null);
return;
}
}
// TODO: Remove CBCJVM.jar
}
/*
* (non-Javadoc)
*
* @see org.eclipse.core.resources.IProjectNature#getProject()
*/
public IProject getProject() {
return project;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.core.resources.IProjectNature#setProject(org.eclipse.core.resources.IProject)
*/
public void setProject(IProject project) {
this.project = project;
}
}