Coverage Report - net.sf.webphotos.gui.util.Login
 
Classes in this File Line Coverage Branch Coverage Complexity
Login
83%
52/62
50%
6/12
1,615
Login$1
87%
7/8
50%
1/2
1,615
Login$2
100%
6/6
N/A
1,615
 
 1  
 /**
 2  
  * Copyright 2008 WebPhotos
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package net.sf.webphotos.gui.util;
 17  
 
 18  
 import java.awt.Container;
 19  
 import java.awt.Frame;
 20  
 import java.awt.event.ActionEvent;
 21  
 import java.awt.event.ActionListener;
 22  
 import javax.swing.JButton;
 23  
 import javax.swing.JDialog;
 24  
 import javax.swing.JLabel;
 25  
 import javax.swing.JOptionPane;
 26  
 import javax.swing.JPasswordField;
 27  
 import javax.swing.JTextField;
 28  
 import net.sf.webphotos.util.Util;
 29  
 
 30  
 /**
 31  
  * Responsável pelas funções relacionadas ao login do usuário.
 32  
  * monta uma tela modal para armazenamento de usuário e senha
 33  
  */
 34  54
 public final class Login {
 35  
 
 36  3
     private static final Login instanciaLogin = new Login();
 37  
     private String usuario;
 38  
     private char[] senha;
 39  12
     private boolean cancelado = false;
 40  
     // elementos GUI
 41  12
     private JLabel lblNome = new JLabel("Nome:");
 42  12
     private JTextField txtNome = new JTextField(8);
 43  12
     private JLabel lblSenha = new JLabel("Senha:");
 44  12
     private JPasswordField txtSenha = new JPasswordField(8);
 45  12
     private JButton btOk = new JButton("Ok");
 46  12
     private JButton btCancelar = new JButton("Cancelar");
 47  
     private static JDialog telaLogin;
 48  
 
 49  
     /**
 50  
      * Construtor da classe. Recebe dois valores e usa um deles para setar o
 51  
      * título da janela. Logo após, chama o método startLogin().
 52  
      *
 53  
      * @param title Object para setar o título.
 54  
      */
 55  0
     public Login(Object title) {
 56  0
         telaLogin.setTitle((String) title);
 57  0
         this.startLogin();
 58  0
     }
 59  
 
 60  
     /**
 61  
      * Construtor da classe. Apenas chama o método startLogin().
 62  
      */
 63  12
     public Login() {
 64  12
         this.startLogin();
 65  12
     }
 66  
 
 67  
     /**
 68  
      * Apresenta tela modal de login, solicitando usuário e senha. Trabalha os
 69  
      * botões ok e cancelar. Checa se o nome do usuário foi preenchido. Desenha
 70  
      * a tela de login, usando coordenadas. Configura a caixa de diálogo e
 71  
      * apresenta.
 72  
      */
 73  
     public void startLogin() {
 74  12
         JDialog.setDefaultLookAndFeelDecorated(true);
 75  12
         telaLogin = new JDialog((Frame) null, "Identificação", true);
 76  12
         telaLogin.setResizable(false);
 77  
 
 78  
         // botoes Ok, Cancelar
 79  12
         btOk.addActionListener(new ActionListener() {
 80  
             @Override
 81  
             public void actionPerformed(ActionEvent e) {
 82  
                 // caso não tenha preenchido o nome, continua pedindo...
 83  6
                 if (txtNome.getText().length() == 0) {
 84  0
                     return;
 85  
                 }
 86  6
                 usuario = txtNome.getText();
 87  6
                 senha = txtSenha.getPassword();
 88  6
                 telaLogin.setVisible(false);
 89  6
                 cancelado = false;
 90  6
             }
 91  
         });
 92  
 
 93  
         // clicando em Cancelar, seta o usuário null e retorna
 94  12
         btCancelar.addActionListener(new ActionListener() {
 95  
             @Override
 96  
             public void actionPerformed(ActionEvent e) {
 97  3
                 usuario = null;
 98  3
                 senha = null;
 99  3
                 telaLogin.setVisible(false);
 100  3
                 cancelado = true;
 101  3
             }
 102  
         });
 103  
 
 104  
         // desenha a tela de login, usando coordenadas
 105  12
         telaLogin.setSize(200, 123);
 106  12
         Container cp = telaLogin.getContentPane();
 107  12
         cp.setLayout(null);
 108  
 
 109  12
         cp.add(lblNome);
 110  12
         cp.add(txtNome);
 111  12
         cp.add(lblSenha);
 112  12
         cp.add(txtSenha);
 113  
 
 114  12
         cp.add(btCancelar);
 115  12
         cp.add(btOk);
 116  
 
 117  12
         lblNome.setBounds(8, 8, 40, 20);
 118  12
         txtNome.setBounds(55, 8, 129, 20);
 119  12
         lblSenha.setBounds(8, 34, 40, 20);
 120  12
         txtSenha.setBounds(55, 34, 129, 20);
 121  
 
 122  12
         btCancelar.setBounds(8, 60, 88, 25);
 123  12
         btOk.setBounds(104, 60, 80, 25);
 124  
 
 125  
         // configura a caixa de diálogo e apresenta
 126  12
         telaLogin.getRootPane().setDefaultButton(btOk);
 127  12
         telaLogin.setLocationRelativeTo(null);
 128  
 
 129  12
     }
 130  
 
 131  
     /**
 132  
      * Seta o título da tela de login e retorna a instância da classe.
 133  
      *
 134  
      * @param titulo Título da tela.
 135  
      * @return Retorna a instância de Login.
 136  
      */
 137  
     public static Login getLogin(String titulo) {
 138  0
         telaLogin.setTitle(titulo);
 139  0
         return instanciaLogin;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Retorna a instância da classe.
 144  
      *
 145  
      * @return Retorna a instância de Login.
 146  
      */
 147  
     public static Login getLogin() {
 148  0
         return instanciaLogin;
 149  
     }
 150  
 
 151  
     /**
 152  
      * Retorna a variável cancelado.
 153  
      *
 154  
      * @return Retorna uma variável lógica.
 155  
      */
 156  
     public boolean cancelado() {
 157  0
         return cancelado;
 158  
     }
 159  
 
 160  
     /**
 161  
      * Retorna o usuário de login.
 162  
      *
 163  
      * @return Retorna um usuário.
 164  
      */
 165  
     public String getUser() {
 166  9
         return usuario;
 167  
     }
 168  
 
 169  
     /**
 170  
      * Retorna a senha de login.
 171  
      *
 172  
      * @return Retorna uma senha.
 173  
      */
 174  
     public char[] getPassword() {
 175  9
         return senha;
 176  
     }
 177  
 
 178  
     /**
 179  
      * Mostra a tela sem anexar uma mensagem de erro.
 180  
      */
 181  
     public void show() {
 182  9
         show("");
 183  9
     }
 184  
 
 185  
     /**
 186  
      * Mostra a tela anexando uma mensagem de erro.
 187  
      *
 188  
      * @param msgErro Mensagem de erro.
 189  
      */
 190  
     public void show(String msgErro) {
 191  9
         if (msgErro != null && !msgErro.isEmpty()) {
 192  0
             JOptionPane.showMessageDialog(telaLogin, msgErro, "Erro", JOptionPane.ERROR_MESSAGE);
 193  
         }
 194  9
         txtSenha.setText("");
 195  9
         txtSenha.setCaretPosition(0);
 196  
 
 197  9
         String tempLogin = Util.getConfig().getString("autoPreencher.Login");
 198  9
         String tempSenha = Util.getConfig().getString("autoPreencher.Pass");
 199  
 
 200  
         /**
 201  
          * preenche com usuario se cadastrado
 202  
          */
 203  9
         if (tempLogin.length() > 0 && !(txtNome.getText().length() > 0)) {
 204  9
             txtNome.setText(tempLogin);
 205  
         }
 206  
 
 207  
         /**
 208  
          * Preenche com senha se cadastrado e usuário for o mesmo que o
 209  
          * autopreenchimento
 210  
          */
 211  9
         if (tempSenha.length() > 0
 212  
                 && txtNome.getText().equals(tempLogin)) {
 213  
 
 214  9
             txtSenha.setText(tempSenha);
 215  
         }
 216  
 
 217  9
         usuario = null;
 218  9
         senha = null;
 219  9
         telaLogin.setVisible(true);
 220  9
     }
 221  
 
 222  
     /**
 223  
      * Retorna a tela de login.
 224  
      *
 225  
      * @return Retorna uma tela Dialog.
 226  
      */
 227  
     public static JDialog getTelaLogin() {
 228  0
         return telaLogin;
 229  
     }
 230  
 }