1 19 20 package org.netbeans.modules.j2ee.earproject.ui.customizer; 21 22 import java.beans.BeanInfo ; 23 import java.io.File ; 24 import javax.swing.Icon ; 25 import javax.swing.ImageIcon ; 26 import org.netbeans.api.project.ant.AntArtifact; 27 import org.netbeans.api.project.libraries.Library; 28 import org.openide.filesystems.FileObject; 29 import org.openide.filesystems.Repository; 30 import org.openide.loaders.DataFolder; 31 import org.openide.util.NbBundle; 32 import org.openide.util.Utilities; 33 34 40 public final class VisualClassPathItem { 41 42 43 public static enum Type { 44 JAR, LIBRARY, ARTIFACT, CLASSPATH 45 }; 46 47 public static final String PATH_IN_WAR_LIB = "WEB-INF/lib"; public static final String PATH_IN_EAR = "/"; public static final String PATH_IN_EAR_NONE = null; 50 51 private static final String RESOURCE_ICON_JAR = 52 "org/netbeans/modules/j2ee/earproject/ui/resources/jar.gif"; private static final String RESOURCE_ICON_LIBRARY = 54 "org/netbeans/modules/j2ee/earproject/ui/resources/libraries.gif"; private static final String RESOURCE_ICON_ARTIFACT = 56 "org/netbeans/modules/j2ee/earproject/ui/resources/projectDependencies.gif"; private static final String RESOURCE_ICON_CLASSPATH = 58 "org/netbeans/modules/j2ee/earproject/ui/resources/referencedClasspath.gif"; 60 private static final Icon ICON_JAR = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_JAR ) ); 61 private static Icon iconFolder; 62 private static final Icon ICON_LIBRARY = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_LIBRARY ) ); 63 private static final Icon ICON_ARTIFACT = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_ARTIFACT ) ); 64 private static final Icon ICON_CLASSPATH = new ImageIcon ( Utilities.loadImage( RESOURCE_ICON_CLASSPATH ) ); 65 66 private final Type type; 67 private final Object cpElement; 68 private String raw; 69 private final String eval; 70 private String pathInEAR; 71 private String origPathInEAR; 72 73 VisualClassPathItem(Object cpElement, Type type, String raw, String eval, String pathInEAR) { 74 this.cpElement = cpElement; 75 this.type = type; 76 this.raw = raw; 77 this.eval = eval; 78 this.pathInEAR = pathInEAR; 79 this.origPathInEAR = pathInEAR; 80 81 if (cpElement != null) { 83 switch ( getType() ) { 84 case JAR: 85 if (!(cpElement instanceof File )) { 86 throw new IllegalArgumentException ("File instance must be " + "passed as object for Type.JAR. Was: "+cpElement.getClass()); } 89 break; 90 case LIBRARY: 91 if (!(cpElement instanceof Library)) { 92 throw new IllegalArgumentException ("Library instance must be " + "passed as object for Type.LIBRARY. Was: "+cpElement.getClass()); } 95 break; 96 case ARTIFACT: 97 if (!(cpElement instanceof AntArtifact)) { 98 throw new IllegalArgumentException ("AntArtifact instance must be " + "passed as object for Type.ARTIFACT. Was: "+cpElement.getClass()); } 101 break; 102 case CLASSPATH: 103 if (!(cpElement instanceof String )) { 104 throw new IllegalArgumentException ("String instance must be " + "passed as object for Type.CLASSPATH. Was: "+cpElement.getClass()); } 107 break; 108 default: 109 throw new IllegalArgumentException ("Unknown type " + "passed. Was: "+getType()); } 112 } 113 } 114 115 public String getPathInEAR() { 116 return pathInEAR; 117 } 118 119 public void setPathInEAR(String path) { 120 pathInEAR = path; 121 } 122 123 public String getOrigPathInEAR() { 124 return origPathInEAR; 125 } 126 127 public Object getObject() { 128 return cpElement; 129 } 130 131 public Type getType() { 132 return type; 133 } 134 135 public void setRaw(String raw) { 136 this.raw = raw; 137 } 138 139 public String getRaw() { 140 return raw; 141 } 142 143 public String getEvaluated() { 144 return eval == null ? getRaw() : eval; 145 } 146 147 public boolean canDelete() { 148 return getType() != Type.CLASSPATH; 149 } 150 151 public Icon getIcon() { 152 if (getObject() == null) { 153 return null; 155 } 156 157 switch(getType()) { 158 case JAR: 159 if (((File ) getObject()).isDirectory()) { 160 return getFolderIcon(); 161 } else { 162 return ICON_JAR; 163 } 164 case LIBRARY: 165 return ICON_LIBRARY; 166 case ARTIFACT: 167 return ICON_ARTIFACT; 168 case CLASSPATH: 169 return ICON_CLASSPATH; 170 default: 171 return null; 172 } 173 174 } 175 176 public String toString() { 177 switch ( getType() ) { 178 case JAR: 179 if (getObject() != null) { 180 return getEvaluated(); 181 } else { 182 return NbBundle.getMessage(VisualClassPathItem.class, "LBL_MISSING_FILE", getFileRefName(getEvaluated())); 183 } 184 case LIBRARY: 185 if (getObject() != null) { 186 return ((Library)this.getObject()).getDisplayName(); 187 } else { 188 return NbBundle.getMessage(VisualClassPathItem.class, "LBL_MISSING_LIBRARY", getLibraryName(getRaw())); 189 } 190 case ARTIFACT: 191 if (getObject() != null) { 192 return getEvaluated(); 193 } else { 194 return NbBundle.getMessage(VisualClassPathItem.class, "LBL_MISSING_PROJECT", getProjectName(getEvaluated())); 195 } 196 case CLASSPATH: 197 return getEvaluated(); 198 default: 199 assert true : "Unknown item type"; return getEvaluated(); 201 } 202 } 203 204 private String getProjectName(String id) { 205 return id.matches("\\$\\{reference\\..*\\.id\\}") ? id.substring(12, id.indexOf('.', 12)) : id; 208 } 209 210 private String getLibraryName(String id) { 211 return id.substring(7, id.indexOf(".classpath")); } 214 215 private String getFileRefName(String id) { 216 return id.substring(17, id.length()-1); 218 } 219 220 public int hashCode() { 221 int hash = getType().ordinal(); 222 switch ( getType() ) { 223 case ARTIFACT: 224 if (getObject() != null) { 225 AntArtifact aa = (AntArtifact)getObject(); 226 hash += aa.getType().hashCode(); 227 hash += aa.getScriptLocation().hashCode(); 228 hash += aa.getArtifactLocations()[0].hashCode(); 229 } else { 230 hash += getRaw().hashCode(); 231 } 232 break; 233 default: 234 if (getObject() != null) { 235 hash += getObject().hashCode(); 236 } else { 237 hash += getRaw().hashCode(); 238 } 239 break; 240 } 241 return hash; 242 } 243 244 public boolean equals( Object object ) { 245 if ( !( object instanceof VisualClassPathItem ) ) { 246 return false; 247 } 248 VisualClassPathItem vcpi = (VisualClassPathItem)object; 249 250 if ( getType() != vcpi.getType() ) { 251 return false; 252 } 253 254 switch ( getType() ) { 255 case ARTIFACT: 256 AntArtifact aa2 = (AntArtifact) vcpi.getObject(); 257 AntArtifact aa1 = (AntArtifact) getObject(); 258 if (aa1 != null && aa2 != null) { 259 if ( aa1.getType() != aa2.getType() ) { 260 return false; 261 } 262 263 if ( !aa1.getScriptLocation().equals( aa2.getScriptLocation() ) ) { 264 return false; 265 } 266 267 if ( !aa1.getArtifactLocations()[0].equals( aa2.getArtifactLocations()[0] ) ) { 268 return false; 269 } 270 271 return true; 272 } else { 273 return getRaw().equals(vcpi.getRaw()); 274 } 275 default: 276 if (getObject() != null) { 277 return getObject().equals(vcpi.getObject()); 278 } else { 279 return getRaw().equals(vcpi.getRaw()); 280 } 281 } 282 283 } 284 285 static VisualClassPathItem createArtifact(AntArtifact artifact, String raw, String pathInEAR, String eval) { 286 return new VisualClassPathItem(artifact, VisualClassPathItem.Type.ARTIFACT, raw, eval, pathInEAR); 287 } 288 289 static VisualClassPathItem createArtifact(AntArtifact artifact, String raw, String pathInEAR) { 290 String eval = artifact != null ? artifact.getArtifactLocations()[0].toString() : null; 291 return createArtifact(artifact, raw, pathInEAR, eval); 292 } 293 294 static VisualClassPathItem createArtifact(AntArtifact antArtifact) { 295 return createArtifact(antArtifact, null, VisualClassPathItem.PATH_IN_EAR); 296 } 297 298 static VisualClassPathItem createJAR(File jarFile, String raw, String pathInEAR, String eval) { 299 return new VisualClassPathItem(jarFile, VisualClassPathItem.Type.JAR, 300 raw, eval, pathInEAR); 301 } 302 303 static VisualClassPathItem createJAR(File jarFile, String raw, String pathInEAR) { 304 return createJAR(jarFile, raw, pathInEAR, jarFile.getPath()); 305 } 306 307 static VisualClassPathItem createJAR(File jarFile) { 308 return createJAR(jarFile, null, VisualClassPathItem.PATH_IN_EAR); 309 } 310 311 static VisualClassPathItem createLibrary(Library library) { 312 return createLibrary(library, VisualClassPathItem.PATH_IN_EAR); 313 } 314 315 static VisualClassPathItem createLibrary(Library library, String pathInEar) { 316 String libraryName = library.getName(); 317 return new VisualClassPathItem(library, Type.LIBRARY, "${libs." + libraryName + ".classpath}", libraryName, pathInEar); 319 } 320 321 public static VisualClassPathItem createClassPath(String wellKnownPath, String eval) { 322 return new VisualClassPathItem(wellKnownPath, 323 VisualClassPathItem.Type.CLASSPATH, 324 wellKnownPath, 325 eval, 326 PATH_IN_EAR_NONE); 327 } 328 329 private static Icon getFolderIcon() { 330 if ( iconFolder == null ) { 331 FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot(); 332 DataFolder dataFolder = DataFolder.findFolder( root ); 333 iconFolder = new ImageIcon ( dataFolder.getNodeDelegate().getIcon( BeanInfo.ICON_COLOR_16x16 ) ); 334 } 335 336 return iconFolder; 337 } 338 339 public String getToolTipText() { 340 String toolTipText = null; 341 switch (getType()) { 342 case JAR: 343 toolTipText = ((File ) cpElement).getAbsolutePath(); 344 break; 345 case LIBRARY: 346 toolTipText = VisualClasspathSupport.getLibraryString((Library) cpElement); 347 break; 348 case ARTIFACT: 349 final AntArtifact artifact = (AntArtifact) cpElement; 350 final FileObject fos[] = artifact.getArtifactFiles(); 351 if (fos.length > 0) { 352 final FileObject f = fos[0]; 353 toolTipText = f == null ? artifact.getArtifactLocations()[0].getPath() : f.getPath(); 354 } 355 break; 356 case CLASSPATH: 357 toolTipText = (String ) cpElement; 358 break; 359 default: 360 toolTipText = null; 361 } 362 return toolTipText; 363 } 364 365 public String getCompletePathInArchive(final boolean original) { 366 String full = getEvaluated(); 367 int lastSlash = full.lastIndexOf('/'); 368 String trimmed = null; 369 trimmed = (lastSlash != -1) ? full.substring(lastSlash+1) : full; 370 String path = original ? getOrigPathInEAR() : getPathInEAR(); 371 return (null != path && path.length() > 1) 372 ? path + '/' + trimmed : trimmed; 373 } 374 375 public String getCompletePathInArchive() { 376 return getCompletePathInArchive(false); 377 } 378 379 } 380 | Popular Tags |