-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainGUI.java
More file actions
184 lines (152 loc) · 5.57 KB
/
MainGUI.java
File metadata and controls
184 lines (152 loc) · 5.57 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
// By: Justin Spedding & Andrew Miller
public class MainGUI implements WindowListener, ActionListener {
private JFrame window; // Main window
private JButton encodeButton, decodeButton, analyzeButton; // Buttons
private JMenuItem encodeMenuItem, decodeMenuItem, analyzeMenuItem, exitMenuItem; // File menu items
private JMenuItem markerMenuItem; // Settings menu items
private JMenuItem helpMenuItem; // Help menu items
private String helpText; // The text from the help file
/**
* Creates the main launcher GUI
*/
public MainGUI() {
buildWindow();
initialize();
}
private void buildWindow() {
// Create window
window = new JFrame("Steganographer " + Steganographer.version);
window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
window.addWindowListener(this);
window.setSize(200,200);
window.setMinimumSize(new Dimension(200, 200));
window.setLocationRelativeTo(null);
// Create menu bar
JMenuBar menuBar = new JMenuBar();
// Create file menu
JMenu fileMenu = new JMenu("File");
encodeMenuItem = new JMenuItem("Encode...");
encodeMenuItem.addActionListener(this);
fileMenu.add(encodeMenuItem);
decodeMenuItem = new JMenuItem("Decode...");
decodeMenuItem.addActionListener(this);
fileMenu.add(decodeMenuItem);
analyzeMenuItem = new JMenuItem("Analyze...");
analyzeMenuItem.addActionListener(this);
fileMenu.add(analyzeMenuItem);
exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(this);
fileMenu.add(exitMenuItem);
// Create settings menu
JMenu settingsMenu = new JMenu("Settings");
markerMenuItem = new JMenuItem("Set marker...");
markerMenuItem.addActionListener(this);
settingsMenu.add(markerMenuItem);
// Create help menu
JMenu helpMenu = new JMenu("Help");
helpMenuItem = new JMenuItem("View help");
helpMenuItem.addActionListener(this);
helpMenu.add(helpMenuItem);
// Complete menu bar
menuBar.add(fileMenu);
menuBar.add(settingsMenu);
menuBar.add(helpMenu);
// Create Panels
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel buttonPanel = new JPanel(new GridLayout(3, 1));
// Build title panel
titlePanel.add(new JLabel("Steganographer " + Steganographer.version));
// Build button panel
JPanel encodePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
encodeButton = new JButton("Encode");
encodeButton.addActionListener(this);
encodePanel.add(encodeButton);
buttonPanel.add(encodePanel);
JPanel decodePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
decodeButton = new JButton("Decode");
decodeButton.addActionListener(this);
decodePanel.add(decodeButton);
buttonPanel.add(decodePanel);
JPanel analyzePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
analyzeButton = new JButton("Analyze");
analyzeButton.addActionListener(this);
analyzePanel.add(analyzeButton);
buttonPanel.add(analyzePanel);
// Finish
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
}
window.setJMenuBar(menuBar);
mainPanel.add(titlePanel, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.CENTER);
window.add(mainPanel);
window.setVisible(true);
}
private void initialize() {
// Load help file
try {
InputStream in = getClass().getResourceAsStream(File.separator + "help.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out = new StringBuilder();
String newLine = System.getProperty("line.separator");
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
out.append(newLine);
}
helpText = out.toString();
} catch (IOException e) {
helpText = "Help documentation not found.";
}
}
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if (obj.equals(encodeButton) || obj.equals(encodeMenuItem)) {
new EncodePopup();
} else if (obj.equals(decodeButton) || obj.equals(decodeMenuItem)) {
new DecodePopup();
} else if (obj.equals(analyzeButton) || obj.equals(analyzeMenuItem)) {
new AnalyzePopup();
} else if (obj.equals(exitMenuItem)) {
exit();
} else if (obj.equals(markerMenuItem)) {
String marker = JOptionPane.showInputDialog(null, "Enter a new marker:\n(Current marker = \"" + Steganographer.getMarker() + "\")");
if (marker != null) {
Steganographer.setMarker(marker);
}
} else if (obj.equals(helpMenuItem)) {
JOptionPane.showMessageDialog(null, helpText);
}
}
public void windowActivated(WindowEvent arg0) {
}
public void windowClosed(WindowEvent arg0) {
}
public void windowClosing(WindowEvent arg0) {
exit();
}
public void windowDeactivated(WindowEvent arg0) {
}
public void windowDeiconified(WindowEvent arg0) {
}
public void windowIconified(WindowEvent arg0) {
}
public void windowOpened(WindowEvent arg0) {
}
public void exit() {
if (Steganographer.getWorkerCount() == 0 || JOptionPane.showConfirmDialog(null, "One or more encode/decode operations are currently running.\nBy closing this window, all of these operations will be stopped.\nAre you sure you want to exit?", "Close?", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
window.setVisible(false);
System.exit(0);
}
}
}