001: package org.acm.seguin.pmd.swingui;
002:
003: import javax.swing.event.TreeSelectionEvent;
004: import javax.swing.event.TreeSelectionListener;
005: import javax.swing.table.DefaultTableModel;
006: import javax.swing.tree.TreePath;
007: import java.io.File;
008: import java.io.FileFilter;
009: import java.io.FileInputStream;
010: import java.io.FileNotFoundException;
011: import java.io.IOException;
012: import java.io.InputStreamReader;
013: import java.text.DateFormat;
014: import java.text.DecimalFormat;
015: import java.text.DecimalFormatSymbols;
016: import java.util.Date;
017: import java.util.Vector;
018:
019: /**
020: *
021: * @author Donald A. Leckie
022: * @since August 17, 2002
023: * @version $Revision: 1.1 $, $Date: 2003/07/29 20:51:59 $
024: */
025: class DirectoryTableModel extends DefaultTableModel {
026: private DateFormat m_dateFormat;
027: private DecimalFormat m_decimalFormat;
028: private String m_fileExtension;
029:
030: //Constants
031: protected static final int FILE_NAME_COLUMN = 0;
032: protected static final int FILE_SIZE_COLUMN = 1;
033: protected static final int FILE_LAST_MODIFIED_COLUMN = 2;
034: private final int FILE_COLUMN = 3; // not a visible column
035:
036: /**
037: *********************************************************************************
038: *
039: */
040: protected DirectoryTableModel(DirectoryTree directoryTree,
041: String fileExtension) {
042: super (createData(), createColumnNames());
043:
044: DecimalFormatSymbols decimalFormatSymbols;
045: StringBuffer buffer;
046: String pattern;
047:
048: m_fileExtension = fileExtension.toLowerCase();
049: m_dateFormat = DateFormat.getDateTimeInstance();
050: decimalFormatSymbols = new DecimalFormatSymbols();
051: buffer = new StringBuffer(25);
052:
053: buffer.append('#');
054: buffer.append(decimalFormatSymbols.getGroupingSeparator());
055: buffer.append('#');
056: buffer.append('#');
057: buffer.append(decimalFormatSymbols.getDigit());
058: buffer.append(" lines");
059:
060: pattern = buffer.toString();
061: m_decimalFormat = new DecimalFormat(pattern,
062: decimalFormatSymbols);
063: new DirectoryTreeSelectionListener(directoryTree);
064: }
065:
066: /**
067: ********************************************************************************
068: *
069: * @return
070: */
071: private static Vector createData() {
072: return new Vector();
073: }
074:
075: /**
076: ********************************************************************************
077: *
078: * @return
079: */
080: private static Vector createColumnNames() {
081: Vector row = new Vector(2);
082:
083: row.add("File Name");
084: row.add("Size");
085: row.add("Last Modified");
086:
087: return row;
088: }
089:
090: /**
091: ********************************************************************************
092: *
093: * @return
094: */
095: protected File getFile(int row) {
096: if ((row >= 0) && (row < getRowCount())) {
097: return (File) getValueAt(row, FILE_COLUMN);
098: }
099:
100: return null;
101: }
102:
103: /**
104: *******************************************************************************
105: *
106: * @param row
107: * @param column
108: *
109: * @return
110: */
111: public boolean isCellEditable(int row, int column) {
112: return false;
113: }
114:
115: /**
116: *******************************************************************************
117: *******************************************************************************
118: *******************************************************************************
119: */
120: private class DirectoryTreeSelectionListener implements
121: TreeSelectionListener {
122:
123: /**
124: *************************************************************************
125: *
126: */
127: private DirectoryTreeSelectionListener(
128: DirectoryTree directoryTree) {
129: directoryTree.addTreeSelectionListener(this );
130: }
131:
132: /**
133: ***************************************************************************
134: *
135: * @param directoryTree
136: */
137: public void valueChanged(TreeSelectionEvent event) {
138: TreePath treePath = event.getPath();
139: DirectoryTreeNode treeNode = (DirectoryTreeNode) treePath
140: .getLastPathComponent();
141: Object userObject = treeNode.getUserObject();
142: Vector rows = getDataVector();
143:
144: for (int n = 0; n < rows.size(); n++) {
145: ((Vector) rows.get(n)).clear();
146: }
147:
148: rows.clear();
149:
150: if (userObject instanceof File) {
151: File directory;
152: File[] files;
153:
154: directory = (File) userObject;
155: files = directory.listFiles(new FilesFilter());
156:
157: if (files != null) {
158: for (int n1 = 0; n1 < files.length; n1++) {
159: Vector row = new Vector(3);
160: String fileName = files[n1].getName();
161: int lineCount = countLines(files[n1]);
162: String size = m_decimalFormat.format(lineCount);
163: Date date = new Date(files[n1].lastModified());
164: String lastModified = m_dateFormat.format(date);
165:
166: row.add(fileName);
167: row.add(size);
168: row.add(lastModified);
169: row.add(files[n1]);
170:
171: rows.add(row);
172: }
173: }
174: }
175:
176: fireTableDataChanged();
177: }
178:
179: /**
180: **************************************************************************
181: *
182: * @param file
183: *
184: * @return
185: */
186: private int countLines(File file) {
187: int lineCount = 0;
188: InputStreamReader reader = null;
189:
190: try {
191: char[] buffer;
192:
193: buffer = new char[10000];
194: reader = new InputStreamReader(
195: new FileInputStream(file));
196:
197: while (reader.ready()) {
198: int numberOfChars = reader.read(buffer);
199:
200: for (int n = 0; n < numberOfChars; n++) {
201: if (buffer[n] == '\n') {
202: lineCount++;
203: }
204: }
205: }
206: } catch (FileNotFoundException exception) {
207: lineCount = 0;
208: } catch (IOException exception) {
209: lineCount = 0;
210: } finally {
211: if (reader != null) {
212: try {
213: reader.close();
214: } catch (IOException exception) {
215: }
216: }
217: }
218:
219: return lineCount;
220: }
221: }
222:
223: /**
224: *******************************************************************************
225: *******************************************************************************
226: *******************************************************************************
227: */
228: private class FilesFilter implements FileFilter {
229:
230: public boolean accept(File file) {
231: if (file.isFile() && (file.isHidden() == false)) {
232: String fileName = file.getName().toLowerCase();
233:
234: return (fileName.endsWith(m_fileExtension));
235: }
236:
237: return false;
238:
239: }
240: }
241: }
|