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