001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: FilesystemTreeView.java,v $ 023 Revision 1.7 2004/05/31 07:51:32 markl 024 rewritten to use new kiwi.ui.model package. 025 026 Revision 1.6 2003/01/19 09:46:48 markl 027 replaced JScrollPane instances with KScrollPane. 028 029 Revision 1.5 2001/03/12 09:27:55 markl 030 Source code and Javadoc cleanup. 031 032 Revision 1.4 1999/06/08 08:56:24 markl 033 Added setFont() method. 034 035 Revision 1.3 1999/04/18 12:57:47 markl 036 Bug fix; moved adapter.dispose() out of constructor into setRoot(). 037 038 Revision 1.2 1999/01/10 02:05:37 markl 039 added GPL header & RCS tag 040 ---------------------------------------------------------------------------- 041*/ 042 043package kiwi.ui; 044 045import java.awt.*; 046import java.io.File; 047import javax.swing.*; 048import javax.swing.tree.*; 049import javax.swing.border.*; 050 051import kiwi.ui.model.*; 052 053/** This class represents a filesystem tree component. It displays hierarchical 054 * data (ultimately obtained from a <code>FilesystemDataSource</code>) in a 055 * <code>JTree</code> component. The filesystem (or portion thereof) being 056 * displayed by the component can be changed at any time. 057 * 058 * <p><center> 059 * <img src="snapshot/FilesystemTreeView.gif"><br> 060 * <i>An example FilesystemTreeView.</i> 061 * </center> 062 * 063 * @see kiwi.ui.model.FilesystemDataSource 064 * @see javax.swing.JTree 065 * 066 * @author Mark Lindner 067 */ 068 069public class FilesystemTreeView extends KPanel 070 { 071 private JTree tree = null; 072 private KTreeModelTreeAdapter adapter = null; 073 private DefaultKTreeModel model = null; 074 private boolean ignoreFiles = false; 075 076 /** Construct a new <code>FilesystemTreeView</code>. The tree initially has 077 * no data model; use <code>setRoot()</code> to initialize the component. 078 * 079 * @see #setRoot 080 */ 081 082 public FilesystemTreeView() 083 { 084 this(false); 085 } 086 087 /** Construct a new <code>FilesystemTreeView</code>. The tree initially has 088 * no data model; use <code>setRoot()</code> to initialize the component. 089 * 090 * @param ignoreFiles A flag specifying whether this list should ignore 091 * files and only display directories. 092 * @see #setRoot 093 */ 094 095 public FilesystemTreeView(boolean ignoreFiles) 096 { 097 this.ignoreFiles = ignoreFiles; 098 099 setLayout(new BorderLayout(0, 0)); 100 101 tree = new JTree(); 102 tree.setRowHeight(18); 103 tree.setBackground(Color.white); 104 adapter = new KTreeModelTreeAdapter(tree); 105 tree.setModel(adapter); 106 107 KScrollPane scrollPane = new KScrollPane(tree); 108 add("Center", scrollPane); 109 110 setMultipleSelectionsAllowed(false); 111 } 112 113 /** Specify whether multiple selections are allowed in this component. 114 * 115 * @param flag If <code>true</code>, multiple discontiguous selections will 116 * be allowed; otherwise only single selection is allowed (the default). 117 */ 118 119 public void setMultipleSelectionsAllowed(boolean flag) 120 { 121 tree.getSelectionModel() 122 .setSelectionMode(flag ? TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION 123 : TreeSelectionModel.SINGLE_TREE_SELECTION); 124 } 125 126 /** Set the root of the filesystem to be displayed by this component. This 127 * causes the component to be reset and repainted. 128 * 129 * @param root The root directory of the filesystem to display; may be 130 * <code>null</code> to display all available filesystems. 131 */ 132 133 public void setRoot(File root) 134 { 135 FilesystemDataSource fds = new FilesystemDataSource(root, ignoreFiles); 136 model = new ExternalKTreeModel(fds); 137 adapter.setTreeModel(model); 138 tree.setCellRenderer(new KTreeModelTreeCellRenderer(model)); 139 tree.setRootVisible(false); 140 repaint(); 141 } 142 143 /** Get the currently selected item in the tree. If there is more than one 144 * item selected in the tree, gets the first selected item. 145 * 146 * @return The <code>File</code> object for the currently selected item in 147 * the tree, or <code>null</code> if there is no selection. 148 * @see #getSelectedFiles 149 */ 150 151 public File getSelectedFile() 152 { 153 TreePath path = tree.getSelectionPath(); 154 return((path == null) ? null : fileForPath(path)); 155 } 156 157 /** Get the currently selected items in the tree. 158 * 159 * @return An array of <code>File</code> objects corresponding to the 160 * currently selected items in the tree. If there is no selection, an empty 161 * array is returned. 162 * @see #getSelectedFile 163 */ 164 165 public File[] getSelectedFiles() 166 { 167 TreePath paths[] = tree.getSelectionPaths(); 168 File f[] = new File[paths.length]; 169 for(int i = 0; i < paths.length; i++) 170 f[i] = fileForPath(paths[i]); 171 172 return(f); 173 } 174 175 /** Get the <code>File</code> object for a given path in the tree. 176 * 177 * @param path The <code>TreePath</code> of the item. 178 * @return The <code>File</code> object at the end of the given path. 179 */ 180 181 protected final File fileForPath(TreePath path) 182 { 183 Object node = (Object)path.getLastPathComponent(); 184 return((node == null) ? null : (File)node); 185 } 186 187 /** Get the <code>JTree</code> that is embedded in this component. */ 188 189 public final JTree getJTree() 190 { 191 return(tree); 192 } 193 194 /** Set the component's opacity. */ 195 196 public void setOpaque(boolean flag) 197 { 198 super.setOpaque(flag); 199 if(tree != null) 200 tree.setOpaque(flag); 201 } 202 203 /** Set the font for this component. 204 * 205 * @param font The new font. 206 */ 207 208 public void setFont(Font font) 209 { 210 super.setFont(font); 211 if(tree != null) 212 tree.setFont(font); 213 } 214 215 } 216 217/* end of source file */