summaryrefslogtreecommitdiffstats
path: root/Bachelor/ERGO/TierheimVerw/thv/PwDialog.java
blob: b5156dcf87eb51d376e1b91301b1173b0869c834 (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
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author Andreas Spirka, Sven Eisenhauer
 * Benutzerauthentifizierungsdialog
 */
public class PwDialog extends AbstractMyJDlg {
	Container c;
	JPanel pTop = new JPanel();
	JPanel pCenter = new JPanel();
	JPanel pBottom = new JPanel();
	JLabel l = new JLabel("login");
	JLabel pw = new JLabel ("password");
	JTextField tf = new JTextField(12);
	JPasswordField pf = new JPasswordField(12);
	JButton okB = new JButton("OK");
	JButton abbrB = new JButton("Abbrechen");
	User authUser = new User("gast","gast","Gast",1024,User.ROLE_GUEST);;
	String fName = new String ("user.dat");
	
	/**
	 * Konstruktor
	 * @param owner
	 * @param title
	 * @param modal
	 */
	public PwDialog(Frame owner, String title, boolean modal) {
		setSize(240,110);
		this.setResizable(false);
		setModal(modal);
		c = getContentPane();
		getRootPane().setDefaultButton(okB);   //mit Eingabetaste best�tigen
		c.setLayout(  new GridLayout(3, 2,6,3 ) );
		ButtonListener2 bl = new ButtonListener2(this);
		okB.addActionListener(bl);
		okB.setActionCommand("submitPW");
		abbrB.addActionListener(bl);
		abbrB.setActionCommand("abbrechen");
		c.add(l);
		c.add(tf);
		c.add(pw);
		c.add(pf);
		c.add(okB);
		c.add(abbrB);
		setTitle(title);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = getSize();
		setLocation((screenSize.width/2)-(frameSize.width/2),(screenSize.height/2)-(frameSize.height/2));
		setVisible(true);
	}
	
	/* (non-Javadoc)
	 * @see AbstractMyJDlg#buttonActionPerformed(java.awt.event.ActionEvent)
	 */
	public void buttonActionPerformed( ActionEvent e )
	{
		if (e.getActionCommand().equals("abbrechen"))
		{
			this.dispose();
		}
		if (e.getActionCommand().equals("submitPW"))
		{
			String name= new String(tf.getText());
			String pw= new String(pf.getPassword());
			UserCon userCon;
			File myFile = new File(fName);
			if(!myFile.exists())
			{
				userCon = new UserCon();
				userCon.addUser(new User("adm","adm","Administrator",0,3));
				try {
					FileOutputStream fos = new FileOutputStream(fName);
					ObjectOutputStream oos = new ObjectOutputStream(fos);
					oos.writeObject(userCon);
					oos.close();
					fos.close();
				} catch (Exception exception) { exception.printStackTrace(); }
			}
			try {
				FileInputStream fis = new FileInputStream(fName);
				ObjectInputStream ois = new ObjectInputStream(fis);
				userCon = (UserCon) ois.readObject();
				ois.close();
				fis.close();
				for (int i=0;i<userCon.size();i++)
				{
					String actname=new String(((User)userCon.elementAt(i)).getUserName());
					String actpw=new String (((User)userCon.elementAt(i)).getPassWd());
					if(name.equals(actname))
					{
						if(pw.equals(actpw))
						{
							//JOptionPane.showMessageDialog(this,"Benutzer "+name+" authentifiziert","Erfolg",JOptionPane.INFORMATION_MESSAGE);
							setVisible(false);
							authUser=(User)userCon.elementAt(i);
						}//end if2
						else
						{
							JOptionPane.showMessageDialog(this,"Benutzer "+name+" nicht authentifiziert","Misserfolg",JOptionPane.INFORMATION_MESSAGE);
						}//end else
					}//end if1
				}//end for
			} //end try
			catch (Exception exception) { exception.printStackTrace(); }
		}//end if0 get.source
	}//buttonActionPerformed
	
	/**
	 * @return User
	 */
	public User getUser(){return authUser;};
}//end class PwDialog