KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > earproject > ui > customizer > VisualClassPathItem


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.earproject.ui.customizer;
21
22 import java.beans.BeanInfo JavaDoc;
23 import java.io.File JavaDoc;
24 import javax.swing.Icon JavaDoc;
25 import javax.swing.ImageIcon JavaDoc;
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 /**
35  * Represents classpath items of various types. Can be used in the model of
36  * classpath editing controls.
37  *
38  * @author phrebejk
39  */

40 public final class VisualClassPathItem {
41     
42     /** Types of the classpath elements. */
43     public static enum Type {
44         JAR, LIBRARY, ARTIFACT, CLASSPATH
45     };
46     
47     public static final String JavaDoc PATH_IN_WAR_LIB = "WEB-INF/lib"; //NOI18N
48
public static final String JavaDoc PATH_IN_EAR = "/"; //NOI18N
49
public static final String JavaDoc PATH_IN_EAR_NONE = null;
50     
51     private static final String JavaDoc RESOURCE_ICON_JAR =
52             "org/netbeans/modules/j2ee/earproject/ui/resources/jar.gif"; //NOI18N
53
private static final String JavaDoc RESOURCE_ICON_LIBRARY =
54             "org/netbeans/modules/j2ee/earproject/ui/resources/libraries.gif"; //NOI18N
55
private static final String JavaDoc RESOURCE_ICON_ARTIFACT =
56             "org/netbeans/modules/j2ee/earproject/ui/resources/projectDependencies.gif"; //NOI18N
57
private static final String JavaDoc RESOURCE_ICON_CLASSPATH =
58             "org/netbeans/modules/j2ee/earproject/ui/resources/referencedClasspath.gif"; //NOI18N
59

60     private static final Icon JavaDoc ICON_JAR = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_JAR ) );
61     private static Icon JavaDoc iconFolder;
62     private static final Icon JavaDoc ICON_LIBRARY = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_LIBRARY ) );
63     private static final Icon JavaDoc ICON_ARTIFACT = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_ARTIFACT ) );
64     private static final Icon JavaDoc ICON_CLASSPATH = new ImageIcon JavaDoc( Utilities.loadImage( RESOURCE_ICON_CLASSPATH ) );
65     
66     private final Type type;
67     private final Object JavaDoc cpElement;
68     private String JavaDoc raw;
69     private final String JavaDoc eval;
70     private String JavaDoc pathInEAR;
71     private String JavaDoc origPathInEAR;
72     
73     VisualClassPathItem(Object JavaDoc cpElement, Type type, String JavaDoc raw, String JavaDoc eval, String JavaDoc 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         // check cpElement parameter
82
if (cpElement != null) {
83             switch ( getType() ) {
84                 case JAR:
85                     if (!(cpElement instanceof File JavaDoc)) {
86                         throw new IllegalArgumentException JavaDoc("File instance must be " + // NOI18N
87
"passed as object for Type.JAR. Was: "+cpElement.getClass()); // NOI18N
88
}
89                     break;
90                 case LIBRARY:
91                     if (!(cpElement instanceof Library)) {
92                         throw new IllegalArgumentException JavaDoc("Library instance must be " + // NOI18N
93
"passed as object for Type.LIBRARY. Was: "+cpElement.getClass()); // NOI18N
94
}
95                     break;
96                 case ARTIFACT:
97                     if (!(cpElement instanceof AntArtifact)) {
98                         throw new IllegalArgumentException JavaDoc("AntArtifact instance must be " + // NOI18N
99
"passed as object for Type.ARTIFACT. Was: "+cpElement.getClass()); // NOI18N
100
}
101                     break;
102                 case CLASSPATH:
103                     if (!(cpElement instanceof String JavaDoc)) {
104                         throw new IllegalArgumentException JavaDoc("String instance must be " + // NOI18N
105
"passed as object for Type.CLASSPATH. Was: "+cpElement.getClass()); // NOI18N
106
}
107                     break;
108                 default:
109                     throw new IllegalArgumentException JavaDoc("Unknown type " + // NOI18N
110
"passed. Was: "+getType()); // NOI18N
111
}
112         }
113     }
114     
115     public String JavaDoc getPathInEAR() {
116         return pathInEAR;
117     }
118     
119     public void setPathInEAR(String JavaDoc path) {
120         pathInEAR = path;
121     }
122     
123     public String JavaDoc getOrigPathInEAR() {
124         return origPathInEAR;
125     }
126     
127     public Object JavaDoc getObject() {
128         return cpElement;
129     }
130     
131     public Type getType() {
132         return type;
133     }
134     
135     public void setRaw(String JavaDoc raw) {
136         this.raw = raw;
137     }
138     
139     public String JavaDoc getRaw() {
140         return raw;
141     }
142     
143     public String JavaDoc getEvaluated() {
144         return eval == null ? getRaw() : eval;
145     }
146     
147     public boolean canDelete() {
148         return getType() != Type.CLASSPATH;
149     }
150     
151     public Icon JavaDoc getIcon() {
152         if (getObject() == null) {
153             // Otherwise get an NPE for a broken project.
154
return null;
155         }
156         
157         switch(getType()) {
158             case JAR:
159                 if (((File JavaDoc) 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 JavaDoc 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"; // NOI18N
200
return getEvaluated();
201         }
202     }
203     
204     private String JavaDoc getProjectName(String JavaDoc id) {
205         // something in the form of "${reference.project-name.id}"
206
return id.matches("\\$\\{reference\\..*\\.id\\}") // NOI18N
207
? id.substring(12, id.indexOf('.', 12)) : id;
208     }
209     
210     private String JavaDoc getLibraryName(String JavaDoc id) {
211         // something in the form of "${libs.junit.classpath}"
212
return id.substring(7, id.indexOf(".classpath")); // NOI18N
213
}
214     
215     private String JavaDoc getFileRefName(String JavaDoc id) {
216         // something in the form of "${file.reference.smth.jar}"
217
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 JavaDoc 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 JavaDoc raw, String JavaDoc pathInEAR, String JavaDoc eval) {
286         return new VisualClassPathItem(artifact, VisualClassPathItem.Type.ARTIFACT, raw, eval, pathInEAR);
287     }
288     
289     static VisualClassPathItem createArtifact(AntArtifact artifact, String JavaDoc raw, String JavaDoc pathInEAR) {
290         String JavaDoc 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 JavaDoc jarFile, String JavaDoc raw, String JavaDoc pathInEAR, String JavaDoc eval) {
299         return new VisualClassPathItem(jarFile, VisualClassPathItem.Type.JAR,
300                 raw, eval, pathInEAR);
301     }
302     
303     static VisualClassPathItem createJAR(File JavaDoc jarFile, String JavaDoc raw, String JavaDoc pathInEAR) {
304         return createJAR(jarFile, raw, pathInEAR, jarFile.getPath());
305     }
306     
307     static VisualClassPathItem createJAR(File JavaDoc 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 JavaDoc pathInEar) {
316         String JavaDoc libraryName = library.getName();
317         return new VisualClassPathItem(library, Type.LIBRARY, "${libs." + libraryName + ".classpath}", // NOI18N
318
libraryName, pathInEar);
319     }
320     
321     public static VisualClassPathItem createClassPath(String JavaDoc wellKnownPath, String JavaDoc 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 JavaDoc getFolderIcon() {
330         if ( iconFolder == null ) {
331             FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
332             DataFolder dataFolder = DataFolder.findFolder( root );
333             iconFolder = new ImageIcon JavaDoc( dataFolder.getNodeDelegate().getIcon( BeanInfo.ICON_COLOR_16x16 ) );
334         }
335         
336         return iconFolder;
337     }
338     
339     public String JavaDoc getToolTipText() {
340         String JavaDoc toolTipText = null;
341         switch (getType()) {
342             case JAR:
343                 toolTipText = ((File JavaDoc) 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 JavaDoc) cpElement;
358                 break;
359             default:
360                 toolTipText = null;
361         }
362         return toolTipText;
363     }
364     
365     public String JavaDoc getCompletePathInArchive(final boolean original) {
366         String JavaDoc full = getEvaluated();
367         int lastSlash = full.lastIndexOf('/');
368         String JavaDoc trimmed = null;
369         trimmed = (lastSlash != -1) ? full.substring(lastSlash+1) : full;
370         String JavaDoc path = original ? getOrigPathInEAR() : getPathInEAR();
371         return (null != path && path.length() > 1)
372                 ? path + '/' + trimmed : trimmed;
373     }
374     
375     public String JavaDoc getCompletePathInArchive() {
376         return getCompletePathInArchive(false);
377     }
378     
379 }
380
Popular Tags