-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
396 lines (348 loc) · 17.9 KB
/
GUI.java
File metadata and controls
396 lines (348 loc) · 17.9 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import javafx.scene.control.ProgressBar;
import net.miginfocom.swing.MigLayout;
import javax.swing.*;
import javax.swing.text.DefaultCaret;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/*
Author: David Walshe
Date: 01/10/2017
Description: This program is used to create a thread safe GUI using the SwingWorker Class. It allows for asynchronous time consuming operations to process in the
background while still allowing full control of the GUI to the user without signs of lag or unresponsiveness. It also includes the logger class to
log important events during the application's life cycle.
The GUI makes use of some basic components as well as use of MigLayout.
The GUI allows for the loading of a large text file into a JTextArea using a SwingWorker.
It also allows for a large text file to be zipped and show the compression ratio and size reduction of the original and compressed file.
*/
public class GUI implements ActionListener {
//Create all GUI element reference holders
JTextArea txtArea;
JButton loadBtn;
JButton cancelBtn;
JProgressBar progressBar;
JLabel timeStamp;
JButton zipBtn;
JFileChooser fileChooser;
private static final String CLASS_NAME = GUI.class.getName();
//Create Swing Workers to process asynchronous blocking requests in the GUI.
public SwingWorker<Integer, String> worker;
public SwingWorker<Integer, String> workerZip;
private static final Logger logger = Logger.getLogger(GUI.class.getName());
private FileHandler fileHandler;
private ConsoleHandler cs;
public GUI()
{
this.addFileHandler(logger);
logger.log(Level.INFO, "");
//Kick off a die thread when the application finishes to format the output from the logger.
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
logger.entering(CLASS_NAME, "");
Process proc;
BufferedReader br;
try{
String path = System.getProperty("user.dir");
while(path.contains("\\")) {
path = path.replace("\\", "/");
}
path += "/GUI.log";
logger.log(Level.INFO, "Executing Perl Formatting Script on GUI Exit");
proc = Runtime.getRuntime().exec("Perl C:\\Users\\David\\Desktop\\Client-Server\\Assignment_1_Thread_Safe_GUI\\src\\Parse.pl " + path);
} catch (Exception e) {
logger.log(Level.SEVERE, "Execption: " + e.getMessage());
}
}
}, "Shutdown-thread"));
}
private void addFileHandler(Logger logger) {
try {
fileHandler = new FileHandler(CLASS_NAME + ".log");
} catch (IOException ex) {
logger.log(Level.SEVERE, "Execption: " + ex.getMessage());
} catch (SecurityException ex) {
logger.log(Level.SEVERE, "Execption: " + ex.getMessage());
}
logger.addHandler(fileHandler);
cs = new ConsoleHandler();
logger.addHandler(cs);
logger.setLevel(Level.FINER);
fileHandler.setLevel(Level.ALL);
cs.setLevel(Level.ALL);
}
//GUI maker method called by main.
public static void createAndShowGUI() {
logger.entering(CLASS_NAME, "createAndShowGUI");
logger.log(Level.INFO, "Creating GUI");
JFrame frame = new JFrame("Thread Safe GUI");
frame.setSize(600, 600);
frame.setLocation(600, 300);
GUI demo = new GUI();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
logger.exiting(CLASS_NAME, "createAndShowGUI");
}
//Create the GUI look and feel. Init and setup up all components in the GUI.
private JPanel createContentPane() {
logger.entering(CLASS_NAME, "createContentPane");
JPanel totalGUI = new JPanel();
totalGUI.setSize(600, 600);
totalGUI.setLayout(new MigLayout("", "", ""));
// The main story for the JTextArea
String text = "";
//Instantiate the TextArea to suit the GUI
txtArea = new JTextArea(text, 5, 30);
txtArea.setEditable(false);
txtArea.setLineWrap(true);
txtArea.setWrapStyleWord(true);
DefaultCaret caret = (DefaultCaret) txtArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
// Create the ScrollPane and instantiate it with the TextArea as an argument
// along with two constants that define the behaviour of the scrollbars.
JScrollPane area = new JScrollPane(txtArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
timeStamp = new JLabel("Time Elapsed: ");
loadBtn = new JButton("Load");
loadBtn.addActionListener(this);
zipBtn = new JButton("Zip");
zipBtn.addActionListener(this);
cancelBtn = new JButton("Cancel");
cancelBtn.addActionListener(this);
cancelBtn.setEnabled(false);
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setString("Progress: 0%");
progressBar.setStringPainted(true);
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//Where the GUI is constructed:
totalGUI.add(area, "span, push, grow");
totalGUI.add(timeStamp, "span, grow");
totalGUI.add(progressBar, "span, grow");
totalGUI.add(loadBtn, "span, grow, split");
totalGUI.add(zipBtn, "span, grow, split");
totalGUI.add(cancelBtn, "span, grow, split");
totalGUI.setOpaque(true);
logger.exiting(CLASS_NAME, "createContentPane");
return totalGUI;
}
// For the Action Events attached to Button elements.
public void actionPerformed(ActionEvent e) {
logger.entering(CLASS_NAME, "actionPerformed");
if (e.getSource() == loadBtn) {
logger.log(Level.INFO, "Load Button Pressed");
if (fileChooser.showOpenDialog(loadBtn) == JFileChooser.APPROVE_OPTION) {
logger.log(Level.INFO, "File Chooser Opened - User Selected File: " + fileChooser.getSelectedFile());
long startTime = System.currentTimeMillis();
txtArea.setText("");
progressBar.setValue(0);
progressBar.setString("Progress: 0%");
Load(startTime, fileChooser.getSelectedFile());
loadBtn.setEnabled(false);
cancelBtn.setEnabled(true);
} else {
logger.log(Level.INFO, "User Cancelled File Chooser");
txtArea.append(">> Text Area File Selection Cancelled\r\n");
}
} else if (e.getSource() == cancelBtn) {
logger.log(Level.INFO, "Cancel Button Pressed");
worker.cancel(true);
loadBtn.setEnabled(true);
cancelBtn.setEnabled(false);
progressBar.setValue(0);
progressBar.setString("Progress: 0%");
} else if (e.getSource() == zipBtn) {
logger.log(Level.INFO, "Zip Button Pressed");
if (fileChooser.showOpenDialog(zipBtn) == JFileChooser.APPROVE_OPTION) {
logger.log(Level.INFO, "File Chooser Opened - User Selected File: " + fileChooser.getSelectedFile());
txtArea.setText("");
Zip(System.currentTimeMillis(), fileChooser.getSelectedFile());
} else {
logger.log(Level.INFO, "User Cancelled File Chooser");
txtArea.append(">> Zip File Selection Cancelled\r\n");
}
}
logger.exiting(CLASS_NAME, "actionPerformed");
}
//Static method to return a structured time format to the GUI.
private static String timeFormatter(long milliseconds) {
logger.entering(CLASS_NAME, "timeFormatter");
int seconds = (int) (milliseconds / 1000) % 60;
int minutes = (int) ((milliseconds / (1000 * 60)) % 60);
int hours = (int) ((milliseconds / (1000 * 60 * 60)) % 24);
milliseconds %= 1000;
String timeFormattedString = "";
timeFormattedString = String.format("%02d", hours);
timeFormattedString += ":";
timeFormattedString += String.format("%02d", minutes);
timeFormattedString += ":";
timeFormattedString += String.format("%02d", seconds);
timeFormattedString += ".";
timeFormattedString += String.format("%03d", milliseconds);
logger.exiting(CLASS_NAME, "timeFormatter");
logger.exiting(CLASS_NAME, "timeFormatter");
return timeFormattedString;
}
// Zip functionality that makes use of the SwingWorker to zip files on a separate thread.
private void Zip(long time, File fileName) {
logger.entering(CLASS_NAME, "Zip");
File zipFileName = new File(fileName.toString().substring(0, fileName.toString().lastIndexOf(".") + 1) + "zip");
String fileNameStr = fileName.getName();
String zipNameStr = zipFileName.getName();
workerZip = new SwingWorker<Integer, String>() {
@Override
protected Integer doInBackground() throws Exception {
logger.entering(CLASS_NAME, "Zip->SwingWorker->doInBackground");
//try with resources for the IO streams for the zip functionality.
try (
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
BufferedReader br = new BufferedReader(new FileReader(fileName))
) {
ZipEntry e = new ZipEntry(zipNameStr);
out.putNextEntry(e);
//Create a zip entry to place the compressed data into.
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line + "\r\n");
}
//Write data into the zip entry.
byte[] data = sb.toString().getBytes();
out.write(data, 0, data.length);
out.closeEntry();
out.close();
} catch (Exception e) {
logger.log(Level.SEVERE, "Execption: " + e.getMessage());
}
logger.exiting(CLASS_NAME, "Zip->SwingWorker->doInBackground");
return 1;
}
@Override
protected void done() {
logger.entering(CLASS_NAME, "Zip->SwingWorker->done");
txtArea.setText("Zip Complete");
Path file = Paths.get(fileName.toString());
Path zipFile = Paths.get(zipFileName.toString());
BasicFileAttributes fileAttr = null;
BasicFileAttributes zipFileAttr = null;
try {
fileAttr = Files.readAttributes(file, BasicFileAttributes.class);
zipFileAttr = Files.readAttributes(zipFile, BasicFileAttributes.class);
} catch (Exception e) {
logger.log(Level.SEVERE, "Execption: " + e.getMessage());
}
//Write to the JTextArea the time of zip, the compression ratio and the size of both files along with the space saved.
StringBuilder sb = new StringBuilder();
sb.append("########################################################\r\n");
sb.append("Zip Information\r\n");
sb.append("########################################################\r\n");
sb.append("Original File Name: " + fileNameStr + "\r\n");
sb.append("Zip File Name: " + zipNameStr + "\r\n");
sb.append("--------------------------------------------------------------------------------------------------\r\n");
sb.append("Creation Time for " + zipNameStr + "\r\n");
sb.append("--------------------------------------------------------------------------------------------------\r\n");
sb.append(">\t" + zipFileAttr.lastModifiedTime() + "\r\n");
sb.append("--------------------------------------------------------------------------------------------------\r\n");
sb.append(fileNameStr + " Size\r\n");
sb.append("--------------------------------------------------------------------------------------------------\r\n");
sb.append(">\t" + fileAttr.size() + " B\r\n");
sb.append(">\t" + (float) fileAttr.size() / (1024 * 1024) + " MB\r\n");
sb.append("--------------------------------------------------------------------------------------------------\r\n");
sb.append(zipNameStr + " Size\r\n");
sb.append("--------------------------------------------------------------------------------------------------\r\n");
sb.append(">\t" + zipFileAttr.size() + " B\r\n");
sb.append(">\t" + (float) zipFileAttr.size() / (1024 * 1024) + " MB\r\n");
sb.append("--------------------------------------------------------------------------------------------------\r\n");
sb.append("Compression Ratio\r\n");
sb.append("--------------------------------------------------------------------------------------------------\r\n");
sb.append(">\t" + (float) zipFileAttr.size() / (float) fileAttr.size() + "\r\n");
sb.append("--------------------------------------------------------------------------------------------------\r\n");
sb.append("File Size Reduction\r\n");
sb.append("--------------------------------------------------------------------------------------------------\r\n");
sb.append(">\t" + (100 - ((float) zipFileAttr.size() / (float) fileAttr.size()) * 100) + "%\r\n");
sb.append("--------------------------------------------------------------------------------------------------\r\n");
txtArea.setText(sb.toString());
long timeElapsed = System.currentTimeMillis() - time;
timeStamp.setText("File Zip Time: " + GUI.timeFormatter(timeElapsed));
logger.log(Level.INFO, "File Zip Time: " + GUI.timeFormatter(timeElapsed));
logger.exiting(CLASS_NAME, "Zip->SwingWorker->done");
}
};
workerZip.execute();
logger.entering(CLASS_NAME, "Zip");
}
//Used to load
private void Load(long time, File fileName) {
logger.entering(CLASS_NAME, "Load");
worker = new SwingWorker<Integer, String>() {
@Override
protected Integer doInBackground() throws Exception {
logger.entering(CLASS_NAME, "Load->SwingWorker->doInBackground");
LineNumberReader lnr = new LineNumberReader(new FileReader(fileName));
lnr.skip(Long.MAX_VALUE);
float lineCount = lnr.getLineNumber() + 1;
// Finally, the LineNumberReader object should be closed to prevent resource leak
lnr.close();
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
StringBuilder sb = new StringBuilder();
int count = 0;
while ((line = br.readLine()) != null) {
count++;
sb.append(count + "- " + line + "\r\n");
setProgress(Math.round((count / lineCount) * 100f));
}
publish(sb.toString());
} catch (IOException ioe) {
logger.log(Level.SEVERE, "Execption: " + ioe.getMessage());
}
logger.exiting(CLASS_NAME, "Load->SwingWorker->doInBackground");
return 1;
}
@Override
protected void process(List<String> chunks) {
logger.entering(CLASS_NAME, "Load->SwingWorker->process");
for (String lines : chunks) {
txtArea.append(lines + "\r\n");
}
int prog = worker.getProgress();
long timeElapsed = System.currentTimeMillis() - time;
timeStamp.setText("File Load Time: " + GUI.timeFormatter(timeElapsed));
logger.log(Level.INFO, "File Load Time: " + GUI.timeFormatter(timeElapsed));
progressBar.setValue(prog);
progressBar.setString("Progress: " + prog + "%");
logger.exiting(CLASS_NAME, "Load->SwingWorker->process");
}
@Override
protected void done() {
logger.entering(CLASS_NAME, "Load->SwingWorker->done");
loadBtn.setEnabled(true);
cancelBtn.setEnabled(false);
if (isCancelled()) {
txtArea.append("\n!! Cancelled !!\n");
} else {
txtArea.append("\n>> Done <<\n");
}
logger.exiting(CLASS_NAME, "Load->SwingWorker->done");
}
};
worker.execute();
logger.exiting(CLASS_NAME, "Load");
}
}