1 19 20 package org.netbeans.modules.projectimport.j2seimport; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.LinkedHashSet ; 28 import java.util.Set ; 29 import java.util.logging.Logger ; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.FileUtil; 32 import org.openide.util.NbBundle; 33 34 38 public final class AbstractProject implements ProjectModel { 39 private final Collection libraries; 40 private final Collection userLibraries; 41 private final Collection sourceRoots; 42 private final Set dependencies; 43 private File jdkDirectory; 44 private String jdkId; 45 private final FileObject projectDir; 46 private final String name; 47 private WarningContainer warnings; 48 49 private boolean isAlreadyImported = false; 50 51 public static final Logger logger = 52 LoggerFactory.getDefault().createLogger(AbstractProject.class); 53 54 55 56 public AbstractProject(String name, FileObject projectDir) { 57 this.projectDir = projectDir; 58 this.name = name; 59 if (name.indexOf('/') != -1) { 60 throw new IllegalArgumentException (); 61 } 62 63 libraries = new LinkedHashSet (); 64 userLibraries = new LinkedHashSet (); 65 sourceRoots = new LinkedHashSet (); 66 dependencies = new LinkedHashSet (); 67 warnings = new WarningContainer(); 68 69 logger.finest("created project: " + "\""+ name + "\" (" + projectDir.getPath() + ")"); 72 } 73 74 public String getName() { 75 return name; 76 } 77 78 public FileObject getProjectDir() { 79 return projectDir; 80 } 81 82 83 public boolean isAlreadyImported() { 84 return isAlreadyImported; 85 } 86 87 public void setAsImported() { 88 isAlreadyImported = true; 89 } 90 91 public Collection getLibraries() { 92 return libraries; 93 } 94 95 private void addWarning(final String warning, boolean userNotification) { 96 StringBuffer sbuf = new StringBuffer (NbBundle.getMessage(AbstractProject.class, "MSG_ProjectDefinitionWarning")); sbuf.append(" ").append(warning); 99 String warningPlusPrefix = sbuf.toString(); 100 warnings.add(warningPlusPrefix, userNotification); 101 AbstractProject.logger.warning(warningPlusPrefix); 102 } 103 104 105 public boolean addLibrary(final AbstractProject.Library lEntry) { 106 if (isAlreadyImported()) { 107 throw new IllegalStateException ("Unexpected usage: project was already imported"); } 109 110 if (!lEntry.isValid()) { 111 addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_InvalidLibrary", 112 lEntry.getArchiv().getAbsolutePath(),this.getName()), false); } 114 115 boolean retVal = libraries.add(lEntry); 116 if (retVal == false) { 117 addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_AlreadyExistsLibrary", 118 lEntry.getArchiv().getAbsolutePath(),this.getName()), false); } 120 121 logger.finest("added library: " + "\"" + lEntry.getArchiv().getAbsolutePath() + "\""); 123 return retVal; 124 } 125 126 127 public Collection getUserLibraries() { 128 return userLibraries; 129 } 130 131 public boolean addUserLibrary(final AbstractProject.UserLibrary uEntry) { 132 if (isAlreadyImported()) { 133 throw new IllegalStateException ("Unexpected usage: project was already imported"); } 135 136 if (!uEntry.isValid()) { 137 addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_InvalidUserLibrary", 138 uEntry.getName(),this.getName()), true); } 140 141 checkUserLibrary(uEntry); 142 boolean retVal = userLibraries.add(uEntry); 143 144 if (retVal == false) { 145 addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_AlreadyExistsUserLibrary", 146 uEntry.getName(),this.getName()), false); } 148 149 logger.finest("added user library: " + "\"" + uEntry.getName() + "\""); 151 return retVal; 152 } 153 154 private void checkUserLibrary(AbstractProject.UserLibrary uLibrary) { 155 for (Iterator it = uLibrary.getLibraries().iterator(); it.hasNext();) { 156 AbstractProject.Library lEntry = (AbstractProject.Library)it.next(); 157 if (getLibraries().contains(lEntry)) { 158 addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_AlreadyExistsLibrary", 159 lEntry.getArchiv().getAbsolutePath(),this.getName()), false); } 161 } 162 } 163 164 public Collection getSourceRoots() { 165 return sourceRoots; 166 } 167 168 public boolean addSourceRoot(final AbstractProject.SourceRoot srcEntry) { 169 if (isAlreadyImported()) { 170 throw new IllegalStateException ("Unexpected usage: project was already imported"); } 172 173 if (!srcEntry.isValid()) { 174 addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_InvalidSourceRoot", 175 srcEntry.getDirectory().getAbsolutePath(),this.getName()), false); return false; 177 } 178 179 boolean retVal = sourceRoots.add(srcEntry); 180 if (retVal == false) { 181 addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_AlreadyExistsSourceRoot", 182 srcEntry.getDirectory().getAbsolutePath(),this.getName()), false); } 184 185 logger.finest("added source root: " + "\"" + srcEntry.getLabel()+" ("+srcEntry.getDirectory().getAbsolutePath()+ ")\"" ); 187 return retVal; 188 } 189 190 public java.util.Set getDependencies() { 191 return dependencies; 192 } 193 194 public boolean addDependency(final AbstractProject projectDefinition) { 195 if (isAlreadyImported()) { 196 throw new IllegalStateException ("Unexpected usage: project was already imported"); } 198 199 logger.finest("added dependency: " + "\""+ projectDefinition.getName() + "\" (" + projectDefinition.getProjectDir().getPath() + ")"); 202 return dependencies.add(projectDefinition); 203 } 204 205 206 public File getJDKDirectory() { 207 return jdkDirectory; 208 } 209 210 public void setJDKDirectory(File jdkDirectory) { 211 this.jdkDirectory = jdkDirectory; 212 } 213 214 public void setInvalidJDK(String expectedJDKId) { 215 addWarning(NbBundle.getMessage(AbstractProject.class, "MSG_JDKDoesnExistUseDefault", 216 this.getName(), expectedJDKId),true); } 218 219 220 public WarningContainer getWarnings() { 221 return warnings; 222 } 223 224 public Collection getErrors() { 225 Collection errors = new HashSet (); 226 DependencyValidator instance = DependencyValidator.checkProject(this); 227 if (!instance.isValid()) { 228 errors.add(instance.getErrorMessage()); 229 } 230 if (getSourceRoots().size() == 0) { 231 errors.add(NbBundle.getMessage( 232 AbstractProject.class,"ERR_NoSourceRoot",this.getName())); 234 } 235 return errors; 236 } 237 238 239 public static final class SourceRoot implements ProjectModel.SourceRoot { 240 private final String label; 241 private final File sourceFolder; 242 243 public SourceRoot(String label, File sourceFolder) { 244 this.label = label; 245 this.sourceFolder = sourceFolder; 246 } 247 public final String getLabel() { 248 return label; 249 } 250 251 public final File getDirectory() { 252 return sourceFolder; 253 } 254 255 256 public final boolean isValid() { 257 return AbstractProject.isValid(getDirectory()); 258 } 259 260 public boolean equals(Object obj) { 261 return (obj instanceof SourceRoot) ? 262 ((SourceRoot)obj).getDirectory().equals(getDirectory()) : false; 263 } 264 265 public int hashCode() { 266 return getLabel().hashCode(); 267 } 268 269 public String toString() { 270 StringBuffer sb = new StringBuffer (); 271 sb.append(NbBundle.getMessage(AbstractProject.class,"TXT_SourceRoot")); sb.append((isValid()) ? "" : "!"); sb.append((sourceFolder != null) ? sourceFolder.getAbsolutePath() : ""); 275 return sb.toString(); 276 } 277 278 } 279 280 public static final class Library implements ProjectModel.Library { 281 private File archiv; 282 public Library(File archiv) { 283 this.archiv = archiv; 284 } 285 286 public java.io.File getArchiv() { 287 return archiv; 288 } 289 290 public final boolean isValid() { 291 return AbstractProject.isValidArchiv(getArchiv()); 292 } 293 294 public boolean equals(Object obj) { 295 return (obj instanceof Library ) ? 296 ((Library )obj).getArchiv().equals(getArchiv()) : false; 297 } 298 299 public String toString() { 300 StringBuffer sb = new StringBuffer (); 301 sb.append(NbBundle.getMessage(AbstractProject.class,"TXT_Library")); sb.append((isValid()) ? "" : "!"); sb.append((archiv != null) ? archiv.getAbsolutePath() : ""); 305 return sb.toString(); 306 } 307 } 308 309 public static final class UserLibrary implements ProjectModel.UserLibrary { 310 private final String name; 311 private Collection libraries; 312 private boolean fileNotFound = false; 313 private Collection dependencies; 314 315 316 public UserLibrary(String name, boolean fileNotFound) { 317 this(name); 318 this.fileNotFound = fileNotFound; 319 } 320 321 public UserLibrary(String name) { 322 this.name = name; 323 libraries = new HashSet (); 324 dependencies = new HashSet (); 325 } 326 327 public UserLibrary(String name, Collection libraries) { 328 this(name); 329 libraries.addAll(libraries); 330 } 331 332 333 public boolean addLibrary(ProjectModel.Library lEntry) { 334 logger.finest("added library: " + "\"" + lEntry.getArchiv().getAbsolutePath() + "\""); 336 return libraries.add(lEntry); 337 } 338 339 public boolean addDependency(ProjectModel.UserLibrary uLibrary) { 340 logger.finest("added library dependency: " + "\"" + uLibrary.getName() + "\""); 342 return dependencies.add(uLibrary); 343 } 344 345 346 347 public String getName() { 348 return name; 349 } 350 351 public Collection getLibraries() { 352 return libraries; 353 } 354 355 public final boolean fileNotFound() { 356 return fileNotFound; 357 } 358 359 public final boolean isValid() { 360 boolean isValid = false; for (Iterator it = getLibraries().iterator(); it.hasNext();) { 362 AbstractProject.Library lEntry = (AbstractProject.Library)it.next(); 363 isValid = lEntry.isValid(); 364 if (!isValid) {break;} 365 366 } 367 return isValid; 368 } 369 370 public String toString() { 371 StringBuffer sb = new StringBuffer (); 372 sb.append(NbBundle.getMessage(AbstractProject.class,"TXT_UserLibrary")); sb.append((isValid()) ? "" : "!"); sb.append(getName()); 375 376 return sb.toString(); 377 } 378 379 public Collection getDependencies() { 380 return dependencies; 381 } 382 383 384 } 385 386 private static boolean isValid(File f) { 387 File srcFolder = FileUtil.normalizeFile(f); 388 return (srcFolder.isDirectory() && FileUtil.toFileObject(srcFolder) != null); 389 } 390 391 private static boolean isValidArchiv(File f) { 392 File srcFolder = FileUtil.normalizeFile(f); 393 FileObject srcFileObject = FileUtil.toFileObject(srcFolder); 394 return (srcFolder.exists() && srcFileObject != null) && FileUtil.isArchiveFile(srcFileObject); 395 } 396 397 public String toString() { 398 StringBuffer sb = new StringBuffer (); 399 400 sb.append(NbBundle.getMessage(AbstractProject.class,"TXT_Project")); sb.append(this.getName()).append("\n"); 403 Collection all = new ArrayList (); 404 all.addAll(getSourceRoots()); 405 all.addAll(getLibraries()); 406 all.addAll(getUserLibraries()); 407 408 for (Iterator it = all.iterator(); it.hasNext();) { 409 sb.append(it.next().toString()).append("\n"); } 411 412 for (Iterator it = getDependencies().iterator(); it.hasNext();) { 413 sb.append(NbBundle.getMessage(AbstractProject.class,"TXT_Deps")); sb.append(((AbstractProject)it.next()).getProjectDir().getPath()).append("\n"); 415 } 416 417 418 419 sb.append(NbBundle.getMessage(AbstractProject.class,"TXT_JDK")); sb.append((getJDKDirectory() != null) ? getJDKDirectory().getAbsolutePath() : "!"); 422 return sb.toString(); 423 } 424 425 public String getJdkId() { 426 return jdkId; 427 } 428 429 public void setJdkId(String jdkId) { 430 this.jdkId = jdkId; 431 } 432 433 } 434 | Popular Tags |