Package pdfdb.data.filetable

Source Code of pdfdb.data.filetable.SavedColumnModel

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

import java.awt.event.ActionEvent;
import java.io.IOException;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenuItem;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.TableColumn;
import pdfdb.data.filetable.MenuManager.PopupMenu;

/** The column model used across all tables, implements the singleton pattern
*  to ensure all events are shared.
* @author ug22cmg */
public class SavedColumnModel extends DefaultTableColumnModel
{

    private static SavedColumnModel instance = null;

    /** Initializes the SavedColumnModel. */
    private SavedColumnModel()
    {
        ColumnData data = ColumnData.getInstance();
        for (String key : data.getKeys(true))
            addColumn(getNewColumn(key));
    }

    /** Gets the single instance of the model.
     * @return The singular instance. */
    public static SavedColumnModel getInstance()
    {
        if (instance == null) instance = new SavedColumnModel();
        return instance;
    }

    /** Fired when the column is moved.
     * @param event The model event. */
    @Override
    protected void fireColumnMoved(TableColumnModelEvent event)
    {
        super.fireColumnMoved(event); // Dont hold up UI
        try
        {
            ColumnData data = ColumnData.getInstance();
            data.changeOrder(event.getFromIndex(), event.getToIndex());
            data.save();
        }
        catch (IOException ex)
        {
        }
    }

    /** Fired when the column margin is changed. */
    @Override
    protected void fireColumnMarginChanged()
    {
        super.fireColumnMarginChanged();
        try
        {
            ColumnData data = ColumnData.getInstance();
            for (TableColumn col : super.tableColumns)
            {
                data.setWidth((String) col.getIdentifier(), col.getWidth());
            }
            data.save();
        }
        catch (IOException ioe)
        {
        }
    }

    /** Gets a new column instance with the specified key.
     * @param key The key in the column config file.
     * @return A new instance of TableColumn. */
    public TableColumn getNewColumn(String key)
    {
        ColumnData data = ColumnData.getInstance();
        int index = data.indexOf(key, true);
        if (index == -1) throw new IllegalStateException();
        TableColumn col = new TableColumn();
        col.setIdentifier(key);
        col.setModelIndex(index);
        col.setHeaderValue(data.getColumnLabel(index));
        if (data.getWidth(key) != -1)
        {
            col.setWidth(data.getWidth(key));
            col.setPreferredWidth(data.getWidth(key));
        }
        return col;
    }

    /** Gets a TableColumn instance of the column with the specified identifier.
     * @param key The identifying key.
     * @return The TableColumn instance or null if not found. */
    private TableColumn getColumn(String key)
    {
        for (int i = 0; i < super.getColumnCount(); i++)
        {
            if (key.equals((String) super.getColumn(i).getIdentifier()))
                return super.getColumn(i);
        }
        return null;
    }

    /** Called when the table menu is clicked.
     * @param event The table menu event. */
    public void menuUpdate(ActionEvent event)
    {
        ColumnData data = ColumnData.getInstance();
        Object src = event.getSource();
        JCheckBoxMenuItem item = (JCheckBoxMenuItem) src;
        PopupMenu menu = (PopupMenu) item.getParent();
        String key = event.getActionCommand();
        int selectedCount = 0;
        for (JMenuItem x : menu)
        {
            selectedCount += x.isSelected() ? 1 : 0;
        }
        if (selectedCount == 0) item.setSelected(true);
        else alterModel(data, item.isSelected(), key);
    }

    /** Alters the model's data.
     * @param data The data to update.
     * @param selected Whether the menu item is now selected.
     * @param key The key to update. */
    private void alterModel(ColumnData data, boolean selected, String key)
    {
        if (selected)
        {
            addColumn(getNewColumn(key));
            try
            {
                data.setVisibility(key, true);
                data.save();
            }
            catch (IOException ioe)
            {
            }
        }
        else
        {
            removeColumn(getColumn(key));
            try
            {
                data.setVisibility(key, false);
                data.save();
            }
            catch (IOException ioe)
            {
            }
        }
    }
}
TOP

Related Classes of pdfdb.data.filetable.SavedColumnModel

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.