1 | /******************************************************************************* |
2 | * Copyright (c) 2007, 2008 IBM Corporation and others. |
3 | * All rights reserved. This program and the accompanying materials |
4 | * are made available under the terms of the Eclipse Public License v1.0 |
5 | * which accompanies this distribution, and is available at |
6 | * http://www.eclipse.org/legal/epl-v10.html |
7 | * |
8 | * Contributors: |
9 | * IBM Corporation - initial API and implementation |
10 | *******************************************************************************/ |
11 | package org.eclipse.pde.api.tools.internal.natures; |
12 | |
13 | import org.eclipse.core.resources.ICommand; |
14 | import org.eclipse.core.resources.IProject; |
15 | import org.eclipse.core.resources.IProjectDescription; |
16 | import org.eclipse.core.resources.IProjectNature; |
17 | import org.eclipse.core.runtime.CoreException; |
18 | import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin; |
19 | |
20 | public class ApiToolProjectNature implements IProjectNature { |
21 | |
22 | IProject project; |
23 | |
24 | /** |
25 | * Add the Api plugin builder in the project build spec |
26 | */ |
27 | public void configure() throws CoreException { |
28 | addToBuildSpec(ApiPlugin.BUILDER_ID); |
29 | } |
30 | |
31 | /** |
32 | * Remove the Api plugin builder from the project build spec |
33 | */ |
34 | public void deconfigure() throws CoreException { |
35 | removeFromBuildSpec(ApiPlugin.BUILDER_ID); |
36 | } |
37 | |
38 | /** |
39 | * Retrieve the current project |
40 | * |
41 | * @return the current project |
42 | */ |
43 | public IProject getProject() { |
44 | return this.project; |
45 | } |
46 | |
47 | /** |
48 | * Sset the current project |
49 | */ |
50 | public void setProject(IProject project) { |
51 | this.project = project; |
52 | } |
53 | |
54 | /** |
55 | * Adds a builder to the build spec for the given project. |
56 | */ |
57 | protected void addToBuildSpec(String builderID) throws CoreException { |
58 | IProjectDescription description = this.project.getDescription(); |
59 | ICommand[] oldBuildSpec = description.getBuildSpec(); |
60 | int oldApiCommandIndex = -1; |
61 | int length = oldBuildSpec.length; |
62 | loop: for (int i = 0; i < length; ++i) { |
63 | if (oldBuildSpec[i].getBuilderName().equals(builderID)) { |
64 | oldApiCommandIndex = i; |
65 | break loop; |
66 | } |
67 | } |
68 | ICommand[] newCommands; |
69 | |
70 | ICommand newCommand = description.newCommand(); |
71 | newCommand.setBuilderName(builderID); |
72 | |
73 | if (oldApiCommandIndex == -1) { |
74 | // Add a API build spec after all existing builders |
75 | newCommands = new ICommand[length + 1]; |
76 | System.arraycopy(oldBuildSpec, 0, newCommands, 0, length); |
77 | newCommands[length] = newCommand; |
78 | } else { |
79 | oldBuildSpec[oldApiCommandIndex] = newCommand; |
80 | newCommands = oldBuildSpec; |
81 | } |
82 | |
83 | // Commit the spec change into the project |
84 | description.setBuildSpec(newCommands); |
85 | this.project.setDescription(description, null); |
86 | } |
87 | |
88 | /** |
89 | * Removes the given builder from the build spec for the given project. |
90 | */ |
91 | protected void removeFromBuildSpec(String builderID) throws CoreException { |
92 | IProjectDescription description = this.project.getDescription(); |
93 | ICommand[] commands = description.getBuildSpec(); |
94 | for (int i = 0; i < commands.length; ++i) { |
95 | if (commands[i].getBuilderName().equals(builderID)) { |
96 | ICommand[] newCommands = new ICommand[commands.length - 1]; |
97 | System.arraycopy(commands, 0, newCommands, 0, i); |
98 | System.arraycopy(commands, i + 1, newCommands, i, commands.length - i - 1); |
99 | description.setBuildSpec(newCommands); |
100 | this.project.setDescription(description, null); |
101 | return; |
102 | } |
103 | } |
104 | } |
105 | } |