/*
 * LoginDialog.java
 *
 * Created on 03.03.2009, 10:24:46
 */
package view.forms;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import org.jdesktop.application.Action;

import control.MainApplication;
import control.UserManager;

/**
 * Anmeldedialog
 * Wird am Start der Anwedung und nach dem Ausloggen angezeigt.
 *
 * @author Maike, Paul
 */
public class LoginDialog extends javax.swing.JDialog {

	private static final long serialVersionUID = -95164198609553597L;
	
	/** Erstellt einen neuen LoginDialog */
    public LoginDialog() {
        // Systemmodal, s. JavaDoc von ModalityType
        super(null, ModalityType.TOOLKIT_MODAL);
        
        initComponents();

        // Systemzentriert anzeigen
        setLocationRelativeTo(null);
    }

    /** Öffnet das Registrierungsformular. */
    @Action
    public void openRegisterForm() {
        JDialog regForm = new RegisterForm(this, this);
        regForm.setLocationRelativeTo(this);
        regForm.setVisible(true);
    }

    /** Aktion, die ausgefügt wird, wenn login-Button betätigt wird. */
    @Action
    public void loginAction() {
        UserManager um = UserManager.getInstance();
        um.login(txtUsername.getText(), txtPassword.getPassword());

        if (um.isLoggedin()) {
            dispose();
        } else {
            txtPassword.setText("");
            JOptionPane.showMessageDialog(this, "Login fehlgeschlagen",
                    "Fehlermeldung", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    /**
     * Schließt das Fenster.
     * Wird das Fenster geschlossen und es ist kein Benutzer eingeloggt, so
     * wird die Anwendung beendet.
     */
    @Override
    public void dispose() {
        super.dispose();

        if (!UserManager.getInstance().isLoggedin()) {
            MainApplication.getApplication().quit(null);
        }
    }

    /** Resetet die Eingaben, da Dialog mehrmals benutzt werden kann. */
    public void reset() {
        txtPassword.setText("");
    }

    /** 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.
     */
    private void initComponents() {

        loginlPanel = new javax.swing.JPanel();
        labelUsername = new javax.swing.JLabel();
        labelPassword = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();
        regPanel = new javax.swing.JPanel();
        btnRegister = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(control.MainApplication.class).getContext().getResourceMap(LoginDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        loginlPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("loginlPanel.border.title"))); // NOI18N
        loginlPanel.setName("loginlPanel"); // NOI18N

        labelUsername.setText(resourceMap.getString("labelUsername.text")); // NOI18N
        labelUsername.setName("labelUsername"); // NOI18N

        labelPassword.setText(resourceMap.getString("labelPassword.text")); // NOI18N
        labelPassword.setName("labelPassword"); // NOI18N

        txtUsername.setText(resourceMap.getString("txtUsername.text")); // NOI18N
        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(control.MainApplication.class).getContext().getActionMap(LoginDialog.class, this);
        txtUsername.setAction(actionMap.get("loginAction")); // NOI18N
        txtUsername.setName("txtUsername"); // NOI18N

        txtPassword.setText(resourceMap.getString("txtPassword.text")); // NOI18N
        txtPassword.setAction(actionMap.get("loginAction")); // NOI18N
        txtPassword.setName("txtPassword"); // NOI18N

        btnLogin.setAction(actionMap.get("loginAction")); // NOI18N
        btnLogin.setText(resourceMap.getString("btnLogin.text")); // NOI18N
        btnLogin.setName("btnLogin"); // NOI18N

        javax.swing.GroupLayout loginlPanelLayout = new javax.swing.GroupLayout(loginlPanel);
        loginlPanel.setLayout(loginlPanelLayout);
        loginlPanelLayout.setHorizontalGroup(
            loginlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginlPanelLayout.createSequentialGroup()
                .addGroup(loginlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(loginlPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(loginlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelUsername)
                            .addComponent(labelPassword))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(loginlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(txtPassword, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtUsername, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, loginlPanelLayout.createSequentialGroup()
                        .addGap(87, 87, 87)
                        .addComponent(btnLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 144, Short.MAX_VALUE)))
                .addContainerGap())
        );
        loginlPanelLayout.setVerticalGroup(
            loginlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(loginlPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(loginlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelUsername)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(loginlPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPassword)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
                .addComponent(btnLogin)
                .addContainerGap())
        );

        regPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("regPanel.border.title"))); // NOI18N
        regPanel.setName("regPanel"); // NOI18N

        btnRegister.setAction(actionMap.get("openRegisterForm")); // NOI18N
        btnRegister.setText(resourceMap.getString("btnRegister.text")); // NOI18N
        btnRegister.setName("btnRegister"); // NOI18N

        javax.swing.GroupLayout regPanelLayout = new javax.swing.GroupLayout(regPanel);
        regPanel.setLayout(regPanelLayout);
        regPanelLayout.setHorizontalGroup(
            regPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, regPanelLayout.createSequentialGroup()
                .addContainerGap(88, Short.MAX_VALUE)
                .addComponent(btnRegister, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        regPanelLayout.setVerticalGroup(
            regPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(regPanelLayout.createSequentialGroup()
                .addComponent(btnRegister)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(regPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(loginlPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(loginlPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(regPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnRegister;
    private javax.swing.JLabel labelPassword;
    private javax.swing.JLabel labelUsername;
    private javax.swing.JPanel loginlPanel;
    private javax.swing.JPanel regPanel;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
