Package DisplayProject.table

Source Code of DisplayProject.table.TableHelper

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject.table;

import javax.swing.DefaultCellEditor;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.DefaultFormatter;

import org.apache.log4j.Logger;

import DisplayProject.ArrayColumn;
import DisplayProject.ArrayColumnModel;
import DisplayProject.factory.DropFillinFactory;
import DisplayProject.factory.TableFactory;
import Framework.DynamicArray;
import Framework.ListElement;

/**
* Builder to faciliate building jtables in a controlled manner
*/
public class TableHelper
{
    private static Logger logger = Logger.getLogger(TableHelper.class);

    int columnCount = 0;
    private JTable table;

    /**
     * Creates a new builder
     * @param rowHeight
     * @param name
     */
    public TableHelper(int rowHeight, String name)
    {
        table = TableFactory.newArrayField(name, rowHeight);
        ArrayColumnModel cm = new ArrayColumnModel();
        table.setColumnModel(cm);
    }

    /**
     * Adds an editable column based on an array of list elements
     * @param header Column header text
     * @param width width of the colum
     * @param elements the elements to choose from when editing
     */
    public void addColumn(String header, int width, ListElement[] elements )
    {
        logger.debug("Add Column [" + header + "]");
        DefaultCellEditor editor = DropFillinFactory.newDropListEditor(elements);
        ListFieldCellRenderer renderer = new ListFieldCellRenderer(new DynamicArray<ListElement>(ListElement.class,elements));
        addColumn(header,width,renderer,editor,true);
    }

    /**
     * Adds an editable column that's formatting for both rendering and editing
     * will use the specified formatter
     * @param header
     * @param width
     * @param formatter
     */
    public void addColumn(String header, int width, DefaultFormatter formatter)
    {
        TableCellEditor editor = new JFormattedTextFieldEditor(formatter);
        FormattedCellRenderer renderer = new FormattedCellRenderer(formatter, JLabel.LEFT);
        addColumn(header,width,renderer,editor,true);
    }

    /**
     * Adds an editable column that uses the specified formatters for rendering and
     * editing the columns value
     * @param header
     * @param width
     * @param rendering
     * @param editing
     */
    public void addColumn(String header, int width, DefaultFormatter rendering, DefaultFormatter editing )
    {
        TableCellEditor editor = new JFormattedTextFieldEditor(editing);
        FormattedCellRenderer renderer = new FormattedCellRenderer(rendering, JLabel.LEFT);
        addColumn(header,width,renderer,editor,true);
    }

    /**
     * Adds a new readonly column with the specified renderer
     * @param header
     * @param width
     * @param renderer
     */
    public void addColumn(String header, int width, TableCellRenderer renderer)
    {
        addColumn(header,width,renderer,null,true);
    }

    /**
     * Adds a new column with default renderer and editor
     * @param header
     * @param width
     * @param editable
     */
    public void addColumn(String header, int width, boolean editable)
    {
        addColumn(header,width,null,null,true);
    }

    /**
     * Adds a new column
     * @param header
     * @param width
     * @param renderer
     * @param editor
     * @param editable
     */
    @SuppressWarnings("deprecation")
  public void addColumn(String header, int width, TableCellRenderer renderer, TableCellEditor editor, boolean editable)
    {
        logger.debug("Add Column [" + header + "]");
        if (renderer == null && editor == null)
        {
            getColumnModel().addColumn(new ArrayColumn(header, columnCount, width, editable));
        }
        else if (renderer != null && editor == null)
        {
            getColumnModel().addColumn(new ArrayColumn(header, columnCount, width, renderer, editable));
        }
        else
        {
            getColumnModel().addColumn(new ArrayColumn(header, columnCount, width, renderer, editor, editable));
        }
        getColumnModel().getColumn(columnCount).setHeaderValue(header);
        columnCount++;
    }

    public JTable getTable()
    {
        return table;
    }

    private ArrayColumnModel getColumnModel()
    {
        return (ArrayColumnModel) table.getColumnModel();
    }

}
TOP

Related Classes of DisplayProject.table.TableHelper

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.