/**
* @author Niels Tijssen
* This package contains the UI classes.
*/
package org.uploadDB.ui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.uploadDB.Connect.*;
import org.uploadDB.controller.ConnectController;
import org.uploadDB.controller.CreateController;
/**
* @author Niels Tijssen 2009
* This class is the user interface for the sample login screen.
* Inner Classes
* ConnectionListener = Listener voor de test connection button.
*/
public class ConnectionScreen {
//This displays all fields listed in the application.
private JTextField fDatabase;
private JTextField fHost;
private JTextField fPort;
private JTextField fUsername;
private JPasswordField fPassword;
private JTextField fTableName;
private JTextField fVendor;
private JFrame frame;
private DBConnection con;
private CreateTable creaTable;
private InsertTable insertTable;
private JFileChooser fileOpen;
private String DBvendor;
private String DBvendorClassPath;
private String port;
private ConnectController Conc;
private CreateController CC;
/**
* This method creates the application screen.
*/
public void go() {
//Creating the screen fields
frame = new JFrame("DB upload");
setFDatabase(new JTextField());
setFHost(new JTextField());
setFPort(new JTextField());
setFUsername(new JTextField());
setFPassword(new JPasswordField());
setFTableName(new JTextField());
setFVendor(new JTextField());
setFileOpen(new JFileChooser());
//Display text lay out in the field. The default layout is left.
getFDatabase().setHorizontalAlignment(JTextField.LEFT);
getFHost().setHorizontalAlignment(JTextField.LEFT);
getFPort().setHorizontalAlignment(JTextField.LEFT);
getFUsername().setHorizontalAlignment(JTextField.LEFT);
getFPassword().setHorizontalAlignment(JTextField.LEFT);
getFTableName().setHorizontalAlignment(JTextField.LEFT);
getFVendor().setHorizontalAlignment(JTextField.LEFT);
//Make the fVendor field invisible
fVendor.setEditable(false);
//Creat JPanels p1 = application panel, p2 = button panel.
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
//Define lay out for the application panel.
p1.setLayout(new GridLayout(7,2));
//Creating the buttons
JButton connect = new JButton("Test connection");
JButton create = new JButton("Create Table");
JButton selectFile = new JButton("Select File");
//Creat the JMenu bar.
JMenuBar menuBar = new JMenuBar();
//Create menu's
JMenu dbMenu = new JMenu("Database Selection");
JMenu helpMenu = new JMenu("Help");
JMenu optionsMenu = new JMenu("Options");
JMenu exitMenu = new JMenu("Exit");
//Create db menu items
JMenuItem pgSQL = new JMenuItem("PostgreSQL");
JMenuItem mySQL = new JMenuItem("Mysql");
JMenuItem oraSQL = new JMenuItem("Oracle");
JMenuItem oraXESQL = new JMenuItem("Oracle XE");
//Add items to dbMenu
dbMenu.add(pgSQL);
dbMenu.add(mySQL);
dbMenu.add(oraSQL);
dbMenu.add(oraXESQL);
//Create options menu Items
JMenuItem csvOptions = new JMenuItem("CSV Options");
//Add items to Help menu
optionsMenu.add(csvOptions);
//Create help menu Items
JMenuItem help = new JMenuItem("Help");
JMenuItem about = new JMenuItem("About UploadDB");
//Add items to Help menu
helpMenu.add(help);
helpMenu.add(about);
//Create Exitmenu item
JMenuItem exitItem = new JMenuItem("Exit");
//Add items to exit menu.
exitMenu.add(exitItem);
//Add menu's dbMenu and exitMenu to the menu bar.
menuBar.add(dbMenu);
menuBar.add(helpMenu);
menuBar.add(optionsMenu);
menuBar.add(exitMenu);
//Add labels and fields to the p1 panel.
p1.add(new JLabel("Database"));
p1.add(getFDatabase());
p1.add(new JLabel("Host"));
p1.add(getFHost());
p1.add(new JLabel("Port Number"));
p1.add(getFPort());
p1.add(new JLabel("User Name"));
p1.add(getFUsername());
p1.add(new JLabel("Password"));
p1.add(getFPassword());
p1.add(new JLabel("Table Name"));
p1.add(getFTableName());
p1.add(new JLabel("DB vendor"));
p1.add(fVendor);
//Add buttons to the p2 panel
p2.add(connect);
p2.add(create);
p2.add(selectFile);
//Screen lay out
//Add p1, p2 and the menu to the frame.
frame.setJMenuBar(menuBar);
frame.getContentPane().add(BorderLayout.CENTER, p1);
frame.getContentPane().add(BorderLayout.SOUTH, p2);
frame.setSize(400,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
//Add actionlisteners to the different application screen items.
connect.addActionListener(new ConnectionListener());
create.addActionListener(new CreateListener());
selectFile.addActionListener(new SelectFileListener());
pgSQL.addActionListener(new pgSQLMenuListener());
mySQL.addActionListener(new mySQLMenuListener());
oraSQL.addActionListener(new oraSQLMenuListener());
oraXESQL.addActionListener(new oraxeSQLMenuListener());
help.addActionListener(new helpListener());
about.addActionListener(new aboutListener());
exitItem.addActionListener(new exitMenuListener());
csvOptions.addActionListener(new optionMenuListener());
}
/**
* This method makes all fields uneditable.
*/
public void lockScreen(){
getFDatabase().setEditable(false);
getFHost().setEditable(false);
getFPort().setEditable(false);
getFUsername().setEditable(false);
getFPassword().setEditable(false);
getFTableName().setEditable(false);
}
/**
* This method makes all fields editable.
*/
public void unlockScreen(){
getFDatabase().setEditable(true);
getFHost().setEditable(true);
getFPort().setEditable(true);
getFUsername().setEditable(true);
getFPassword().setEditable(true);
getFTableName().setEditable(true);
}
/**
* This method clears the screen.
*/
public void clearScreen(){
getFDatabase().setText("");
getFHost().setText("");
getFPort().setText("");
getFUsername().setText("");
getFPassword().setText("");
getFTableName().setText("");
}
/**
* @return con return the connection.
*/
public DBConnection getCon() {
return con;
}
/**
* @param dBvendor the dBvendor to set
*/
public void setDBvendor(String dBvendor) {
DBvendor = dBvendor;
}
/**
* @return the dBvendor
*/
public String getDBvendor() {
return DBvendor;
}
/**
* @param dBvendorClassPath the dBvendorClassPath to set
*/
public void setDBvendorClassPath(String dBvendorClassPath) {
DBvendorClassPath = dBvendorClassPath;
}
/**
* @return the dBvendorClassPath
*/
public String getDBvendorClassPath() {
return DBvendorClassPath;
}
/**
* @param port the port to set
*/
public void setPort(String port) {
this.port = port;
}
/**
* @return the port
*/
public String getPort() {
return port;
}
/**
* @param fPort the fPort to set
*/
public void setFPort(JTextField fPort) {
this.fPort = fPort;
}
/**
* @return the fPort
*/
public JTextField getFPort() {
return fPort;
}
/**
* @param creaTable the creaTable to set
*/
public void setCreaTable(CreateTable creaTable) {
this.creaTable = creaTable;
}
/**
* @return the creaTable
*/
public CreateTable getCreaTable() {
return creaTable;
}
/**
* @param fHost the fHost to set
*/
public void setFHost(JTextField fHost) {
this.fHost = fHost;
}
/**
* @return the fHost
*/
public JTextField getFHost() {
return fHost;
}
/**
* @param fTableName the fTableName to set
*/
public void setFTableName(JTextField fTableName) {
this.fTableName = fTableName;
}
/**
* @return the fTableName
*/
public JTextField getFTableName() {
return fTableName;
}
/**
* @param fPassword the fPassword to set
*/
public void setFPassword(JPasswordField fPassword) {
this.fPassword = fPassword;
}
/**
* @return the fPassword
*/
public JPasswordField getFPassword() {
return fPassword;
}
/**
* @param fUsername the fUsername to set
*/
public void setFUsername(JTextField fUsername) {
this.fUsername = fUsername;
}
/**
* @return the fUsername
*/
public JTextField getFUsername() {
return fUsername;
}
/**
* @param insertTable the insertTable to set
*/
public void setInsertTable(InsertTable insertTable) {
this.insertTable = insertTable;
}
/**
* @return the insertTable
*/
public InsertTable getInsertTable() {
return insertTable;
}
/**
* @param fDatabase the fDatabase to set
*/
public void setFDatabase(JTextField fDatabase) {
this.fDatabase = fDatabase;
}
/**
* @return the fDatabase
*/
public JTextField getFDatabase() {
return fDatabase;
}
/**
* @param fileOpen the fileOpen to set
*/
public void setFileOpen(JFileChooser fileOpen) {
this.fileOpen = fileOpen;
}
/**
* @return the fileOpen
*/
public JFileChooser getFileOpen() {
return fileOpen;
}
/**
* @param conc the conc to set
*/
public void setConc(ConnectController conc) {
Conc = conc;
}
/**
* @return the conc
*/
public ConnectController getConc() {
return Conc;
}
/**
* @param cC the cC to set
*/
public void setCC(CreateController cC) {
CC = cC;
}
/**
* @return the cC
*/
public CreateController getCC() {
return CC;
}
/**
* @return the fVendor
*/
public JTextField getFVendor() {
return fVendor;
}
/**
* @param vendor the fVendor to set
*/
public void setFVendor(JTextField vendor) {
fVendor = vendor;
}
/**
* Deze class start de connect() method from the connection class.
* @author tijssenn
*
*/
public class ConnectionListener implements ActionListener {
//Verbinding maken met de database vai de Connectie Class.
public void actionPerformed(ActionEvent e) {
//Lock Screen
lockScreen();
//Set the datbase port.
setPort(getFPort().getText());
con = new DBConnection(getFUsername().getText(),getFPassword().getPassword(),getFHost().getText(),getFPort().getText(),getFDatabase().getText(),getDBvendor(),getDBvendorClassPath());
//Maak een verbinding met de DB.
con.Connect();
//Unlock Screen
unlockScreen();
}
}
/**
* This listener is activated by the create table button en starts the method
* @author tijssenn
*
*/
public class CreateListener implements ActionListener {
//Verbinding maken met de database van de Connectie Class.
public void actionPerformed(ActionEvent e) {
//Lock Screen
lockScreen();
Conc = new ConnectController();
CC = new CreateController();
Conc.CreateConnection(org.uploadDB.core.UpdateDB.getCs());
CC.CreateTable(org.uploadDB.core.UpdateDB.getCs(), Conc, org.uploadDB.core.UpdateDB.getCP());
unlockScreen();
}
}
/**
* This method opens the file selection dialog to select a CSV file for import into the database.
* @author tijssenn
*
*/
public class SelectFileListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//Open file selection dialog.
getFileOpen().showOpenDialog(frame);
}
}
/**
* This class sets the database connection for PostgreSQL.
* @author tijssenn
*
*/
public class pgSQLMenuListener implements ActionListener {
//Verbinding maken met de database vai de Connectie Class.
public void actionPerformed(ActionEvent e) {
clearScreen();
unlockScreen();
setDBvendor("jdbc:postgresql://");
setDBvendorClassPath("org.postgresql.Driver");
getFPort().setText("5432");
fVendor.setText("PostgreSQL");
}
}
/**
* This class test the database connection for MySQL.
* @author tijssenn
*
*/
public class mySQLMenuListener implements ActionListener {
//Verbinding maken met de database vai de Connectie Class.
public void actionPerformed(ActionEvent e) {
clearScreen();
unlockScreen();
setDBvendor("jdbc:mysql://");
setDBvendorClassPath("com.mysql.jdbc.Driver");
getFPort().setText("3306");
fVendor.setText("MySQL");
}
}
/**
* This class tests the database connection for Oracle
* @author tijssenn
*
*/
public class oraSQLMenuListener implements ActionListener {
//Verbinding maken met de database vai de Connectie Class.
public void actionPerformed(ActionEvent e) {
clearScreen();
unlockScreen();
setDBvendor("jdbc:oracle:thin:@");
setDBvendorClassPath("oracle.jdbc.driver.OracleDriver");
getFPort().setText("1521");
fVendor.setText("Oracle");
}
}
public class oraxeSQLMenuListener implements ActionListener {
//Verbinding maken met de database vai de Connectie Class.
public void actionPerformed(ActionEvent e) {
clearScreen();
unlockScreen();
setDBvendor("jdbc:oracle:thin:@");
setDBvendorClassPath("oracle.jdbc.driver.OracleDriver");
getFPort().setText("1521");
fVendor.setText("Oracle");
//Defaults for Oracle XE
fDatabase.setText("XE");
fDatabase.setEditable(false);
}
}
public class optionMenuListener implements ActionListener {
//aanmaken van het optiescherm.
public void actionPerformed(ActionEvent e) {
CSVConfigScreen csvConf = new CSVConfigScreen();
csvConf.go();
}
}
/**
* This class describes the help text and a user guide in a JOptionPane.
* @author tijssenn
*
*/
public class helpListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String help =
"Thank your for using UPLoadDB. \n\n" + "With this program you can create a database table from a CSV file:\n" +
"PostgreSQL Oracle an MySQL are supported.\n" +
"Known limitations: tables are only imported as Varchar to retain flexibility use the SQL functions for conversion.\n" +
"The application defaults to a MS Excel CSV file with a ; as separator, \" as encapsulation character.";
JOptionPane.showMessageDialog(null, help, "Help", JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* This class shows the about rule in the
* @author tijssenn
*
*/
public class aboutListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String about = "Upload DB V 1.1.0 Copyright 2009 by Niels Tijssen. \n" +
"All referenced libaries are copyrighted by the authors or corporations and are available under \n" +
"the applicable license terms";
JOptionPane.showMessageDialog(null, about, "About", JOptionPane.INFORMATION_MESSAGE);
}
}
/**
* This menu performs the actions for the exit button and closes the application.
* @author tijssenn
*
*/
public class exitMenuListener implements ActionListener {
//Verbinding maken met de database vai de Connectie Class.
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
}
}