001/*
002 * To change this template, choose Tools | Templates
003 * and open the template in the editor.
004 */
005package csheets.ext.database.ui;
006
007import csheets.core.Cell;
008import csheets.ext.database.controller.ControllerImport;
009import csheets.ext.database.controller.ControllerUpdate;
010import java.awt.BorderLayout;
011import java.awt.Color;
012import java.awt.Container;
013import java.awt.Dimension;
014import java.awt.GridLayout;
015import java.awt.event.ActionEvent;
016import java.awt.event.ActionListener;
017import java.util.ArrayList;
018import javax.swing.JButton;
019import javax.swing.JFrame;
020import javax.swing.JLabel;
021import javax.swing.JList;
022import javax.swing.JPanel;
023import javax.swing.JScrollPane;
024import javax.swing.ListSelectionModel;
025import csheets.ui.ctrl.UIController;
026import javax.swing.JOptionPane;
027
028
029/**
030 * Table selection GUI (to select a table from the database)
031 * @author João Carreira
032 */
033public class UITableSelectUpdate extends JFrame
034{
035    /* labels */
036    JLabel sysMsg = new JLabel("Select one table from above");
037    
038    /* buttons */
039    private JButton btnOk = new JButton("OK");
040    private JButton btnCancel = new JButton("Cancel");
041    
042    /* array with table list */
043    private String[] tableArray;
044    private ArrayList arrayQueries;
045    
046    /* array with database table content */
047    private String[][] tableData;
048    
049    /* tablelist */
050    private JList tableList;
051    
052    private ControllerUpdate ctrlUp;
053   
054    private UIController uiCtrl;
055    
056    private Cell [][]cells;
057    private String [][]selectedCells;
058    
059    private int numberOfCols;
060     
061    /**
062     * constructor of the GUI for table selection 
063     * @param dbName name of the database
064     * @throws Exception 
065     */
066    public UITableSelectUpdate(Cell [][]cells, String dbName, ControllerUpdate ctrlUp)
067    {
068        /* window title */
069        super("Select a table from " + dbName);
070        
071        /* labels */
072        sysMsg.setForeground(Color.BLUE);
073        
074        this.ctrlUp = ctrlUp;
075        
076        this.cells = cells;
077        
078        /* number of columns of current selected cells */
079        this.numberOfCols = cells[0].length;
080        
081        /* gets the table list */
082        tableArray = ctrlUp.getTableList();
083             
084        /* Jlist with table list for database */
085        tableList = new JList(tableArray);
086        tableList.setVisibleRowCount(5);
087        tableList.setPreferredSize(new Dimension(100,100));
088        tableList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
089        tableList.setSelectedIndex(0);
090        JScrollPane scroll = new JScrollPane(tableList); 
091        
092        /* main panel */
093        JPanel mainPanel = new JPanel(new GridLayout(3,3));
094        
095        /* list panel */
096        JPanel anotherPanel = new JPanel();
097        anotherPanel.add(scroll);
098        anotherPanel.add(sysMsg);
099        
100        /* sysMsg panel */
101        JPanel msgPanel = new JPanel();
102        msgPanel.add(sysMsg);
103        
104        /* button panel */
105        JPanel buttonPanel = new JPanel();
106        buttonPanel.add(btnOk);
107//        buttonPanel.add(btnPreview);
108        buttonPanel.add(btnCancel);
109        
110        /* setting up action listeners */
111        HandlesEvent t = new HandlesEvent();
112        btnOk.addActionListener(t);
113        btnCancel.addActionListener(t);
114//        btnPreview.addActionListener(t);
115        
116        /* putting everything together */
117        Container c = getContentPane();
118        mainPanel.add(anotherPanel);
119        mainPanel.add(msgPanel, BorderLayout.CENTER);
120        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
121        c.add(mainPanel);     
122        
123        /* other window settings */
124        setSize(300,350);
125        setVisible(true);
126        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
127        setLocationRelativeTo(null);
128        setResizable(false);
129    }
130    
131    
132    /**
133     * handles events on different GUI objects
134     */
135    public class HandlesEvent implements ActionListener
136    {
137        @Override
138        public void actionPerformed(ActionEvent e) 
139        {
140            boolean flag = false;
141            
142            /* ok button */
143            if(e.getSource() == btnOk)
144            {
145                /* loads a given database table to the table data array */
146                tableData = ctrlUp.loadTable(tableList.getSelectedValue().toString());
147                int numberColsTargetTable = tableData[0].length - 1;
148                /* if column number is the same we can update */
149                if(numberColsTargetTable == numberOfCols)
150                {
151                    selectedCells = ctrlUp.cellsTo2dArray(cells);
152                    boolean isDifferent = ctrlUp.compareCellsWithDB(tableData, selectedCells);
153                    if(isDifferent)
154                    {
155                        ctrlUp.updateTable(tableList.getSelectedValue().toString(), tableData, selectedCells, cells);
156                        JOptionPane.showMessageDialog(null, tableList.getSelectedValue().toString() + " database: data successfully updated!");
157                    }
158                    else if(selectedCells.length < tableData.length)
159                    {
160                       ctrlUp.updateTableWithDeletion(tableList.getSelectedValue().toString(), tableData, selectedCells, cells);
161                    }
162                    else
163                    {
164                        JOptionPane.showMessageDialog(null, "Database table is already up-to-date!");
165                    }
166                }
167                /* if it's not we can't update */
168                else
169                {
170                   JOptionPane.showMessageDialog(null, "Error: you can't proceed with update!\nTables differ in column number!\nSelected columns = " + numberOfCols + "\nTable columns = " + numberColsTargetTable);
171                }
172            }
173            /* cancel button */
174            else if(e.getSource() == btnCancel)
175            {
176                dispose();
177            }     
178        }
179    }
180}
181