/*
*
* Panbox - encryption for cloud storage
* Copyright (C) 2014-2015 by Fraunhofer SIT and Sirrix AG
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additonally, third party code may be provided with notices and open source
* licenses from communities and third parties that govern the use of those
* portions, and any licenses granted hereunder do not alter any rights and
* obligations you may have under such open source licenses, however, the
* disclaimer of warranty and limitation of liability provisions of the GPLv3
* will apply to all the product.
*
*/
package org.panbox.desktop.common.gui;
import org.panbox.Settings;
import javax.swing.*;
import java.awt.*;
import java.util.ResourceBundle;
public class PasswordEnterDialog extends javax.swing.JDialog {
private static ResourceBundle bundle;
private static final long serialVersionUID = -7407536332662595384L;
public static enum PermissionType {
USER, DEVICE, DEVICE_FILESLAVE, SHARE
};
public static char[] invoke(PermissionType permissionType) {
bundle = ResourceBundle.getBundle(
"org.panbox.desktop.common.gui.Messages", Settings
.getInstance().getLocale());
String message = null;
switch (permissionType) {
case USER:
message = bundle.getString("passwordenterdialog.message.user");
break;
case DEVICE:
message = bundle.getString("passwordenterdialog.message.device");
break;
case DEVICE_FILESLAVE:
message = bundle.getString("passwordenterdialog.message.device.slavefile");
return invoke(message, false);
case SHARE:
message = bundle.getString("passwordenterdialog.message.share");
break;
default:
message = "";
break;
}
return invoke(message, true);
}
public static char[] invoke(String message, boolean showIdentityMessage) {
GraphicsEnvironment.getLocalGraphicsEnvironment();
if (GraphicsEnvironment.isHeadless()) {
return "test".toCharArray();
} else {
PasswordEnterDialog dialog = new PasswordEnterDialog(message, showIdentityMessage);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
return dialog.password;
}
}
private char[] password;
/**
* Creates ll new form PasswordEnterDialog
*/
public PasswordEnterDialog(String message, boolean showIdentityMessage) {
super((JFrame) null, true);
initComponents();
messageLabel.setText(message);
if(!showIdentityMessage) {
passwordLabel.setVisible(false);
}
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed"
// <editor-fold defaultstate="collapsed"
// desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
passwordLabel = new javax.swing.JLabel();
jPanel1 = new javax.swing.JPanel();
okButton = new javax.swing.JButton();
messageLabel = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
passwordField = new javax.swing.JPasswordField();
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
setTitle(bundle.getString("PasswordEnterDialog.title")); // NOI18N
setPreferredSize(null);
passwordLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
passwordLabel.setText(bundle.getString("client.passwordenter.message")); // NOI18N
okButton.setText(bundle.getString("PasswordEnterDialog.okButton.text")); // NOI18N
okButton.setMaximumSize(new java.awt.Dimension(75, 23));
okButton.setMinimumSize(new java.awt.Dimension(75, 23));
okButton.setPreferredSize(new java.awt.Dimension(75, 23));
okButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okButtonActionPerformed(evt);
}
});
jPanel1.add(okButton);
messageLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
messageLabel.setText(bundle
.getString("PasswordEnterDialog.messageLabel.text")); // NOI18N
passwordField.setMinimumSize(new java.awt.Dimension(250, 19));
passwordField.setPreferredSize(new java.awt.Dimension(250, 19));
passwordField.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
passwordFieldActionPerformed(evt);
}
});
jPanel2.add(passwordField);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
jPanel1,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(
jPanel2,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(
messageLabel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(
passwordLabel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addContainerGap()));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addContainerGap()
.addComponent(messageLabel)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(passwordLabel)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
.addComponent(jPanel2,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)));
pack();
}// </editor-fold>//GEN-END:initComponents
private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_okButtonActionPerformed
password = passwordField.getPassword();
dispose();
}// GEN-LAST:event_okButtonActionPerformed
private void passwordFieldActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_passwordFieldActionPerformed
okButtonActionPerformed(evt);
}// GEN-LAST:event_passwordFieldActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JLabel messageLabel;
private javax.swing.JButton okButton;
private javax.swing.JPasswordField passwordField;
private javax.swing.JLabel passwordLabel;
// End of variables declaration//GEN-END:variables
}