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