Package pdfdb.gui.panels

Source Code of pdfdb.gui.panels.MainPanel

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.gui.panels;

import java.io.IOException;
import pdfdb.gui.*;
import pdfdb.gui.customcomponents.*;
import pdfdb.data.filetable.FileTable;
import pdfdb.app.*;
import java.awt.*;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.event.*;
import pdfdb.gui.frames.MainFrame;
import pdfdb.search.*;

/** The main panel that holds everything in the main window.
* @author ug22cmg */
public class MainPanel extends JPanel implements ChangeListener,
        ListSelectionListener
{

    private JTabbedPane tabbedPane = new JTabbedPane();
    private JTextField searchField = new DarkTextField();
    private boolean connected = false;
    private FileTable summaryTable = null;
    private FileTable oldTable = null;
    private SidePanel sidePanel = null;

    /** Initializes the MainPanel.
     * @param connected Whether to immediately connect to the data pipeline.
     * @throws java.sql.SQLException If an error occurs. */
    public MainPanel(boolean connected) throws SQLException
    {
        this.connected = connected;
        setLayout(new BorderLayout());
        initComponents();
    }

    /** Connects all tables up to the data pipeline.
     * @throws java.sql.SQLException If an error occurs. */
    public void connect() throws SQLException
    {
        if (!isConnected())
        {
            summaryTable.connect();
            for (int i = 1; i < tabbedPane.getTabCount(); i++)
            {
                getTableForTab(tabbedPane.getComponent(i)).connect();
            }
            setConnected(true);
        }
    }

    /** Gets the current connected state in a thread safe way.
     * @return the connected state */
    public synchronized boolean isConnected()
    {
        return connected;
    }

    /** Sets the connected state in a thread-safe way.
     * @param connected The new value of the connected state */
    private synchronized void setConnected(boolean connected)
    {
        this.connected = connected;
    }

    /** Initializes the GUI components.
     * @throws java.sql.SQLException If an error occurs. */
    private void initComponents() throws SQLException
    {
        summaryTable = new FileTable("Summary", isConnected());
        JComponent summaryContainer = getContainerForTable(summaryTable);
        JButton searchBtn = new DarkButton("Search");
        JPanel topPanel = new DarkPanel();
        JPanel searchPanel = new JPanel();
        String cmd = String.valueOf(EventController.OP_SEARCH);
        searchField.addKeyListener(EventController.getKeyListener());

        // Search button
        searchBtn.addActionListener(EventController.getActionListener());
        searchBtn.setActionCommand(cmd);

        searchPanel.setOpaque(false);
        searchPanel.setLayout(new BorderLayout());
        searchPanel.setBorder(BorderFactory.createEmptyBorder(7, 0, 8, 0));
        searchPanel.add(searchField, BorderLayout.CENTER);

        // top panel
        topPanel.setOpaque(false);
        topPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 0));
        topPanel.setLayout(new BorderLayout());
        topPanel.add(searchPanel, BorderLayout.CENTER);
        topPanel.add(searchBtn, BorderLayout.EAST);

        // Tabbed pane
        this.tabbedPane.addTab("Last Added", summaryContainer);
        this.tabbedPane.setBorder(
                BorderFactory.createEmptyBorder(10, 10, 10, 10));
        this.tabbedPane.getModel().addChangeListener(this);
        this.summaryTable.getSelectionModel().addListSelectionListener(this);
        this.oldTable = this.summaryTable;
        this.sidePanel = new SidePanel();

        // Add containers
        super.add(this.tabbedPane, BorderLayout.CENTER);
        super.add(topPanel, BorderLayout.NORTH);
        super.add(this.sidePanel, BorderLayout.EAST);
    }

    /** Manages changes with the tab selected index.
     * @param event The ChangeEvent. */
    @Override
    public void stateChanged(ChangeEvent event)
    {
        SingleSelectionModel model = (SingleSelectionModel) event.getSource();
        if (model.getSelectedIndex() != -1)
        {
            JComponent comp = (JComponent) tabbedPane.getComponentAt(model.
                    getSelectedIndex());

            if (!(oldTable == summaryTable && model.getSelectedIndex() == 0))
            {
                FileTable newTable = getTableForTab(comp);

                if (oldTable != null)
                {
                    oldTable.getSelectionModel().removeListSelectionListener(
                            this);
                }
                newTable.getSelectionModel().addListSelectionListener(this);
                oldTable = newTable;
            }
        }
    }

    /** Manages changes in selections of the table.
     * @param event The event. */
    @Override
    public void valueChanged(ListSelectionEvent event)
    {
        int index = oldTable.getSelectedRow();
        try
        {
            if (index == -1)
            {
                sidePanel.updateData(null);
            }
            else
            {
                sidePanel.updateData(oldTable.getFile(index));
            }
        }
        catch (IOException ex)
        {
        }
    }

    /** Gets the current search string.
     * @return An instance of the text field object. */
    public String getSearchString()
    {
        return searchField.getText();
    }

    /** Generates a containing component that contains the specified table.
     * @param table The table to add.
     * @return The containing component.*/
    private JComponent getContainerForTable(final FileTable table)
    {
        JScrollPane scrollPane = new JScrollPane(table);
        scrollPane.setDoubleBuffered(true);
        scrollPane.getViewport().setBackground(Color.WHITE);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        scrollPane.setOpaque(false);
        scrollPane.setViewportBorder(BorderFactory.createEmptyBorder());
        return scrollPane;
    }

    /** Creates a search component suitable for adding to a tab.
     * @param keywords The keywords of the search.
     * @return The search component.
     * @throws java.sql.SQLException If an error occurs. */
    private JComponent createSearchComponent(String keywords) throws
            SQLException
    {
        Search searcher = SearchProvider.getSearch(SearchType.BASIC);
        FileTable table = new FileTable("Search", searcher, keywords,
                isConnected());
        return getContainerForTable(table);
    }

    /** Adds a search tab.
     * @param text The label.
     * @param andSelect True if the tab should be set as the active tab */
    public void addSearchTab(String text, boolean andSelect)
    {
        try
        {
            final String cmd = String.valueOf(EventController.OP_CLOSE_TAB);
            int index = tabbedPane.getTabCount();
            if (text != null && text.length() > 0)
            {
                JComponent component = createSearchComponent(text);
                ClosableTab tab = new ClosableTab(text, index);
                tabbedPane.addTab(null, component);
                tabbedPane.setTabComponentAt(index, tab);
                tab.getButton().setActionCommand(cmd);
                tab.getButton().addActionListener(EventController.getActionListener());
                if (andSelect)
                {
                    tabbedPane.setSelectedComponent(component);
                }
            }
            else
            {
                String msg = "Please enter a search first.";
                JOptionPane.showMessageDialog(MainFrame.getMainFrame(), msg);
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    /** Shuts down the tab and removes it from the JTabbedPane.
     * @param index A valid index. */
    public void removeTab(int index)
    {
        if (index >= tabbedPane.getComponentCount() || index < 0)
        {
            throw new IllegalArgumentException();
        }
        Component component = tabbedPane.getComponentAt(index);
        FileTable table = getTableForTab(component);
        if (table != null)
        {
            table.shutdown();
        }
        tabbedPane.remove(index);
    }

    /** Gets the selected tab component.
     * @return The panel for the specified tab.*/
    public Component getSelectedTab()
    {
        return tabbedPane.getSelectedComponent();
    }

    /** Gets the table instance for the specified component registered with
     * the JTabbedPane.
     * @param c0 The component.
     * @return The FileTable or null if the tab has been inproperly editted. */
    public FileTable getTableForTab(Component c0)
    {
        if (c0 instanceof JScrollPane)
        {
            JScrollPane scrollPane = (JScrollPane) c0;
            return (FileTable) Hierarchy.findAnyChild(FileTable.class,
                    scrollPane);
        }
        return null;
    }
}
TOP

Related Classes of pdfdb.gui.panels.MainPanel

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.