-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemModel.java
More file actions
193 lines (147 loc) · 5.43 KB
/
Copy pathFileSystemModel.java
File metadata and controls
193 lines (147 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// FileSystemModel.java
// TreeModel implementation using File objects as tree nodes.
// Java core packages
import java.io.*;
import java.util.*;
// Java extension packages
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class FileSystemModel implements TreeModel {
// hierarchy root
private File root;
// TreeModelListeners
private Vector listeners = new Vector();
// FileSystemModel constructor
public FileSystemModel( File rootDirectory )
{
root = rootDirectory;
}
// get hierarchy root (root directory)
public Object getRoot()
{
return root;
}
// get parent's child at given index
public Object getChild( Object parent, int index )
{
// get parent File object
try{
File directory = ( File ) parent;
// get list of files in parent directory
String[] children = directory.list();
// return File at given index and override toString
// method to return only the File's name
return new TreeFile( directory, children[ index ] );
}catch(Exception e){
System.err.println("Error in getchild:" + e);
}
return null;
}
// get parent's number of children
public int getChildCount( Object parent )
{
// get parent File object
File file = ( File ) parent;
// get number of files in directory
if ( file.isDirectory() ) {
String[] fileList = file.list();
if ( fileList != null )
return file.list().length;
}
return 0; // childCount is 0 for files
}
// return true if node is a file, false if it is a directory
public boolean isLeaf( Object node )
{
File file = ( File ) node;
return file.isFile();
}
// get numeric index of given child node
public int getIndexOfChild( Object parent, Object child )
{
// get parent File object
File directory = ( File ) parent;
// get child File object
File file = ( File ) child;
// get File list in directory
String[] children = directory.list();
// search File list for given child
for ( int i = 0; i < children.length; i++ ) {
if ( file.getName().equals( children[ i ] ) ) {
// return matching File's index
return i;
}
}
return -1; // indicate child index not found
} // end method getIndexOfChild
// invoked by delegate if value of Object at given
// TreePath changes
public void valueForPathChanged( TreePath path,
Object value )
{
// get File object that was changed
File oldFile = ( File ) path.getLastPathComponent();
// get parent directory of changed File
String fileParentPath = oldFile.getParent();
// get value of newFileName entered by user
String newFileName = ( String ) value;
// create File object with newFileName to rename oldFile
File targetFile = new File( fileParentPath, newFileName );
// rename oldFile to targetFile
oldFile.renameTo( targetFile );
// get File object for parent directory
File parent = new File( fileParentPath );
// create int array for renamed File's index
int[] changedChildrenIndices =
{ getIndexOfChild( parent, targetFile) };
// create Object array containing only renamed File
Object[] changedChildren = { targetFile };
// notify TreeModelListeners of node change
fireTreeNodesChanged( path.getParentPath(),
changedChildrenIndices, changedChildren );
} // end method valueForPathChanged
// notify TreeModelListeners that children of parent at
// given TreePath with given indices were changed
private void fireTreeNodesChanged( TreePath parentPath,
int[] indices, Object[] children )
{
// create TreeModelEvent to indicate node change
TreeModelEvent event = new TreeModelEvent( this,
parentPath, indices, children );
Iterator iterator = listeners.iterator();
TreeModelListener listener = null;
// send TreeModelEvent to each listener
while ( iterator.hasNext() ) {
listener = ( TreeModelListener ) iterator.next();
listener.treeNodesChanged( event );
}
} // end method fireTreeNodesChanged
// add given TreeModelListener
public void addTreeModelListener(
TreeModelListener listener )
{
listeners.add( listener );
}
// remove given TreeModelListener
public void removeTreeModelListener(
TreeModelListener listener )
{
listeners.remove( listener );
}
// TreeFile is a File subclass that overrides method
// toString to return only the File name.
private class TreeFile extends File {
// TreeFile constructor
public TreeFile( File parent, String child )
{
super( parent, child );
}
// override method toString to return only the File name
// and not the full path
public String toString()
{
return getName();
}
} // end inner class TreeFile
}