01: /*
02: * Copyright 2008 Paul Hinds
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.tp23.gui.tree.model;
17:
18: import java.io.File;
19: import java.io.FilenameFilter;
20:
21: /**
22: * ItemModel that uses a java.io.File for data.
23: * @author teknopaul
24: *
25: */
26: public class FileModel implements ItemModel {
27:
28: private File item;
29: private FilenameFilter filter;
30:
31: public FileModel(File item) {
32: this .item = item;
33: }
34:
35: public FileModel(File item, FilenameFilter filter) {
36: this .item = item;
37: this .filter = filter;
38: }
39:
40: public String getName() {
41: return item.getName();
42: }
43:
44: public boolean isHidden() {
45: return item.getName().startsWith(".");
46: }
47:
48: public String getFullPath() {
49: return item.getAbsolutePath();
50: }
51:
52: public boolean isLeaf() {
53: return item.isFile();
54: }
55:
56: public Object getData() {
57: return item;
58: }
59:
60: public int compareTo(Object o) {
61: return item.compareTo(((ItemModel) o).getData());
62: }
63:
64: public ItemModel[] getChildren() {
65: if (isLeaf()) {
66: return new ItemModel[0];
67: }
68: File[] files;
69: if (filter != null) {
70: files = item.listFiles(filter);
71: } else {
72: files = item.listFiles();
73: }
74: ItemModel[] children = new ItemModel[files.length];
75: for (int i = 0; i < children.length; i++) {
76: children[i] = new FileModel(files[i], filter);
77: }
78: return children;
79: }
80:
81: public String toString() {
82: return item.toString();
83: }
84:
85: }
|