//Author: ^-^Veerle^-^ import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JMenuBar; import javax.swing.JOptionPane; import javax.swing.JTabbedPane; import java.awt.Dimension; import java.awt.Color; import java.awt.Container; import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.sql.Connection; import java.sql.SQLException; public class databaseGUI extends JPanel implements ActionListener { private final String strFileName = "database.mdb"; private final int width = 800, height = 200; private JTabbedPane tabbedSystem; private databaseToolbar panToolbar; private databaseMenu menuSystem; private databaseListTables listTables; private Connection connection; private void setupConnection () { try { connection = databaseConnection.getConnection(strFileName, "", ""); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error", JOptionPane.ERROR_MESSAGE); System.exit(0); } } private void setupdatabaseListTables () { try { listTables = new databaseListTables(connection); } catch (SQLException ex) { JOptionPane.showMessageDialog(null, "Error reading from\n" + System.getProperty("user.dir") + "\\" + strFileName + ". Please check permissions", "Database Access Error", JOptionPane.ERROR_MESSAGE); System.exit(0); } } public databaseGUI () { this.setPreferredSize(new Dimension(width, height)); this.setLayout(new BorderLayout()); tabbedSystem = new JTabbedPane(); tabbedSystem.setPreferredSize(new Dimension(width, (height - 40))); setupConnection(); setupdatabaseListTables(); menuSystem = new databaseMenu(this); menuSystem.tableOpen(false); panToolbar = new databaseToolbar(this); panToolbar.tableOpen(false); this.add(panToolbar, BorderLayout.NORTH); this.add(tabbedSystem, BorderLayout.SOUTH); } public Container getGUI () { return this; } public JMenuBar getMenu () { return menuSystem; } private boolean isTableOpen (String strTableName) { for (int i = 0; i < tabbedSystem.getTabCount(); i++) { if (tabbedSystem.getTitleAt(i).equalsIgnoreCase(strTableName)) { return true; } } return false; } private void tableOpen (boolean state) { menuSystem.tableOpen(state); panToolbar.tableOpen(state); } public void actionPerformed (ActionEvent e) { String strCommand = e.getActionCommand(); if (strCommand.equalsIgnoreCase("newTable")) { databaseCreateTable digCreate = new databaseCreateTable(null, listTables.getList()); digCreate.setVisible(true); String strTableName = digCreate.getChosenTable(); if (!strTableName.equals("")) { try { listTables.addTable(strTableName); tabbedSystem.addTab(strTableName, new databaseTab(strTableName, connection)); tabbedSystem.setSelectedIndex(tabbedSystem.getTabCount() - 1); JOptionPane.showMessageDialog(null, "Table " + strTableName + " was created successfully\nand is now open for inputting records into", "Table Created", JOptionPane.INFORMATION_MESSAGE); tableOpen(true); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error", JOptionPane.ERROR_MESSAGE); } } } else if (strCommand.equalsIgnoreCase("databaseSelectTable")) { databaseSelectTable digSelect = new databaseSelectTable(null, listTables.getList()); digSelect.setVisible(true); String strTableName = digSelect.getChosenTable(); if (!strTableName.equals("")) { if (isTableOpen(strTableName)) { JOptionPane.showMessageDialog(null, "Table is already open", "Database Access Error", JOptionPane.ERROR_MESSAGE); } else { try { tabbedSystem.addTab(strTableName, new databaseTab(strTableName, connection)); tableOpen(true); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error", JOptionPane.ERROR_MESSAGE); } } } } else if (strCommand.equalsIgnoreCase("databaseDeleteTable")) { databaseDeleteTable digDelete = new databaseDeleteTable(null, listTables.getList()); digDelete.setVisible(true); String strTableName = digDelete.getChosenTable(); if (!strTableName.equals("")) { if (isTableOpen(strTableName)) { JOptionPane.showMessageDialog(null, "Can not delete a table that is currently open", "Database Access Error", JOptionPane.ERROR_MESSAGE); } else { try { listTables.databaseDeleteTable(strTableName); JOptionPane.showMessageDialog(null, "Table " + strTableName + " deleted successfully", "Table Deleted", JOptionPane.INFORMATION_MESSAGE); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage(), "Database Access Error", JOptionPane.ERROR_MESSAGE); } } } } else if (strCommand.equalsIgnoreCase("closeTable")) { tabbedSystem.remove(tabbedSystem.getSelectedIndex()); if (tabbedSystem.getTabCount() == 0) { tableOpen(false); } } else if (strCommand.equalsIgnoreCase("closeAll")) { tabbedSystem.removeAll(); tableOpen(false); } else if (strCommand.equalsIgnoreCase("outputTXT")) { try { String strTable = tabbedSystem.getTitleAt(tabbedSystem.getSelectedIndex()); databaseOutput.TXT(listTables.getRecords(strTable), strTable); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage(), "Writing Error", JOptionPane.ERROR_MESSAGE); } } else if (strCommand.equalsIgnoreCase("outputXML")) { try { String strTable = tabbedSystem.getTitleAt(tabbedSystem.getSelectedIndex()); databaseOutput.XML(listTables.getRecords(strTable), strTable); } catch (Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage(), "Writing Error", JOptionPane.ERROR_MESSAGE); } } else if (strCommand.equalsIgnoreCase("exit")) { System.exit(0); } } }