001package csheets.ext.database.ui;
002
003import csheets.core.Spreadsheet;
004import csheets.core.formula.compiler.FormulaCompilationException;
005import java.awt.BorderLayout;
006import java.awt.Color;
007import java.awt.Container;
008import java.awt.GridLayout;
009import java.awt.event.ActionEvent;
010import java.awt.event.ActionListener;
011import java.util.logging.Level;
012import java.util.logging.Logger;
013import javax.swing.JButton;
014import javax.swing.JFrame;
015import javax.swing.JLabel;
016import javax.swing.JOptionPane;
017import javax.swing.JPanel;
018import javax.swing.JScrollPane;
019import javax.swing.JTable;
020
021/**
022 * A window for user to confirm data to be imported from the database
023 * @author João Carreira
024 */
025class ConfirmImportUI extends JFrame
026{
027    private Spreadsheet spreadSheet;
028    private JLabel sysMsg;
029    private JTable table;
030    private JScrollPane scroll;
031    private JButton btnOk, btnCancel;
032    private String[][] tableData;
033    private boolean flag;
034    
035    public ConfirmImportUI(Spreadsheet spreadSheet, String [][] tableData)
036    {
037        setTitle("Data import preview window");
038        this.tableData = tableData;
039        this.spreadSheet = spreadSheet;
040        this.flag = flag;
041        
042        /* setting up column names */
043        String []columns = new String[tableData[0].length - 1];
044        for(int i = 0; i < columns.length; i++)
045        {
046            columns[i] = tableData[0][i + 1];
047        }
048        
049        /* setting up table data */
050        String [][]data = new String[tableData.length - 1][tableData[0].length - 1];
051        for(int i = 0; i < data.length; i++)
052        {
053            for(int j = 0; j < data[0].length; j++)
054            {
055                data[i][j] = tableData[i + 1][j + 1];
056            }
057        }
058        
059        /* label for text */
060        JLabel sysMsg = new JLabel("Confirm import?");
061        sysMsg.setForeground(Color.BLUE);
062        
063        /* buttons */
064        btnOk = new JButton("OK");
065        btnCancel = new JButton("Cancel");
066        
067        /* main panel */
068        JPanel mainPanel = new JPanel(new GridLayout(3,3));
069        
070        /* table panel */
071        JPanel tabPanel = new JPanel(new BorderLayout());
072        table = new JTable(data, columns);
073        scroll = new JScrollPane(table);
074        tabPanel.add(scroll, BorderLayout.CENTER);
075        
076        /* msg panel */
077        JPanel msgPanel = new JPanel();
078        msgPanel.add(sysMsg);
079        
080        /* button panel */
081        JPanel btnPanel = new JPanel();
082        btnPanel.add(btnOk);
083        btnPanel.add(btnCancel);
084        
085        /* setting up action listeners */
086        HandlesEvent t = new HandlesEvent();
087        btnOk.addActionListener(t);
088        btnCancel.addActionListener(t);
089        
090        /* putting everything together */
091        Container c = getContentPane();
092        mainPanel.add(tabPanel);
093        mainPanel.add(msgPanel);
094        mainPanel.add(btnPanel);
095        c.add(mainPanel);
096        
097        /* other settings */
098        //pack();
099        setSize(400,300);
100        setVisible(true);
101        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
102        setLocationRelativeTo(null);
103        setResizable(false);
104    }
105    
106        /**
107     * handles events on different GUI objects
108     */
109    public class HandlesEvent implements ActionListener
110    {
111        @Override
112        public void actionPerformed(ActionEvent e) 
113        {
114            /* ok button */
115            if(e.getSource() == btnOk)
116            {
117                try 
118                {
119                    /* getting the starting row, which is defined in any of the first columns */
120                    int startRow = Integer.parseInt(tableData[1][0]) - 1;
121                    /* cycles the entire tableData array */
122                    for(int i = 0; i < tableData.length; i++)
123                    {
124                        for(int j = 1; j < tableData[0].length; j++)
125                        {         
126                         /* changes the content of the given cell taking into account the row
127                         (we have to subtract 2 to go to right place) */
128                         spreadSheet.getCell(j - 1, i + startRow).setContent(tableData[i][j]);
129                         /* the next line doesn't take into account the initial row */
130                         //spreadSheet.getCell(j - 1, i).setContent(tableData[i][j]);
131                        }
132                    }
133                        JOptionPane.showMessageDialog(null, "Data successfully imported");
134                }
135                catch (FormulaCompilationException ex) 
136                {
137                    JOptionPane.showMessageDialog(null, "Error: importing data");
138                    Logger.getLogger(UITableSelect.class.getName()).log(Level.SEVERE, null, ex);
139                }
140                dispose();
141            }
142            /* cancel button */
143            if(e.getSource() == btnCancel)
144            {
145                System.out.println("Confirm Import = " + getFlag());
146                dispose();
147            }     
148        }
149    }
150    
151     
152    public boolean getFlag()
153    {
154        return this.flag;
155    }
156    
157    public void setFlag()
158    {
159        this.flag = true;
160    }
161}