-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileExplorer
138 lines (118 loc) · 4.74 KB
/
FileExplorer
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
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
public class FileExplorer extends JFrame {
private JTree directoryTree;
private JTextArea fileInfoTextArea;
private JButton copyButton;
private JButton moveButton;
private JButton renameButton;
private JButton deleteButton;
public FileExplorer() {
setTitle("File Explorer");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel filePanel = new JPanel(new BorderLayout());
fileInfoTextArea = new JTextArea(10, 40);
fileInfoTextArea.setEditable(false);
filePanel.add(new JScrollPane(fileInfoTextArea), BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
copyButton = new JButton("Copy");
moveButton = new JButton("Move");
renameButton = new JButton("Rename");
deleteButton = new JButton("Delete");
buttonPanel.add(copyButton);
buttonPanel.add(moveButton);
buttonPanel.add(renameButton);
buttonPanel.add(deleteButton);
filePanel.add(buttonPanel, BorderLayout.SOUTH);
mainPanel.add(filePanel, BorderLayout.CENTER);
directoryTree = new JTree();
directoryTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
JScrollPane treeScrollPane = new JScrollPane(directoryTree);
treeScrollPane.setPreferredSize(new Dimension(200, 0));
mainPanel.add(treeScrollPane, BorderLayout.WEST);
add(mainPanel);
// Add action listeners for buttons
copyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Implement copy operation
}
});
moveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Implement move operation
}
});
renameButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Implement rename operation
}
});
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Implement delete operation
}
});
// Populate directory tree
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
DefaultTreeModel treeModel = new DefaultTreeModel(root);
directoryTree.setModel(treeModel);
File[] roots = File.listRoots();
for (File file : roots) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(file.getPath());
root.add(node);
addChildren(node, file);
}
directoryTree.addTreeSelectionListener(e -> {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) directoryTree.getLastSelectedPathComponent();
if (selectedNode != null) {
File file = new File(selectedNode.getUserObject().toString());
if (file.isFile()) {
try {
fileInfoTextArea.setText(FileUtils.readFile(file));
} catch (IOException exception) {
exception.printStackTrace();
fileInfoTextArea.setText("Error reading file: " + exception.getMessage());
}
} else {
fileInfoTextArea.setText("");
}
}
});
setVisible(true);
}
private void addChildren(DefaultMutableTreeNode parentNode, File parentFile) {
File[] children = parentFile.listFiles();
if (children != null) {
for (File child : children) {
DefaultMutableTreeNode node = new DefaultMutableTreeNode(child.getPath());
parentNode.add(node);
if (child.isDirectory()) {
addChildren(node, child);
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(FileExplorer::new);
}
}
class FileUtils {
public static String readFile(File file) throws IOException {
StringBuilder content = new StringBuilder();
Files.lines(file.toPath()).forEach(line -> content.append(line).append("\n"));
return content.toString();
}
}