001: /*
002: This library is free software; you can redistribute it and/or
003: modify it under the terms of the GNU General Public
004: License as published by the Free Software Foundation; either
005: version 2 of the license, or (at your option) any later version.
006: */
007:
008: package org.gjt.jclasslib.browser.config.classpath;
009:
010: import javax.swing.tree.DefaultTreeModel;
011: import java.io.File;
012: import java.io.IOException;
013:
014: /**
015: Base class for classpath entries.
016:
017: @author <a href="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
018: @version $Revision: 1.1 $ $Date: 2003/08/18 08:10:15 $
019: */
020: public abstract class ClasspathEntry implements ClasspathComponent {
021:
022: /** Suffix for class files. */
023: protected static final String CLASSFILE_SUFFIX = ".class";
024:
025: private String fileName;
026: private File file;
027:
028: /**
029: * Get the name of the classpath entry.
030: * @return the name
031: */
032: public String getFileName() {
033: return fileName;
034: }
035:
036: /**
037: * Set the name of the classpath entry.
038: * @param fileName the name.
039: */
040: public void setFileName(String fileName) {
041:
042: this .fileName = fileName;
043: file = new File(fileName);
044: try {
045: file = file.getCanonicalFile();
046: } catch (IOException e) {
047: file = null;
048: }
049: }
050:
051: public boolean equals(Object other) {
052:
053: if (this == other) {
054: return true;
055: }
056: if (other.getClass() != getClass()) {
057: return false;
058: }
059:
060: return fileName.equals(((ClasspathEntry) other).fileName);
061: }
062:
063: public int hashCode() {
064: return fileName.hashCode();
065: }
066:
067: // classpath entries are immutable
068: public void addClasspathChangeListener(
069: ClasspathChangeListener listener) {
070: }
071:
072: public void removeClasspathChangeListener(
073: ClasspathChangeListener listener) {
074: }
075:
076: /**
077: * Get the file for the classpath entry. May be <tt>null</tt> if the entry is invalid.
078: * @return the file.
079: */
080: protected File getFile() {
081: return file;
082: }
083:
084: /**
085: * Convenience method to get a node or add a new class of package node to
086: * a parent node. New nodes will be added in correct sort order, packages first.
087: * @param newNodeName the name of the new node.
088: * @param parentNode the parent node.
089: * @param packageNode whether the new node is a package node or not.
090: * @param model the tree model.
091: * @param reset whether a reset operation is in progress.
092: * @return the fould or created node.
093: */
094: protected ClassTreeNode addOrFindNode(String newNodeName,
095: ClassTreeNode parentNode, boolean packageNode,
096: DefaultTreeModel model, boolean reset) {
097: int childCount = parentNode.getChildCount();
098:
099: ClassTreeNode newNode = new ClassTreeNode(newNodeName,
100: packageNode);
101: for (int i = 0; i < childCount; i++) {
102: ClassTreeNode childNode = (ClassTreeNode) parentNode
103: .getChildAt(i);
104: String childNodeName = childNode.toString();
105: if (childNode.getChildCount() > 0 && !packageNode) {
106: continue;
107: } else if (childNode.getChildCount() == 0 && packageNode) {
108: insertNode(newNode, parentNode, i, model, reset);
109: return newNode;
110: } else if (newNodeName.equals(childNodeName)) {
111: return childNode;
112: } else if (newNodeName.compareTo(childNodeName) < 0) {
113: insertNode(newNode, parentNode, i, model, reset);
114: return newNode;
115: }
116: }
117: insertNode(newNode, parentNode, childCount, model, reset);
118:
119: return newNode;
120: }
121:
122: /**
123: * Strip the class suffix from the supplied file name.
124: * @param name the file name.
125: * @return the stripped name.
126: */
127: protected String stripClassSuffix(String name) {
128: return name.substring(0, name.length()
129: - CLASSFILE_SUFFIX.length());
130: }
131:
132: private void insertNode(ClassTreeNode newNode,
133: ClassTreeNode parentNode, int insertionIndex,
134: DefaultTreeModel model, boolean reset) {
135: parentNode.insert(newNode, insertionIndex);
136: if (!reset) {
137: model.nodesWereInserted(parentNode,
138: new int[] { insertionIndex });
139: }
140: }
141:
142: }
|