001: /*
002: * @(#)FileSystemTree.java 9/12/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: import com.jidesoft.swing.TreeSearchable;
010:
011: import javax.swing.*;
012: import javax.swing.event.TreeExpansionEvent;
013: import javax.swing.event.TreeExpansionListener;
014: import javax.swing.event.TreeWillExpandListener;
015: import javax.swing.tree.ExpandVetoException;
016: import javax.swing.tree.TreePath;
017: import java.awt.*;
018: import java.awt.event.MouseEvent;
019:
020: class BasicFileSystemTree extends JTree {
021: public BasicFileSystemTree(FolderChooser folderChooser) {
022: super (new BasicFileSystemTreeModel(folderChooser));
023: initComponents();
024: }
025:
026: protected void initComponents() {
027: setCellRenderer(new BasicFileSystemTreeCellRenderer());
028: setShowsRootHandles(false);
029: setRootVisible(false);
030: setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
031: setRowHeight(17);
032: expandRow(0);
033: FolderTreeListener treeListener = new FolderTreeListener();
034: addTreeWillExpandListener(treeListener);
035: addTreeExpansionListener(treeListener);
036: new TreeSearchable(this ) {
037: @Override
038: protected String convertElementToString(Object object) {
039: if (object instanceof TreePath) {
040: Object treeNode = ((TreePath) object)
041: .getLastPathComponent();
042: if (treeNode instanceof BasicFileSystemTreeNode) {
043: return ((BasicFileSystemTreeNode) treeNode)
044: .getName();
045: }
046: }
047: return super .convertElementToString(object);
048: }
049: };
050: }
051:
052: private class FolderTreeListener implements TreeWillExpandListener,
053: TreeExpansionListener {
054: private Cursor oldCursor;
055:
056: // ------------------------------------------------------------------------------------------
057: // TreeWillExpandListener methods
058:
059: public void treeWillExpand(TreeExpansionEvent event)
060: throws ExpandVetoException {
061: // change to busy cursor
062: Window window = SwingUtilities
063: .getWindowAncestor(BasicFileSystemTree.this );
064: if (window != null) {
065: oldCursor = window.getCursor();
066: window.setCursor(Cursor
067: .getPredefinedCursor(Cursor.WAIT_CURSOR));
068: }
069: }
070:
071: public void treeWillCollapse(TreeExpansionEvent event)
072: throws ExpandVetoException {
073: }
074:
075: // ------------------------------------------------------------------------------------------
076: // TreeExpansionListener methods
077:
078: public void treeExpanded(TreeExpansionEvent event) {
079: // change cursor back
080: Window window = SwingUtilities
081: .getWindowAncestor(BasicFileSystemTree.this );
082: if (window != null) {
083: window.setCursor(oldCursor != null ? oldCursor : Cursor
084: .getDefaultCursor());
085: }
086: oldCursor = null;
087: }
088:
089: public void treeCollapsed(TreeExpansionEvent event) {
090: }
091:
092: }
093:
094: @Override
095: public String getToolTipText(MouseEvent event) {
096: TreePath path = getPathForLocation(event.getX(), event.getY());
097: if (path != null
098: && path.getLastPathComponent() instanceof BasicFileSystemTreeNode) {
099: BasicFileSystemTreeNode node = (BasicFileSystemTreeNode) path
100: .getLastPathComponent();
101: String typeDescription = node.getTypeDescription();
102: if (typeDescription == null
103: || typeDescription.length() == 0) {
104: return node.toString();
105: } else {
106: return node.toString() + " - " + typeDescription;
107: }
108: } else {
109: return null;
110: }
111: }
112: }
|