import java.awt.*;

import java.awt.event.*;

import java.io.*;

 

import javax.swing.*;

 

import free.general.reader.*;

import free.ui.*;

import free.ui.table.*;

 

public class Demo1 extends JFrame implements ActionListener

{

    private static final long serialVersionUID = 1L;

    private JPanel jContentPane = null;

    private JScrollPane jScrollPane = null;

    private SpreadSheet jTable = null;

    //  @jve:decl-index=0:visual-constraint="260,296"

    private JButton dbButton = null;

    private JPanel ButtonPanel = null;

    private JButton textButton = null;

    private DbDialog dbd = null;

    private RemoveDialog rmdia = null;

    private FileManager filemanager = null;

    private JLabel copyrightLabel = null;

 

    private FileManager getFileManager()

    {

        if (filemanager == null)

        {

            filemanager = new FileManager(System.getProperty("user.dir").

                                        toString(), new String[]

                                        {"txt", ""});

        }

 

        return filemanager;

    }

 

    private RemoveDialog getRemoveDialog()

    {

        if (rmdia == null)

        {

            rmdia = new RemoveDialog(this, getJTable());

        }

        rmdia.setLocation(getLocation().x + 100, getLocation().y + 100);

        rmdia.setVisible(true);

 

        return rmdia;

    }

 

    /**

     * This method initializes dbButton.

     * @return javax.swing.JButton.

     */

 

    private JButton getDbButton()

    {

        if (dbButton == null)

        {

            dbButton = new JButton();

            dbButton.setText("database selection");

            dbButton.addActionListener(new java.awt.event.ActionListener()

            {

                public void actionPerformed(java.awt.event.ActionEvent e)

                {

                    databaseRoutine();

                }

            });

        }

 

        return dbButton;

    }

 

    private void databaseRoutine()

    {

        dbd = new DbDialog(this);

        dbd.setVisible(true);

        if (dbd.isLoaded())

        {

            if (dbd.isLoadWithoutChange())

            {

                getJTable().setSelectedData(dbd.getReader().getData());

            }

            else

            {

                getJTable().setModel(dbd.getReader().getData());

            }

        }

    }

 

    /**

     * This method initializes ButtonPanel.

     * @return javax.swing.JPanel.

     */

 

    private JPanel getButtonPanel()

    {

        if (ButtonPanel == null)

        {

            copyrightLabel = new JLabel();

            copyrightLabel.setText("2005-2006 by littlefree");

            copyrightLabel.setFont(new java.awt.Font("Dialog",

                    java.awt.Font.ITALIC, 12));

            copyrightLabel.setHorizontalAlignment(

javax.swing.SwingConstants.CENTER);

            ButtonPanel = new JPanel();

            ButtonPanel.setLayout(new BorderLayout());

            ButtonPanel.add(getDbButton(), java.awt.BorderLayout.WEST);

            ButtonPanel.add(getTextButton(), java.awt.BorderLayout.EAST);

            ButtonPanel.add(copyrightLabel, java.awt.BorderLayout.CENTER);

        }

 

        return ButtonPanel;

    }

 

    /**

     * This method initializes textButton.

     * @return javax.swing.JButton.

     */

 

    private JButton getTextButton()

    {

        if (textButton == null)

        {

            textButton = new JButton();

            textButton.setText("text file selection");

            textButton.addActionListener(new java.awt.event.ActionListener()

            {

                public void actionPerformed(java.awt.event.ActionEvent e)

                {

                    getTextFileRoutine();

                }

            });

        }

 

        return textButton;

    }

 

    private void getTextFileRoutine()

    {

        String filepath = getFileManager().getFileChooser(this, "open");

        if (filepath != null)

        {

            File file = new File(filepath);

            int n = JOptionPane.showConfirmDialog(

                    this,

                    "Do you want to load the first line?",

                    "Column Name Import",

                    JOptionPane.YES_NO_OPTION);

            if (n == JOptionPane.YES_OPTION)

            {

                TextSingleTableReader.noload_title_as_data = false;

            }

            else

            {

                TextSingleTableReader.noload_title_as_data = true;

            }

            try

            {

                TextSingleTableReader textreader;

                int x = JOptionPane.showConfirmDialog(this,

                        "Do you want to token for the space char?",

                        "Space Token", JOptionPane.YES_NO_OPTION);

                if (x == JOptionPane.YES_OPTION)

                {

                    textreader = new TextSingleTableReader(file, true);

                }

                else

                {

                    textreader = new TextSingleTableReader(file, false);

                }

                getJTable().setModel(textreader.getData());

            }

            catch (IOException e)

            {

                // TODO Auto-generated catch block

                System.out.println(e.getMessage());

            }

        }

    }

 

    /**

     * The main class.

     * @param args the input arguments.

     */

 

    public static void main(String[] args)

    {

        Demo1 x = new Demo1();

        x.setDefaultCloseOperation(

            javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        x.setVisible(true);

    }

 

    /**

     * This is the default constructor.

     */

 

    public Demo1()

    {

        super();

        initialize();

    }

 

    /**

     * This method initializes this.

     */

 

    private void initialize()

    {

        this.setSize(422, 360);

        this.setDefaultCloseOperation(javax.swing.WindowConstants.

                                      DISPOSE_ON_CLOSE);

        this.setContentPane(getJContentPane());

        this.setTitle(

             "Demo for SpreadSheet, database and text file reader modules");

        this.setLocation(200, 200);

        this.setVisible(true);

    }

 

    /**

     * This method initializes jContentPane.

     * @return javax.swing.JPanel.

     */

 

    private JPanel getJContentPane()

    {

        if (jContentPane == null)

        {

            jContentPane = new JPanel();

            jContentPane.setLayout(new BorderLayout());

            jContentPane.add(getJScrollPane(), java.awt.BorderLayout.CENTER);

            jContentPane.add(getButtonPanel(), java.awt.BorderLayout.SOUTH);

        }

        return jContentPane;

    }

 

    /**

     * This method initializes jScrollPane.

     * @return javax.swing.JScrollPane.

     */

 

    private JScrollPane getJScrollPane()

    {

        if (jScrollPane == null)

        {

            jScrollPane = getJTable().getJScrollPane();

        }

 

        return jScrollPane;

    }

 

    /**

     * This method initializes jTable.

     * @return javax.swing.JTable.

     */

 

    public SpreadSheet getJTable()

    {

        if (jTable == null)

        {

            jTable = new SpreadSheet(new String[][]

                                   {{"1", "2"},

                                    {"3", "4"}},

                                    new String[]

                                    {null, null});

            jTable.createPopupMenu(this);

        }

 

        return jTable;

    }

 

    /* (non-Javadoc)

     * @see java.awt.event.ActionListener#actionPerformed(ActionEvent)

     */

 

    public void actionPerformed(ActionEvent e)

    {

        JMenuItem source = (JMenuItem) (e.getSource());

        if (source.getText().equals("Copy (Ctrl+C)"))

        {

            getJTable().copytoClipBoard();

        }

        else if (source.getText().equals("Paste (Ctrl+V)"))

        {

            getJTable().pastefromClipBoard();

        }

        else if (source.getText().equals("Cut (Ctrl+X)"))

        {

            getJTable().cuttoClipBoard();

        }

        else if (source.getText().equals("Remove..."))

        {

            getRemoveDialog();

        }

    }

 

} //  @jve:decl-index=0:visual-constraint="8,60"

 

Results: