KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > browser > config > classpath > ClasspathEntry


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.browser.config.classpath;
9
10 import javax.swing.tree.DefaultTreeModel JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 /**
15     Base class for classpath entries.
16
17     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
18     @version $Revision: 1.1 $ $Date: 2003/08/18 08:10:15 $
19 */

20 public abstract class ClasspathEntry implements ClasspathComponent {
21
22     /** Suffix for class files. */
23     protected static final String JavaDoc CLASSFILE_SUFFIX = ".class";
24
25     private String JavaDoc fileName;
26     private File JavaDoc file;
27
28     /**
29      * Get the name of the classpath entry.
30      * @return the name
31      */

32     public String JavaDoc getFileName() {
33         return fileName;
34     }
35
36     /**
37      * Set the name of the classpath entry.
38      * @param fileName the name.
39      */

40     public void setFileName(String JavaDoc fileName) {
41
42         this.fileName = fileName;
43         file = new File JavaDoc(fileName);
44         try {
45             file = file.getCanonicalFile();
46         } catch (IOException JavaDoc e) {
47             file = null;
48         }
49     }
50
51     public boolean equals(Object JavaDoc other) {
52
53         if (this == other) {
54             return true;
55         }
56         if (other.getClass() != getClass()) {
57             return false;
58         }
59
60         return fileName.equals(((ClasspathEntry)other).fileName);
61     }
62
63     public int hashCode() {
64         return fileName.hashCode();
65     }
66
67     // classpath entries are immutable
68
public void addClasspathChangeListener(ClasspathChangeListener listener) {
69     }
70
71     public void removeClasspathChangeListener(ClasspathChangeListener listener) {
72     }
73
74     /**
75      * Get the file for the classpath entry. May be <tt>null</tt> if the entry is invalid.
76      * @return the file.
77      */

78     protected File JavaDoc getFile() {
79         return file;
80     }
81
82     /**
83      * Convenience method to get a node or add a new class of package node to
84      * a parent node. New nodes will be added in correct sort order, packages first.
85      * @param newNodeName the name of the new node.
86      * @param parentNode the parent node.
87      * @param packageNode whether the new node is a package node or not.
88      * @param model the tree model.
89      * @param reset whether a reset operation is in progress.
90      * @return the fould or created node.
91      */

92     protected ClassTreeNode addOrFindNode(String JavaDoc newNodeName,
93                                           ClassTreeNode parentNode,
94                                           boolean packageNode,
95                                           DefaultTreeModel JavaDoc model,
96                                           boolean reset)
97     {
98         int childCount = parentNode.getChildCount();
99
100         ClassTreeNode newNode = new ClassTreeNode(newNodeName, packageNode);
101         for (int i = 0; i < childCount; i++) {
102             ClassTreeNode childNode = (ClassTreeNode)parentNode.getChildAt(i);
103             String JavaDoc childNodeName = childNode.toString();
104             if (childNode.getChildCount() > 0 && !packageNode) {
105                 continue;
106             } else if (childNode.getChildCount() == 0 && packageNode) {
107                 insertNode(newNode, parentNode, i, model, reset);
108                 return newNode;
109             } else if (newNodeName.equals(childNodeName)) {
110                 return childNode;
111             } else if (newNodeName.compareTo(childNodeName) < 0) {
112                 insertNode(newNode, parentNode, i, model, reset);
113                 return newNode;
114             }
115         }
116         insertNode(newNode, parentNode, childCount, model, reset);
117
118         return newNode;
119     }
120
121     /**
122      * Strip the class suffix from the supplied file name.
123      * @param name the file name.
124      * @return the stripped name.
125      */

126     protected String JavaDoc stripClassSuffix(String JavaDoc name) {
127         return name.substring(0, name.length() - CLASSFILE_SUFFIX.length());
128     }
129
130     private void insertNode(ClassTreeNode newNode,
131                               ClassTreeNode parentNode,
132                               int insertionIndex,
133                               DefaultTreeModel JavaDoc model,
134                               boolean reset)
135     {
136         parentNode.insert(newNode, insertionIndex);
137         if (!reset) {
138             model.nodesWereInserted(parentNode, new int[] {insertionIndex});
139         }
140     }
141
142 }
143
Popular Tags