001: /*=============================================================================
002: * Copyright Texas Instruments 2001, 2002. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package ti.chimera.fs;
020:
021: import ti.swing.treetable.*;
022:
023: import oscript.exceptions.ProgrammingErrorException;
024: import oscript.fs.AbstractFile;
025: import oscript.fs.AbstractFileSystem;
026:
027: /**
028: * FileSystemTreeTableModel is the adapter that makes a FileSystemTreeModel
029: * look like what the TreeTable wants it to look like. The model has the
030: * following columns:
031: * <ul>
032: * <li> Path
033: * <li> Size
034: * <li> Last Modified
035: * </ul>
036: *
037: * @author Rob Clark
038: * @version 0.1
039: */
040: public class FileSystemTreeTableModel extends FileSystemTreeModel
041: implements TreeTableModel {
042: private static final boolean enableWindozeHacks = System
043: .getProperty("os.name").toLowerCase().indexOf("windows") != -1;
044:
045: private static final String[] COLUMN_NAMES = new String[] { "Path",
046: "Size", "Last Modified" };
047:
048: private static final Class[] COLUMN_CLASSES = new Class[] {
049: TreeTableModel.class, String.class, java.util.Date.class };
050:
051: /**
052: * Class Constructor.
053: *
054: * @param path the path to the root node of the tree
055: * @param filter the filter, or <code>null</code>
056: */
057: public FileSystemTreeTableModel(String path,
058: FileSystemTreeFilter filter) {
059: this (pathToFile(path), filter);
060: }
061:
062: /**
063: * Class Constructor.
064: *
065: * @param root the root node of the tree
066: * @param filter the filter, or <code>null</code>
067: */
068: public FileSystemTreeTableModel(AbstractFile root,
069: FileSystemTreeFilter filter) {
070: super (root, filter);
071: }
072:
073: public int getColumnCount() {
074: return COLUMN_NAMES.length;
075: }
076:
077: public String getColumnName(int col) {
078: return COLUMN_NAMES[col];
079: }
080:
081: public Class getColumnClass(int col) {
082: return COLUMN_CLASSES[col];
083: }
084:
085: public Object getValueAt(Object onode, int col) {
086: FileSystemNode node = (FileSystemNode) onode;
087: String name = node.getFile().getName();
088:
089: switch (col) {
090: case 0:
091: return name;
092:
093: case 1:
094: // avoid accessing meta-data for drive letters, in case no disk in drive:
095: if (enableWindozeHacks && (name.length() == 2)
096: && (name.charAt(1) == ':'))
097: return null;
098:
099: if (node.length() == -1)
100: return null;
101:
102: return " " + bytesToSizeString(node.length());
103:
104: case 2:
105: // avoid accessing meta-data for drive letters, in case no disk in drive:
106: if (enableWindozeHacks && (name.length() == 2)
107: && (name.charAt(1) == ':'))
108: return null;
109:
110: if (node.lastModified() == -1)
111: return null;
112:
113: return new java.util.Date(node.lastModified());
114:
115: default:
116: return null;
117: }
118: }
119:
120: /**
121: * Nothing is editable.
122: */
123: public boolean isCellEditable(Object node, int col) {
124: // Hmm, looks like the TreeTable column must be editable:
125: return COLUMN_CLASSES[col] == TreeTableModel.class;
126: }
127:
128: /**
129: * No-op, none of the cells are editable
130: */
131: public void setValueAt(Object aValue, Object node, int column) {
132: }
133:
134: /**
135: * Utility to convert a size (in bytes) to a human readible string.
136: */
137: private final static String bytesToSizeString(long b) {
138: if (b > (1024 * 1024))
139: return floatToString((float) b / (float) (1024 * 1024))
140: + "m";
141: else if (b > 1024)
142: return floatToString((float) b / (float) (1024)) + "k";
143: else
144: return b + "b";
145: }
146:
147: /**
148: * Utility to convert a float to string, truncating at two decimals
149: * past decimal point.
150: */
151: private final static String floatToString(float f) {
152: String str = "" + f;
153: int idx = str.indexOf('.');
154:
155: idx = Math.min((idx > 0) ? (idx + 2) : str.length(), str
156: .length());
157:
158: return str.substring(0, idx);
159: }
160: }
161:
162: /*
163: * Local Variables:
164: * tab-width: 2
165: * indent-tabs-mode: nil
166: * mode: java
167: * c-indentation-style: java
168: * c-basic-offset: 2
169: * eval: (c-set-offset 'substatement-open '0)
170: * eval: (c-set-offset 'case-label '+)
171: * eval: (c-set-offset 'inclass '+)
172: * eval: (c-set-offset 'inline-open '0)
173: * End:
174: */
|