001: package com.calipso.reportgenerator.userinterface;
002:
003: import com.calipso.reportgenerator.common.InfoException;
004: import com.calipso.reportgenerator.common.ShowExceptionMessageDialog;
005:
006: import javax.swing.*;
007: import java.awt.*;
008: import java.awt.event.ActionListener;
009: import java.awt.event.ActionEvent;
010:
011: /**
012: * Diálogo para validar el usuario
013: */
014: public class LoginFrame extends JDialog {
015:
016: private JPanel mainPanel;
017: private JPanel southPanel;
018: private JPanel centerPanel;
019: private JLabel userNameLabel;
020: private JTextField userNameText;
021: private JLabel passwordLabel;
022: private JPasswordField passwordText;
023: private JButton okButton;
024: private JButton cancelButton;
025:
026: private LoginValidator validator;
027: private int loginResult;
028: private boolean cancelled;
029:
030: /**
031: * Inicializa el
032: * @param defaultUserName
033: * @param validator
034: */
035: public LoginFrame(String defaultUserName, LoginValidator validator) {
036: setModal(true);
037: createControls();
038: setSize(240, 110);
039: java.awt.Dimension screenSize = java.awt.Toolkit
040: .getDefaultToolkit().getScreenSize();
041: setLocation((screenSize.width - 240) / 2,
042: (screenSize.height - 110) / 2);
043: setTitle(com.calipso.reportgenerator.common.LanguageTraslator
044: .traslate("296"));
045: this .validator = validator;
046: loginResult = -1;
047: getUserNameText().setText(defaultUserName);
048: }
049:
050: public LoginValidator getValidator() {
051: return validator;
052: }
053:
054: public void setValidator(LoginValidator validator) {
055: this .validator = validator;
056: }
057:
058: public int getLoginResult() {
059: return loginResult;
060: }
061:
062: public void setLoginResult(int loginResult) {
063: this .loginResult = loginResult;
064: }
065:
066: private void createControls() {
067: getContentPane().add(createMainPanel());
068: }
069:
070: private Component createMainPanel() {
071: mainPanel = new JPanel(new BorderLayout());
072: mainPanel.add(createCenterPanel(), BorderLayout.CENTER);
073: mainPanel.add(createSouthPanel(), BorderLayout.SOUTH);
074: return mainPanel;
075: }
076:
077: private Component createSouthPanel() {
078: FlowLayout layout = new FlowLayout(FlowLayout.RIGHT);
079: southPanel = new JPanel(layout);
080: okButton = new JButton(
081: com.calipso.reportgenerator.common.LanguageTraslator
082: .traslate("112"));
083: cancelButton = new JButton(
084: com.calipso.reportgenerator.common.LanguageTraslator
085: .traslate("113"));
086: okButton.addActionListener(new ActionListener() {
087: public void actionPerformed(ActionEvent e) {
088: try {
089: validateLogin();
090: } catch (Exception er) {
091: ShowExceptionMessageDialog
092: .initExceptionDialogMessage(
093: com.calipso.reportgenerator.common.LanguageTraslator
094: .traslate("295"), er);
095: }
096: setVisible(false);
097: }
098: });
099:
100: cancelButton.addActionListener(new ActionListener() {
101: public void actionPerformed(ActionEvent e) {
102: cancelLogin();
103: }
104: });
105:
106: southPanel.add(okButton);
107: southPanel.add(cancelButton);
108: return southPanel;
109: }
110:
111: private void cancelLogin() {
112: loginResult = -1;
113: cancelled = true;
114: setVisible(false);
115: }
116:
117: private void validateLogin() throws InfoException {
118: loginResult = getValidator().validate(getUserName(),
119: getPassword());
120: }
121:
122: private String getPassword() {
123: return new String(passwordText.getPassword());
124: }
125:
126: private String getUserName() {
127: return userNameText.getText();
128: }
129:
130: private Component createCenterPanel() {
131: centerPanel = new JPanel(new GridLayout(2, 2));
132: userNameLabel = new JLabel(
133: com.calipso.reportgenerator.common.LanguageTraslator
134: .traslate("297"));
135: userNameText = new JTextField();
136: passwordLabel = new JLabel(
137: com.calipso.reportgenerator.common.LanguageTraslator
138: .traslate("298"));
139: passwordText = new JPasswordField();
140:
141: centerPanel.add(userNameLabel);
142: centerPanel.add(userNameText);
143: centerPanel.add(passwordLabel);
144: centerPanel.add(passwordText);
145:
146: return centerPanel;
147: }
148:
149: public JTextField getUserNameText() {
150: return userNameText;
151: }
152:
153: public boolean login() {
154: setVisible(true);
155: return connected();
156: }
157:
158: private boolean connected() {
159: return getValidator().getConnected();
160: }
161:
162: private boolean cancelled() {
163: return cancelled;
164: }
165:
166: public static void login(ReportLoginValidator validator)
167: throws InfoException {
168: login("", validator);
169: }
170:
171: public static void login(String defaultUserName,
172: LoginValidator validator) throws InfoException {
173: LoginFrame loginFrame = new LoginFrame(defaultUserName,
174: validator);
175: if (!loginFrame.login()) {
176: if (loginFrame.cancelled()) {
177: throw new InfoException(
178: com.calipso.reportgenerator.common.LanguageTraslator
179: .traslate("299"));
180: } else {
181: throw new InfoException(
182: com.calipso.reportgenerator.common.LanguageTraslator
183: .traslate("300"));
184: }
185: }
186: }
187: }
|