KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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;
12
13 import org.eclipse.core.resources.*;
14 import org.eclipse.core.runtime.*;
15 /**
16  */

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