· 9 years ago · May 19, 2016, 09:09 PM
1package net.minecraft;
2
3import java.awt.BorderLayout;
4import java.awt.Color;
5import java.awt.Component;
6import java.awt.Cursor;
7import java.awt.Dimension;
8import java.awt.Font;
9import java.awt.FontMetrics;
10import java.awt.Graphics;
11import java.awt.GridBagLayout;
12import java.awt.GridLayout;
13import java.awt.event.ActionEvent;
14import java.awt.event.ActionListener;
15import java.awt.event.MouseAdapter;
16import java.awt.event.MouseEvent;
17import java.io.DataInputStream;
18import java.io.DataOutputStream;
19import java.io.File;
20import java.io.FileInputStream;
21import java.io.FileOutputStream;
22import java.net.URL;
23import java.util.Random;
24import javax.crypto.Cipher;
25import javax.crypto.CipherInputStream;
26import javax.crypto.CipherOutputStream;
27import javax.crypto.SecretKey;
28import javax.crypto.SecretKeyFactory;
29import javax.crypto.spec.PBEKeySpec;
30import javax.crypto.spec.PBEParameterSpec;
31import javax.swing.JLabel;
32import javax.swing.JPanel;
33import javax.swing.JPasswordField;
34import javax.swing.JScrollPane;
35import javax.swing.JTextField;
36import javax.swing.JTextPane;
37import javax.swing.border.MatteBorder;
38import javax.swing.event.HyperlinkEvent;
39import javax.swing.event.HyperlinkListener;
40
41public class LoginForm extends TransparentPanel
42{
43 private static final int PANEL_SIZE = 100;
44 private static final long serialVersionUID = 1L;
45 private static final Color LINK_COLOR = new Color(8421631);
46
47 public JTextField userName = new JTextField(20);
48 public JPasswordField password = new JPasswordField(20);
49 private TransparentCheckbox rememberBox = new TransparentCheckbox("Se souvenir de mon mot de passe");
50 private TransparentButton launchButton = new TransparentButton("Se connecter");
51 private TransparentButton optionsButton = new TransparentButton("Options");
52 private TransparentButton retryButton = new TransparentButton("Ré-essayer");
53 private TransparentButton offlineButton = new TransparentButton("Jouer hors ligne");
54 private TransparentLabel errorLabel = new TransparentLabel("", 0);
55 private LauncherFrame launcherFrame;
56 private boolean outdated = false;
57 private JScrollPane scrollPane;
58
59 public LoginForm(final LauncherFrame launcherFrame)
60 {
61 this.launcherFrame = launcherFrame;
62
63 BorderLayout gbl = new BorderLayout();
64 setLayout(gbl);
65
66 add(buildMainLoginPanel(), "Center");
67
68 readUsername();
69
70 ActionListener al = new ActionListener() {
71 public void actionPerformed(ActionEvent arg0) {
72 LoginForm.this.doLogin();
73 }
74 };
75 this.userName.addActionListener(al);
76 this.password.addActionListener(al);
77
78 this.retryButton.addActionListener(new ActionListener() {
79 public void actionPerformed(ActionEvent ae) {
80 LoginForm.this.errorLabel.setText("");
81 LoginForm.this.removeAll();
82 LoginForm.this.add(LoginForm.this.buildMainLoginPanel(), "Center");
83 LoginForm.this.validate();
84 }
85 });
86 this.offlineButton.addActionListener(new ActionListener() {
87 public void actionPerformed(ActionEvent ae) {
88 launcherFrame.playCached(LoginForm.this.userName.getText());
89 }
90 });
91 this.launchButton.addActionListener(al);
92
93 this.optionsButton.addActionListener(new ActionListener() {
94 public void actionPerformed(ActionEvent ae) {
95 new OptionsPanel(launcherFrame).setVisible(true);
96 }
97 });
98 }
99
100 public void doLogin() {
101 setLoggingIn();
102 new Thread() {
103 public void run() {
104 try {
105 LoginForm.this.launcherFrame.login(LoginForm.this.userName.getText(), new String(LoginForm.this.password.getPassword()));
106 } catch (Exception e) {
107 LoginForm.this.setError(e.toString());
108 }
109 }
110 }
111 .start();
112 }
113
114 private void readUsername() {
115 try {
116 File lastLogin = new File(Util.getWorkingDirectory(), "lastlogin");
117
118 Cipher cipher = getCipher(2, "passwordfile");
119 DataInputStream dis;
120 if (cipher != null)
121 dis = new DataInputStream(new CipherInputStream(new FileInputStream(lastLogin), cipher));
122 else {
123 dis = new DataInputStream(new FileInputStream(lastLogin));
124 }
125 this.userName.setText(dis.readUTF());
126 this.password.setText(dis.readUTF());
127 this.rememberBox.setSelected(this.password.getPassword().length > 0);
128 dis.close();
129 } catch (Exception e) {
130 e.printStackTrace();
131 }
132 }
133
134 private void writeUsername() {
135 try {
136 File lastLogin = new File(Util.getWorkingDirectory(), "lastlogin");
137
138 Cipher cipher = getCipher(1, "passwordfile");
139 DataOutputStream dos;
140 if (cipher != null)
141 dos = new DataOutputStream(new CipherOutputStream(new FileOutputStream(lastLogin), cipher));
142 else {
143 dos = new DataOutputStream(new FileOutputStream(lastLogin));
144 }
145 dos.writeUTF(this.userName.getText());
146 dos.writeUTF(this.rememberBox.isSelected() ? new String(this.password.getPassword()) : "");
147 dos.close();
148 } catch (Exception e) {
149 e.printStackTrace();
150 }
151 }
152
153 private Cipher getCipher(int mode, String password) throws Exception {
154 Random random = new Random(43287234L);
155 byte[] salt = new byte[8];
156 random.nextBytes(salt);
157 PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 5);
158
159 SecretKey pbeKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(password.toCharArray()));
160 Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
161 cipher.init(mode, pbeKey, pbeParamSpec);
162 return cipher;
163 }
164
165 private JScrollPane getUpdateNews()
166 {
167 if (this.scrollPane != null) return this.scrollPane;
168 try
169 {
170 JTextPane editorPane = new JTextPane()
171 {
172 private static final long serialVersionUID = 1L;
173 };
174 editorPane.setText("Chargement de la page web ...");
175 editorPane.addHyperlinkListener(new HyperlinkListener() {
176 public void hyperlinkUpdate(HyperlinkEvent he) {
177 if (he.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
178 try {
179 Util.openLink(he.getURL().toURI());
180 } catch (Exception e) {
181 e.printStackTrace();
182 }
183 }
184 });
185 new LoginFormThread(editorPane).start();
186 editorPane.setBackground(Color.DARK_GRAY);
187 editorPane.setEditable(false);
188 this.scrollPane = new JScrollPane(editorPane);
189 this.scrollPane.setBorder(null);
190 editorPane.setMargin(null);
191
192 this.scrollPane.setBorder(new MatteBorder(0, 0, 2, 0, Color.BLACK));
193 } catch (Exception e2) {
194 e2.printStackTrace();
195 }
196
197 return this.scrollPane;
198 }
199
200 private JPanel buildMainLoginPanel() {
201 JPanel p = new TransparentPanel(new BorderLayout());
202 p.add(getUpdateNews(), "Center");
203
204 JPanel southPanel = new TexturedPanel();
205 southPanel.setLayout(new BorderLayout());
206 southPanel.add(new LogoPanel(), "West");
207 southPanel.add(new TransparentPanel(), "Center");
208 southPanel.add(center(buildLoginPanel()), "East");
209 southPanel.setPreferredSize(new Dimension(100, 100));
210
211 p.add(southPanel, "South");
212 return p;
213 }
214
215 private JPanel buildLoginPanel() {
216 TransparentPanel panel = new TransparentPanel();
217 panel.setInsets(4, 0, 4, 0);
218
219 BorderLayout layout = new BorderLayout();
220 layout.setHgap(0);
221 layout.setVgap(8);
222 panel.setLayout(layout);
223
224 GridLayout gl1 = new GridLayout(0, 1);
225 gl1.setVgap(2);
226 GridLayout gl2 = new GridLayout(0, 1);
227 gl2.setVgap(2);
228 GridLayout gl3 = new GridLayout(0, 1);
229 gl3.setVgap(2);
230
231 TransparentPanel titles = new TransparentPanel(gl1);
232 TransparentPanel values = new TransparentPanel(gl2);
233
234 titles.add(new TransparentLabel("Nom de compte:", 4));
235 titles.add(new TransparentLabel("Mot de passe:", 4));
236 titles.add(new TransparentLabel("", 4));
237
238 values.add(this.userName);
239 values.add(this.password);
240 values.add(this.rememberBox);
241
242 panel.add(titles, "West");
243 panel.add(values, "Center");
244
245 TransparentPanel loginPanel = new TransparentPanel(new BorderLayout());
246
247 TransparentPanel third = new TransparentPanel(gl3);
248 titles.setInsets(0, 0, 0, 4);
249 third.setInsets(0, 10, 0, 10);
250
251 third.add(this.optionsButton);
252 third.add(this.launchButton);
253 try
254 {
255 if (this.outdated) {
256 TransparentLabel accountLink = getUpdateLink();
257 third.add(accountLink);
258 }
259 else
260 {
261 TransparentLabel accountLink = new TransparentLabel("Besoin d'un compte ?") {
262 private static final long serialVersionUID = 0L;
263
264 public void paint(Graphics g) { super.paint(g);
265
266 int x = 0;
267 int y = 0;
268
269 FontMetrics fm = g.getFontMetrics();
270 int width = fm.stringWidth(getText());
271 int height = fm.getHeight();
272
273 if (getAlignmentX() == 2.0F) x = 0;
274 else if (getAlignmentX() == 0.0F) x = getBounds().width / 2 - width / 2;
275 else if (getAlignmentX() == 4.0F) x = getBounds().width - width;
276 y = getBounds().height / 2 + height / 2 - 1;
277
278 g.drawLine(x + 2, y, x + width - 2, y); }
279
280 public void update(Graphics g)
281 {
282 paint(g);
283 }
284 };
285 accountLink.setCursor(Cursor.getPredefinedCursor(12));
286 accountLink.addMouseListener(new MouseAdapter() {
287 public void mousePressed(MouseEvent arg0) {
288 try {
289 Util.openLink(new URL("http://www.minecraft.net/register.jsp").toURI());
290 } catch (Exception e) {
291 e.printStackTrace();
292 }
293 }
294 });
295 accountLink.setForeground(LINK_COLOR);
296 third.add(accountLink);
297 }
298
299 }
300 catch (Error localError)
301 {
302 }
303
304 loginPanel.add(third, "Center");
305 panel.add(loginPanel, "East");
306
307 this.errorLabel.setFont(new Font(null, 2, 16));
308 this.errorLabel.setForeground(new Color(16728128));
309 this.errorLabel.setText("");
310 panel.add(this.errorLabel, "North");
311
312 return panel;
313 }
314
315 private TransparentLabel getUpdateLink() {
316 TransparentLabel accountLink = new TransparentLabel("Vous devez faire la mise à jour du Launcher !") {
317 private static final long serialVersionUID = 0L;
318
319 public void paint(Graphics g) { super.paint(g);
320
321 int x = 0;
322 int y = 0;
323
324 FontMetrics fm = g.getFontMetrics();
325 int width = fm.stringWidth(getText());
326 int height = fm.getHeight();
327
328 if (getAlignmentX() == 2.0F) x = 0;
329 else if (getAlignmentX() == 0.0F) x = getBounds().width / 2 - width / 2;
330 else if (getAlignmentX() == 4.0F) x = getBounds().width - width;
331 y = getBounds().height / 2 + height / 2 - 1;
332
333 g.drawLine(x + 2, y, x + width - 2, y); }
334
335 public void update(Graphics g)
336 {
337 paint(g);
338 }
339 };
340 accountLink.setCursor(Cursor.getPredefinedCursor(12));
341 accountLink.addMouseListener(new MouseAdapter() {
342 public void mousePressed(MouseEvent arg0) {
343 try {
344 Util.openLink(new URL("http://www.minecraft.net/download.jsp").toURI());
345 } catch (Exception e) {
346 e.printStackTrace();
347 }
348 }
349 });
350 accountLink.setForeground(LINK_COLOR);
351 return accountLink;
352 }
353
354 private JPanel buildMainOfflinePanel() {
355 JPanel p = new TransparentPanel(new BorderLayout());
356 p.add(getUpdateNews(), "Center");
357
358 JPanel southPanel = new TexturedPanel();
359 southPanel.setLayout(new BorderLayout());
360 southPanel.add(new LogoPanel(), "West");
361 southPanel.add(new TransparentPanel(), "Center");
362 southPanel.add(center(buildOfflinePanel()), "East");
363 southPanel.setPreferredSize(new Dimension(100, 100));
364
365 p.add(southPanel, "South");
366 return p;
367 }
368
369 private Component center(Component c) {
370 TransparentPanel tp = new TransparentPanel(new GridBagLayout());
371 tp.add(c);
372 return tp;
373 }
374
375 private TransparentPanel buildOfflinePanel()
376 {
377 TransparentPanel panel = new TransparentPanel();
378 panel.setInsets(0, 0, 0, 20);
379
380 BorderLayout layout = new BorderLayout();
381 panel.setLayout(layout);
382
383 TransparentPanel loginPanel = new TransparentPanel(new BorderLayout());
384
385 GridLayout gl = new GridLayout(0, 1);
386 gl.setVgap(2);
387 TransparentPanel pp = new TransparentPanel(gl);
388 pp.setInsets(0, 8, 0, 0);
389
390 pp.add(this.retryButton);
391 pp.add(this.offlineButton);
392
393 loginPanel.add(pp, "East");
394
395 boolean canPlayOffline = this.launcherFrame.canPlayOffline(this.userName.getText());
396 this.offlineButton.setEnabled(canPlayOffline);
397 if (!canPlayOffline) {
398 loginPanel.add(new TransparentLabel("(Non téléchargé)", 4), "South");
399 }
400 panel.add(loginPanel, "Center");
401
402 TransparentPanel p2 = new TransparentPanel(new GridLayout(0, 1));
403 this.errorLabel.setFont(new Font(null, 2, 16));
404 this.errorLabel.setForeground(new Color(16728128));
405 p2.add(this.errorLabel);
406 if (this.outdated) {
407 TransparentLabel accountLink = getUpdateLink();
408 p2.add(accountLink);
409 }
410
411 loginPanel.add(p2, "Center");
412
413 return panel;
414 }
415
416 public void setError(String errorMessage) {
417 removeAll();
418 add(buildMainLoginPanel(), "Center");
419 this.errorLabel.setText(errorMessage);
420 validate();
421 }
422
423 public void loginOk() {
424 writeUsername();
425 }
426
427 public void setLoggingIn() {
428 removeAll();
429 JPanel panel = new JPanel(new BorderLayout());
430 panel.add(getUpdateNews(), "Center");
431
432 JPanel southPanel = new TexturedPanel();
433 southPanel.setLayout(new BorderLayout());
434 southPanel.add(new LogoPanel(), "West");
435 southPanel.add(new TransparentPanel(), "Center");
436 JLabel label = new TransparentLabel("Connexion en cours... ", 0);
437 label.setFont(new Font(null, 1, 16));
438 southPanel.add(center(label), "East");
439 southPanel.setPreferredSize(new Dimension(100, 100));
440
441 panel.add(southPanel, "South");
442
443 add(panel, "Center");
444 validate();
445 }
446
447 public void setNoNetwork() {
448 removeAll();
449 add(buildMainOfflinePanel(), "Center");
450 validate();
451 }
452
453 public void checkAutologin() {
454 if (this.password.getPassword().length > 0)
455 this.launcherFrame.login(this.userName.getText(), new String(this.password.getPassword()));
456 }
457
458 public void setOutdated()
459 {
460 this.outdated = true;
461 }
462}