-
Notifications
You must be signed in to change notification settings - Fork 1
/
ControlPanel.java
224 lines (197 loc) · 7.45 KB
/
ControlPanel.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class ControlPanel extends JPanel {
// Create left side of the GUI that contains the file chooser and
// buttons to select different functions
private static String fileObjectName = "";
private static File fileObject = null; // File object chosen as the root of
// the file tree
private static FileStructure tree = null;
private JLabel fileChosen; // Displays the name of the file object chosen
private JFileChooser chooser; // GUI object to select File object
private JButton dirByNameButton; // Buttons to print the file tree
private JButton dirBySizeButton;
private JButton typeButton;
private JTextField typeField;
private JButton findButton;
private JTextField findField;
private JButton dupFiles;
private JButton dupFolders;
private String fileMssg = "<html>File object selected: </html>";
private String fileMssgNo = "File object selected: ";
/**
* Creates left panel of the graphical user interface.
*/
public ControlPanel() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
// GUI for choosing the file object
chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.addActionListener(new FileChooserListener());
chooser.setApproveButtonText("Select");
chooser.setCurrentDirectory(new File(".")); // Strarting directory is
// current directory
// Label for displaying the name of the initial file object
JPanel choice = new JPanel();
choice.setLayout(new FlowLayout(FlowLayout.LEFT)); // Panel needed to
// print file object
// name left
// justified
fileChosen = new JLabel(fileMssg);
fileChosen.setHorizontalAlignment(SwingConstants.LEFT);
fileChosen.setHorizontalTextPosition(SwingConstants.LEFT);
fileChosen.setFont(new Font("Helvetica", Font.BOLD, 14));
fileChosen.setForeground(Color.blue);
choice.add((fileChosen));
// Panel to display the buttons to print the file tree
JPanel printPanelName = new JPanel();
printPanelName.setLayout(new FlowLayout(FlowLayout.LEFT));
JPanel printPanelSize = new JPanel();
printPanelSize.setLayout(new FlowLayout(FlowLayout.LEFT));
dirByNameButton = new JButton("Print File Objects Ordered by Name");
dirBySizeButton = new JButton("Print File Objects Ordered by Size ");
ButtonListener listener = new ButtonListener();
dirByNameButton.addActionListener(listener);
dirBySizeButton.addActionListener(listener);
printPanelName.add(dirByNameButton);
printPanelSize.add(dirBySizeButton);
// Panel to display the button to choose files by type
JPanel typePanel = new JPanel();
typePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
typeField = new JTextField(6);
JLabel typeLabel = new JLabel("Find files of this type: ");
typeButton = new JButton("Find files");
typeButton.addActionListener(listener);
typePanel.add(typeLabel);
typePanel.add(typeField);
typePanel.add(typeButton);
// Panel to display the button to find files by name
JPanel findPanel = new JPanel();
findPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
findField = new JTextField(25);
JLabel findLabel = new JLabel("File name: ");
findButton = new JButton("Find file");
findButton.addActionListener(listener);
findPanel.add(findLabel);
findPanel.add(findField);
findPanel.add(findButton);
// Add the file chooser, label, and buttons to the GUI
add(chooser);
add(Box.createVerticalGlue());
add(Box.createRigidArea(new Dimension(0, 10)));
add(choice);
add(Box.createVerticalGlue());
add(Box.createRigidArea(new Dimension(0, 10)));
add(printPanelName);
add(Box.createVerticalGlue());
add(Box.createRigidArea(new Dimension(0, 10)));
add(printPanelSize);
add(typePanel);
add(findPanel);
// add(duplicatedPanel);
}
public static String getFileObjectName() {
return fileObjectName;
}
public static File getFileObject() {
return fileObject;
}
private class ButtonListener implements ActionListener {
// Invokes the methods to print the File Structure
public void actionPerformed(ActionEvent event) {
JTextArea canvas = SplitPanel.getPage();
String text = "";
try {
// Check that a file object has been selected and that a file
// tree has been built
if (fileObject != null)
if (tree == null || !tree.getRoot().getData().getLongName().equals(fileObject.getCanonicalPath())) {
text = ".";
// SplitPanel.setPageText(text);
FileObject.resetNumFileObjects();
tree = new FileStructure(fileObject.getCanonicalPath());
}
if (tree == null) {
text = "Select File Object";
canvas.setText(text);
return;
}
// Print directory sorted by name or by size
PrintFileStructure p = null;
if (event.getSource() == dirByNameButton || event.getSource() == dirBySizeButton) {
if (event.getSource() == dirByNameButton)
p = new PrintFileStructure(PrintFileStructure.SORT_BY_NAME);
else if (event.getSource() == dirBySizeButton)
p = new PrintFileStructure(PrintFileStructure.SORT_BY_SIZE);
text = "";
text = p.printTree(tree.getRoot(), 0);
text = p.formatTreeOutput(text);
} else if (event.getSource() == typeButton) {
// Find files of specified type
p = new PrintFileStructure(0);
Iterator<String> list = tree.filesOfType(typeField.getText());
text = p.printIterator(list);
if (text.length() == 0)
text = "No files found";
} else if (event.getSource() == findButton) {
// Find specified dile
String result = tree.findFile(findField.getText());
if (result.equals(""))
text = "File not found";
else
text = "File found: " + result + "\n";
/* } else if (event.getSource() == dupFiles) {
p = new PrintFileStructure(0);
Iterator<String> list = tree.duplicatedFiles();
text = p.printIterator(list);
if (text.length() == 0)
text = "No files found";
} else if (event.getSource() == dupFolders) {
p = new PrintFileStructure(0);
Iterator<String> list = tree.duplicatedFolders();
text = p.printIterator(list);
if (text.length() == 0)
text = "No folders found";
*/
}
if (!text.equals("No files found") && !text.equals("No folders found"))
text = text + "\n Number of files: " + FileObject.getNumFileObjects() + "\n";
canvas.setText(text);
} catch (FileObjectException | IOException e) {
text = "Error accessing file object " + ControlPanel.getFileObjectName();
canvas.setText(text);
}
}
}
private class FileChooserListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
String selection = fileMssgNo;
if (command.equals(JFileChooser.APPROVE_SELECTION)) {
fileObject = chooser.getSelectedFile();
fileObjectName = fileObject.getName();
selection = selection + fileObjectName;
} else if (command.equals(JFileChooser.CANCEL_SELECTION)) {
fileObjectName = "";
fileObject = null;
}
fileChosen.setText("<html>" + fileMssgNo + "<font color=\"red\">" + fileObjectName + "</font></html>");
}
}
}