Package it.freedomotic.jfrontend.utils

Source Code of it.freedomotic.jfrontend.utils.SplashLogin

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package it.freedomotic.jfrontend.utils;

import it.freedomotic.api.API;
import it.freedomotic.jfrontend.JavaDesktopFrontend;
import it.freedomotic.util.I18n.I18n;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

/**
*
* @author Matteo Mazzoni <matteo@bestmazzo.it>
*/
public class SplashLogin extends javax.swing.JFrame {

    private API api;
    private I18n I18n;
    private JavaDesktopFrontend master;

    public SplashLogin(JavaDesktopFrontend jd) {
        this.master = jd;
        this.api = jd.getApi();
        this.I18n = api.getI18n();
        initComponents();
        logo.setIcon(new javax.swing.ImageIcon(api.getResource("Freedomotic_noBack.png")));
        // center window on screen
        setLocationRelativeTo(null);

        // as a tip for first login attempts
        setDefaultLoginData();
        setVisible(true);
    }

    @Deprecated
    private void setDefaultLoginData() {
        username.setText("admin");
        password.setText("admin");
    }

    public void trySSO() {
        username.setEnabled(false);
        password.setEnabled(false);
        msgBox.setText(I18n.msg("trying_sso_msg"));

        ActionListener taskPerformer = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                if (api.getConfig().getBooleanProperty("KEY_ENABLE_SSO", false) &&
                    api.getAuth().bindFakeUser(System.getProperty("user.name"))) {
                    master.createMainWindow();
                } else {
                    msgBox.setText(api.getI18n().msg("login_disclaimer"));
                    username.setEnabled(true);
                    password.setEnabled(true);
                }
            }
        };
        Timer doSSO = new Timer(3000, taskPerformer);
        doSSO.setRepeats(false);
        doSSO.start();
    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnLogin = new javax.swing.JButton();
        username = new javax.swing.JTextField();
        password = new javax.swing.JPasswordField();
        logo = new javax.swing.JLabel();
        loginLbl = new javax.swing.JLabel();
        msgBox = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setUndecorated(true);
        setResizable(false);

        btnLogin.setText(I18n .msg("login"));
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        username.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                usernameActionPerformed(evt);
            }
        });

        logo.setToolTipText("");
        logo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        loginLbl.setText(I18n.msg("login_msg"));

        msgBox.setForeground(new java.awt.Color(255, 0, 0));

        jLabel1.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N
        jLabel1.setText("Opensource Building Automation");

        jLabel2.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(207, 7, 7));
        jLabel2.setText("DATA MASHUP BUILDING AUTOMATION FRAMEWORK");

        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(msgBox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(loginLbl)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(username))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnLogin)
                        .addGap(6, 6, 6))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(logo, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                        .addGap(56, 56, 56))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, 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(logo, javax.swing.GroupLayout.DEFAULT_SIZE, 68, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(msgBox, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(loginLbl)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnLogin))
                .addContainerGap())
        );

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

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        // TODO add your handling code here:
        boolean loginSuccessfull = api.getAuth().login(username.getText(), password.getPassword());
        if (loginSuccessfull) {
            master.createMainWindow();
        } else {
            username.setText(null);
            username.setEnabled(false);

            password.setText(null);
            password.setEnabled(false);
            msgBox.setText(I18n.msg("incorrect_login_msg"));

            ActionListener taskPerformer = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    setDefaultLoginData();
                    msgBox.setText(api.getI18n().msg("login_disclaimer"));
                    username.setEnabled(true);
                    password.setEnabled(true);
                }
            };
            Timer doWaitForNewLogin = new Timer(4000, taskPerformer);
            doWaitForNewLogin.setRepeats(false);
            doWaitForNewLogin.start();
        }
    }//GEN-LAST:event_btnLoginActionPerformed

    private void usernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_usernameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_usernameActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel loginLbl;
    private javax.swing.JLabel logo;
    private javax.swing.JLabel msgBox;
    private javax.swing.JPasswordField password;
    private javax.swing.JTextField username;
    // End of variables declaration//GEN-END:variables
}
TOP

Related Classes of it.freedomotic.jfrontend.utils.SplashLogin

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.