diff --git a/src/de/katho/kBorrow/Main.java b/src/de/katho/kBorrow/Main.java index c65ed7e..74f0ef6 100644 --- a/src/de/katho/kBorrow/Main.java +++ b/src/de/katho/kBorrow/Main.java @@ -1,6 +1,9 @@ package de.katho.kBorrow; -import java.awt.EventQueue; +import java.io.IOException; +import java.sql.SQLException; + +import javax.swing.UnsupportedLookAndFeelException; import de.katho.kBorrow.db.DbConnector; import de.katho.kBorrow.db.SqlConnector; @@ -9,6 +12,7 @@ import de.katho.kBorrow.gui.MainWindow; public class Main { private DbConnector dbCon; + private Settings set; public static void main(String[] args){ new Main(); @@ -18,24 +22,41 @@ public class Main { /* * Create the apps main window. */ - Settings set = new Settings(); + this.set = new Settings(); if(set.getProperty("dBType").equals("sqlite")){ - this.dbCon = new SqliteConnector(set.getProperty("sqlitePath")); + try { + this.dbCon = new SqliteConnector(set.getProperty("sqlitePath")); + } catch (ClassNotFoundException | SQLException | IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + System.exit(1); + } + } else if(set.getProperty("dBType").equals("mysql")) { this.dbCon = new SqlConnector(); } + try { + MainWindow window = new MainWindow(this); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } - EventQueue.invokeLater(new Runnable() { + /*EventQueue.invokeLater(new Runnable() { public void run() { try { - MainWindow window = new MainWindow(); + MainWindow window = new MainWindow((Main)this); } catch (Exception e) { e.printStackTrace(); } } - }); + });*/ + } + + public boolean userSave(String pName, String pSurname) { + return this.dbCon.createUser(pName, pSurname); } } diff --git a/src/de/katho/kBorrow/db/DbConnector.java b/src/de/katho/kBorrow/db/DbConnector.java index ab69949..f4900d0 100644 --- a/src/de/katho/kBorrow/db/DbConnector.java +++ b/src/de/katho/kBorrow/db/DbConnector.java @@ -2,4 +2,5 @@ package de.katho.kBorrow.db; public interface DbConnector { + public boolean createUser(String pName, String pSurname); } diff --git a/src/de/katho/kBorrow/db/SqlConnector.java b/src/de/katho/kBorrow/db/SqlConnector.java index eff56b9..2ef3d38 100644 --- a/src/de/katho/kBorrow/db/SqlConnector.java +++ b/src/de/katho/kBorrow/db/SqlConnector.java @@ -1,5 +1,11 @@ package de.katho.kBorrow.db; public class SqlConnector implements DbConnector{ + + @Override + public boolean createUser(String pName, String pSurname) { + // TODO Auto-generated method stub + return false; + } } diff --git a/src/de/katho/kBorrow/db/SqliteConnector.java b/src/de/katho/kBorrow/db/SqliteConnector.java index 52bc595..fd73806 100644 --- a/src/de/katho/kBorrow/db/SqliteConnector.java +++ b/src/de/katho/kBorrow/db/SqliteConnector.java @@ -26,46 +26,35 @@ public class SqliteConnector implements DbConnector { * @param pHandle This string contains the path to database file the connector has to use * @throws FileNotFoundException, SQLException */ - public SqliteConnector(String pHandle) { + public SqliteConnector(String pHandle) throws ClassNotFoundException, SQLException, IOException { this.dbHandle = pHandle; this.sqlScheme = this.loadScheme(); - try { - File dbFile = new File(this.dbHandle); - Class.forName("org.sqlite.JDBC"); + File dbFile = new File(this.dbHandle); + Class.forName("org.sqlite.JDBC"); - if(dbFile.exists()){ - if(dbFile.isFile()){ - this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle); + if(dbFile.exists()){ + if(dbFile.isFile()){ + this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle); - if(!this.isValidDB(this.sqlScheme, this.connection)){ - throw new SQLException("The given db file doesn't match the required sql schema."); - } - else { - System.out.println("Db Scheme looks fine to me."); - } + if(!this.isValidDB(this.sqlScheme, this.connection)){ + throw new SQLException("The given db file doesn't match the required sql schema."); } else { - throw new IOException("Provided db handle may not be a file but a directory or a symlink!"); + System.out.println("Db Scheme looks fine to me."); } } else { - System.out.println("There is no db file yet... creating a new db."); - dbFile.createNewFile(); - - this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle); - this.initNewDB(this.sqlScheme, this.connection); - } + throw new IOException("Provided db handle may not be a file but a directory or a symlink!"); + } } - catch (ClassNotFoundException e){ - e.printStackTrace(); - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + else { + System.out.println("There is no db file yet... creating a new db."); + dbFile.createNewFile(); + + this.connection = DriverManager.getConnection("jdbc:sqlite:"+this.dbHandle); + this.initNewDB(this.sqlScheme, this.connection); } } @@ -196,4 +185,19 @@ public class SqliteConnector implements DbConnector { return text.toString(); } + public boolean createUser(String pName, String pSurname){ + try { + Statement st = this.connection.createStatement(); + String query = "INSERT INTO user (name, surname) VALUES ('"+pName+"', '"+pSurname+"')"; + + st.executeUpdate(query); + + return true; + + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + } + } diff --git a/src/de/katho/kBorrow/gui/MainWindow.java b/src/de/katho/kBorrow/gui/MainWindow.java index aeed6ea..79de4b3 100644 --- a/src/de/katho/kBorrow/gui/MainWindow.java +++ b/src/de/katho/kBorrow/gui/MainWindow.java @@ -2,48 +2,180 @@ package de.katho.kBorrow.gui; import javax.swing.JFrame; import javax.swing.JTabbedPane; +import javax.swing.UIManager; +import javax.swing.UnsupportedLookAndFeelException; + import java.awt.BorderLayout; + +import javax.swing.JOptionPane; import javax.swing.JPanel; +import javax.swing.JScrollPane; import javax.swing.JTextField; -public class MainWindow { +import javax.swing.BoxLayout; + + +import javax.swing.border.TitledBorder; +import javax.swing.JLabel; +import javax.swing.JButton; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + + + + +import de.katho.kBorrow.Main; + +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 boolean userModeSave = true; + private JFrame frame; private JTextField textField; private JTextField textField_1; + private JTabbedPane tabbedPane; + private JPanel panelUserEdit; + private JLabel lblUserName; + private JLabel lblUserSurname; + private JTextField textFieldUserName; + private JTextField textFieldUserSurname; + private JButton btnUserSave; + private JLabel lblUserStatus; + private JTable userTable; + private JScrollPane scrollUserList; + private JPanel panelUserList; /** * Create the application. + * @throws UnsupportedLookAndFeelException + * @throws IllegalAccessException + * @throws InstantiationException + * @throws ClassNotFoundException */ - public MainWindow() { + public MainWindow(Main pMainObject) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException { + this.mainObject = pMainObject; initialize(); this.frame.setVisible(true); } /** * Initialize the contents of the frame. + * @throws UnsupportedLookAndFeelException + * @throws IllegalAccessException + * @throws InstantiationException + * @throws ClassNotFoundException */ - private void initialize() { - frame = new JFrame(); - frame.setBounds(100, 100, 450, 300); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + private void initialize() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException { + this.frame = new JFrame(); + frame.setResizable(false); + this.frame.setBounds(100, 100, 600, 500); + this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP); - frame.getContentPane().add(tabbedPane, BorderLayout.CENTER); + this.tabbedPane = new JTabbedPane(JTabbedPane.TOP); + this.frame.getContentPane().add(tabbedPane, BorderLayout.CENTER); JPanel panel = new JPanel(); - tabbedPane.addTab("New tab", null, panel, null); + this.tabbedPane.addTab("New tab", null, panel, null); - textField_1 = new JTextField(); - panel.add(textField_1); - textField_1.setColumns(10); + this.textField_1 = new JTextField(); + panel.add(this.textField_1); + this.textField_1.setColumns(10); - textField = new JTextField(); - panel.add(textField); - textField.setColumns(10); + this.textField = new JTextField(); + panel.add(this.textField); + this.textField.setColumns(10); + + this.initUserTab(); + } + + private void initUserTab(){ + JPanel panelUser = new JPanel(); + this.tabbedPane.addTab("Benutzer verwalten", null, panelUser, null); + + panelUser.setLayout(null); + + panelUserList = new JPanel(); + panelUserList.setBounds(0, 0, 589, 344); + 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.setFillsViewportHeight(true); + + scrollUserList = new JScrollPane(userTable); + panelUserList.add(scrollUserList); + scrollUserList.setViewportBorder(null); + + // User-Edit-Pane + panelUserEdit = new JPanel(); + panelUserEdit.setBounds(0, 355, 589, 87); + 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); + + lblUserName = new JLabel("Vorname"); + lblUserName.setBounds(6, 27, 55, 20); + + textFieldUserName = new JTextField(); + textFieldUserName.setBounds(71, 27, 120, 20); + textFieldUserName.setColumns(10); + + lblUserSurname = new JLabel("Nachname"); + lblUserSurname.setBounds(6, 58, 62, 20); + + textFieldUserSurname= new JTextField(); + textFieldUserSurname.setBounds(70, 58, 121, 20); + textFieldUserSurname.setColumns(10); + + btnUserSave = new JButton("Speichern"); + btnUserSave.addActionListener(this); + btnUserSave.setBounds(333, 58, 86, 20); + + lblUserStatus = new JLabel(""); + lblUserStatus.setBounds(6, 89, 413, 14); + + + panelUserEdit.add(this.lblUserName); + panelUserEdit.add(this.textFieldUserName); + panelUserEdit.add(this.lblUserSurname); + panelUserEdit.add(this.textFieldUserSurname); + panelUserEdit.add(this.textFieldUserSurname); + panelUserEdit.add(this.btnUserSave); + panelUserEdit.add(this.lblUserStatus); + + } + + @Override + public void actionPerformed(ActionEvent e) { + if(e.getSource() == this.btnUserSave){ + if(this.userModeSave){ + if(this.mainObject.userSave(this.textFieldUserName.getText(), this.textFieldUserSurname.getText())){ + this.lblUserStatus.setText("Benutzer \""+this.textFieldUserName.getText()+" "+this.textFieldUserSurname.getText()+"\" erfolgreich hinzugefügt."); + this.textFieldUserName.setText(""); + this.textFieldUserSurname.setText(""); + } + else { + this.lblUserStatus.setText("Benutzer konnte nicht erstellt werden."); + } + } + else { + // Hier kommt die Funktion rein, die Benutzer editieren lässt. + } + + } - JPanel panel_1 = new JPanel(); - tabbedPane.addTab("New tab", null, panel_1, null); } - } diff --git a/src/de/katho/kBorrow/gui/UserTableModel.java b/src/de/katho/kBorrow/gui/UserTableModel.java new file mode 100644 index 0000000..01728aa --- /dev/null +++ b/src/de/katho/kBorrow/gui/UserTableModel.java @@ -0,0 +1,30 @@ +package de.katho.kBorrow.gui; + +import javax.swing.table.AbstractTableModel; + +public class UserTableModel extends AbstractTableModel { + + /** + * + */ + private static final long serialVersionUID = 435829735305533728L; + + @Override + public int getColumnCount() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public int getRowCount() { + // TODO Auto-generated method stub + return 0; + } + + @Override + public Object getValueAt(int arg0, int arg1) { + // TODO Auto-generated method stub + return null; + } + +}