KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > events > BuildCommand


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.core.internal.events;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15 import org.eclipse.core.internal.resources.ModelObject;
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.runtime.*;
18
19 /**
20  * The concrete implementation of <tt>ICommand</tt>. This object
21  * stores information about a particular builder, including a reference
22  * to the builder instance itself if it has been instantiated.
23  */

24 public class BuildCommand extends ModelObject implements ICommand {
25     /**
26      * Internal flag masks for different build triggers.
27      */

28     private static final int MASK_AUTO = 0x01;
29     private static final int MASK_INCREMENTAL = 0x02;
30     private static final int MASK_FULL = 0x04;
31     private static final int MASK_CLEAN = 0x08;
32
33     /**
34      * Flag bit indicating if this build command is configurable
35      */

36     private static final int MASK_CONFIGURABLE = 0x10;
37     
38     /**
39      * Flag bit indicating if the configurable bit has been loaded from
40      * the builder extension declaration in XML yet.
41      */

42     private static final int MASK_CONFIG_COMPUTED = 0x20;
43
44     private static final int ALL_TRIGGERS = MASK_AUTO | MASK_CLEAN | MASK_FULL | MASK_INCREMENTAL;
45
46     protected HashMap JavaDoc arguments;
47     
48     /**
49      * The builder instance for this command. Null if the builder has
50      * not yet been instantiated.
51      */

52     protected IncrementalProjectBuilder builder;
53
54     /**
55      * The triggers that this builder will respond to. Since build triggers are not
56      * bit-maskable, we use internal bit masks to represent each
57      * trigger (MASK_* constants). By default, a command responds to all
58      * build triggers.
59      */

60     private int triggers = ALL_TRIGGERS;
61
62     /**
63      * Returns the trigger bit mask for the given trigger constant.
64      */

65     private static int maskForTrigger(int trigger) {
66         switch (trigger) {
67             case IncrementalProjectBuilder.AUTO_BUILD :
68                 return MASK_AUTO;
69             case IncrementalProjectBuilder.INCREMENTAL_BUILD :
70                 return MASK_INCREMENTAL;
71             case IncrementalProjectBuilder.FULL_BUILD :
72                 return MASK_FULL;
73             case IncrementalProjectBuilder.CLEAN_BUILD :
74                 return MASK_CLEAN;
75         }
76         return 0;
77     }
78
79     public BuildCommand() {
80         super(""); //$NON-NLS-1$
81
this.arguments = new HashMap JavaDoc(0);
82     }
83
84     public Object JavaDoc clone() {
85         BuildCommand result = null;
86         result = (BuildCommand) super.clone();
87         if (result == null)
88             return null;
89         result.setArguments(getArguments());
90         //don't let references to builder instances leak out because they reference trees
91
result.setBuilder(null);
92         return result;
93     }
94
95     /**
96      * Computes whether this build command allows configuration of its
97      * triggers, based on information in the builder extension declaration.
98      */

99     private void computeIsConfigurable() {
100         triggers |= MASK_CONFIG_COMPUTED;
101         IExtension extension = Platform.getExtensionRegistry().getExtension(ResourcesPlugin.PI_RESOURCES, ResourcesPlugin.PT_BUILDERS, name);
102         if (extension != null) {
103             IConfigurationElement[] configs = extension.getConfigurationElements();
104             if (configs.length != 0) {
105                 String JavaDoc value = configs[0].getAttribute("isConfigurable"); //$NON-NLS-1$
106
setConfigurable(value != null && value.equalsIgnoreCase(Boolean.TRUE.toString()));
107             }
108         }
109     }
110
111     /* (non-Javadoc)
112      * Method declared on Object
113      */

114     public boolean equals(Object JavaDoc object) {
115         if (this == object)
116             return true;
117         if (!(object instanceof BuildCommand))
118             return false;
119         BuildCommand command = (BuildCommand) object;
120         // equal if same builder name and equal argument tables
121
return getBuilderName().equals(command.getBuilderName()) && getArguments(false).equals(command.getArguments(false)) && triggers == command.triggers;
122     }
123
124     /**
125      * @see ICommand#getArguments()
126      */

127     public Map JavaDoc getArguments() {
128         return getArguments(true);
129     }
130
131     public Map JavaDoc getArguments(boolean makeCopy) {
132         return arguments == null ? null : (makeCopy ? (Map JavaDoc) arguments.clone() : arguments);
133     }
134
135     public IncrementalProjectBuilder getBuilder() {
136         return builder;
137     }
138
139     /**
140      * @see ICommand#getBuilderName()
141      */

142     public String JavaDoc getBuilderName() {
143         return getName();
144     }
145
146     /* (non-Javadoc)
147      * Method declared on Object
148      */

149     public int hashCode() {
150         // hash on name alone
151
return 37 * getName().hashCode() + triggers;
152     }
153
154     /**
155      * @see ICommand#isBuilding(int)
156      */

157     public boolean isBuilding(int trigger) {
158         return (triggers & maskForTrigger(trigger)) != 0;
159     }
160
161     public boolean isConfigurable() {
162         if ((triggers & MASK_CONFIG_COMPUTED) == 0)
163             computeIsConfigurable();
164         return (triggers & MASK_CONFIGURABLE) != 0;
165     }
166
167     /**
168      * @see ICommand#setArguments(Map)
169      */

170     public void setArguments(Map JavaDoc value) {
171         // copy parameter for safety's sake
172
arguments = value == null ? null : new HashMap JavaDoc(value);
173     }
174
175     public void setBuilder(IncrementalProjectBuilder builder) {
176         this.builder = builder;
177     }
178
179     /**
180      * @see ICommand#setBuilderName(String)
181      */

182     public void setBuilderName(String JavaDoc value) {
183         //don't allow builder name to be null
184
setName(value == null ? "" : value); //$NON-NLS-1$
185
}
186
187     /**
188      * @see ICommand#setBuilding(int, boolean)
189      */

190     public void setBuilding(int trigger, boolean value) {
191         if (!isConfigurable())
192             return;
193         if (value)
194             triggers |= maskForTrigger(trigger);
195         else
196             triggers &= ~maskForTrigger(trigger);
197     }
198
199     /**
200      * Sets whether this build command allows its build triggers to be configured.
201      * This value should only be set when the builder extension declaration is
202      * read from the registry, or when a build command is read from the project
203      * description file on disk. The value is not otherwise mutable.
204      */

205     public void setConfigurable(boolean value) {
206         triggers |= MASK_CONFIG_COMPUTED;
207         if (value)
208             triggers |= MASK_CONFIGURABLE;
209         else
210             triggers = ALL_TRIGGERS;
211     }
212     
213     /**
214      * For debugging purposes only
215      */

216     public String JavaDoc toString() {
217         return "BuildCommand(" + getName() + ")";//$NON-NLS-1$ //$NON-NLS-2$
218
}
219 }
220
Popular Tags