KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > natures > BaseProject


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.internal.core.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.resources.IWorkspace;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.PlatformObject;
20 import org.eclipse.pde.internal.core.PDECore;
21
22 /**
23  */

24 public abstract class BaseProject extends PlatformObject implements IProjectNature {
25     private IProject project;
26
27     public BaseProject() {
28         super();
29     }
30
31     protected void addToBuildSpec(String JavaDoc builderID) throws CoreException {
32
33         IProjectDescription description = getProject().getDescription();
34         ICommand builderCommand = getBuilderCommand(description, builderID);
35
36         if (builderCommand == null) {
37             // Add a new build spec
38
ICommand command = description.newCommand();
39             command.setBuilderName(builderID);
40             setBuilderCommand(description, command);
41         }
42     }
43
44     private ICommand getBuilderCommand(IProjectDescription description, String JavaDoc builderId)
45             throws CoreException {
46         ICommand[] commands = description.getBuildSpec();
47         for (int i = 0; i < commands.length; ++i) {
48             if (commands[i].getBuilderName().equals(builderId)) {
49                 return commands[i];
50             }
51         }
52         return null;
53     }
54
55     public IProject getProject() {
56         return project;
57     }
58
59     protected IWorkspace getWorkspace() {
60         return PDECore.getWorkspace();
61     }
62
63     protected void removeFromBuildSpec(String JavaDoc builderID) throws CoreException {
64         IProjectDescription description = getProject().getDescription();
65         ICommand[] commands = description.getBuildSpec();
66         for (int i = 0; i < commands.length; ++i) {
67             if (commands[i].getBuilderName().equals(builderID)) {
68                 ICommand[] newCommands = new ICommand[commands.length - 1];
69                 System.arraycopy(commands, 0, newCommands, 0, i);
70                 System
71                         .arraycopy(commands, i + 1, newCommands, i, commands.length - i
72                                 - 1);
73                 description.setBuildSpec(newCommands);
74                 return;
75             }
76         }
77     }
78
79     private void setBuilderCommand(IProjectDescription description, ICommand newCommand)
80             throws CoreException {
81
82         ICommand[] oldCommands = description.getBuildSpec();
83         ICommand oldBuilderCommand = getBuilderCommand(description, newCommand
84                 .getBuilderName());
85
86         ICommand[] newCommands;
87
88         if (oldBuilderCommand == null) {
89             // Add a build spec after other builders
90
newCommands = new ICommand[oldCommands.length + 1];
91             System.arraycopy(oldCommands, 0, newCommands, 0, oldCommands.length);
92             newCommands[oldCommands.length] = newCommand;
93         } else {
94             for (int i = 0, max = oldCommands.length; i < max; i++) {
95                 if (oldCommands[i] == oldBuilderCommand) {
96                     oldCommands[i] = newCommand;
97                     break;
98                 }
99             }
100             newCommands = oldCommands;
101         }
102
103         // Commit the spec change into the project
104
description.setBuildSpec(newCommands);
105         getProject().setDescription(description, null);
106     }
107
108     public void setProject(IProject project) {
109         this.project = project;
110     }
111 }
112
Popular Tags