/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pdfdb.data.filetable;
import java.awt.Color;
import java.awt.Dimension;
import java.sql.SQLException;
import javax.swing.JTable;
import javax.swing.ToolTipManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import pdfdb.app.EventController;
import pdfdb.gui.customcomponents.CustomTableRenderer;
import pdfdb.gui.customcomponents.CustomTableRenderer.RendererType;
import pdfdb.gui.customcomponents.TableCell;
import pdfdb.search.Search;
import pdfdb.structure.IndexableFile;
/** An extension of the JTable that enables central management for several
* features including reacting to persisted changes in required columns,
* ensuring a consistent look for all FileTable instances. This means that
* user requested columns are always used.
*
* The centrally managed default table model is installed by default. This
* means that all FileTable's are hooked up to the main data-changed event
* pipeline allowing for results to be displayed more dynamically.
*
* Additionally the FileTable is set up with additional appearence settings
* to ensure they always appear the same without additional configuration.
* @author ug22cmg */
public class FileTable extends JTable
{
private TableModel tableModel = null;
private Search s = null;
private String keywords = null;
private boolean connected = false; // whether the table is the connected to db pipeline
private String name;
static
{
/* sets the menu up */
TableMenuImplementation.assertAsMenuController();
}
/** Initializes the file table.
* @param name A name to identify the table.
* @param s A searcher or null.
* @param keywords The search keywords to use with the searcher or null.
* @param connected Whether the table should connect to the data pipeline
* immediately.
* @throws java.sql.SQLException If an error occurs. */
public FileTable(String name, Search s, String keywords, boolean connected)
throws
SQLException
{
this.s = s;
this.keywords = keywords;
this.tableModel = connected ? new FileTableModel(s, keywords) : new DefaultTableModel();
this.name = name;
setConnected(connected);
init();
}
/** Initializes the FileTable as a browsable list.
* @param name The name of the table.
* @param connected Whether the table is should be immediately connected to
* the data pipeline.
* @throws java.sql.SQLException If an error occurs. */
public FileTable(String name, boolean connected) throws SQLException
{
this.tableModel = connected ? new FileTableModel() : new DefaultTableModel();
this.name = name;
setConnected(connected);
init();
}
/** Initializes the FileTable.
* @throws java.sql.SQLException If an error occurs while connecting to
* the database. */
private void init() throws SQLException
{
initData();
initGui();
}
/** Connects a dummy FileTable that has no data to a valid
* FileTableModel.
* @throws java.sql.SQLException If there is an error. */
public void connect() throws SQLException
{
if (!isConnected())
{
if (s != null)
{
this.tableModel = new FileTableModel(s, keywords);
}
else
{
this.tableModel = new FileTableModel();
}
super.setModel(this.tableModel);
setConnected(true);
}
else
{
throw new IllegalArgumentException();
}
}
/** Gets the file for the specified index.
* @param index The index in the file table model.
* @return The indexable file instance or null. */
public IndexableFile getFile(int index)
{
if (this.tableModel instanceof FileTableModel)
{
return ((FileTableModel) tableModel).getFile(index);
}
else
{
return null;
}
}
/** Initializes properties concerned with the data and sets the table
* default table model and table column model.
* @throws java.sql.SQLException If an error occurs connecting to the
* database. */
private void initData() throws SQLException
{
super.setAutoCreateColumnsFromModel(false);
super.setColumnModel(SavedColumnModel.getInstance());
super.setModel(this.tableModel);
}
/** Initializes GUI changes that need to be made to the base JTable. */
private void initGui()
{
RendererType type1 = RendererType.TABLE;
RendererType type2 = RendererType.HEADER;
CustomTableRenderer tablePainter = new CustomTableRenderer(type1);
CustomTableRenderer headerPainter = new CustomTableRenderer(type2);
super.setShowHorizontalLines(true);
super.setShowVerticalLines(false);
super.setOpaque(true);
super.setDoubleBuffered(true);
//super.setGridColor(new Color(255, 255, 255, 0));//Color.LIGHT_GRAY);
super.setDefaultRenderer(String.class, tablePainter);
super.setIntercellSpacing(new Dimension(0, 0));
super.getTableHeader().setDefaultRenderer(headerPainter);
super.setRowHeight(TableCell.H);
// For performance: Removed the tooltip feature
ToolTipManager.sharedInstance().unregisterComponent(this);
ToolTipManager.sharedInstance().unregisterComponent(getTableHeader());
this.addKeyListener(EventController.getKeyListener());
// For right click menu
this.getTableHeader().addMouseListener(EventController.getMouseListener());
}
/** Disconnects the FileTable from receiving change events so
* that memory leeks dont occur. */
public void shutdown()
{
if (tableModel != null)
{
if (tableModel instanceof FileTableModel)
{
((FileTableModel) tableModel).shutdown();
}
}
this.getTableHeader().removeMouseListener(EventController.getMouseListener());
}
/** Whether the table is in "live" state or has no data.
* @return True if connected. */
public synchronized boolean isConnected()
{
return connected;
}
/** Sets the connected variable, safe for mutlithreaded contexts.
* @param connected The new value. */
private synchronized void setConnected(boolean connected)
{
this.connected = connected;
}
/** Gets a string representation of the class for debugging purposes.
* @return String representation of the class. */
@Override
public String toString()
{
return "[ Name: " + name + " Search: " + ((s == null) ? "No" : keywords) + "]";
}
}