001: package com.calipso.reportgenerator.usermanager;
002:
003: import com.calipso.reportgenerator.common.LanguageTraslator;
004: import com.calipso.reportgenerator.common.ReportGeneratorConfiguration;
005: import com.calipso.reportgenerator.common.InfoException;
006:
007: import javax.swing.*;
008: import java.awt.event.ActionListener;
009: import java.awt.event.ActionEvent;
010: import java.awt.*;
011: import java.io.File;
012:
013: /**
014: *
015: * User: eslosse
016: * Date: 22-sep-2005
017: * Time: 11:57:38
018: *
019: */
020:
021: /**
022: * Representa el Frame para validacion de usuarios
023: */
024:
025: public class UserManagerLoginFrame extends JDialog implements
026: ActionListener {
027:
028: protected JPasswordField pfUser;
029: private static JTextField tfName;
030: private JButton btAccept;
031: private JButton btCancel;
032: private String defaultUserName;
033: private boolean hasCanceled;
034: private File file;
035: private ReportGeneratorConfiguration reportGeneratorConfiguration;
036: private UserManager userManager;
037:
038: /**
039: * Crea una instancia de <code>UserLoginFrame</code>
040: * @param owner
041: * @param title
042: * @param modal
043: * @param defaultUserName
044: * @throws java.awt.HeadlessException
045: */
046: public UserManagerLoginFrame(Frame owner, String title,
047: boolean modal, String defaultUserName,
048: ReportGeneratorConfiguration reportGeneratorConfiguration,
049: UserManager userManager) throws HeadlessException,
050: InfoException {
051:
052: super (owner, title, modal);
053: this .reportGeneratorConfiguration = reportGeneratorConfiguration;
054: this .userManager = userManager;
055: file = new File(reportGeneratorConfiguration
056: .getUsersRepositoryPath());
057:
058: Image icon = reportGeneratorConfiguration.getImage("ICON");
059: if (icon != null && owner != null) {
060: owner.setIconImage(icon);
061: }
062: this .defaultUserName = defaultUserName;
063: hasCanceled = false;
064: createComponents();
065: }
066:
067: /**
068: * Crea los componentes graficos
069: */
070: protected void createComponents() {
071: getContentPane().setLayout(new BorderLayout());
072: getContentPane().add(createNorthPanel(), BorderLayout.CENTER);
073: getContentPane().add(createSouthPanel(), BorderLayout.SOUTH);
074: java.awt.Dimension screenSize = java.awt.Toolkit
075: .getDefaultToolkit().getScreenSize();
076: setLocation((screenSize.width - 250) / 2,
077: (screenSize.height - 200) / 2);
078: setSize(new Dimension(250, 110));
079: //show();
080: }
081:
082: /**
083: * Crea el panel del sur
084: * @return
085: */
086: protected JComponent createSouthPanel() {
087: FlowLayout layout = new FlowLayout(FlowLayout.RIGHT);
088: JPanel pnlSouth = new JPanel(layout);
089: btAccept = new JButton(LanguageTraslator.traslate("112"));
090: btAccept.addActionListener(this );
091: btCancel = new JButton(LanguageTraslator.traslate("113"));
092: btCancel.addActionListener(this );
093: pnlSouth.add(btAccept);
094: pnlSouth.add(btCancel);
095: return pnlSouth;
096: }
097:
098: /**
099: * Crea el panel del norte
100: * @return
101: */
102: protected JComponent createNorthPanel() {
103: JPanel pnlNorth = new JPanel(new GridLayout(2, 2));
104: JLabel label = new JLabel(LanguageTraslator.traslate("232"));
105: tfName = new JTextField(defaultUserName);
106: tfName.setEditable(false);
107: pnlNorth.add(label);
108: pnlNorth.add(tfName);
109: label = new JLabel("Password");
110: pfUser = new JPasswordField();
111: pnlNorth.add(label);
112: pnlNorth.add(pfUser);
113: return pnlNorth;
114: }
115:
116: /**
117: * Retorna valor que representa si el usuario
118: * ha cancelado la validacion
119: * @return
120: */
121: public boolean getHasCanceled() {
122: return hasCanceled;
123: }
124:
125: /**
126: * Devuelve el nombre de usuario
127: * @return
128: */
129: public static String getUserName() {
130: return tfName.getText().trim();
131: }
132:
133: /**
134: * Retorna el campo para ingresar el password
135: * @return
136: */
137: public JPasswordField getPfUser() {
138: return pfUser;
139: }
140:
141: /**
142: * Devuelve el boton accept del panel
143: * @return
144: */
145: protected JButton getBtAccept() {
146: return btAccept;
147: }
148:
149: /**
150: * Devuelve la contrasena de usuario.
151: * @return
152: */
153: public String getUserPasswd() {
154: return new String(pfUser.getPassword());
155: }
156:
157: /**
158: * Metodo que devuelve si la validacion ha sido o no
159: * satisfactoria en caso de que el usuario no
160: * haya cancelado la validacion.
161: * @return
162: */
163: public boolean login() throws InfoException {
164: boolean result = false;
165: int intentos = 0;
166: if (!file.exists()) {
167: throw new InfoException(LanguageTraslator.traslate("19"));
168: }
169: for (int i = 0; i < 3; i++) {
170: setVisible(true);
171: if (this .getHasCanceled()) {
172: return false;
173: }
174: String userName = this .getUserName();
175: String userPassword = this .getUserPasswd();
176: result = userManager.validate(this .getUserName(), this
177: .getUserPasswd());
178: if (result == false) {
179: intentos++;
180: JOptionPane.showMessageDialog(this , LanguageTraslator
181: .traslate("451"));
182: } else {
183: return true;
184: }
185: }
186: if (intentos == 3) {
187: JOptionPane.showMessageDialog(this , LanguageTraslator
188: .traslate("300"));
189: }
190: return result;
191: }
192:
193: public void actionPerformed(ActionEvent e) {
194: if (e.getSource() == btAccept) {
195: dispose();
196: } else {
197: hasCanceled = true;
198: dispose();
199: }
200: }
201: }
|