public class DeleteUser extends JDialog implements ActionListener
{
private JFrame frame;
private Vector<User> users;
private String[][] data;
private JTable table;
private MyTableModel myTableModel;
private String[] colheads = {"Vorname", "Name"};;
public DeleteUser(JFrame frame)
{
super(frame, true);
this.frame = frame;
try
{
FileInputStream fs = new FileInputStream("user.ser");
ObjectInputStream is = new ObjectInputStream(fs);
users = (Vector)is.readObject();
is.close();
}
catch (Exception e1)
{
users = new Vector<User>();
writeFile();
}
createModel();
table = new JTable();
table.setModel(myTableModel);
JScrollPane scrollPane = new JScrollPane(table);
JPanel panelNorth = new JPanel(new GridLayout(1, 0, 10, 10));
panelNorth.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
panelNorth.add(scrollPane);
JButton buttonNeu = new JButton("Neu");
buttonNeu.addActionListener(this);
JButton buttonÄndern = new JButton("Ändern");
buttonÄndern.addActionListener(this);
JButton buttonLöschen = new JButton("Löschen");
buttonLöschen.addActionListener(this);
JButton buttonAbbruch = new JButton("Abbruch");
buttonAbbruch.addActionListener(this);
JPanel buttonPanel = new JPanel(new GridLayout(1,0, 30, 30));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
buttonPanel.add(buttonNeu);
buttonPanel.add(buttonLöschen);
buttonPanel.add(buttonAbbruch);
buttonPanel.add(buttonÄndern);
setLayout(new BorderLayout());
add(panelNorth, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
setTitle("Benutzer bearbeiten");
setSize(600, 400);
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getActionCommand().equals("Neu"))
{
dispose();
new NewUser(frame, this);
}
if(evt.getActionCommand().equals("Ändern"))
{
User userAktuell = users.elementAt(table.getSelectedRow());
if(table.getSelectedRow() != -1)
{
String vorname = userAktuell.getVorname();
String name = userAktuell.getName();
String altesPasswort = userAktuell.getPasswort();
dispose();
new ModifyUserPassword(frame, this, vorname, name, table.getSelectedRow(), altesPasswort);
}
else
{
JOptionPane.showMessageDialog(this,
"kein Benutzer selektiert",
"Fehler",
JOptionPane.ERROR_MESSAGE);
}
}
if(evt.getActionCommand().equals("Löschen"))
{
if(table.getSelectedRow() != -1)
{
deleteUser();
table.setModel(myTableModel);
}
else
{
JOptionPane.showMessageDialog(this,
"kein Benutzer selektiert",
"Fehler",
JOptionPane.ERROR_MESSAGE);
}
}
if(evt.getActionCommand().equals("Abbruch"))
{
dispose();
}
}
public void addUser(String name, String vorname, String passwort)
{
User user = new User(name, vorname, passwort);
users.addElement(user);
writeFile();
}
public void writeFile()
{
try
{
FileOutputStream fs = new FileOutputStream("user.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(users);
os.close();
}
catch(Exception e2)
{
System.err.println(e2.toString());
}
}
public void deleteUser()
{
int selectedUser = table.getSelectedRow();
users.removeElementAt(selectedUser);
writeFile();
}
public void deleteUser(int selectedUser)
{
users.removeElementAt(selectedUser);
writeFile();
}
public Vector getUsers()
{
return users;
}
public void createModel()
{
data = new String[users.size()][2];
for(int i=0; i<users.size(); i++)
{
data[i][0] = (String) users.elementAt(i).getVorname();
data[i][1] = (String) users.elementAt(i).getName();
}
myTableModel = new MyTableModel(colheads, 0);
for(int i=0; i<data.length; i++)
{
myTableModel.addRow(data[i]);
}
}
}