-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathMainFrame.java
More file actions
108 lines (90 loc) · 3.87 KB
/
MainFrame.java
File metadata and controls
108 lines (90 loc) · 3.87 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
package prog;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainFrame {
private static JFrame createMainFrame() {
JFrame frame = new JFrame("File Compresser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(350, 170, 550, 300);
return frame;
}
private static void setupContentPane(JFrame frame) {
Main demo = new Main();
frame.setContentPane(demo.createContentPane());
}
private static JMenuItem createMenuItem(String text, ActionListener listener) {
JMenuItem item = new JMenuItem(text);
item.addActionListener(listener);
return item;
}
private static JMenu createFileMenu() {
JMenu fileMenu = new JMenu("File");
// Open item
fileMenu.add(createMenuItem("Open", new ActionListener() {
public void actionPerformed(ActionEvent event) {
handleFileOpen();
}
}));
// Exit item
fileMenu.add(createMenuItem("Exit", e -> System.exit(0)));
return fileMenu;
}
private static void handleFileOpen() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
Main.openedFile = fileChooser.getSelectedFile();
Main.originalSize = Main.openedFile.length();
Main.originalSizeValue.setText(Main.originalSize + "Bytes");
Main.compressedSizeValue.setText("NotYetCalculated");
}
}
private static JMenu createHelpMenu() {
JMenu helpMenu = new JMenu("Help");
// How To item
helpMenu.add(createMenuItem("How To", e -> showHowToDialog()));
// About item
helpMenu.add(createMenuItem("About", e -> showAboutDialog()));
return helpMenu;
}
private static void showHowToDialog() {
String message = "Two algorithms are used for file compression\n" +
"1. Huffmans Codes\n" +
"2. Lampel-Ziv-Welch\n" +
"To compress a file first open the file then click\n" +
"ZIP HuffZ to zip the file using Huffmans algo.A zipped\n" +
"file with extension .huffz will be created in the same\n" +
"folder. This is the zipped file. During unzipping just open\n" +
"this file and click UNZIP HuffZ button.\n\n" +
"Same will happen for LZW. Zipped file's extension will be .LmZWp\n" +
"for LZW. Always make sure that during unzipping you must use\n" +
"the same algorithm that you used for zipping ";
JOptionPane.showMessageDialog(null, message, "How To...", JOptionPane.PLAIN_MESSAGE);
}
private static void showAboutDialog() {
String message = "File Compresser is a software to zip and unzip files\n" +
"It is developed on JAVA, as a term project of\n" +
" Level - 2, Term - 1,Dept. of CSE,BUET.\n" +
"It is completed by Nahiyan Kamal (St. ID 0805006) under\n" +
"the supervision of Jesun Shahariar of Dept. of CSE, BUET.";
JOptionPane.showMessageDialog(null, message, "About", JOptionPane.PLAIN_MESSAGE);
}
private static JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
menuBar.add(createFileMenu());
menuBar.add(createHelpMenu());
return menuBar;
}
public static JFrame createAndShowGUI() {
JFrame frame = createMainFrame();
setupContentPane(frame);
frame.setJMenuBar(createMenuBar());
frame.setVisible(true);
return frame;
}
public static void init() {
SwingUtilities.invokeLater(() -> createAndShowGUI());
}
}