| 12
 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
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 
 |  
 
 
 
import java.awt.Checkbox;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
 
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
 
import com.trilead.ssh2.Connection;
 
public class client_temp extends JPanel
{
	String		username 		= "";
	String		password 		= "";
	String 		hostname		= "";
	boolean		authenticated 	= false;
	Connection	conn 			= null;
	boolean		connAuto 		= false;
	boolean		memAdr			= false;	
	boolean		memPass			= false;
	boolean		userPrincipal 	= true;
	String 		sParam			= "param_temp.ser";
	myApp		frameParent		= null;
	boolean 	messageTelecharge = false;
 
	JLabel	fond = new JLabel(new ImageIcon(getClass().getResource("/fileaccess.jpg")));
 
	final JLabel boutonC = new JLabel(new ImageIcon(getClass().getResource("/btn2.jpg")));
	final JLabel boutonC1 = new JLabel(new ImageIcon(getClass().getResource("/connexion.png")));
	TextField	userTxt;
	TextField	passTxt;
	Checkbox	memAdrChk;
	Checkbox	memPassChk;
	Checkbox	memConnChk;
	Panel 		panConn;
 
	static int compteur = 0;
 
	final splash mySplash = new splash();
 
	public client_temp(myApp frameParent)
	{
		this.username = "";
		this.password = "";
		this.authenticated = false;
		this.conn = null;		
		this.connAuto=false;
		this.memAdr = false;
		this.memPass = false;
 
		this.frameParent = frameParent;
		this.compteur++;
		initialise(this.username, this.password, this.memAdr, this.memPass, this.userPrincipal);
	}
 
	public client_temp(myApp frameParent, String host, String user, String pass, boolean memAdr, boolean memPass, boolean connAuto, boolean message)
	{
		this.frameParent = frameParent;
		this.username = user;
		this.password = pass;
		this.memAdr = memAdr;
		this.memPass = memPass;
		this.connAuto = connAuto;
		this.authenticated = false;
		this.conn = null;
		this.hostname = host;
		this.compteur++;
		this.messageTelecharge = message;
		initialise(this.username, this.password, this.memAdr, this.memPass, this.userPrincipal);
	}
 
	public void finalize()
	{
		this.compteur--;
	}
 
//	public void init()
//	{
//		//test pour l'utilisateur principal
//		if(this.userPrincipal) sParam = "param.ser";
//		else sParam = "param1.ser";
//	
//		//Si le fichier de parametres existe
//		if(new File(sParam).exists()) 
//		{
//			//on le lit
//			FileInputStream fisParam;
//			try 
//			{
//				fisParam = new FileInputStream(sParam);
//				ObjectInputStream oisParam = new ObjectInputStream(fisParam);
//				Object i = oisParam.readObject();
//				oisParam.close();
//
//				//on recupere les valeurs des proprietes de l'objet deserialise
//				//Si il n'y a pas de connexion automatique
//				if(!(((initialisation)i).getConnexion()))
//				{
//					//on lit la valeur du login
//					if(!(((initialisation)i).getUsername().equals("")))
//					{
//						this.username = ((initialisation)i).getUsername();
//						this.memAdr = true;
//					}
//					
//					//on lit la valeur du password
//					if(!(((initialisation)i).getPassword().equals("")))
//					{
//						this.password = ((initialisation)i).getPassword();
//						this.memPass = true;
//					}
//					//on lance l'initialisation du panel
//					this.initialise(username, password,memAdr, memPass, true);
//				}
//				//Si il y a une connexion automatique
//				else
//				{
//					//on verifie l'authentification
//					boolean authentifieOK = this.authentification(((initialisation)i).getUsername(),((initialisation)i).getPassword(), this.hostname);
//					if(authentifieOK)
//					{ 
//						//on reinitialise les controles du panel
//						this.username = "";
//						this.password = "";
//						this.memAdr = false;
//						this.memPass = false;
//						this.connAuto = false;
//						
//						//on appelle la fonction de la frame parent qui effectue les modifications d'affichage
//						//et qui appelle l'interface d'administration
//						this.frameParent.appelAdmin(((initialisation)i).getUsername(), ((initialisation)i).getPassword());
//					}
//					else
//					{
//						//Mettre un message d'erreur
//						//	log.initialise("","",false, false);
//						this.username = "";
//						this.password = "";
//						this.memAdr = false;
//						this.memPass = false;
//						this.connAuto = false;
//					}
//				}
//			} 
//			catch (FileNotFoundException e)	{ e.printStackTrace(); } 
//			catch (IOException e) { e.printStackTrace(); } 
//			catch (ClassNotFoundException e) { e.printStackTrace(); }
//		}
//		else this.initialise(username, password, false, false, true);	
//	}
 
	public void initialise(String user, String pass, boolean memAdr, boolean memPass, boolean userPrincipal)
	{
		// on definit le layout du panel
		this.setLayout(null);
 
		mySplash.setLocation(0,0);
		mySplash.setVisible(false);
 
		//on definit le type d'utilisateur
		this.userPrincipal = userPrincipal;
 
		//on definit la police de caracteres des textfields
		Font policeTxtField = new Font("Arial Sans Serif", Font.BOLD, 15);
 
		//on definit les zones de textes
		userTxt	= new TextField(username);	//login
		passTxt	= new TextField(password);	//password
		passTxt.setEchoChar('*');			//caractere affiche lors de la saisie du password
 
		//on definit le fond du panel
 
		//on definit les checkbox pour les options de connexion
		memAdrChk	= new Checkbox("");
		memPassChk	= new Checkbox("");
		memConnChk	= new Checkbox("");
 
		//Si des options doivent etre memorisees, on les precoche
		memAdrChk.setState(memAdr);
		memPassChk.setState(memPass);
 
 
		//on ajoute le fond
		fond.setPreferredSize(new Dimension(290, 368));
		fond.setLocation(0,0);
		fond.setSize(290,368);
 
 
		//on definit le champ texte du login
		userTxt.setBounds(18, 73, 255, 26);
		userTxt.setFont(policeTxtField);
		userTxt.setEnabled(true);
		userTxt.setFocusable(true);
		userTxt.setEditable(true);
 
		//on definit le champ texte du mot de passe
		passTxt.setBounds(18, 153, 255, 26);
		passTxt.setFont(policeTxtField);
		passTxt.setEnabled(true);
		passTxt.setFocusable(true);
		passTxt.setEditable(true);
 
		//on definit la case a cocher du login
		memAdrChk.setBounds(23, 225, 13, 13);
		memAdrChk.setEnabled(true);
 
		//on definit la case a cocher du mot de passe
		memPassChk.setBounds(23, 250, 13, 13);
		memPassChk.setEnabled(true);
 
		//on definit la case a cocher de la connexion auto
		memConnChk.setBounds(23, 275, 13, 13);
		memConnChk.setEnabled(true);
 
		//on definit le bouton de connexion
		panConn = new Panel(null);
//		final JLabel boutonC = new JLabel(new ImageIcon(getClass().getResource("/btn2.jpg")));
//		final JLabel boutonC1 = new JLabel(new ImageIcon(getClass().getResource("/connexion.png")));
		JLabel labSplash = new JLabel(new ImageIcon(getClass().getResource("/ajax-loader.gif")));
		labSplash.setLocation(80, 311);
 
		boutonC1.setBounds(0,0,154,34);
		boutonC.setBounds(0,0,154,34);
		boutonC1.setVisible(true);
		boutonC.setVisible(false);
		panConn.add(boutonC);
		panConn.add(boutonC1);
		panConn.setBounds(68,311, 154,34);
		panConn.setVisible(true);
 
		//on ajoute les elements au panel
		this.add(fond);
		this.add(userTxt);
		this.add(passTxt);
		this.add(memAdrChk);
		this.add(memPassChk);
		this.add(memConnChk);
		this.add(panConn);
		this.add(mySplash);
		this.validate();
 
		//on ajoute un listener sur le champ de texte du password
		passTxt.addKeyListener(new KeyAdapter()
				{
					public void keyTyped(KeyEvent arg0)
					{
						if(arg0.getKeyCode()!= KeyEvent.VK_ENTER) passTxt.repaint(0);
					}
					public void keyReleased(KeyEvent arg0) 
					{
						if(arg0.getKeyCode()== KeyEvent.VK_ENTER)
						{
							mySplash.setVisible(true);
							connexionClient();
						}
						else passTxt.repaint(0);
					}
				}
		);
 
		//on ajoute un listener sur le champ de texte du login
		userTxt.addKeyListener(new KeyAdapter()
				{
					public void keyTyped(KeyEvent arg0)
					{
						if(arg0.getKeyCode()!= KeyEvent.VK_ENTER) userTxt.repaint(0);
					}
 
					public void keyReleased(KeyEvent arg0) 
					{
						if(arg0.getKeyCode()== KeyEvent.VK_ENTER) 
						{
							mySplash.setVisible(true);
							connexionClient();
						}
						else userTxt.repaint(0);
					}
				}
		);
		//on ajoute un listener de souris sur le bouton de connexion
		panConn.addMouseListener(new MouseAdapter()
				{
					//Evenement clic sur le panel
					public void mouseClicked(MouseEvent arg0) 
					{
						mySplash.setVisible(true);
						connexionClient(); 
					}
 
					public void mouseEntered(MouseEvent arg0) 
					{
						//si le pointeur de la souris survole le bouton, on active le bouton
						boutonC1.setVisible(false);
						boutonC.setVisible(true);
					}
 
					public void mouseExited(MouseEvent arg0) 
					{
						//si le pointeur de la souris ne survole plus le bouton, on desactive le bouton
						boutonC1.setVisible(true);
						boutonC.setVisible(false);
					}
				});			
	}
 
	public void connexionClient()
	{
		userTxt.setVisible(false);
		passTxt.setVisible(false);
		memAdrChk.setVisible(false);
		memPassChk.setVisible(false);
		memConnChk.setVisible(false);
		panConn.setVisible(false);
		fond.setVisible(false);
		boutonC1.setVisible(false);
		boutonC.setVisible(true);
 
 
		mySplash.setVisible(true);		
		if(!userTxt.getText().equals("") && !passTxt.getText().equals(""))
		{		
 
			//on verifie l'authentification de l'utilisateur
			boolean b = authentification(userTxt.getText(), passTxt.getText(), "mybooo.com");
			//si l'utilisateur est authentifie
			if(b)
			{
				//on recupere les etats des cases cochees pour les placer dans un objet a serialiser
				initialisation initial = new initialisation();
				if(memAdrChk.getState()==true) initial.setUsername(userTxt.getText());
				if(memPassChk.getState()==true) initial.setPassword(passTxt.getText());
				initial.setMessageTelecharge(this.messageTelecharge);
 
				if(memConnChk.getState()==true) 
				{
					if(!userTxt.getText().equals("") && !userTxt.getText().equals("")) 
						initial.setConnexionAuto(true);
				}
 
				//on genere le fichier de serialisation
				try 
				{
					if(userPrincipal)
					{
						System.out.println("mise à jour du fichier de serialisation");	
						FileOutputStream fosParam = new FileOutputStream(sParam);
						ObjectOutputStream oosParam = new ObjectOutputStream(fosParam);
						oosParam.writeObject(initial);
						oosParam.close();
					}
				} 
				catch (FileNotFoundException e) 
				{
					//si le fichier de serialisation n'existe pas, on en cree un
					File fParam = new File(sParam);
					try 
					{
						System.out.println("Création d'un objet sérialisé dans " + sParam);
						fParam.createNewFile();
						FileOutputStream fosParam = new FileOutputStream(sParam);
						ObjectOutputStream oosParam = new ObjectOutputStream(fosParam);
						oosParam.writeObject(initial);
						oosParam.close();
					} 
					catch (FileNotFoundException e1) {}
					catch (IOException e1) { }
					finally
					{
						//on lance ensuite l'appel au panel d'administration en appelant la 
						//fonction d'appel contenue dans le conteneur principal
						frameParent.appelAdmin(userTxt.getText(), passTxt.getText());
 
					}			
				  } 
					catch (IOException e) { }
					finally
					{ 
						if(authentification(userTxt.getText(), passTxt.getText(), "mybooo.com"))
						{
							frameParent.appelAdmin(userTxt.getText(), passTxt.getText());
						}
					}
				}
				else
				{
					JOptionPane.showMessageDialog(null,"Login ou mot de passe erroné(s)", "Erreur de connexion", JOptionPane.ERROR_MESSAGE, null);
					//vider les champs texte au lieu de fermer la fenetre
					passTxt.setText("");
					userTxt.setText("");
					//mySplash.setVisible(false);
 
					boutonC1.setVisible(true);
					boutonC.setVisible(false);
					this.initialise("","",false,false,userPrincipal);
					fond.setVisible(true);
				}
		}
		else{}
	}
 
	public boolean isOptimizedDrawingEnabled() 
	{ 
		return false; 
	}
 
	public static boolean authentification(String username, String password, String hostname){
		try
		{
			Connection conn = new Connection(hostname);
			conn.connect();
			boolean isAuthenticated = conn.authenticateWithPassword(username, password);
			conn.close();
			if(isAuthenticated) return true;
			else return false;
 
		}
		catch(IOException ioe) { return false; }
 
	}
} | 
Partager