/*
 * WANDORA
 * Knowledge Extraction, Management, and Publishing Application
 * http://wandora.org
 * 
 * Copyright (C) 2004-2023 Wandora Team
 * 
 * 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/>.
 *
 * 
 * PasswordPrompt.java
 *
 * Created on July 19, 2004, 12:41 PM
 */

package org.wandora.application.gui;



import javax.swing.Icon;

import org.wandora.application.gui.simple.SimpleButton;
import org.wandora.application.gui.simple.SimpleField;
import org.wandora.application.gui.simple.SimpleLabel;




/**
 *
 * @author  olli, ak
 */
public class PasswordPrompt extends javax.swing.JDialog {
    


	private static final long serialVersionUID = 1L;

	
	private boolean cancelled;
    public static Icon lockIcon = UIBox.getIcon("gui/icons/dialog/lock.gif");
    
    
    /** Creates new form PasswordPrompt */
    public PasswordPrompt(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        cancelled=false;
        
        if(parent.isVisible()) {
            this.setLocation(parent.getLocation().x+parent.getWidth()/2-this.getWidth()/2, 
                             parent.getLocation().y+parent.getHeight()/2-this.getHeight()/2);
        }
        else {
            UIBox.centerScreen(this);
        }
        this.setAlwaysOnTop(true);
    }
    
    
    public void setLabelText(String str) {
        this.passwordInfoLabel.setText(str);
    }
    
    
    /** 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" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        fieldPanel = new javax.swing.JPanel();
        passwordFieldPanel = new javax.swing.JPanel();
        usernameLabel = new SimpleLabel();
        passwordLabel2 = new SimpleLabel();
        usernameField = new SimpleField();
        passwordField = new javax.swing.JPasswordField();
        passwordLabelPanel = new javax.swing.JPanel();
        passwordInfoLabel = new SimpleLabel();
        iconPanel = new javax.swing.JPanel();
        iconLabel = new javax.swing.JLabel();
        passwordButtonPanel = new javax.swing.JPanel();
        passwordDummyPanel = new javax.swing.JPanel();
        okButton = new SimpleButton();
        cancelButton = new SimpleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        fieldPanel.setLayout(new java.awt.GridBagLayout());

        passwordFieldPanel.setMinimumSize(new java.awt.Dimension(300, 50));
        passwordFieldPanel.setLayout(new java.awt.GridBagLayout());

        usernameLabel.setText("Username");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 2, 0);
        passwordFieldPanel.add(usernameLabel, gridBagConstraints);

        passwordLabel2.setText("Password");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        passwordFieldPanel.add(passwordLabel2, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 2, 0);
        passwordFieldPanel.add(usernameField, gridBagConstraints);

        passwordField.setMargin(new java.awt.Insets(2, 2, 2, 3));
        passwordField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                passwordFieldKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        passwordFieldPanel.add(passwordField, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 0);
        fieldPanel.add(passwordFieldPanel, gridBagConstraints);

        passwordLabelPanel.setMinimumSize(new java.awt.Dimension(220, 20));
        passwordLabelPanel.setPreferredSize(new java.awt.Dimension(220, 20));
        passwordLabelPanel.setLayout(new java.awt.BorderLayout());

        passwordInfoLabel.setText("Enter username and password to access URL:");
        passwordInfoLabel.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        passwordInfoLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        passwordLabelPanel.add(passwordInfoLabel, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 0, 0);
        fieldPanel.add(passwordLabelPanel, gridBagConstraints);

        iconLabel.setMaximumSize(new java.awt.Dimension(65, 65));
        iconLabel.setMinimumSize(new java.awt.Dimension(65, 65));
        iconLabel.setPreferredSize(new java.awt.Dimension(65, 65));
        iconLabel.setIcon(lockIcon);
        iconPanel.add(iconLabel);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 0);
        fieldPanel.add(iconPanel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(fieldPanel, gridBagConstraints);

        passwordButtonPanel.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        passwordButtonPanel.add(passwordDummyPanel, gridBagConstraints);

        okButton.setText("OK");
        okButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        okButton.setPreferredSize(new java.awt.Dimension(70, 25));
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 3);
        passwordButtonPanel.add(okButton, gridBagConstraints);

        cancelButton.setText("Cancel");
        cancelButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        cancelButton.setPreferredSize(new java.awt.Dimension(70, 25));
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        passwordButtonPanel.add(cancelButton, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = java.awt.GridBagConstraints.RELATIVE;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(passwordButtonPanel, gridBagConstraints);

        setBounds(0, 0, 437, 187);
    }// </editor-fold>//GEN-END:initComponents

    private void passwordFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordFieldKeyPressed
        if(evt.getKeyCode()==evt.VK_ENTER){
            cancelled=false;
            this.setVisible(false);
        }
}//GEN-LAST:event_passwordFieldKeyPressed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        cancelled=false;
        this.setVisible(false);
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        cancelled=true;
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel fieldPanel;
    private javax.swing.JLabel iconLabel;
    private javax.swing.JPanel iconPanel;
    private javax.swing.JButton okButton;
    private javax.swing.JPanel passwordButtonPanel;
    private javax.swing.JPanel passwordDummyPanel;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JPanel passwordFieldPanel;
    private javax.swing.JLabel passwordInfoLabel;
    private javax.swing.JLabel passwordLabel2;
    private javax.swing.JPanel passwordLabelPanel;
    private javax.swing.JTextField usernameField;
    private javax.swing.JLabel usernameLabel;
    // End of variables declaration//GEN-END:variables
    
    public String getUsername(){
        return usernameField.getText();
    }
    public char[] getPassword(){
        return passwordField.getPassword();
    }
    public boolean wasCancelled(){
        return cancelled;
    }
}
