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: FilesystemTableView.java,v $
023   Revision 1.6  2004/05/31 07:51:32  markl
024   rewritten to use new kiwi.ui.model package.
025
026   Revision 1.5  2003/01/19 09:46:48  markl
027   replaced JScrollPane instances with KScrollPane.
028
029   Revision 1.4  2001/03/12 09:27:55  markl
030   Source code and Javadoc cleanup.
031
032   Revision 1.3  1999/06/08 08:56:10  markl
033   Added setFont() method.
034
035   Revision 1.2  1999/01/10 02:05:37  markl
036   added GPL header & RCS tag
037   ----------------------------------------------------------------------------
038*/
039
040package kiwi.ui;
041
042import java.awt.*;
043import java.awt.event.*;
044import java.io.File;
045import javax.swing.*;
046import javax.swing.event.*;
047import javax.swing.border.*;
048import javax.swing.table.*;
049
050import kiwi.ui.model.*;
051
052/** This class represents a filesystem table component. It displays
053  * hierarchical data (ultimately obtained from a
054  * <code>FilesystemDataSource</code>) in a <code>KTreeTable</code> component.
055  * The filesystem (or portion thereof) being displayed by the component can
056  * be changed at any time.
057  *
058  * <p><center>
059  * <img src="snapshot/FilesystemTableView.gif"><br>
060  * <i>An example FilesystemTableView.</i>
061  * </center>
062  *
063  * @see kiwi.ui.model.FilesystemDataSource
064  * @see kiwi.ui.KTreeTable
065  *
066  * @author Mark Lindner
067  */
068
069public class FilesystemTableView extends KPanel
070  {
071  private KTreeTable table;
072  private KTreeModel model = null;
073  private boolean ignoreFiles = false;
074
075  /** Construct a new <code>FilesystemTableView</code>. The table initially has
076    * no data model; use <code>setRoot()</code> to initialize the component.
077    *
078    * @see #setRoot
079    */
080
081  public FilesystemTableView()
082    {
083    this(false);
084    }
085
086  /** Construct a new <code>FilesystemTableView</code>. The view initially has
087    * no data model; use <code>setRoot()</code> to initialize one.
088    *
089    * @param ignoreFiles A flag specifying whether this table should ignore
090    * files and only display directories.
091    * @see #setRoot
092    */
093
094  public FilesystemTableView(boolean ignoreFiles)
095    {
096    this.ignoreFiles = ignoreFiles;
097
098    table = new KTreeTable();
099    table.setRowHeight(18);
100    table.setBackground(Color.white);
101
102    KScrollPane scrollPane = new KScrollPane(table);
103    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants
104                                          .VERTICAL_SCROLLBAR_ALWAYS);
105    add("Center", scrollPane);
106
107    setMultipleSelectionsAllowed(false);
108    }
109
110  /** Specify whether multiple selections are allowed in this component.
111    *
112    * @param flag If <code>true</code>, multiple discontiguous selections will
113    * be allowed; otherwise only single selection is allowed (the default).
114    */
115
116  public void setMultipleSelectionsAllowed(boolean flag)
117    {
118    table.getSelectionModel()
119      .setSelectionMode(flag ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
120                        : ListSelectionModel.SINGLE_SELECTION);
121    }
122  
123  /** Set the root of the filesystem to be displayed by this component. This
124    * causes the component to be reset and repainted.
125    *
126    * @param root The root directory of the filesystem to display. May be
127    * <code>null</code>, indicating that all available filesystems should be
128    * displayed.
129    */
130
131  public void setRoot(File root)
132    {
133    FilesystemDataSource fds = new FilesystemDataSource(root, ignoreFiles);
134    model = new ExternalKTreeModel(fds);
135
136    table.setTreeModel(model);
137
138    DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
139    rightRenderer.setHorizontalAlignment(SwingConstants.RIGHT);
140
141    table.configureColumn(0, 100, 100, 1000);
142    table.configureColumn(1, 70, 70, 70, rightRenderer, null);
143    table.configureColumn(2, 100, 100, 100, rightRenderer, null);
144    table.configureColumn(3, 70, 70, 70, rightRenderer, null);
145    
146    repaint();
147    }
148  
149  /** Get the currently selected item in the table. If there is more than one
150    * item selected in the table, gets the last or most recently selected item.
151    *
152    * @return The <code>File</code> object for the currently selected item in
153    * the table, or <code>null</code> if there is no selection.
154    * @see #getSelectedFiles
155    */
156
157  public File getSelectedFile()
158    {
159    return((File)table.getSelectedItem());
160    }
161
162  /** Get the currently selected items in the table.
163    *
164    * @return An array of <code>File</code> objects corresponding to the
165    * currently selected items in the table. If there is no selection, an empty
166    * array is returned.
167    * @see #getSelectedFile
168    */
169  
170  public File[] getSelectedFiles()
171    {
172    return((File[])table.getSelectedItems());
173    }
174
175  /** Get the <code>KTreeTable</code> component for this component.
176   */
177
178  public KTreeTable getTable()
179    {
180    return(table);
181    }
182  
183  }
184
185/* end of source file */