blob: e8892453aab6100f8d1e61dfed7f84df6cd7b666 [file] [log] [blame]
/**
*
* Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany)
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation
*/
package org.eclipse.osbp.ide.core.ui.softwarefactory.nature;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.resources.ICommand;
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.osbp.ide.core.ui.softwarefactory.builder.OSBP2Builder;
import org.eclipse.osbp.ide.core.ui.softwarefactory.constants.Constants;
public class OSBP2Nature implements IProjectNature {
public static String NATURE_ID = Constants.NATURE_ID;
private IProject project;
public void configure() throws CoreException {
IProjectDescription desc = project.getDescription();
ICommand[] commands = desc.getBuildSpec();
List<ICommand> temp = new ArrayList<ICommand>(Arrays.asList(commands));
if (!contains(temp, OSBP2Builder.BUILDER_ID)) {
ICommand command = desc.newCommand();
command.setBuilderName(OSBP2Builder.BUILDER_ID);
temp.add(command);
}
temp.sort(new Comparator<ICommand>() {
@Override
public int compare(ICommand o1, ICommand o2) {
return getIndex(o1.getBuilderName())
- getIndex(o2.getBuilderName());
}
private int getIndex(String bsn) {
if (bsn.equals("org.eclipse.xtext.ui.shared.xtextBuilder")) {
return 0;
} else if (bsn.startsWith("org.eclipse.osbp")) {
return 1;
} else if (bsn.startsWith("org.eclipse.osbp")) {
return 2;
} else if (bsn.equals("org.eclipse.jdt.core.javabuilder")) {
return 3;
}
return 3;
}
});
desc.setBuildSpec(temp.toArray(new ICommand[temp.size()]));
project.setDescription(desc, null);
}
private boolean contains(List<ICommand> temp, String builderId) {
for (ICommand cmd : temp) {
if (cmd.getBuilderName().equals(builderId)) {
return true;
}
}
return false;
}
private void remove(List<ICommand> temp, String builderId) {
for (Iterator<ICommand> iterator = temp.iterator(); iterator.hasNext();) {
ICommand iCommand = iterator.next();
if (iCommand.getBuilderName().equals(builderId)) {
iterator.remove();
return;
}
}
}
public void deconfigure() throws CoreException {
IProjectDescription description = getProject().getDescription();
ICommand[] commands = description.getBuildSpec();
List<ICommand> temp = new ArrayList<ICommand>(Arrays.asList(commands));
remove(temp, OSBP2Builder.BUILDER_ID);
description.setBuildSpec(temp.toArray(new ICommand[temp.size()]));
project.setDescription(description, null);
}
public IProject getProject() {
return project;
}
public void setProject(IProject project) {
this.project = project;
}
}