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 @Override
047 public int compare(ICommand c1, ICommand c2) {
048 if (GRANITE_BUILDER_ID.equals(c1.getBuilderName())) {
049 if (JAVA_BUILDER_ID.equals(c2.getBuilderName()))
050 return 1;
051 if (FLEX_BUILDER_ID.equals(c2.getBuilderName()))
052 return -1;
053 }
054 else if (JAVA_BUILDER_ID.equals(c1.getBuilderName())) {
055 if (GraniteBuilder.GRANITE_BUILDER_ID.equals(c2.getBuilderName()) || FLEX_BUILDER_ID.equals(c2.getBuilderName()))
056 return -1;
057 }
058 else if (FLEX_BUILDER_ID.equals(c1.getBuilderName())) {
059 if (GRANITE_BUILDER_ID.equals(c2.getBuilderName()) || JAVA_BUILDER_ID.equals(c2.getBuilderName()))
060 return 1;
061 }
062 return 0;
063 }
064 };
065
066 private IProject project;
067
068 @Override
069 public void configure() throws CoreException {
070 IProjectDescription desc = project.getDescription();
071 ICommand[] commands = desc.getBuildSpec();
072
073 for (ICommand command : commands) {
074 if (command.getBuilderName().equals(GRANITE_BUILDER_ID))
075 return;
076 }
077
078 ICommand[] newCommands = new ICommand[commands.length + 1];
079 System.arraycopy(commands, 0, newCommands, 0, commands.length);
080 ICommand command = desc.newCommand();
081 command.setBuilderName(GRANITE_BUILDER_ID);
082 newCommands[newCommands.length - 1] = command;
083 Arrays.sort(newCommands, BUILDER_COMPARATOR);
084 desc.setBuildSpec(newCommands);
085 project.setDescription(desc, null);
086 }
087
088 @Override
089 public void deconfigure() throws CoreException {
090 IProjectDescription description = project.getDescription();
091 ICommand[] commands = description.getBuildSpec();
092
093 for (int i = 0; i < commands.length; i++) {
094 if (commands[i].getBuilderName().equals(GRANITE_BUILDER_ID)) {
095 ICommand[] newCommands = new ICommand[commands.length - 1];
096 System.arraycopy(commands, 0, newCommands, 0, i);
097 System.arraycopy(commands, i + 1, newCommands, i, commands.length - i - 1);
098 description.setBuildSpec(newCommands);
099 return;
100 }
101 }
102 }
103
104 @Override
105 public IProject getProject() {
106 return project;
107 }
108
109 @Override
110 public void setProject(IProject project) {
111 this.project = project;
112 }
113 }