Started implementing the UserTableModel.
This commit is contained in:
@@ -38,7 +38,7 @@ public class Main {
|
||||
this.dbCon = new SqlConnector();
|
||||
}
|
||||
try {
|
||||
MainWindow window = new MainWindow(this);
|
||||
MainWindow window = new MainWindow(this.dbCon);
|
||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
@@ -55,8 +55,4 @@ public class Main {
|
||||
}
|
||||
});*/
|
||||
}
|
||||
|
||||
public boolean userSave(String pName, String pSurname) {
|
||||
return this.dbCon.createUser(pName, pSurname);
|
||||
}
|
||||
}
|
||||
|
||||
25
src/de/katho/kBorrow/data/KUser.java
Normal file
25
src/de/katho/kBorrow/data/KUser.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package de.katho.kBorrow.data;
|
||||
|
||||
public class KUser {
|
||||
private String name;
|
||||
private String surname;
|
||||
private int id;
|
||||
|
||||
public KUser(int pId, String pName, String pSurname){
|
||||
this.name = pName;
|
||||
this.surname = pSurname;
|
||||
this.id = pId;
|
||||
}
|
||||
|
||||
public String getName(){
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getSurname(){
|
||||
return this.surname;
|
||||
}
|
||||
|
||||
public int getId(){
|
||||
return this.id;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,12 @@
|
||||
package de.katho.kBorrow.db;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import de.katho.kBorrow.data.KUser;
|
||||
|
||||
public interface DbConnector {
|
||||
|
||||
public boolean createUser(String pName, String pSurname);
|
||||
public ArrayList<KUser> getUserList();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
package de.katho.kBorrow.db;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import de.katho.kBorrow.data.KUser;
|
||||
|
||||
public class SqlConnector implements DbConnector{
|
||||
|
||||
@Override
|
||||
@@ -7,5 +11,11 @@ public class SqlConnector implements DbConnector{
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<KUser> getUserList() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,9 +8,12 @@ import java.sql.DriverManager;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import de.katho.kBorrow.data.KUser;
|
||||
|
||||
/**
|
||||
* @class sqliteConnector
|
||||
* @author Servicepoint
|
||||
@@ -108,45 +111,39 @@ public class SqliteConnector implements DbConnector {
|
||||
|
||||
private Hashtable<String, String> loadScheme(){
|
||||
Hashtable<String, String> tScheme= new Hashtable<String, String>();
|
||||
|
||||
tScheme.put("kborrow",
|
||||
"CREATE TABLE kborrow ("
|
||||
+ "setting_name TEXT,"
|
||||
+ "value INT"
|
||||
+ ")");
|
||||
|
||||
|
||||
tScheme.put("article",
|
||||
"CREATE TABLE article ("
|
||||
+ "id INT PRIMARY KEY,"
|
||||
+ "id INTEGER PRIMARY KEY NOT NULL,"
|
||||
+ "name TEXT NOT NULL,"
|
||||
+ "description TEXT"
|
||||
+ ")");
|
||||
|
||||
tScheme.put("lender",
|
||||
"CREATE TABLE lender ("
|
||||
+ "id INT PRIMARY KEY,"
|
||||
+ "id INTEGER PRIMARY KEY NOT NULL,"
|
||||
+ "name TEXT,"
|
||||
+ "surname TEXT,"
|
||||
+ "student_number INT,"
|
||||
+ "student_number INTEGER,"
|
||||
+ "comment TEXT"
|
||||
+ ")");
|
||||
|
||||
tScheme.put("user",
|
||||
"CREATE TABLE user ("
|
||||
+ "id INT PRIMARY KEY,"
|
||||
+ "id INTEGER PRIMARY KEY NOT NULL,"
|
||||
+ "name TEXT,"
|
||||
+ "surname TEXT"
|
||||
+ ")");
|
||||
|
||||
tScheme.put("lending",
|
||||
"CREATE TABLE lending ("
|
||||
+ "id INT PRIMARY KEY,"
|
||||
+ "article_id INT,"
|
||||
+ "user_id INT,"
|
||||
+ "lender_id INT,"
|
||||
+ "start_date DATE DEFAULT CURRENT_DATE,"
|
||||
+ "expected_end_date DATE,"
|
||||
+ "end_date DATE,"
|
||||
+ "id INTEGER PRIMARY KEY NOT NULL,"
|
||||
+ "article_id INTEGER,"
|
||||
+ "user_id INTEGER,"
|
||||
+ "lender_id INTEGER,"
|
||||
+ "start_date INTEGER DEFAULT CURRENT_DATE,"
|
||||
+ "expected_end_date INTEGER,"
|
||||
+ "end_date INTEGER,"
|
||||
+ "comment TEXT"
|
||||
+ ")");
|
||||
|
||||
@@ -200,4 +197,24 @@ public class SqliteConnector implements DbConnector {
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<KUser> getUserList(){
|
||||
ArrayList<KUser> userArr = new ArrayList<KUser>();
|
||||
|
||||
try {
|
||||
Statement st = this.connection.createStatement();
|
||||
String query = "SELECT id, name, surname FROM user";
|
||||
ResultSet rs = st.executeQuery(query);
|
||||
|
||||
while (rs.next()){
|
||||
userArr.add(new KUser(rs.getInt("id"), rs.getString("name"), rs.getString("surname")));
|
||||
}
|
||||
|
||||
return userArr;
|
||||
}
|
||||
catch (SQLException e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,16 +7,12 @@ import javax.swing.UnsupportedLookAndFeelException;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
|
||||
import javax.swing.BoxLayout;
|
||||
|
||||
|
||||
import javax.swing.border.TitledBorder;
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
import javax.swing.table.TableModel;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JButton;
|
||||
|
||||
@@ -26,19 +22,17 @@ import java.awt.event.ActionListener;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
import de.katho.kBorrow.Main;
|
||||
import de.katho.kBorrow.db.DbConnector;
|
||||
|
||||
import javax.swing.JTable;
|
||||
import javax.swing.ScrollPaneConstants;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.FlowLayout;
|
||||
import java.awt.Component;
|
||||
import java.awt.GridLayout;
|
||||
|
||||
public class MainWindow implements ActionListener {
|
||||
|
||||
private Main mainObject;
|
||||
private DbConnector dbCon;
|
||||
private boolean userModeSave = true;
|
||||
|
||||
private JFrame frame;
|
||||
@@ -53,8 +47,10 @@ public class MainWindow implements ActionListener {
|
||||
private JButton btnUserSave;
|
||||
private JLabel lblUserStatus;
|
||||
private JTable userTable;
|
||||
private UserTableModel userTableModel;
|
||||
private JScrollPane scrollUserList;
|
||||
private JPanel panelUserList;
|
||||
|
||||
|
||||
/**
|
||||
* Create the application.
|
||||
@@ -63,8 +59,8 @@ public class MainWindow implements ActionListener {
|
||||
* @throws InstantiationException
|
||||
* @throws ClassNotFoundException
|
||||
*/
|
||||
public MainWindow(Main pMainObject) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
|
||||
this.mainObject = pMainObject;
|
||||
public MainWindow(DbConnector pDbCon) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
|
||||
this.dbCon = pDbCon;
|
||||
initialize();
|
||||
this.frame.setVisible(true);
|
||||
}
|
||||
@@ -107,13 +103,16 @@ public class MainWindow implements ActionListener {
|
||||
panelUser.setLayout(null);
|
||||
|
||||
panelUserList = new JPanel();
|
||||
panelUserList.setBounds(0, 0, 589, 344);
|
||||
panelUserList.setBounds(0, 0, 589, 320);
|
||||
panelUserList.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Benutzerliste", TitledBorder.LEADING, TitledBorder.TOP, null, null));
|
||||
panelUser.add(panelUserList);
|
||||
panelUserList.setLayout(new BorderLayout(0, 0));
|
||||
|
||||
userTable = new JTable(new UserTableModel());
|
||||
userTable = new JTable(new UserTableModel(this.dbCon));
|
||||
userTable.setFillsViewportHeight(true);
|
||||
userTableModel = (UserTableModel)userTable.getModel();
|
||||
//userTableModel.
|
||||
|
||||
|
||||
scrollUserList = new JScrollPane(userTable);
|
||||
panelUserList.add(scrollUserList);
|
||||
@@ -121,7 +120,7 @@ public class MainWindow implements ActionListener {
|
||||
|
||||
// User-Edit-Pane
|
||||
panelUserEdit = new JPanel();
|
||||
panelUserEdit.setBounds(0, 355, 589, 87);
|
||||
panelUserEdit.setBounds(0, 331, 589, 111);
|
||||
panelUserEdit.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Benutzer hinzuf\u00FCgen / bearbeiten", TitledBorder.LEADING, TitledBorder.TOP, null, null));
|
||||
panelUserEdit.setLayout(null);
|
||||
panelUser.add(this.panelUserEdit);
|
||||
@@ -162,10 +161,11 @@ public class MainWindow implements ActionListener {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if(e.getSource() == this.btnUserSave){
|
||||
if(this.userModeSave){
|
||||
if(this.mainObject.userSave(this.textFieldUserName.getText(), this.textFieldUserSurname.getText())){
|
||||
if(this.dbCon.createUser(this.textFieldUserName.getText(), this.textFieldUserSurname.getText())){
|
||||
this.lblUserStatus.setText("Benutzer \""+this.textFieldUserName.getText()+" "+this.textFieldUserSurname.getText()+"\" erfolgreich hinzugef<65>gt.");
|
||||
this.textFieldUserName.setText("");
|
||||
this.textFieldUserSurname.setText("");
|
||||
this.userTableModel.updateTable();
|
||||
}
|
||||
else {
|
||||
this.lblUserStatus.setText("Benutzer konnte nicht erstellt werden.");
|
||||
|
||||
@@ -1,30 +1,50 @@
|
||||
package de.katho.kBorrow.gui;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
|
||||
import de.katho.kBorrow.data.KUser;
|
||||
import de.katho.kBorrow.db.DbConnector;
|
||||
|
||||
public class UserTableModel extends AbstractTableModel {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 435829735305533728L;
|
||||
|
||||
private DbConnector dbCon;
|
||||
private String[] header = {"Vorname", "Nachname", "Aktion"};
|
||||
private ArrayList<KUser> data;
|
||||
|
||||
public UserTableModel(DbConnector pDbCon){
|
||||
this.dbCon = pDbCon;
|
||||
this.updateTable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColumnCount() {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
return this.header.length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRowCount() {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
return this.data.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValueAt(int arg0, int arg1) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
public String getValueAt(int row, int col) {
|
||||
if(col == 0) return this.data.get(row).getName();
|
||||
else if (col == 1) return this.data.get(row).getSurname();
|
||||
else return null;
|
||||
}
|
||||
|
||||
public String getColumnName(int index){
|
||||
return header[index];
|
||||
}
|
||||
|
||||
public void updateTable(){
|
||||
this.data = this.dbCon.getUserList();
|
||||
this.fireTableDataChanged();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user