2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
42
package org.netbeans.modules.groovy.groovyproject.ui.customizer;
43
import java.beans.BeanInfo;
47
import javax.swing.Icon;
48
import javax.swing.ImageIcon;
49
import org.netbeans.api.project.ant.AntArtifact;
50
import org.netbeans.api.project.libraries.Library;
51
import org.openide.filesystems.FileObject;
53
import org.openide.filesystems.Repository;
54
import org.openide.loaders.DataFolder;
57
import org.openide.util.NbBundle;
59
import org.openide.util.Utilities;
61
/** Represents classpath items of various types. Can be used in the model
62
* of classpath editing controls.
64
* @author Petr Hrebejk, David Konecny
66
public class VisualClassPathItem {
68
// Types of the classpath elements
69
public static final int TYPE_JAR = 0;
70
public static final int TYPE_LIBRARY = 1;
71
public static final int TYPE_ARTIFACT = 2;
72
public static final int TYPE_CLASSPATH = 3;
74
private static String RESOURCE_ICON_JAR = "org/netbeans/modules/groovy/groovyproject/resources/jar.gif"; //NOI18N
75
private static String RESOURCE_ICON_LIBRARY = "org/netbeans/modules/groovy/groovyproject/resources/libraries.gif"; //NOI18N
76
private static String RESOURCE_ICON_ARTIFACT = "org/netbeans/modules/groovy/groovyproject/resources/projectDependencies.gif"; //NOI18N
77
private static String RESOURCE_ICON_CLASSPATH = "org/netbeans/modules/groovy/groovyproject/resources/referencedClasspath.gif"; //NOI18N
79
private static Icon ICON_JAR = new ImageIcon( Utilities.loadImage( RESOURCE_ICON_JAR ) );
80
private static Icon ICON_FOLDER = null;
81
private static Icon ICON_LIBRARY = new ImageIcon( Utilities.loadImage( RESOURCE_ICON_LIBRARY ) );
82
private static Icon ICON_ARTIFACT = new ImageIcon( Utilities.loadImage( RESOURCE_ICON_ARTIFACT ) );
83
private static Icon ICON_CLASSPATH = new ImageIcon( Utilities.loadImage( RESOURCE_ICON_CLASSPATH ) );
86
private Object cpElement;
90
VisualClassPathItem( Object cpElement, int type, String raw, String eval ) {
91
this.cpElement = cpElement;
96
// check cpElement parameter
97
if (cpElement != null) {
98
switch ( getType() ) {
100
if (!(cpElement instanceof File)) {
101
throw new IllegalArgumentException("File instance must be " + // NOI18N
102
"passed as object for TYPE_JAR. Was: "+cpElement.getClass()); // NOI18N
106
if (!(cpElement instanceof Library)) {
107
throw new IllegalArgumentException("Library instance must be " + // NOI18N
108
"passed as object for TYPE_LIBRARY. Was: "+cpElement.getClass()); // NOI18N
112
if (!(cpElement instanceof AntArtifact)) {
113
throw new IllegalArgumentException("AntArtifact instance must be " + // NOI18N
114
"passed as object for TYPE_ARTIFACT. Was: "+cpElement.getClass()); // NOI18N
118
if (!(cpElement instanceof String)) {
119
throw new IllegalArgumentException("String instance must be " + // NOI18N
120
"passed as object for TYPE_CLASSPATH. Was: "+cpElement.getClass()); // NOI18N
124
throw new IllegalArgumentException("Unknown type " + // NOI18N
125
"passed. Was: "+getType()); // NOI18N
130
public Object getObject() {
134
public int getType() {
138
public String getRaw() {
142
public String getEvaluated() {
143
return eval == null ? getRaw() : eval;
146
public boolean canDelete() {
147
return getType() != TYPE_CLASSPATH;
150
public Icon getIcon() {
151
if (getObject() == null) {
152
// Otherwise get an NPE for a broken project.
156
switch( getType() ) {
158
if ( ((File)getObject()).isDirectory() ) {
159
return getFolderIcon();
167
return ICON_ARTIFACT;
169
return ICON_CLASSPATH;
176
public String toString() {
177
switch ( getType() ) {
179
if (getObject() != null) {
180
return getEvaluated();
182
return NbBundle.getMessage(VisualClassPathItem.class, "LBL_MISSING_FILE", getFileRefName(getEvaluated()));
185
if (getObject() != null) {
186
return ((Library)this.getObject()).getDisplayName();
188
return NbBundle.getMessage(VisualClassPathItem.class, "LBL_MISSING_LIBRARY", getLibraryName(getRaw()));
191
if (getObject() != null) {
192
return getEvaluated();
194
return NbBundle.getMessage(VisualClassPathItem.class, "LBL_MISSING_PROJECT", getProjectName(getEvaluated()));
197
return getEvaluated();
199
assert true : "Unknown item type"; // NOI18N
200
return getEvaluated();
204
private String getProjectName(String ID) {
205
// something in the form of "${reference.project-name.id}"
206
return ID.substring(12, ID.indexOf(".", 12)); // NOI18N
209
private String getLibraryName(String ID) {
210
// something in the form of "${libs.junit.classpath}"
211
return ID.substring(7, ID.indexOf(".classpath")); // NOI18N
214
private String getFileRefName(String ID) {
215
// something in the form of "${file.reference.smth.jar}"
216
return ID.substring(17, ID.length()-1);
219
public int hashCode() {
221
int hash = getType();
223
switch ( getType() ) {
225
if (getObject() != null) {
226
AntArtifact aa = (AntArtifact)getObject();
228
hash += aa.getType().hashCode();
229
hash += aa.getScriptLocation().hashCode();
230
hash += aa.getArtifactLocations()[0].hashCode();
232
hash += getRaw().hashCode();
236
if (getObject() != null) {
237
hash += getObject().hashCode();
239
hash += getRaw().hashCode();
247
public boolean equals( Object object ) {
249
if ( !( object instanceof VisualClassPathItem ) ) {
252
VisualClassPathItem vcpi = (VisualClassPathItem)object;
254
if ( getType() != vcpi.getType() ) {
258
switch ( getType() ) {
260
if (getObject() != null) {
261
AntArtifact aa1 = (AntArtifact)getObject();
262
AntArtifact aa2 = (AntArtifact)vcpi.getObject();
264
if ( aa1.getType() != aa2.getType() ) {
268
if ( !aa1.getScriptLocation().equals( aa2.getScriptLocation() ) ) {
272
if ( !aa1.getArtifactLocations()[0].equals( aa2.getArtifactLocations()[0] ) ) {
278
return getRaw().equals(vcpi.getRaw());
281
if (getObject() != null) {
282
return getObject().equals(vcpi.getObject());
284
return getRaw().equals(vcpi.getRaw());
290
public static VisualClassPathItem create (Library library) {
291
String libraryName = library.getName();
292
return new VisualClassPathItem(library,TYPE_LIBRARY,
293
"${libs."+libraryName+".classpath}",libraryName); // NOI18N
296
public static VisualClassPathItem create (File archiveFile) {
297
return new VisualClassPathItem( archiveFile,
298
VisualClassPathItem.TYPE_JAR,
300
archiveFile.getPath());
303
public static VisualClassPathItem create (AntArtifact artifact) {
304
return new VisualClassPathItem( artifact,
305
VisualClassPathItem.TYPE_ARTIFACT,
307
artifact.getArtifactLocations()[0].toString() );
310
private static Icon getFolderIcon() {
312
if ( ICON_FOLDER == null ) {
313
FileObject root = Repository.getDefault().getDefaultFileSystem().getRoot();
314
DataFolder dataFolder = DataFolder.findFolder( root );
315
ICON_FOLDER = new ImageIcon( dataFolder.getNodeDelegate().getIcon( BeanInfo.ICON_COLOR_16x16 ) );