001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.builder;
022
023 import static org.granite.builder.GraniteBuilder.GRANITE_BUILDER_ID;
024 import static org.granite.builder.GraniteBuilder.FLEX_BUILDER_ID;
025 import static org.granite.builder.GraniteBuilder.JAVA_BUILDER_ID;
026
027 import java.util.Arrays;
028 import java.util.Comparator;
029
030 import org.eclipse.core.resources.ICommand;
031 import org.eclipse.core.resources.IProject;
032 import org.eclipse.core.resources.IProjectDescription;
033 import org.eclipse.core.resources.IProjectNature;
034 import org.eclipse.core.runtime.CoreException;
035
036 /**
037 * @author Franck WOLFF
038 */
039 public class GraniteNature implements IProjectNature {
040
041 public static final String NATURE_ID = "org.granite.builder.granitenature";
042
043 private static final Comparator<ICommand> BUILDER_COMPARATOR = new Comparator<ICommand>() {
044
045 // java -> granite [-> flex]
046 public int compare(ICommand c1, ICommand c2) {
047 if (GRANITE_BUILDER_ID.equals(c1.getBuilderName())) {
048 if (JAVA_BUILDER_ID.equals(c2.getBuilderName()))
049 return 1;
050 if (FLEX_BUILDER_ID.equals(c2.getBuilderName()))
051 return -1;
052 }
053 else if (JAVA_BUILDER_ID.equals(c1.getBuilderName())) {
054 if (GraniteBuilder.GRANITE_BUILDER_ID.equals(c2.getBuilderName()) || FLEX_BUILDER_ID.equals(c2.getBuilderName()))
055 return -1;
056 }
057 else if (FLEX_BUILDER_ID.equals(c1.getBuilderName())) {
058 if (GRANITE_BUILDER_ID.equals(c2.getBuilderName()) || JAVA_BUILDER_ID.equals(c2.getBuilderName()))
059 return 1;
060 }
061 return 0;
062 }
063 };
064
065 private IProject project;
066
067 public void configure() throws CoreException {
068 IProjectDescription desc = project.getDescription();
069 ICommand[] commands = desc.getBuildSpec();
070
071 for (ICommand command : commands) {
072 if (command.getBuilderName().equals(GRANITE_BUILDER_ID))
073 return;
074 }
075
076 ICommand[] newCommands = new ICommand[commands.length + 1];
077 System.arraycopy(commands, 0, newCommands, 0, commands.length);
078 ICommand command = desc.newCommand();
079 command.setBuilderName(GRANITE_BUILDER_ID);
080 newCommands[newCommands.length - 1] = command;
081 Arrays.sort(newCommands, BUILDER_COMPARATOR);
082 desc.setBuildSpec(newCommands);
083 project.setDescription(desc, null);
084 }
085
086 public void deconfigure() throws CoreException {
087 IProjectDescription description = project.getDescription();
088 ICommand[] commands = description.getBuildSpec();
089
090 for (int i = 0; i < commands.length; i++) {
091 if (commands[i].getBuilderName().equals(GRANITE_BUILDER_ID)) {
092 ICommand[] newCommands = new ICommand[commands.length - 1];
093 System.arraycopy(commands, 0, newCommands, 0, i);
094 System.arraycopy(commands, i + 1, newCommands, i, commands.length - i - 1);
095 description.setBuildSpec(newCommands);
096 return;
097 }
098 }
099 }
100
101 public IProject getProject() {
102 return project;
103 }
104
105 public void setProject(IProject project) {
106 this.project = project;
107 }
108 }