~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to scripting/groovy/groovyproject/src/org/netbeans/modules/groovy/groovyproject/ui/customizer/VisualClassPathItem.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.groovy.groovyproject.ui.customizer;
 
43
import java.beans.BeanInfo;
 
44
 
 
45
 
 
46
import java.io.File;
 
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;
 
52
 
 
53
import org.openide.filesystems.Repository;
 
54
import org.openide.loaders.DataFolder;
 
55
 
 
56
 
 
57
import org.openide.util.NbBundle;
 
58
 
 
59
import org.openide.util.Utilities;
 
60
 
 
61
/** Represents classpath items of various types. Can be used in the model
 
62
 * of classpath editing controls.
 
63
 *
 
64
 * @author  Petr Hrebejk, David Konecny
 
65
 */
 
66
public class VisualClassPathItem {
 
67
            
 
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;
 
73
 
 
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
 
78
    
 
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 ) );
 
84
    
 
85
    private int type;
 
86
    private Object cpElement;
 
87
    private String raw;
 
88
    private String eval;
 
89
 
 
90
    VisualClassPathItem( Object cpElement, int type, String raw, String eval ) {
 
91
        this.cpElement = cpElement;
 
92
        this.type = type;
 
93
        this.raw = raw;
 
94
        this.eval = eval;
 
95
        
 
96
        // check cpElement parameter
 
97
        if (cpElement != null) {
 
98
            switch ( getType() ) {
 
99
                case TYPE_JAR:
 
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
 
103
                    }
 
104
                    break;
 
105
                case TYPE_LIBRARY:
 
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
 
109
                    }
 
110
                    break;
 
111
                case TYPE_ARTIFACT:
 
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
 
115
                    }
 
116
                    break;
 
117
                case TYPE_CLASSPATH:
 
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
 
121
                    }
 
122
                    break;
 
123
                default:
 
124
                    throw new IllegalArgumentException("Unknown type " + // NOI18N
 
125
                        "passed. Was: "+getType()); // NOI18N
 
126
            }
 
127
        }
 
128
    }
 
129
 
 
130
    public Object getObject() {
 
131
        return cpElement;
 
132
    }
 
133
    
 
134
    public int getType() {
 
135
        return type;
 
136
    }
 
137
 
 
138
    public String getRaw() {
 
139
        return raw;
 
140
    }
 
141
 
 
142
    public String getEvaluated() {
 
143
        return eval == null ? getRaw() : eval;
 
144
    }
 
145
    
 
146
    public boolean canDelete() {
 
147
        return getType() != TYPE_CLASSPATH;
 
148
    }
 
149
    
 
150
    public Icon getIcon() {
 
151
        if (getObject() == null) {
 
152
            // Otherwise get an NPE for a broken project.
 
153
            return null;
 
154
        }
 
155
        
 
156
        switch( getType() ) {
 
157
            case TYPE_JAR:
 
158
                if ( ((File)getObject()).isDirectory() ) {
 
159
                    return getFolderIcon();
 
160
                }
 
161
                else {
 
162
                    return ICON_JAR;
 
163
                }
 
164
            case TYPE_LIBRARY:
 
165
                return ICON_LIBRARY;
 
166
            case TYPE_ARTIFACT:
 
167
                return ICON_ARTIFACT;
 
168
            case TYPE_CLASSPATH:
 
169
                return ICON_CLASSPATH;
 
170
            default:
 
171
                return null;
 
172
        }
 
173
         
 
174
    }
 
175
 
 
176
    public String toString() {
 
177
        switch ( getType() ) {
 
178
            case TYPE_JAR:
 
179
                if (getObject() != null) {
 
180
                    return getEvaluated();
 
181
                } else {
 
182
                    return NbBundle.getMessage(VisualClassPathItem.class, "LBL_MISSING_FILE", getFileRefName(getEvaluated()));
 
183
                }
 
184
            case TYPE_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 TYPE_ARTIFACT:
 
191
                if (getObject() != null) {
 
192
                    return getEvaluated();
 
193
                } else {
 
194
                    return NbBundle.getMessage(VisualClassPathItem.class, "LBL_MISSING_PROJECT", getProjectName(getEvaluated()));
 
195
                }
 
196
            case TYPE_CLASSPATH:
 
197
                return getEvaluated();
 
198
            default:
 
199
                assert true : "Unknown item type"; // NOI18N
 
200
                return getEvaluated();
 
201
        }
 
202
    }
 
203
    
 
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
 
207
    }
 
208
    
 
209
    private String getLibraryName(String ID) {
 
210
        // something in the form of "${libs.junit.classpath}"
 
211
        return ID.substring(7, ID.indexOf(".classpath")); // NOI18N
 
212
    }
 
213
    
 
214
    private String getFileRefName(String ID) {
 
215
        // something in the form of "${file.reference.smth.jar}"
 
216
        return ID.substring(17, ID.length()-1);
 
217
    }
 
218
            
 
219
    public int hashCode() {
 
220
        
 
221
        int hash = getType();
 
222
        
 
223
        switch ( getType() ) {
 
224
            case TYPE_ARTIFACT:
 
225
                if (getObject() != null) {
 
226
                    AntArtifact aa = (AntArtifact)getObject();
 
227
 
 
228
                    hash += aa.getType().hashCode();                
 
229
                    hash += aa.getScriptLocation().hashCode();
 
230
                    hash += aa.getArtifactLocations()[0].hashCode();
 
231
                } else {
 
232
                    hash += getRaw().hashCode();
 
233
                }
 
234
                break;
 
235
            default:
 
236
                if (getObject() != null) {
 
237
                    hash += getObject().hashCode();
 
238
                } else {
 
239
                    hash += getRaw().hashCode();
 
240
                }
 
241
                break;
 
242
        }
 
243
        
 
244
        return hash;
 
245
    }
 
246
    
 
247
    public boolean equals( Object object ) {
 
248
        
 
249
        if ( !( object instanceof VisualClassPathItem ) ) {
 
250
            return false;
 
251
        }
 
252
        VisualClassPathItem vcpi = (VisualClassPathItem)object;
 
253
        
 
254
        if ( getType() != vcpi.getType() ) {
 
255
            return false;
 
256
        }
 
257
        
 
258
        switch ( getType() ) {
 
259
            case TYPE_ARTIFACT:
 
260
                if (getObject() != null) {
 
261
                    AntArtifact aa1 = (AntArtifact)getObject();
 
262
                    AntArtifact aa2 = (AntArtifact)vcpi.getObject();
 
263
 
 
264
                    if ( aa1.getType() != aa2.getType() ) {
 
265
                        return false;
 
266
                    }
 
267
 
 
268
                    if ( !aa1.getScriptLocation().equals( aa2.getScriptLocation() ) ) {
 
269
                        return false;
 
270
                    }
 
271
 
 
272
                    if ( !aa1.getArtifactLocations()[0].equals( aa2.getArtifactLocations()[0] ) ) {
 
273
                        return false;
 
274
                    }
 
275
 
 
276
                    return true;
 
277
                } else {
 
278
                    return getRaw().equals(vcpi.getRaw());
 
279
                }
 
280
            default:
 
281
                if (getObject() != null) {
 
282
                    return getObject().equals(vcpi.getObject());
 
283
                } else {
 
284
                    return getRaw().equals(vcpi.getRaw());
 
285
                }
 
286
        }
 
287
        
 
288
    }
 
289
 
 
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
 
294
    }
 
295
 
 
296
    public static VisualClassPathItem create (File archiveFile) {
 
297
        return new VisualClassPathItem( archiveFile,
 
298
                    VisualClassPathItem.TYPE_JAR,
 
299
                    null,
 
300
                    archiveFile.getPath());
 
301
    }
 
302
 
 
303
    public static VisualClassPathItem create (AntArtifact artifact) {
 
304
        return new VisualClassPathItem( artifact,
 
305
                    VisualClassPathItem.TYPE_ARTIFACT,
 
306
                    null,
 
307
                    artifact.getArtifactLocations()[0].toString() );
 
308
    }
 
309
    
 
310
    private static Icon getFolderIcon() {
 
311
        
 
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 ) );            
 
316
        }
 
317
        
 
318
        return ICON_FOLDER;
 
319
   
 
320
    }
 
321
 
 
322
}