001: /*
002: * @(#)FileTreeNode.java 10/9/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.plaf.basic;
007:
008: import com.jidesoft.swing.FolderChooser;
009:
010: import javax.swing.*;
011: import java.io.File;
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.HashMap;
015: import java.util.List;
016:
017: class BasicFileSystemTreeNode extends LazyMutableTreeNode implements
018: Comparable {
019: private FolderChooser _folderChooser;
020: private File _file;
021:
022: static HashMap _icons = new HashMap();
023:
024: static HashMap _nodes = new HashMap();
025:
026: protected BasicFileSystemTreeNode(File file) {
027: this (file, null);
028: }
029:
030: protected BasicFileSystemTreeNode(File file,
031: FolderChooser folderChooser) {
032: _file = file;
033: _folderChooser = folderChooser;
034: }
035:
036: @Override
037: public boolean isLeaf() {
038: if (!isLoaded()) {
039: return false;
040: } else {
041: return super .isLeaf();
042: }
043: }
044:
045: public boolean hasChildren() {
046: if (!_loaded) {
047: if (BasicFolderChooserUI.isFileSystem(_file)
048: && _file.isDirectory()) {
049: File[] files = _folderChooser.getFileSystemView()
050: .getFiles(_file,
051: _folderChooser.isFileHidingEnabled());
052: for (int i = 0; i < files.length; i++) {
053: File childFile = files[i];
054: if (childFile.isDirectory()) {
055: return true;
056: }
057: }
058: _loaded = true; // no children so it is loaded
059: }
060: return false;
061: } else {
062: return getChildCount() != 0;
063: }
064: }
065:
066: @Override
067: protected void initChildren() {
068: if (_folderChooser == null) {
069: return;
070: }
071: if (_file.isDirectory()) {
072: File[] files = new File[0];
073: try {
074: files = _folderChooser.getFileSystemView().getFiles(
075: _file, _folderChooser.isFileHidingEnabled());
076: } catch (Error e) {
077: // catch error like java.lang.InternalError: Unable to bind C:\blah blah\::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\::{3D6BE802-FC0D-4595-A304-E611F97089DC} to parent
078: } catch (Exception e) {
079: }
080: List children = new ArrayList();
081: for (int i = 0; i < files.length; i++) {
082: File childFile = files[i];
083: if (childFile.isDirectory()) {
084: BasicFileSystemTreeNode fileTreeNode = BasicFileSystemTreeNode
085: .createFileSystemTreeNode(childFile,
086: _folderChooser);
087: children.add(fileTreeNode);
088: }
089: }
090: BasicFileSystemTreeNode[] result = (BasicFileSystemTreeNode[]) children
091: .toArray(new BasicFileSystemTreeNode[children
092: .size()]);
093: Arrays.sort(result);
094: for (int i = 0; i < result.length; i++) {
095: add(result[i]);
096: }
097: }
098: }
099:
100: public File getFile() {
101: return _file;
102: }
103:
104: public String getName() {
105: return getName(getFile());
106: }
107:
108: public Icon getIcon() {
109: Icon icon = (Icon) _icons.get(this );
110: if (icon == null) {
111: icon = getIcon(getFile());
112: _icons.put(this , icon);
113: return icon;
114: } else {
115: return icon;
116: }
117: }
118:
119: public String getTypeDescription() {
120: String desc = getTypeDescription(getFile());
121: return desc == null ? "" : desc;
122: }
123:
124: public Icon getIcon(File file) {
125: return _folderChooser.getFileSystemView().getSystemIcon(file);
126: }
127:
128: public String getTypeDescription(File file) {
129: return _folderChooser.getFileSystemView()
130: .getSystemTypeDescription(file);
131: }
132:
133: public String getName(File file) {
134: return _folderChooser.getFileSystemView().getSystemDisplayName(
135: file);
136: }
137:
138: @Override
139: public boolean equals(Object o) {
140: if (this == o)
141: return true;
142: if (o == null || getClass() != o.getClass())
143: return false;
144:
145: final BasicFileSystemTreeNode that = (BasicFileSystemTreeNode) o;
146:
147: return !(_file != null ? !_file.equals(that._file)
148: : that._file != null);
149:
150: }
151:
152: public int compareTo(Object o) {
153: if (!(o instanceof BasicFileSystemTreeNode)) {
154: return 0;
155: }
156: return getFile().compareTo(
157: ((BasicFileSystemTreeNode) o).getFile());
158: }
159:
160: @Override
161: public int hashCode() {
162: return (_file != null ? _file.hashCode() : 0);
163: }
164:
165: public boolean canEnqueue() {
166: return !isLoaded()
167: && !_folderChooser.getFileSystemView().isFloppyDrive(
168: getFile())
169: && !_folderChooser.getFileSystemView()
170: .isFileSystemRoot(getFile());
171: }
172:
173: @Override
174: public String toString() {
175: return _file != null ? _file.toString() : "null";
176: }
177:
178: /**
179: * Caches the tree nodes ever created for the performance.
180: *
181: * @param file
182: * @param folderChooser
183: * @return tree node. If it is created before, returns the previous created instance.
184: */
185: public static BasicFileSystemTreeNode createFileSystemTreeNode(
186: File file, FolderChooser folderChooser) {
187: BasicFileSystemTreeNode node = (BasicFileSystemTreeNode) _nodes
188: .get(file);
189: if (node == null) {
190: node = new BasicFileSystemTreeNode(file, folderChooser);
191: _nodes.put(file, node);
192: }
193: return node;
194: }
195:
196: /**
197: * Clears the cache of all the tree nodes.
198: */
199: public static void clearCache() {
200: _nodes.clear();
201: }
202: }
|