summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/gui/LoginBox.java
blob: f813cefdffb0fecbc05bfc4bae078575f524295a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package com.c2kernel.gui;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import com.c2kernel.common.InvalidDataException;
import com.c2kernel.entity.proxy.AgentProxy;
import com.c2kernel.process.Gateway;
import com.c2kernel.utils.Language;
import com.c2kernel.utils.Logger;
import com.c2kernel.utils.Resource;


//import com.borland.jbcl.layout.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public  class LoginBox extends JFrame {
  private int xMov;
  private int yMov;
  public String errorMessage=new String("");
  private int maxNumberLogon;
  public boolean action = false;
  public int loginAttemptNumber= 0;
  JLabel passwordLabel = new JLabel();
  JTextField username = new JTextField();
  JButton OK = new JButton();
  JLabel errorLabel = new JLabel();
  JPasswordField password = new JPasswordField();
  JButton Cancel = new JButton();
  JLabel userLabel = new JLabel();
  ImageIcon imageMainHolder = new ImageIcon();
  JLabel pictureLabel = new JLabel();
  GridBagLayout gridBagLayout1 = new GridBagLayout();
  MainFrame mainFrameFather;
  public  static AgentProxy userAgent;
  private boolean logged;
  private boolean errorSet;

  public LoginBox(int attempt,String title,String lastUser,String bottomMessage,
                  javax.swing.ImageIcon imageHolder,MainFrame mainFrame) {
	String iconFile = Gateway.getProperty("AppIcon");
	if (iconFile != null)
		this.setIconImage(Resource.getImageResource(iconFile).getImage());
    this.errorLabel.setText(bottomMessage);
    if (errorMessage.compareTo("")!=0) this.errorLabel.setText(errorMessage);
    mainFrameFather=mainFrame;
    xMov=imageHolder.getIconWidth()+90;
    yMov=imageHolder.getIconHeight()+40;
    imageMainHolder=imageHolder;
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
    if (attempt==0) maxNumberLogon=5;
    else maxNumberLogon=attempt;
    if (title == null)
      title = "Cristal2";
    title = Language.translate("Log in to ")+title;
    setTitle(title);
    username.setText(lastUser);

  }
//OK button pressed OR Enter Hit
  private void loginClicked(){
    errorSet=false;
    try {
      if (this.getUser().length()>0 && this.getPassword().length()>0)
        userAgent = Gateway.connect(this.getUser(), this.getPassword());
      logged = (userAgent != null);
      Logger.msg(7, "AbstractMain::standardSetUp() - Gateway.connect() OK.");
    }
    catch (InvalidDataException ex) {
      String message = ex.getMessage();
      int i = ex.getMessage().indexOf(' ');
      if (i > -1 ) message = message.substring(i);
                                                                  //Here us elanguage translate I guess :)
      //if (message.length()>65 && message.substring(1,5).compareTo("User")==0)
      //  message = (message.substring(1,50)+ "... not found" );
      this.errorLabel.setText(message);
      logged= false;
      errorSet=true;
    }
    if (!logged) {
      Logger.msg("Login attempt "+loginAttemptNumber+" of "+maxNumberLogon+" failed");
      if (loginAttemptNumber>=maxNumberLogon) Logger.die("Login failure limit reached");
      if (!errorSet) this.errorLabel.setText(Language.translate("Please enter username & password"));
//      int posx=xMov+120;
//      int posy=yMov;
//      if (posy<135) posy=135;
//      float texstSize = errorLabel.getFont().getSize2D();
//      if (posx-xMov<errorLabel.getText().length()*(texstSize/2))
//        posx=errorLabel.getText().length()*(int)(texstSize/2)+xMov;


      // obtain screen dimensions
//    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
//    this.setBounds(screen.width/2-posx/2, screen.height/2-posy/2,posx,posy);
    this.validate();
    }

    else {
      MainFrame.userAgent = userAgent;
      this.setVisible(false);
      mainFrameFather.mainFrameShow();
      Logger.msg(1, "Login attempt "+loginAttemptNumber+" of "+maxNumberLogon+" succeeded.");
    }
  }


  private void jbInit() throws Exception {

    //this.getContentPane().setBackground(SystemColor.control);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setEnabled(true);
    this.setLocale(java.util.Locale.getDefault());
    this.setResizable(false);
    this.setState(Frame.NORMAL);
    this.setTitle("");
    LoginBox_this_keyAdapter submitListener = new LoginBox_this_keyAdapter(this);
    username.addKeyListener(submitListener);
    password.addKeyListener(submitListener);
    this.getContentPane().setLayout(gridBagLayout1);

    passwordLabel.setText(Language.translate("Password")+":");

    OK.setActionCommand("OK");
    OK.setSelected(true);
    OK.setText(Language.translate("OK"));
    OK.addActionListener(new Frame2_OK_actionAdapter(this));
    OK.setPreferredSize(new Dimension(80,30));
    
    Cancel.setActionCommand("Cancel");
    Cancel.setText(Language.translate("Cancel"));
    Cancel.addActionListener(new Frame2_Cancel_actionAdapter(this));
    Cancel.setPreferredSize(new Dimension(80,30));

    userLabel.setText(Language.translate("User")+":");
    pictureLabel= new JLabel(imageMainHolder);
    pictureLabel.setBorder(new EmptyBorder(0,0,0,5));
    password.setText("");

    username.setText("");

    GridBagConstraints c = new GridBagConstraints();
    initBasicConstraints(c,1,1,1,4);
    c.anchor=GridBagConstraints.CENTER;
    c.fill = GridBagConstraints.NONE;
    c.weightx=0;
    c.weighty=1;
    getContentPane().add(pictureLabel,c);

    initBasicConstraints(c,2,1,1,1);
    c.anchor=GridBagConstraints.SOUTHWEST;
    c.fill = GridBagConstraints.NONE;
    c.weightx=0;
    c.weighty=1;
    getContentPane().add(userLabel,c);
    initBasicConstraints(c,2,2,1,1);
    c.anchor=GridBagConstraints.SOUTHWEST;
    c.fill = GridBagConstraints.NONE;
    c.weightx=0;
    c.weighty=1;
    getContentPane().add(passwordLabel,c);
    
    initBasicConstraints(c,3,1,1,1);
    c.anchor=GridBagConstraints.SOUTH;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx=1;
    c.weighty=1;
    getContentPane().add(username,c);

    initBasicConstraints(c,3,2,1,1);
    c.anchor=GridBagConstraints.SOUTH;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx=1;
    c.weighty=1;
    getContentPane().add(password,c);

    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new BoxLayout(buttonPane,BoxLayout.X_AXIS));
    buttonPane.add(Box.createGlue());
    buttonPane.add(OK);
    buttonPane.add(Box.createRigidArea(new Dimension(5,0)));
    buttonPane.add(Cancel);
    buttonPane.add(Box.createGlue());
    buttonPane.setBorder(new EmptyBorder(5,0,0,0));

    initBasicConstraints(c,2,3,2,1);
    c.weightx=0;
    c.weighty=1;
    c.anchor=GridBagConstraints.SOUTH;
    c.fill = GridBagConstraints.BOTH;
    getContentPane().add(buttonPane,c);

    initBasicConstraints(c,2,4,2,1);
    c.weightx=1;
    c.weighty=1;
    c.fill = GridBagConstraints.BOTH;
    c.anchor= GridBagConstraints.SOUTHEAST;
    JPanel msgPane = new JPanel();
    msgPane.setLayout(new BoxLayout(msgPane,BoxLayout.X_AXIS));
    msgPane.add(Box.createGlue());
    msgPane.add(errorLabel);
    msgPane.add(Box.createGlue());
    getContentPane().add(msgPane,c);
    
    ((JPanel)getContentPane()).setBorder(new EmptyBorder(0,0,0,5));
    pack();
  Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
  setLocation(screen.width/2-getWidth()/2, screen.height/2-getHeight()/2);

  }



  protected void initBasicConstraints(GridBagConstraints constraints,int x,int y,int width,int height)
  {
    constraints.gridx=x;
    constraints.gridy=y;
    constraints.gridwidth=width;
    constraints.gridheight=height;
  }

  public String getUser() {
    return username.getText();
  }

  public String getPassword() {
    return String.valueOf(password.getPassword());
  }

  void Cancel_actionPerformed(ActionEvent e) {
    Logger.die("User cancelled login.");
  }

  void OK_actionPerformed(ActionEvent e) {
    try{
      this.loginAttemptNumber++;
      loginClicked();}
    catch (Exception ex){
        Logger.error(ex);
    }
  }

  void this_keyPressed(KeyEvent e) {
    if (e.getKeyCode()==10){
      try{
        this.loginAttemptNumber++;
        loginClicked();
      }
      catch (Exception ex){
          Logger.error(ex);
      }
    }
  }

}

class Frame2_Cancel_actionAdapter implements java.awt.event.ActionListener {
  LoginBox adaptee;

  Frame2_Cancel_actionAdapter(LoginBox adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.Cancel_actionPerformed(e);
  }
}

class Frame2_OK_actionAdapter implements java.awt.event.ActionListener {
  LoginBox adaptee;

  Frame2_OK_actionAdapter(LoginBox adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.OK_actionPerformed(e);
  }
}

class LoginBox_this_keyAdapter extends java.awt.event.KeyAdapter {
  LoginBox adaptee;

  LoginBox_this_keyAdapter(LoginBox adaptee) {
    this.adaptee = adaptee;
  }
  public void keyPressed(KeyEvent e) {
    adaptee.this_keyPressed(e);
  }
}