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