1 11 package org.eclipse.core.internal.resources; 12 13 import java.net.URI ; 14 import java.util.Arrays ; 15 import java.util.HashMap ; 16 import org.eclipse.core.filesystem.URIUtil; 17 import org.eclipse.core.internal.events.BuildCommand; 18 import org.eclipse.core.internal.utils.FileUtil; 19 import org.eclipse.core.resources.*; 20 import org.eclipse.core.runtime.Assert; 21 import org.eclipse.core.runtime.IPath; 22 23 public class ProjectDescription extends ModelObject implements IProjectDescription { 24 private static final ICommand[] EMPTY_COMMAND_ARRAY = new ICommand[0]; 25 private static final IProject[] EMPTY_PROJECT_ARRAY = new IProject[0]; 27 private static final String [] EMPTY_STRING_ARRAY = new String [0]; 28 protected static boolean isReading = false; 29 30 protected static boolean isWriting = false; 34 protected ICommand[] buildSpec = EMPTY_COMMAND_ARRAY; 35 39 protected IProject[] cachedRefs = null; 40 protected String comment = ""; protected IProject[] dynamicRefs = EMPTY_PROJECT_ARRAY; 42 43 47 protected HashMap linkDescriptions = null; 48 49 protected URI location = null; 51 protected String [] natures = EMPTY_STRING_ARRAY; 52 protected IProject[] staticRefs = EMPTY_PROJECT_ARRAY; 53 54 public ProjectDescription() { 55 super(); 56 } 57 58 public Object clone() { 59 ProjectDescription clone = (ProjectDescription) super.clone(); 60 clone.linkDescriptions = null; 62 clone.buildSpec = getBuildSpec(true); 63 return clone; 64 } 65 66 69 private IProject[] copyAndRemoveDuplicates(IProject[] projects) { 70 IProject[] result = new IProject[projects.length]; 71 int count = 0; 72 next: for (int i = 0; i < projects.length; i++) { 73 IProject project = projects[i]; 74 for (int j = 0; j < count; j++) 76 if (project.equals(result[j])) 77 continue next; 78 result[count++] = project; 80 } 81 if (count < projects.length) { 82 IProject[] reduced = new IProject[count]; 84 System.arraycopy(result, 0, reduced, 0, count); 85 return reduced; 86 } 87 return result; 88 } 89 93 public IProject[] getAllReferences(boolean makeCopy) { 94 if (cachedRefs == null) { 95 IProject[] statik = getReferencedProjects(false); 96 IProject[] dynamic = getDynamicReferences(false); 97 if (dynamic.length == 0) { 98 cachedRefs = statik; 99 } else if (statik.length == 0) { 100 cachedRefs = dynamic; 101 } else { 102 IProject[] result = new IProject[dynamic.length + statik.length]; 104 System.arraycopy(statik, 0, result, 0, statik.length); 105 System.arraycopy(dynamic, 0, result, statik.length, dynamic.length); 106 cachedRefs = copyAndRemoveDuplicates(result); 107 } 108 } 109 return makeCopy ? (IProject[]) cachedRefs.clone() : cachedRefs; 111 } 112 113 116 public ICommand[] getBuildSpec() { 117 return getBuildSpec(true); 118 } 119 120 public ICommand[] getBuildSpec(boolean makeCopy) { 121 ICommand[] oldCommands = this.buildSpec; 123 if (oldCommands == null) 124 return EMPTY_COMMAND_ARRAY; 125 if (!makeCopy) 126 return oldCommands; 127 ICommand[] result = new ICommand[oldCommands.length]; 128 for (int i = 0; i < result.length; i++) 129 result[i] = (ICommand) ((BuildCommand) oldCommands[i]).clone(); 130 return result; 131 } 132 133 136 public String getComment() { 137 return comment; 138 } 139 140 143 public IProject[] getDynamicReferences() { 144 return getDynamicReferences(true); 145 } 146 147 public IProject[] getDynamicReferences(boolean makeCopy) { 148 if (dynamicRefs == null) 149 return EMPTY_PROJECT_ARRAY; 150 return makeCopy ? (IProject[]) dynamicRefs.clone() : dynamicRefs; 151 } 152 153 157 public URI getLinkLocationURI(IPath aPath) { 158 if (linkDescriptions == null) 159 return null; 160 LinkDescription desc = (LinkDescription) linkDescriptions.get(aPath); 161 return desc == null ? null : desc.getLocationURI(); 162 } 163 164 169 public HashMap getLinks() { 170 return linkDescriptions; 171 } 172 173 177 public IPath getLocation() { 178 if (location == null) 179 return null; 180 return FileUtil.toPath(location); 181 } 182 183 186 public URI getLocationURI() { 187 return location; 188 } 189 190 193 public String [] getNatureIds() { 194 return getNatureIds(true); 195 } 196 197 public String [] getNatureIds(boolean makeCopy) { 198 if (natures == null) 199 return EMPTY_STRING_ARRAY; 200 return makeCopy ? (String []) natures.clone() : natures; 201 } 202 203 206 public IProject[] getReferencedProjects() { 207 return getReferencedProjects(true); 208 } 209 210 public IProject[] getReferencedProjects(boolean makeCopy) { 211 if (staticRefs == null) 212 return EMPTY_PROJECT_ARRAY; 213 return makeCopy ? (IProject[]) staticRefs.clone() : staticRefs; 214 } 215 216 219 public boolean hasNature(String natureID) { 220 String [] natureIDs = getNatureIds(false); 221 for (int i = 0; i < natureIDs.length; ++i) 222 if (natureIDs[i].equals(natureID)) 223 return true; 224 return false; 225 } 226 227 232 public boolean hasPrivateChanges(ProjectDescription description) { 233 if (!Arrays.equals(dynamicRefs, description.getDynamicReferences(false))) 234 return true; 235 IPath otherLocation = description.getLocation(); 236 if (location == null) 237 return otherLocation != null; 238 return !location.equals(otherLocation); 239 } 240 241 246 public boolean hasPublicChanges(ProjectDescription description) { 247 if (!getName().equals(description.getName())) 248 return true; 249 if (!comment.equals(description.getComment())) 250 return true; 251 if (!Arrays.equals(buildSpec, description.getBuildSpec(false))) 253 return true; 254 if (!Arrays.equals(staticRefs, description.getReferencedProjects(false))) 255 return true; 256 if (!Arrays.equals(natures, description.getNatureIds(false))) 257 return true; 258 HashMap otherLinks = description.getLinks(); 259 if (linkDescriptions == null) 260 return otherLinks != null; 261 return !linkDescriptions.equals(otherLinks); 262 } 263 264 267 public ICommand newCommand() { 268 return new BuildCommand(); 269 } 270 271 274 public void setBuildSpec(ICommand[] value) { 275 Assert.isLegal(value != null); 276 ICommand[] result = new ICommand[value.length]; 278 for (int i = 0; i < result.length; i++) { 279 result[i] = (ICommand) ((BuildCommand) value[i]).clone(); 280 for (int j = 0; j < buildSpec.length; j++) { 283 if (result[i].equals(buildSpec[j])) { 284 ((BuildCommand) result[i]).setBuilder(((BuildCommand) buildSpec[j]).getBuilder()); 285 break; 286 } 287 } 288 } 289 buildSpec = result; 290 } 291 292 295 public void setComment(String value) { 296 comment = value; 297 } 298 299 302 public void setDynamicReferences(IProject[] value) { 303 Assert.isLegal(value != null); 304 dynamicRefs = copyAndRemoveDuplicates(value); 305 cachedRefs = null; 306 } 307 308 313 public void setLinkDescriptions(HashMap linkDescriptions) { 314 this.linkDescriptions = linkDescriptions; 315 } 316 317 321 public void setLinkLocation(IPath path, LinkDescription description) { 322 HashMap tempMap = linkDescriptions; 323 if (description != null) { 324 if (tempMap == null) 326 tempMap = new HashMap (10); 327 else 328 tempMap = (HashMap ) tempMap.clone(); 330 tempMap.put(path, description); 331 linkDescriptions = tempMap; 332 } else { 333 if (tempMap != null) { 335 HashMap newMap = (HashMap ) tempMap.clone(); 337 newMap.remove(path); 338 linkDescriptions = newMap.size() == 0 ? null : newMap; 339 } 340 } 341 } 342 343 346 public void setLocation(IPath path) { 347 this.location = path == null ? null : URIUtil.toURI(path); 348 } 349 350 public void setLocationURI(URI location) { 351 this.location = location; 352 } 353 354 357 public void setName(String value) { 358 super.setName(value); 359 } 360 361 364 public void setNatureIds(String [] value) { 365 natures = (String []) value.clone(); 366 } 367 368 371 public void setReferencedProjects(IProject[] value) { 372 Assert.isLegal(value != null); 373 staticRefs = copyAndRemoveDuplicates(value); 374 cachedRefs = null; 375 } 376 } 377 | Popular Tags |