-
Notifications
You must be signed in to change notification settings - Fork 627
/
Copy pathfileexplorer.java
206 lines (182 loc) · 4.93 KB
/
fileexplorer.java
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
194
195
196
197
198
199
200
201
202
203
204
205
206
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;
/***********************************/
class Explorer extends JPanel implements ActionListener
{
JTextField jtf;
JTextArea jta;
JTree tree;
JButton refresh;
JTable jtb;
JScrollPane jsp;
JScrollPane jspTable;
String currDirectory=null;
final String[] colHeads={"File Name","SIZE(in Bytes)","Read Only","Hidden"};
String[][]data={{"","","","",""}};
/////////////////////////////////
Explorer(String path)
{
jtf=new JTextField();
jta=new JTextArea(5,30);
refresh=new JButton("Refresh");
File temp=new File(path);
DefaultMutableTreeNode top=createTree(temp);
//if(top!=null)
tree=new JTree(top);
jsp=new JScrollPane(tree);
final String[] colHeads={"File Name","SIZE(in Bytes)","Read Only","Hidden"};
String[][]data={{"","","","",""}};
jtb=new JTable(data, colHeads);
jspTable=new JScrollPane(jtb);
setLayout(new BorderLayout());
add(jtf,BorderLayout.NORTH);
add(jsp,BorderLayout.WEST);
add(jspTable,BorderLayout.CENTER);
add(refresh,BorderLayout.SOUTH);
tree.addMouseListener(
new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
doMouseClicked(me);
}
});
jtf.addActionListener(this);
refresh.addActionListener(this);
}
///////////////////////////////
public void actionPerformed(ActionEvent ev)
{
File temp=new File(jtf.getText());
DefaultMutableTreeNode newtop=createTree(temp);
if(newtop!=null)
tree=new JTree(newtop);
remove(jsp);
jsp=new JScrollPane(tree);
setVisible(false);
add(jsp,BorderLayout.WEST);
tree.addMouseListener(
new MouseAdapter()
{
public void mouseClicked(MouseEvent me)
{
doMouseClicked(me);
}
});
setVisible(true);
}
//////////////////////////////
DefaultMutableTreeNode createTree(File temp)
{
DefaultMutableTreeNode top=new DefaultMutableTreeNode(temp.getPath());
if(!(temp.exists() && temp.isDirectory()))
return top;
fillTree(top,temp.getPath());
return top;
}
//////////////////////////////
void fillTree(DefaultMutableTreeNode root, String filename)
{
File temp=new File(filename);
if(!(temp.exists() && temp.isDirectory()))
return;
//System.out.println(filename);
File[] filelist=temp.listFiles();
for(int i=0; i<filelist.length; i++)
{
if(!filelist[i].isDirectory())
continue;
final DefaultMutableTreeNode tempDmtn=new DefaultMutableTreeNode(filelist[i].getName());
root.add(tempDmtn);
final String newfilename=new String(filename+"\\"+filelist[i].getName());
Thread t=new Thread()
{
public void run()
{
fillTree(tempDmtn,newfilename);
}//run
};//thread
if(t==null)
{System.out.println("no more thread allowed "+newfilename);return;}
t.start();
}//for
}//function
//////////////////////////////
void doMouseClicked(MouseEvent me)
{
TreePath tp=tree.getPathForLocation(me.getX(),me.getY());
if(tp==null) return;
//jtf.setText(tp.toString());
String s=tp.toString();
s=s.replace("[","");
s=s.replace("]","");
s=s.replace(", ","\\");
//s=s.replace(" ","");
//int z=s.lastIndexOf("\"\\\"");
//s="\'"+s; s=s+"\'";
jtf.setText(s);
showFiles(s);
//java.util.StringTokenizer st=new java.util.StringTokenizer(s,",");
//jtf.setText(jtf.getText()+"="+s);
}
////////////////////////////////
void showFiles(String filename)
{
File temp=new File(filename);
data=new String[][]{{"","","",""}};
remove(jspTable);
jtb=new JTable(data, colHeads);
jspTable=new JScrollPane(jtb);
setVisible(false);
add(jspTable,BorderLayout.CENTER);
setVisible(true);
if(!temp.exists()) return;
if(!temp.isDirectory()) return;
//System.out.println(filename);
File[] filelist=temp.listFiles();
int fileCounter=0;
data=new String[filelist.length][4];
for(int i=0; i<filelist.length; i++)
{
if(filelist[i].isDirectory())
continue;
data[fileCounter][0]=new String(filelist[i].getName());
data[fileCounter][1]=new String(filelist[i].length()+"");
data[fileCounter][2]=new String(!filelist[i].canWrite()+"");
data[fileCounter][3]=new String(filelist[i].isHidden()+"");
fileCounter++;
}//for
String dataTemp[][]=new String[fileCounter][4];
for(int k=0; k<fileCounter; k++)
dataTemp[k]=data[k];
data=dataTemp;
//System.out.println(data);
remove(jspTable);
jtb=new JTable(data, colHeads);
jspTable=new JScrollPane(jtb);
setVisible(false);
add(jspTable,BorderLayout.CENTER);
setVisible(true);
}
////////////////////////////////
///////////////////////////////
}
/***********************************/
class ExplorerTest extends JFrame
{
ExplorerTest(String path)
{
super("Windows Exploder - Javatpoint");
add(new Explorer(path),"Center");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400,400);
setVisible(true);
}
public static void main(String[] args)
{
new ExplorerTest(".");
}
}