package cologne.eck.all_peas.gui;
/*
* Peafactory - Production of Password Encryption Archives
* Copyright (C) 2015 Axel von dem Bruch
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* See: http://www.gnu.org/licenses/gpl-2.0.html
* You should have received a copy of the GNU General Public License
* along with this library.
*/
/**
* View of the pea dialog to type the password.
*/
import java.awt.*;
import java.awt.event.*;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.ResourceBundle;
import javax.accessibility.AccessibleContext;
import javax.imageio.ImageIO;
import javax.swing.*;
import settings.PeaSettings;
import cologne.eck.all_peas.control.PeaControl;
import cologne.eck.all_peas.data.Attachments;
import cologne.eck.all_peas.data.PeaProperties;
import cologne.eck.all_peas.files.FileTypePanel;
import cologne.eck.peafactory.crypto.CipherStuff;
import cologne.eck.peafactory.crypto.HashStuff;
import cologne.eck.peafactory.crypto.kdf.KeyDerivation;
import cologne.eck.tools.EntropyPool;
import cologne.eck.tools.KeyRandomCollector;
import cologne.eck.tools.MouseRandomCollector;
import cologne.eck.tools.Zeroizer;
/*
* View of NotesControl, CalendarControl, FileControl,
* ImageControl
*/
@SuppressWarnings("serial")
public class PswDialogView extends JDialog implements WindowListener,
ActionListener, KeyListener {
private static final Color plainPeaColor = new Color(222, 244, 224);//(242, 255, 243);//(229, 255, 231);
private static final Color cipherPeaColor = new Color(183, 229, 187);//(206, 229, 208);
private static final Color peaColor = new Color(218, 242, 220);//Farbton 125,Saettigung 10, Wert 95
//private static final Color errorColor = new Color(252, 194, 171);
/**
* The used font size in menus and dialogs
*/
private static int fontSize = 12;
// screen:
private static final Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
private static final double screenWidth = screenSize.getWidth();
private static final double screenHeight = screenSize.getHeight();
// label above password field (if undefined: type password...)
private static JLabel pswLabel;
// password field
private static JPasswordField pswField;
// Button to start decryption
private static JButton okButton;
// used for notebook/image: displays all available, valid files to decrypt
// private static JPanel availableFilesPanel;
// button group for available files
// private static ButtonGroup fileGroup;
// indicates the type of the PEA (text, file, image...)
// private static String fileTypex = "";
// remember the selected files? -> store in path file
// only for file type
private static JCheckBox rememberCheck;
// static instance
private static PswDialogView dialog;
// display warnings and error messages
private static JLabel errorMessageLabel;
// status/progress
//private static JLabel statusLabel;
// thread while typing the password - still not used
private static WhileTypingThread t;
// pea image icon for frames
private static Image image;
// language support (set in PeaControl)
private static ResourceBundle languagesBundle;
// to check if decryption was startet by ok-button
private static boolean started = false;
// if started first time (no password, no content)
// can be set manually in menu
private static boolean initializing = false;
private PswDialogView() {
// start the EntropyPool:
EntropyPool.getInstance().updateThread(System.nanoTime());
languagesBundle = PeaProperties.getBundle();
// fileType = PeaProperties.getFileType();
dialog = this;
this.setTitle(PeaSettings.getJarFileName() );
//this.setBackground(peaColor);
image = IconManager.loadIcon("pea-lock.png",
PeaProperties.getBundle().getString("pea_with_keyhole"),
null,
PeaProperties.getBundle().getString("logo_of_pea") ).getImage();
this.setIconImage(image);
this.addWindowListener(this);
this.addMouseMotionListener(new MouseRandomCollector() );
JPanel topPanel = (JPanel) this.getContentPane();
topPanel.setBorder(PeaBorderFactory.createBorder(true));//threeBorder);
topPanel.addMouseMotionListener(new MouseRandomCollector() );
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
loadComponents(topPanel, true);
this.setLocation(100,30);
pack();
}
public final static PswDialogView getInstance() {
if (dialog == null) {
dialog = new PswDialogView();
} else {
//return null
}
return dialog;
}
public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessiblePswDialogView();
}
return accessibleContext;
}
public final void displayErrorMessages(String topic, boolean error) {
setMessage("###" + topic + ":\n", error);
setMessage(CipherStuff.getErrorMessage(), error);
PswDialogView.clearPassword();
}
@Override
public void keyPressed(KeyEvent kpe) {
if (kpe.getKeyChar() == KeyEvent.VK_ENTER && initializing == false){
// ok button
okButton.doClick();
} else if (kpe.isControlDown()) {
if (errorMessageLabel.getText().equals(PeaProperties.getBundle().getString("password_failed"))){
errorMessageLabel.setText(" ");
}
if (kpe.getKeyCode() == KeyEvent.VK_T ) {
CharTable table = new CharTable(this, pswField,0);
table.setVisible(true);
} else if (kpe.getKeyCode() == KeyEvent.VK_K) {
Keyboard keyboard = new Keyboard(this, pswField);
keyboard.setVisible(true);
} else if (kpe.getKeyCode() == KeyEvent.VK_F1 ) {
CharTable table = new CharTable(this, pswField, 0);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F2 ) {
CharTable table = new CharTable(this, pswField, 1);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F3 ) {
CharTable table = new CharTable(this, pswField, 2);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F4 ) {
CharTable table = new CharTable(this, pswField, 3);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F5 ) {
CharTable table = new CharTable(this, pswField, 4);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F6 ) {
CharTable table = new CharTable(this, pswField, 5);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F7 ) {
CharTable table = new CharTable(this, pswField, 6);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F8 ) {
CharTable table = new CharTable(this, pswField, 7);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F9 ) {
CharTable table = new CharTable(this, pswField, 8);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F10 ) {
CharTable table = new CharTable(this, pswField, 9);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F11 ) {
CharTable table = new CharTable(this, pswField, 10);
table.setVisible(true);
}else if (kpe.getKeyCode() == KeyEvent.VK_F12 ) {
CharTable table = new CharTable(this, pswField, 11);
table.setVisible(true);
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {}
@Override
public void keyTyped(KeyEvent arg0) {}
@Override
public void actionPerformed(ActionEvent ape) {
String command = ape.getActionCommand();
//System.out.println("command: " + command);
if(command.equals("open")){
//System.out.println("open files");
PeaControl.getDialog().openEncryptedFiles();
} else if (command.equals("init")){
//System.out.println("initialize");
// PeaProperties.setInsideJar(false);
initialize();
} else if (command.startsWith("FILE")){
String fileName = command.substring(4, command.length());
//System.out.println("selected file: " + fileName);
// use the file inside the jar archive (resources/text.lock)
/* if (fileName.equals(languagesBundle.getString("image_inside_jar_archive"))){
PeaProperties.setInsideJar(true);
} else {
PeaProperties.setInsideJar(false);
} */
PeaSettings.setExternalFilePath(fileName);
// PeaControl.setEncryptedFileName(fileName);
} else if (command.equals("quit")){
System.out.println("Exit");
System.exit(0);
} else if(command.equals("keyboard")){
// clear error messages from previous password errors
if (errorMessageLabel.getText().equals(PeaProperties.getBundle().getString("password_failed"))){
errorMessageLabel.setText(" ");
}
Keyboard keyboard = new Keyboard(this, pswField);
keyboard.setVisible(true);
//PeaSettings.getKeyboard().setVisible(true);
} else if (command.equals("charTable1")){
// clear error messages from previous password errors
if (errorMessageLabel.getText().equals(PeaProperties.getBundle().getString("password_failed"))){
errorMessageLabel.setText(" ");
}
CharTable table = new CharTable(this, pswField,0);
table.setVisible(true);
/* } else if (command.equals("selectAll")){
if (availableFilesPanel != null){
JCheckBox button = (JCheckBox) ape.getSource();
Component[] comps = availableFilesPanel.getComponents();
if (comps != null) {
int len = comps.length;
boolean selected = button.isSelected();
for (int i = 0; i < len; i++) {
if (comps[i] instanceof JCheckBox){
((JCheckBox)comps[i]).setSelected(selected);
}
}
}
} */
} else if (command.equals("ok")){
// reset error message
errorMessageLabel.setText("");
try { // always reset the cursor
this.setCursor(CursorManager.getWaitCursor());//Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
/* if (fileType.equals("text file") || (fileType.equals("image"))) {
Component[] checkBoxes = availableFilesPanel.getComponents();
// clear fileNames:
PeaControl.setAvailableFileNames(new ArrayList<String>());
ArrayList <String> newFileNames = new ArrayList<String>();
for (int i = 0; i < checkBoxes.length; i++) {
JCheckBox check = (JCheckBox) availableFilesPanel.getComponent(i);
if (check.isSelected()){
//System.out.println("ok selected file: " + check.getText());
newFileNames.add(check.getText());
//PeaControl.addAvailableFileName(check.getText());
}
}
// sort new list:
Collections.sort(newFileNames);
// set as avai��able file names:
PeaControl.setAvailableFileNames(newFileNames);
}*/
boolean rememberPath = false;
//if (fileType.equals("file") && rememberCheck != null) {
if (rememberCheck != null) {
if ( rememberCheck.isSelected() == true){
rememberPath = true;
}
}
PeaControl.getDialog().startExecution(rememberPath);
} finally {
// always reset the cursor:
this.setCursor(CursorManager.getDefaultCursor());
}
/* } else if (command.startsWith("lfs")){
System.out.println(command.substring(3, command.length()));
try {
UIManager.setLookAndFeel(command.substring(3, command.length()));
} catch (Exception e) {
System.err.println("Manually changing of Look&Feel failed for \n"
+ UIManager.getSystemLookAndFeelClassName() + "\n"
+ e.toString() + ", " + e.getMessage());
}
System.out.println(UIManager.getLookAndFeel().getName());
SwingUtilities.updateComponentTreeUI(this);
this.pack(); */
}
else {
System.err.println("Invalid command: " + command);
}
}
@Override
public void windowActivated(WindowEvent arg0) {}
@Override
public void windowClosed(WindowEvent arg0) { // dispose
this.dispose();
}
@Override
public void windowClosing(WindowEvent arg0) { // x
if (started == true) {
return;
} else {
// if attacker has access to memory but not to file
if (Attachments.getNonce() != null) {
Zeroizer.zero(Attachments.getNonce());
}
if (PeaControl.getDialog() != null) {
PeaControl.getDialog().clearSecretValues();
}
System.exit(0);
}
}
@Override
public void windowDeactivated(WindowEvent arg0) {}
@Override
public void windowDeiconified(WindowEvent arg0) {}
@Override
public void windowIconified(WindowEvent arg0) {}
@Override
public void windowOpened(WindowEvent arg0) {
// do some expensive computations while user tries to remember the password
t = this.new WhileTypingThread();
t.setPriority( Thread.MAX_PRIORITY );// 10
t.start();
if (initializing == false) {
pswField.requestFocus();
}
}
//=======================================
// Helper Functions
public final static void setUI(boolean encrypted) {
// UIManager.put("OptionPane.messageFont", new Font("System", Font.PLAIN, 14));
/* FontUIResource f = new FontUIResource(Font.SANS_SERIF, Font.PLAIN, fontSize );
java.util.Enumeration<Object> keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof javax.swing.plaf.FontUIResource) {
UIManager.put(key, f);
}
} */
/* if (UIManager.getSystemLookAndFeelClassName().contains("GTK")){
// do not set
} else { */
Color mainColor;
if (encrypted == true) {
mainColor = cipherPeaColor;
} else {
mainColor = plainPeaColor;
}
try {
// for backward compatibility: In Java 6 Nimbus package was renamed
UIManager.LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
int len = infos.length;
for (int i = 0; i < len; i++) {
if ("Nimbus".equals(infos[i].getName())) {
UIManager.setLookAndFeel(infos[i].getClassName());
// Set the primary colors:
UIManager.put("control", mainColor);//peaColor);
UIManager.put("info", new Color(242,242,189));
UIManager.put("nimbusAlertYellow", new Color(255,220,35));
UIManager.put("nimbusBase", new Color(76, 127, 80));//new Color(255,255,255));// new Color(51, 102, 55));
UIManager.put("nimbusDisabledText", new Color(142,143,145));
UIManager.put("nimbusFocus",new Color(102, 127, 104));//Color.RED);// new Color(114, 229, 124));
UIManager.put("nimbusGreen", new Color(157, 178, 158));
UIManager.put("nimbusInfoBlue", new Color(33, 165, 44));
UIManager.put("nimbusLightBackground", new Color(255,255,255));
UIManager.put("nimbusOrange", new Color(191,98,4));//(148, 178, 0));
UIManager.put("nimbusRed", new Color(169,46,34));
UIManager.put("nimbusSelectedText", new Color(255,255,255));
UIManager.put("nimbusSelectionBackground", new Color(76, 153, 82));//(74, 165, 82));
UIManager.put("text", new Color(0,0,0));
// correction:
UIManager.put("MenuItem.opaque", true); // peaColor background
UIManager.put("Panel.opaque", true); // peaColor background
//UIManager.put("Button.contentMargins", new Insets(0,0,0,0));
break;
}
}
} catch (Exception e) {
System.err.println("Look&Feel Nimbus first try failed");
try {
// works for debian: package name for Nimbus was changed
// Bug JDK-6647622
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
System.err.println("Package name for Nimbus renamed, start with new packege name...");
UIManager.put("control", peaColor);
} catch (Exception e2) {
System.err.println("Setting of Look&Feel failed: \n"
+ UIManager.getSystemLookAndFeelClassName() + "\n"
+ e.toString() + ", " + e.getMessage());
// set Metal Look&Feel
try {
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
} catch (Exception e1) {
System.err.println("Setting of Look&Feel failed: \n"
+ UIManager.getSystemLookAndFeelClassName() + "\n"
+ e.toString() + ", " + e.getMessage());
}
}
}
}
private void loadComponents(JPanel topPanel, boolean updateFiles) {
JPanel menuPanel = new JPanel();
menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.X_AXIS));
menuPanel.setMinimumSize(new Dimension(300, 40));
menuPanel.setMaximumSize(new Dimension((int)getScreenwidth(), 30));
PeaMenuBar menuBar = new PeaMenuBar();
JMenu menu = new JMenu(languagesBundle.getString("file"));
// menu.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
// menu.setMnemonic(KeyEvent.VK_M);
menu.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("file_mnemonic")).getKeyCode());
menuBar.add(menu);
JMenuItem openItem = new JMenuItem(languagesBundle.getString("open_encrypted_file"));
openItem.setActionCommand("open");
// openItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//openItem.setMnemonic(KeyEvent.VK_O);
openItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("open_mnemonic")).getKeyCode());
openItem.addActionListener(dialog);
menu.add(openItem);
JMenuItem initItem = new JMenuItem(languagesBundle.getString("init_new_file"));
initItem.setActionCommand("init");
// initItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//initItem.setMnemonic(KeyEvent.VK_I);
initItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("init_mnemonic")).getKeyCode());
initItem.addActionListener(dialog);
menu.add(initItem);
JMenuItem quitItem = new JMenuItem(languagesBundle.getString("quit"));//"Quit");
quitItem.setActionCommand("quit");
// quitItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//quitItem.setMnemonic(KeyEvent.VK_Q);
quitItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("quit_mnemonic")).getKeyCode());
quitItem.addActionListener(dialog);
menu.add(quitItem);
JMenu extrasMenu = new JMenu(languagesBundle.getString("extras"));
// extrasMenu.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//extrasMenu.setMnemonic(KeyEvent.VK_M);
extrasMenu.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("extras_mnemonic")).getKeyCode());
menuBar.add(extrasMenu);
JMenuItem charTableItem = new JMenuItem(languagesBundle.getString("char_table") + " (CTRL + T) / (CTRL + F1...)");
charTableItem.setActionCommand("charTable1");
// charTableItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//charTableItem.setMnemonic(KeyEvent.VK_I);
charTableItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("char_table_mnemonic")).getKeyCode());
charTableItem.addActionListener(dialog);
extrasMenu.add(charTableItem);
JMenuItem keyboardItem = new JMenuItem(languagesBundle.getString("keyboard") + " (CTRL + K)");
keyboardItem.setActionCommand("keyboard");
// keyboardItem.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
//keyboardItem.setMnemonic(KeyEvent.VK_I);
keyboardItem.setMnemonic(KeyStroke.getKeyStroke(languagesBundle.getString("keyboard_mnemonic")).getKeyCode());
keyboardItem.addActionListener(dialog);
extrasMenu.add(keyboardItem);
/* JMenu lookAndFeelMenu = new JMenu("Look&Feel");
lookAndFeelMenu.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
//help.setMnemonic(KeyEvent.VK_H);
lookAndFeelMenu.setMnemonic(KeyStroke.getKeyStroke("L").getKeyCode());
extrasMenu.add(lookAndFeelMenu);
LookAndFeelInfo[] lfs = UIManager.getInstalledLookAndFeels();
// List<String> lookAndFeelsDisplay = new ArrayList<>();
// List<String> lookAndFeelsRealNames = new ArrayList<>();
int lfsLen = lfs.length;
for (int i = 0; i < lfsLen; i++) {
JMenuItem item = new JMenuItem(lfs[i].getName());
item.addActionListener(PswDialogView.getInstance());
item.setActionCommand("lfs" + lfs[i].getClassName());
lookAndFeelMenu.add(item);
} */
// if (PeaProperties.getFileType().equals("file")){
JMenu viewMenu = new ViewMenu((FileTypePanel)PeaProperties.getTypePanel(), false);// plainModus false
menuBar.add(viewMenu);
// }
JMenu helpMenu = new HelpMenu();
menuBar.add(helpMenu);
menuPanel.add(menuBar);
menuPanel.add(Box.createHorizontalGlue());
topPanel.add(menuPanel);
topPanel.add(Box.createVerticalStrut(5));
JPanel errorMessagePanel = new JPanel();
errorMessagePanel.setLayout(new BoxLayout(errorMessagePanel, BoxLayout.X_AXIS));
errorMessageLabel = new JLabel(" ");
errorMessageLabel.setForeground(Color.RED);
errorMessagePanel.add(errorMessageLabel);
errorMessagePanel.add(Box.createHorizontalGlue());
topPanel.add(errorMessagePanel);
topPanel.add(Box.createVerticalStrut(5));
okButton = new JButton(languagesBundle.getString("ok"));
okButton.setPreferredSize(new Dimension( 60, 30));
okButton.setActionCommand("ok");
okButton.addActionListener(dialog);
// if there is no password, this might be the only
// chance to start EntropyPool
okButton.addMouseMotionListener(new MouseRandomCollector() );
if (initializing == false) {
if (PeaProperties.getWorkingMode().equals("-t")) { // test mode
JLabel rescueLabel = new JLabel("TEST MODE");
topPanel.add(rescueLabel);
try { //redirect stderr to file
System.setErr(new PrintStream(new FileOutputStream("error_log.txt")));
// print some informations to stderr:
System.err.println(System.getProperty("os.name") + "\n"
+ "Java " + System.getProperty("java.version") + "\n"
+ "PEA type: " + PeaProperties.getFileType() + ", version: " + PeaControl.getDialog().getVersion() );//VERSION);
System.err.println(KeyDerivation.getKdf().getName() + ", "
+ CipherStuff.getCipherAlgo().getAlgorithmName()
+ ", " + HashStuff.getHashAlgo().getAlgorithmName());
} catch (Exception e) {
System.err.println("Redirection of stderr failed: " + e.toString() + ", " + e.getMessage());
}
if (PeaProperties.getFileType().equals("image")) { // test mode
String names[] = ImageIO.getReaderFormatNames();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < names.length; ++i) {
builder.append("\"" + names[i] + "\",");
//System.out.println(names[i]);
}
//System.out.println(builder.substring(0, builder.lastIndexOf(",")));
System.err.println("Supported image formats: " + builder.substring(0, builder.lastIndexOf(",")));// delete the last ','
}
} else if (PeaProperties.getWorkingMode().equals("-r")) { // rescue mode
JLabel rescueLabel = new JLabel(languagesBundle.getString("rescue_mode"));
rescueLabel.setForeground(Color.RED);
topPanel.add(rescueLabel);
}
// if (fileType.equals("file") ) {
JPanel filePanel = (JPanel) PeaProperties.getTypePanel();
filePanel.addMouseMotionListener(new MouseRandomCollector() );
topPanel.add(Box.createVerticalStrut(10));
topPanel.add(filePanel);
/* }
else {
if(availableFilesPanel == null){
availableFilesPanel = new JPanel();
availableFilesPanel.setLayout(new BoxLayout(availableFilesPanel, BoxLayout.Y_AXIS));
if ( ! ( fileType.equals("text file") || fileType.equals("image") ) ){ // not editor
fileGroup = new ButtonGroup();
}
}
topPanel.add(availableFilesPanel);
if (updateFiles == true) {
PeaControl.searchSingleFile();
}
} */
topPanel.add(Box.createVerticalStrut(5));
/* if (fileType.equals("text file") || fileType.equals("image")) {
JPanel togglePanel = new JPanel();
togglePanel.setLayout(new BoxLayout(togglePanel, BoxLayout.X_AXIS));
JCheckBox selectAll = new JCheckBox(languagesBundle.getString("all"));
//selectAll.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 10));
selectAll.setMargin(new Insets(1,1,1,1));
selectAll.addActionListener(dialog);
selectAll.setActionCommand("selectAll");
//togglePanel.add(Box.createHorizontalGlue());
togglePanel.add(selectAll);
togglePanel.add(Box.createHorizontalGlue());
topPanel.add(togglePanel);
topPanel.add(Box.createVerticalStrut(5));
}*/
JPanel pswLabelPanel = new JPanel();
pswLabelPanel.addMouseMotionListener(new MouseRandomCollector() );
pswLabelPanel.setLayout(new BoxLayout(pswLabelPanel, BoxLayout.X_AXIS));
topPanel.add(Box.createVerticalStrut(10));
topPanel.add(pswLabelPanel);
if (PeaSettings.getLabelText() == null) {
pswLabel = new JLabel( languagesBundle.getString("enter_password") );
} else {
pswLabel = new JLabel( PeaSettings.getLabelText() );
}
pswLabel.setPreferredSize(new Dimension( 460, 20));
pswLabelPanel.add(pswLabel);
pswLabelPanel.add(Box.createHorizontalGlue());
JPanel pswPanel = new JPanel();
pswPanel.setLayout(new BoxLayout(pswPanel, BoxLayout.X_AXIS));
pswField = new JPasswordField();
pswField.setBackground(new Color(231, 231, 231) );
pswField.setPreferredSize(new Dimension(400, 30));
pswField.setMinimumSize(new Dimension(400, 30));
pswField.setMaximumSize(new Dimension(400, 30));
pswField.addKeyListener(dialog);
pswField.addKeyListener(new KeyRandomCollector() );
pswPanel.add(pswField);
pswPanel.add(okButton);
topPanel.add(pswPanel);
} else {// initializing
//
}
// if (fileType.equals("file")){
JPanel checkRememberPanel = new JPanel();
checkRememberPanel.setLayout(new BoxLayout(checkRememberPanel, BoxLayout.X_AXIS));
rememberCheck = new JCheckBox();
// rememberCheck.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, fontSize));
rememberCheck.setText(languagesBundle.getString("remember_file_names"));//"remember selected file names");
rememberCheck.setToolTipText(languagesBundle.getString("file_storage_info")// "file names will be saved in the file " +
+ " " + PeaSettings.getJarFileName() + ".path");// in current directory");
checkRememberPanel.add(Box.createVerticalStrut(10));
checkRememberPanel.add(rememberCheck);
checkRememberPanel.add(Box.createHorizontalGlue() );
topPanel.add(checkRememberPanel);
// }
}
public final static void updateView(){
/* problems for editor tabs
dialog.getContentPane().removeAll();
loadComponents((JPanel) dialog.getContentPane(), false);
dialog.pack();*/
dialog.getContentPane().invalidate();
dialog.getContentPane().validate();
dialog.invalidate();
dialog.validate();
dialog.pack();
dialog.repaint();
}
/**
* Initialize the PEA with a new password, new salt and new content
*/
private final static void initialize(){
initializing = true;
NewPasswordDialog.setRandomCollector(true);
String hint = null;
if (PeaProperties.getFileType().equals("file")){
hint = PeaProperties.getBundle().getString("hint_initialization");
}
NewPasswordDialog newPswDialog = NewPasswordDialog.getInstance(
dialog, PeaProperties.getBundle().getString("initialization"),
hint, dialog.getLocation());
PeaControl.getDialog().setInitializedPassword( newPswDialog.getDialogInput() );
NewPasswordDialog.setRandomCollector(false);
if (newPswDialog.getDialogInput() == null) {
System.err.println("initialization canceled, program exits");
System.exit(0);
}
PeaControl.getDialog().initializeNewFiles();
// dialog.setVisible(false);
PeaControl.getDialog().startExecution(false);
}
//===============================================
// Getter & Setter
public final static char[] getPassword() {
return pswField.getPassword();
}
protected final static void setPassword(char[] psw) { // for Keyboard
pswField.setText( new String(psw) );
}
protected final static void setPassword(char pswChar) { // for charTable
pswField.setText("" + pswChar );
}
public final static void clearPassword() {
if (pswField != null) {
pswField.setText("");
}
}
public final static void setMessage(String message, boolean error) {
if (error == true) {
errorMessageLabel.setForeground(Color.RED);
} else {
errorMessageLabel.setForeground(Color.BLUE);
}
//System.out.println("PswDialogView - setMessage: " + message);
if (message != null) {
if (message.startsWith("###")){
errorMessageLabel.setText(message.substring(3, message.length()));
} else {
try {
String i18nMessage = languagesBundle.getString(message);
errorMessageLabel.setText(i18nMessage);
} catch (Exception e) {
errorMessageLabel.setText(message);
}
}
}
}
public final static void setLabelText(String labelText) {
if (initializing == true){
pswLabel.setText(labelText);
}
}
/* public final static boolean validFileAvailable() {
if (fileGroup == null){
return false;
}
if (fileGroup.getButtonCount() > 0){
return true;
} else {
return false;
}
} */
/**
* Adds a file name to display as JRadioButton (simple notebook, image)
* or JCheckBox (editor).
*
* @param fileName the file name to add
* @param newSelected set the JRadioButton or JCheckBox selected
*/
/* public final static void addAvailableFile(String fileName, boolean newSelected) {
if (fileType.equals("text file") || fileType.equals("image")) {
// check if fileName is already listed:
if (availableFilesPanel.getComponentCount() > 0) {
int len = availableFilesPanel.getComponentCount();
for (int i = 0; i < len; i++) {
if (((JCheckBox)availableFilesPanel.getComponent(i)).getText().equals(fileName)){
return;
}
}
}
// register file inside jar
if (fileName.equals(languagesBundle.getString("image_inside_jar_archive"))){
PeaProperties.setInsideJar(true);
} else {
PeaProperties.setInsideJar(false);
}
JCheckBox check = new JCheckBox(fileName);
availableFilesPanel.add(check);
check.setSelected(newSelected);
availableFilesPanel.invalidate();
availableFilesPanel.validate();
updateView();
} else {
JPanel fileRadioPanel = new JPanel();
fileRadioPanel.setLayout(new BoxLayout(fileRadioPanel, BoxLayout.X_AXIS));
JRadioButton fileRadioButton = new JRadioButton();
// if first file: select
if (fileGroup.getButtonCount() == 0){
fileRadioButton.setSelected(true);
if (fileName.equals(languagesBundle.getString("image_inside_jar_archive"))){
PeaProperties.setInsideJar(true);
} else {
PeaProperties.setInsideJar(false);
}
}
fileGroup.add(fileRadioButton);
fileRadioButton.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12));
fileRadioButton.setText(fileName);//"remember selected file names");
fileRadioButton.addActionListener(dialog);
fileRadioButton.setActionCommand("FILE" + fileName);
availableFilesPanel.add(Box.createVerticalStrut(3));
fileRadioPanel.add(fileRadioButton);
fileRadioPanel.add(Box.createHorizontalGlue() );
availableFilesPanel.add(fileRadioPanel);
if (newSelected == true) {
fileRadioButton.setSelected(true);
availableFilesPanel.revalidate();
//dialog.revalidate();
dialog.repaint();
dialog.pack();
errorMessageLabel.setText(" ");
}
}
} */
/**
* Show a message dialog with links and email, related to this PEA
*
* @param websiteFolder folder with leading slash after the language,
* that is "/html/xxx_pea.html"
* @param downloadLink full link to download the source code
*/
public final static void showInfoDialog(Window parent){
String websiteFolder = PeaControl.getDialog().getWebsite();
String downloadLink = PeaControl.getDialog().getSourceLink();
String lang = System.getProperty("user.language"); // en, de
if (lang != "de" && lang != "en") {
lang = "en";
}
String infoSite = "http://eck.cologne/peafactory/" + lang + websiteFolder;
JOptionPane.showMessageDialog(parent, //PswDialogView.getView(),
PeaProperties.getBundle().getString("find_web_infos") + ": \n"
+ " " + infoSite + "\n\n"
+ PeaProperties.getBundle().getString("general_infos") + ": \n"
+ " http://eck.cologne/peafactory/" + lang + "/index.html\n\n"
+ PeaProperties.getBundle().getString("find_source_code") + ": \n"
+ " " + downloadLink + "\n\n"
+ PeaProperties.getBundle().getString("contact") + ": \n"
+ " peafactory@eck.cologne\n",
PeaProperties.getBundle().getString("help"),
JOptionPane.INFORMATION_MESSAGE);
}
/**
* Show a dialog with informations about the properties of the current PEA
*
* @param peaType the type or name of the PEA
* @param extraLine an optional extra line for PEA dependent properties
*/
public final static void showAboutDialog(String peaType, String extraLine, Window parent) {
String x = PeaProperties.getBundle().getString("with") + " ";
if (PeaSettings.getKeyFileProperty() == false){
x = PeaProperties.getBundle().getString("without") + " ";
}
if (extraLine == null){
extraLine = " ";
}
JOptionPane.showMessageDialog(
parent,//PswDialogView.getView(),
peaType + " - " + PeaProperties.getBundle().getString("part_of") + " PeaFactory\n"
+ PeaProperties.getBundle().getString("version") + ": " + PeaControl.getDialog().getVersion() + " (" + PeaControl.getDialog().getYearOfPublication() + ")\n" +
x + PeaProperties.getBundle().getString("key_file_property") + "\n" +
extraLine + "\n"
+ PeaProperties.getBundle().getString("used_primitives") + "\n"
+ "Key derivation: " + KeyDerivation.getKdf().getName()
+ ", time parameter: " + KeyDerivation.gettCost()
+ ", memory parameter: " + KeyDerivation.getmCost() + "\n"
+ "Cipher: " + CipherStuff.getCipherAlgo().getAlgorithmName() + "\n"
+ "Mode: " + CipherStuff.getCipherMode().getClass().getSimpleName() + "\n"
+ "Hash Function: " + HashStuff.getHashAlgo().getAlgorithmName(),
null, JOptionPane.PLAIN_MESSAGE);
}
public final static WhileTypingThread getThread() {
return t;
}
public final static Image getImage() {
return image;
}
public final static PswDialogView getView() {
return dialog;
}
public final static Color getPeaColor(){
return peaColor;
}
public final static void setStarted(boolean _started) {
started = _started;
}
/**
* @return the initializing
*/
public static boolean isInitializing() {
return initializing;
}
/**
* @param initializing the initializing to set
*/
public static void setInitializing(boolean initialize) {
PswDialogView.initializing = initialize;
}
/**
* @return the screenwidth
*/
public static double getScreenwidth() {
return screenWidth;
}
/**
* @return the screenheight
*/
public static double getScreenheight() {
return screenHeight;
}
/**
* @return the fontsize
*/
public static int getFontsize() {
return fontSize;
}
public static void setFontSize(int newFontSize) {
fontSize = newFontSize;
}
protected class AccessiblePswDialogView extends AccessibleJDialog {
//Inherit everything, override nothing.
}
//================================================
// inner class
public class WhileTypingThread extends Thread {
// some pre-computations while typing the password
@Override
public void run() {
if (PeaControl.getDialog() != null) {
PeaControl.getDialog().preComputeInThread();
}
}
}
}