package com.talixa.steganos.frames;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import com.talixa.steganos.filters.BitMapFilter;
import com.talixa.steganos.filters.JpegFilter;
import com.talixa.steganos.filters.PngFilter;
import com.talixa.steganos.helpers.StegoEncoder;
import com.talixa.steganos.helpers.StegoOptions;
import com.talixa.steganos.helpers.StegoOptions.BitInversion;
import com.talixa.steganos.helpers.StegoOptions.BitReversal;
import com.talixa.steganos.helpers.StegoOptions.Encoding;
import com.talixa.steganos.shared.SteganosConstants;
import com.talixa.steganos.widgets.ImagePanel;
public class FrameStegoOptions {
private static final int WIDTH = 400;
private static final int HEIGHT = 200;
private static File selectedFile = null;
public static void createAndShowGUI(final ImagePanel imagePanel, final JScrollPane scrollPane, final JTextArea lblText, final boolean isFileLoad) {
// reset selected file
selectedFile = null;
// Create frame
final JFrame frame = new JFrame(isFileLoad ? SteganosConstants.TITLE_OPEN_FILE : SteganosConstants.TITLE_SAVE_FILE);
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// panel outputfile
JPanel fileDataPanel = new JPanel(new FlowLayout());
final JLabel fileLabel = new JLabel(SteganosConstants.NO_FILE_SELECTED);
JButton selectFileButton = new JButton(isFileLoad ? SteganosConstants.SELECT_INPUT_FILE : SteganosConstants.SELECT_OUTPUT_FILE);
selectFileButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
int result;
if (isFileLoad) {
fc.addChoosableFileFilter(new BitMapFilter());
fc.addChoosableFileFilter(new JpegFilter());
fc.addChoosableFileFilter(new PngFilter());
result = fc.showOpenDialog(frame);
} else {
fc.setFileFilter(new PngFilter());
result = fc.showSaveDialog(frame);
}
if (result == JFileChooser.APPROVE_OPTION) {
selectedFile = fc.getSelectedFile();
fileLabel.setText(selectedFile.getName());
}
}
});
fileDataPanel.add(selectFileButton);
fileDataPanel.add(fileLabel);
// encoding panel
JPanel encodingPanel = new JPanel(new FlowLayout());
JLabel encodingLabel = new JLabel("Encoding: ");
final JRadioButton radioAscii8 = new JRadioButton("Ascii 8");
final JRadioButton radioAscii7 = new JRadioButton("Ascii 7");
final JRadioButton radioEbcidic = new JRadioButton("Ebcidic");
ButtonGroup bgEncoding = new ButtonGroup();
bgEncoding.add(radioAscii8);
bgEncoding.add(radioAscii7);
bgEncoding.add(radioEbcidic);
radioEbcidic.setEnabled(false);
bgEncoding.setSelected(radioAscii8.getModel(), true);
encodingPanel.add(encodingLabel);
encodingPanel.add(radioAscii8);
encodingPanel.add(radioAscii7);
encodingPanel.add(radioEbcidic);
// inversion panel
JPanel inversionPanel = new JPanel(new FlowLayout());
JLabel inversionLabel = new JLabel("Inversion: ");
final JRadioButton radioInversionOff = new JRadioButton("Off");
final JRadioButton radioInversionOn = new JRadioButton("On");
final JRadioButton radioInversionEveryOther = new JRadioButton("Every Other Bit");
ButtonGroup bgInversion = new ButtonGroup();
bgInversion.add(radioInversionOff);
bgInversion.add(radioInversionOn);
bgInversion.add(radioInversionEveryOther);
bgInversion.setSelected(radioInversionOff.getModel(), true);
inversionPanel.add(inversionLabel);
inversionPanel.add(radioInversionOff);
inversionPanel.add(radioInversionOn);
inversionPanel.add(radioInversionEveryOther);
// reversal panel
JPanel reversalPanel = new JPanel(new FlowLayout());
JLabel reversalLabel = new JLabel("Reversal: ");
final JRadioButton radioReversalOff = new JRadioButton("Off");
final JRadioButton radioReversalOn = new JRadioButton("On");
ButtonGroup bgReversal = new ButtonGroup();
bgReversal.add(radioReversalOff);
bgReversal.add(radioReversalOn);
bgReversal.setSelected(radioReversalOff.getModel(), true);
reversalPanel.add(reversalLabel);
reversalPanel.add(radioReversalOff);
reversalPanel.add(radioReversalOn);
// put panels together
JPanel mainPanel = new JPanel(new GridLayout(4, 1));
mainPanel.add(fileDataPanel);
mainPanel.add(encodingPanel);
mainPanel.add(inversionPanel);
mainPanel.add(reversalPanel);
JButton okButton = new JButton(SteganosConstants.LABEL_OK);
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (selectedFile != null) {
StegoOptions.Encoding encoding;
if (radioAscii7.isSelected()) {
encoding = Encoding.ASCII7;
} else if (radioAscii8.isSelected()) {
encoding = Encoding.ASCII8;
} else {
encoding = Encoding.EBCIDIC;
}
// get inversion option
StegoOptions.BitInversion inversion;
if (radioInversionOn.isSelected()) {
inversion = BitInversion.ON;
} else if (radioInversionOff.isSelected()) {
inversion = BitInversion.OFF;
} else {
inversion = BitInversion.EVERY_OTHER;
}
// get reversal options
StegoOptions.BitReversal reversal;
if (radioReversalOff.isSelected()) {
reversal = BitReversal.OFF;
} else {
reversal = BitReversal.ON;
}
// set options
StegoOptions opts = new StegoOptions(encoding, inversion, reversal);
if (isFileLoad) {
try {
imagePanel.setImage(selectedFile);
scrollPane.setPreferredSize(imagePanel.getPreferredSize());
scrollPane.updateUI(); // force scroll bars to appear if necessary
String message = StegoEncoder.retrieveStringFromImage(imagePanel.getImage(), opts);
lblText.setText(message);
lblText.setCaretPosition(0);
frame.dispose();
} catch (IOException exception) {
JOptionPane.showMessageDialog(frame, SteganosConstants.ERROR_FILE_LOAD);
}
} else { // save file
String text = lblText.getText();
try {
StegoEncoder.createStegoImage(imagePanel.getImage(), text, selectedFile.getAbsolutePath(), opts);
frame.dispose();
} catch (IOException e1) {
JOptionPane.showMessageDialog(frame, SteganosConstants.ERROR_FILE_SAVE);
}
}
}
}
});
// Add to frame
frame.add(mainPanel, BorderLayout.CENTER);
frame.add(okButton, BorderLayout.SOUTH);
// Set location and display
Dimension screenSize = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
frame.setPreferredSize(new Dimension(WIDTH,HEIGHT));
int left = (screenSize.width/2) - (WIDTH/2);
int top = (screenSize.height/2) - (HEIGHT/2);
frame.pack();
frame.setLocation(left,top);
frame.setVisible(true);
}
}