-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyzePopup.java
More file actions
214 lines (182 loc) · 6.25 KB
/
Copy pathAnalyzePopup.java
File metadata and controls
214 lines (182 loc) · 6.25 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import javax.imageio.ImageIO;
// By: Justin Spedding & Andrew Miller
public class AnalyzePopup implements ActionListener, PropertyChangeListener, WindowListener {
private JFrame window; // Main window
private ImagePanel hostImage; // Image panel
private JButton analyzeButton, cancelButton; // Buttons
private JTextField hostImageText; // Text boxes
private JButton hostImageBrowse; // File path browse buttons
private String hostImagePath; // Remember info while decoding
private JLabel statusText; // Status label
private JProgressBar progressBar; // Progress bar
private AnalyzeWorker analyzeWorker; // Worker thread
private boolean workerBusy; // Is the worker busy?
public AnalyzePopup() {
buildWindow();
initialize();
}
/**
* Create a popup window for analysis
*/
private void buildWindow() {
// Create window
window = new JFrame("Analyze");
window.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
window.addWindowListener(this);
window.setSize(800,600);
window.setMinimumSize(new Dimension(500, 400));
window.setLocationRelativeTo(null);
// Create panels
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel imagePanel = new JPanel(new GridLayout(1, 1));
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new BoxLayout(controlPanel, BoxLayout.Y_AXIS));
JPanel statusPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel hostImageSelectorPanel = new JPanel(new BorderLayout());
JPanel progressBarPanel = new JPanel(new GridLayout(1, 1));
// Build host image panel
JPanel hostImagePanel = new JPanel(new BorderLayout());
JLabel hostImageLabel = new JLabel("Host Image");
hostImageLabel.setHorizontalAlignment(SwingConstants.CENTER);
hostImagePanel.add(hostImageLabel, BorderLayout.NORTH);
hostImage = new ImagePanel();
hostImage.loadText("No host image loaded");
hostImagePanel.add(hostImage, BorderLayout.CENTER);
imagePanel.add(hostImagePanel);
// Build the status panel
statusText = new JLabel("Press 'Analyze' to analyze the host image.");
statusPanel.add(statusText);
controlPanel.add(statusPanel);
// Build the button panel
analyzeButton = new JButton("Analyze");
analyzeButton.addActionListener(this);
buttonPanel.add(analyzeButton);
cancelButton = new JButton("Cancel");
cancelButton.addActionListener(this);
buttonPanel.add(cancelButton);
controlPanel.add(buttonPanel);
// Build the host image selector panel
hostImageSelectorPanel.add(new JLabel("Host Image: "), BorderLayout.WEST);
hostImageText = new JTextField();
hostImageSelectorPanel.add(hostImageText, BorderLayout.CENTER);
hostImageBrowse = new JButton("Browse");
hostImageBrowse.addActionListener(this);
hostImageSelectorPanel.add(hostImageBrowse, BorderLayout.EAST);
controlPanel.add(hostImageSelectorPanel);
// Build the progress bar panel
progressBar = new JProgressBar();
progressBar.setString("");
progressBar.setStringPainted(true);
progressBarPanel.add(progressBar);
controlPanel.add(progressBarPanel);
// Finish
mainPanel.add(imagePanel, BorderLayout.CENTER);
mainPanel.add(controlPanel, BorderLayout.SOUTH);
window.add(mainPanel);
window.setVisible(true);
}
private void initialize() {
hostImagePath = "";
workerBusy = false;
}
private void updateStatus() {
if (!hostImageText.getText().equals(hostImagePath)) {
hostImagePath = hostImageText.getText(); // Update the host image path
statusText.setText("Press 'Analyze' to analyze the host image.");
try {
hostImage.loadImage(ImageIO.read(new File(hostImagePath)));
} catch (IOException e) {
hostImage.loadText("Invalid host image path.");
}
}
}
public void actionPerformed(ActionEvent event) {
Object obj = event.getSource();
if (obj.equals(analyzeButton)) {
if (!workerBusy) {
updateStatus();
try {
analyzeWorker = new AnalyzeWorker(hostImagePath); // Create a worker thread for analysis
analyzeWorker.addPropertyChangeListener(this);
workerBusy = true;
statusText.setText("Analyzing...");
analyzeWorker.execute();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Invalid host image path.");
}
} else {
printBusyMessage();
}
} else if (obj.equals(cancelButton)) {
exit();
} else if (obj.equals(hostImageBrowse)) {
if (!workerBusy) {
String temp = FileBrowsers.browseHostImage();
if (temp != null) {
hostImageText.setText(temp);
updateStatus();
}
} else {
printBusyMessage();
}
}
}
public void propertyChange(PropertyChangeEvent event) {
try {
int progress = (Integer) event.getNewValue();
progressBar.setValue(progress);
progressBar.setString("Analyzing: " + progress + "%");
if (progress == 100 && !analyzeWorker.isCancelled()) {
workerBusy = false;
progressBar.setString("Done!");
try {
if (analyzeWorker.get()) {
statusText.setText("This image most likely DOES contain a stego.");
} else {
statusText.setText("This image most likely does NOT contain a stego.");
}
} catch (InterruptedException | ExecutionException | CancellationException e) {
progressBar.setString("Operation Failed!");
statusText.setText("Internal error: Something unusual went wrong...");
}
}
} catch (ClassCastException e) {
// Do nothing
}
}
private void printBusyMessage() {
JOptionPane.showMessageDialog(null, "Please wait for the current process to complete.");
}
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
exit();
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
private void exit() {
if(analyzeWorker != null && !analyzeWorker.isDone()) {
analyzeWorker.cancel(true);
}
window.setVisible(false);
window.dispose();
}
}