001package csheets.ext.database.ui; 002 003import csheets.core.Cell; 004import csheets.ext.database.controller.ControllerExport; 005import csheets.ext.database.core.Database; 006import csheets.ext.database.core.ThreadExport; 007import java.awt.BorderLayout; 008import java.awt.Color; 009import java.awt.Container; 010import java.awt.GridLayout; 011import java.awt.event.ActionEvent; 012import java.awt.event.ActionListener; 013import java.sql.SQLException; 014import java.util.ArrayList; 015import java.util.Observable; 016import java.util.Observer; 017import javax.swing.JButton; 018import javax.swing.JComboBox; 019import javax.swing.JFrame; 020import javax.swing.JLabel; 021import javax.swing.JOptionPane; 022import javax.swing.JPanel; 023import javax.swing.JPasswordField; 024import javax.swing.JTextField; 025 026/** 027 * GUI to export to database 028 * @author João Carreira 029 */ 030public class UIExport extends JFrame implements Observer 031{ 032 /* database available drivers stored in a string and displayed in a combobox */ 033 private String [][]dbDrivers; 034 private String []driversName; 035 private JComboBox comboDrivers; 036 037 /* controller object for GUI-controller pattern */ 038 private ControllerExport ctrlExp; 039 040 /* export thread */ 041 ThreadExport thrExp; 042 043 /* buttons */ 044 private JButton btnOk = new JButton("OK"); 045 private JButton btnCancel = new JButton("Cancel"); 046 private JButton btnUrl = new JButton("Get URL"); 047 048 /* selected cells to export in a 2D array*/ 049 private Cell [][]cells; 050 051 /* textfields for username, passord, database and table name */ 052 private JTextField userTxt, dbTxt, tableTxt, urlTxt; 053 private JPasswordField pwd; 054 055 /* label to display system information to the user */ 056 JLabel sysMsg = new JLabel(); 057 058 /** 059 * Export GUI constructor 060 * @param cells 061 * @throws Exception 062 */ 063 public UIExport(Cell[][] cells) throws Exception 064 { 065 /* window title */ 066 super("Export information to database"); 067 068 /* saving argument of this function is class variable */ 069 this.cells = cells; 070 071 /* creating a new controller */ 072 ctrlExp = new ControllerExport(this); 073 074 /* getting the list of supported databases and putting it in the combo box */ 075 dbDrivers = ctrlExp.getDBlist(); 076 driversName = new String[dbDrivers.length]; 077 for(int i = 0; i < dbDrivers.length; i++) 078 { 079 driversName[i] = dbDrivers[i][0]; 080 } 081 comboDrivers = new JComboBox(driversName); 082 083 /* main panel */ 084 JPanel mainPanel = new JPanel(new BorderLayout()); 085 086 /* defining labels */ 087 JLabel lblDBdrivers = new JLabel("Database"); 088 JLabel lblUser = new JLabel("Username"); 089 JLabel lblPwd = new JLabel("Password"); 090 JLabel lblUrl = new JLabel("URL"); 091 JLabel lblTableName = new JLabel("Table name"); 092 093 /* setting default system message text and color */ 094 sysMsg.setText("Fill the required fields"); 095 sysMsg.setForeground(Color.BLUE); 096 097 /* defining text fields */ 098 dbTxt = new JTextField(30); 099 userTxt = new JTextField(30); 100 pwd = new JPasswordField(""); 101 tableTxt = new JTextField(30); 102 urlTxt = new JTextField(30); 103 104 /* defining another panel */ 105 JPanel anotherPanel = new JPanel(new GridLayout(7,1)); 106 anotherPanel.add(lblDBdrivers); 107 anotherPanel.add(comboDrivers); 108 anotherPanel.add(lblUser); 109 anotherPanel.add(userTxt); 110 anotherPanel.add(lblPwd); 111 anotherPanel.add(pwd); 112 anotherPanel.add(lblUrl); 113 anotherPanel.add(urlTxt); 114 anotherPanel.add(lblTableName); 115 anotherPanel.add(tableTxt); 116 anotherPanel.add(sysMsg); 117 118 /* defining panel for buttons */ 119 JPanel panelBtn = new JPanel(); 120 panelBtn.add(btnOk); 121 panelBtn.add(btnCancel); 122 panelBtn.add(btnUrl); 123 124 /* setting up action listeners */ 125 HandlesEvent t = new HandlesEvent(); 126 btnOk.addActionListener(t); 127 btnCancel.addActionListener(t); 128 btnUrl.addActionListener(t); 129 130 /* adding all object to build window */ 131 Container c = getContentPane(); 132 mainPanel.add(anotherPanel); 133 mainPanel.add(panelBtn, BorderLayout.SOUTH); 134 c.add(mainPanel); 135 136 /* other window settings */ 137 pack(); 138 setVisible(true); 139 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 140 setLocationRelativeTo(null); 141 setResizable(false); 142 } 143 144 /** 145 * handles event on different GUI objects 146 */ 147 public class HandlesEvent implements ActionListener 148 { 149 @Override 150 public void actionPerformed(ActionEvent e) 151 { 152 /* default url button */ 153 if(e.getSource() == btnUrl) 154 { 155 urlTxt.setText(dbDrivers[comboDrivers.getSelectedIndex()][1]); 156 } 157 158 /* button OK*/ 159 else if(e.getSource() == btnOk) 160 { 161 /* checks if there's at least two rows to proceed with export */ 162 if(cells.length < 2) 163 { 164 JOptionPane.showMessageDialog(null, "Error: you must select at least\ntwo rows to export!"); 165 dispose(); 166 } 167 168 /* checks if all fields are filled */ 169 if(userTxt.getText().trim().length() == 0 170 || pwd.getPassword().length == 0 171 || tableTxt.getText().trim().length() == 0) 172 { 173 sysMsg.setText("Username/password/tablename required!"); 174 sysMsg.setForeground(Color.RED); 175 } 176 /* if all fields are filled tries to connect */ 177 else 178 { 179 /* the combo index indicates which database will be used */ 180 int index = comboDrivers.getSelectedIndex(); 181 182 /* the following comented code DOES NOT use a thread to export data */ 183 /* connects to a database */ 184 //ctrlExp.connect(dbDrivers[index][1], userTxt.getText(), pwd.getText(), dbDrivers[index][0]); 185 /* setting data to be exported */ 186 //ctrlExp.setDataToExport(cells, userTxt.getText(), pwd.getText(), tableTxt.getText()); 187 188 /* creating a new thread to export data */ 189 thrExp = new ThreadExport(cells, dbDrivers[index][1], userTxt.getText(), pwd.getText(), tableTxt.getText(), dbDrivers[index][0], ctrlExp); 190 thrExp.run(); 191 dispose(); 192 193 194 } 195 } 196 /* button cancel */ 197 else if(e.getSource() == btnCancel) 198 { 199 dispose(); 200 } 201 } 202 } 203 204 @Override 205 public void update(Observable o, Object arg) 206 { 207 /* TODO auto-generated code */ 208 } 209 210}