Package DisplayProject.factory

Source Code of DisplayProject.factory.TableFactory

/*
Copyright (c) 2003-2008 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.factory;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.KeyboardFocusManager;
import java.awt.event.KeyEvent;

import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

import DisplayProject.ArrayFieldModel;
import DisplayProject.Array_Of_OutlineColumnDesc;
import DisplayProject.ForteMouseAdapter;
import DisplayProject.OutlineColumnDesc;
import DisplayProject.PsuedoDoubleClickAction;
import DisplayProject.controls.ArrayField;
import DisplayProject.controls.ListView;
import DisplayProject.controls.OutlineField;

/**
* This is a factory to create a <code>JTable</code> to emulate either a Forte {@link DisplayProject.controls.ArrayField} or a Forte {@link DisplayProject.controls.ListView} in details mode.
*
*/
public class TableFactory {

    /**
     * The colour of the selected row in a ListView.
     */
    public static final Color HIGHLIGHT_COLOUR_FOCUS = new Color(28, 95, 196);
    /**
     * The colour of the selected row in an ArrayList.
     */
    public static final Color HIGHLIGHT_COLOUR_FOCUS_ARRAY = new Color(215, 235, 255);
    /**
     * The colour of the selected row in a ListView without focus
     */
    public static final Color HIGHLIGHT_COLOUR_NO_FOCUS = new Color(212, 210, 194); // new Color(236, 233, 216);

    private TableFactory() {
        super();
    }
    /**
     * creates an instance of a <code>JTable</code> that emulates a Forte ListView or ArrayField.
     * For ListView emulation in other modes see <code>JListView</code>
     * @param name name of the field
     */
    public static JTable newListView(String name){
        JTable jt = new JTable() {
            /**
             *
             */
            private static final long serialVersionUID = 1238807906012572395L;

            /*
             * Override processKeyEvent() so the tab key moves focus to the next/previous
             * component in the form rather than move within the table.
             */
            protected void processKeyEvent( KeyEvent ke ) {
                if (ke.getKeyChar() == KeyEvent.VK_TAB) {
                    if (ke.getID() == KeyEvent.KEY_TYPED) {
                        KeyboardFocusManager focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
                        if (ke.isShiftDown()) {
                            focusManager.focusPreviousComponent();
                        } else {
                            focusManager.focusNextComponent();
                        }
                    }
                    ke.consume();
                } else {
                    super.processKeyEvent(ke);
                }
            }
            public String toString() {
                return "ListView: " + getName();
            }
        };
        jt.setName( name );
        jt.setDoubleBuffered(true);
        jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        jt.getTableHeader().setReorderingAllowed(false);
        jt.setShowGrid(false);
        jt.setColumnSelectionAllowed(false);
        jt.setCellSelectionEnabled(false);
        jt.setRowSelectionAllowed(true);
        jt.setIntercellSpacing(new Dimension()); // CraigM: 19/07/2007 - Remove the gaps between the rows
        jt.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jt.addMouseListener(new ForteMouseAdapter());
        jt.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), PsuedoDoubleClickAction.PSUED0_DOUBLE_CLICK);
        jt.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_MASK),PsuedoDoubleClickAction.PSUED0_DOUBLE_CLICK);
        jt.getActionMap().put(PsuedoDoubleClickAction.PSUED0_DOUBLE_CLICK, new PsuedoDoubleClickAction());
        return jt;
    }

    /**
     * creates an instance of a <code>JTable</code> that emulates a Forte ListView in detail mode only <code>Constants.LT_DETAIL</code>.
     * For ListView emulation in other modes see <code>JListView</code></p>
     * Sets the list view column title to <code>name</code> and sets the font style to <code>headerFontStyle</code>.
     *
     * @param   headerFontStyle the font style of the column header e.g. Font.PLAIN
     * @param   name of the field
     */

    public static JTable newListView(int headerFontStyle, String name){
        JTable jt = newListView(name);

        if (headerFontStyle != Font.PLAIN) {
            JTableHeader header = jt.getTableHeader();
            final Font fontStyle = header.getFont().deriveFont(headerFontStyle);
            final TableCellRenderer headerRenderer = header.getDefaultRenderer();
            header.setDefaultRenderer( new TableCellRenderer() {
                public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column ) {
                    Component comp = headerRenderer.getTableCellRendererComponent( table, value, isSelected, hasFocus, row, column );
                    comp.setFont( fontStyle );
                    return comp;
                }
            });
        }

        return jt;
    }

    /**
     * creates an instance of a <code>JTable</code> that emulates a Forte ListView in the followin styles:
     * <li>Constants.LT_SMALLICON</li>
     * <li>Constants.LT_IMAGE</li>
     * <li>Constants.LT_LIST</li>
     * <li>Constants.LT_DETAIL</li>
     * Sets the list view column title to <code>name</code> and sets the font style to <code>headerFontStyle</code>.
     *
     */

    public static ListView newListView(String name, int ListStyle, Font font, int HeaderStyle ){
        ListView jt = new ListView(name, ListStyle, font, HeaderStyle);
        return jt;
    }

    /**
     * creates an instance of a <code>ArrayField</code> that emulates a Forte ArrayField
     * @param name name of the field
     * @param rowHeight height of the rows in pixels
     */
    public static ArrayField newArrayField(String name, int rowHeight){
        return newArrayField(name, rowHeight, Font.PLAIN);
    }
    /**
     *
     * @param name name of the field
     * @param rowHeight height of the rows in pixels
     * @param headerFontStyle the font style of the column header e.g. Font.PLAIN
     */
    public static ArrayField newArrayField(String name, int rowHeight, int headerFontStyle){
        ArrayField jt = new ArrayField(name, rowHeight, headerFontStyle);

        return jt;
    }

    /**
     * creates an instance of a <code>ArrayField</code> that emulates a Forte ArrayField
     * @param name name of the field
     * @param rowHeight height of the rows in pixels
     */
    public static ArrayField newArrayField(String name){
        return newArrayField(name, 0, Font.PLAIN);
    }

    /**
     * Creates a table that replicates a Forte table behaviour. You can Tab
     * within the table using Tab key but once you get to the end or the
     * begining of the table it will jump to the next or previous component
     * respectivly.
     * @deprecated
     * @param name
     *            Name of the table.
     * @return Returns a Jtable with a Forte like behaviour.
     */
    public static JTable newForteTable(String name, int rowHeight) {
        JTable jt = new JTable() {

            /**
             *
             */
            private static final long serialVersionUID = -7070139458707895434L;

            public void setRowHeight(int rowHeight) {
                super.setRowHeight(rowHeight);
            }

            /*
             * Override processKeyEvent() so the tab key moves focus to the
             * next/previous component in the form rather than move within the
             * table.
             */

            protected void processKeyEvent(KeyEvent ke) {

                if (ke.getKeyChar() == KeyEvent.VK_TAB) {//TAB key ?
                    if (getModel().getRowCount() == (getSelectedRow() + 1)) {//last row ?
                        if (getSelectedColumn() == (getColumnCount() - 1)) {//last column ?
                            if (ke.getID() == KeyEvent.KEY_TYPED) {//shift-TAB ?
                                KeyboardFocusManager focusManager = KeyboardFocusManager
                                .getCurrentKeyboardFocusManager();
                                if (ke.isShiftDown()) {
                                    focusManager.focusPreviousComponent();
                                } else {
                                    //int selRow = getSelectedRow();
                                    clearSelection();
                                    focusManager.focusNextComponent();
                                }
                            }
                        } else {
                            setColumnSelectionInterval(getColumnCount() - 1, getColumnCount() - 1);
                        }
                    } else {
                        if (ke.isShiftDown()) {
                            KeyboardFocusManager focusManager = KeyboardFocusManager
                            .getCurrentKeyboardFocusManager();
                            if (getSelectedRow() == 0) {
                                if (getSelectedColumn() == 0) {
                                    clearSelection();
                                    focusManager.focusPreviousComponent();
                                } else {
                                    setColumnSelectionInterval(
                                            getColumnCount() - 1,
                                            getColumnCount() - 1);
                                    super.processKeyEvent(ke);
                                }
                            } else {
                                setColumnSelectionInterval(0, 0);
                                super.processKeyEvent(ke);
                            }
                        } else {
                            if (getModel().getRowCount() == (getSelectedRow() + 1)) {
                                setColumnSelectionInterval(0, 0);
                            } else {
                                setColumnSelectionInterval(
                                        getColumnCount() - 1,
                                        getColumnCount() - 1);
                            }
                            super.processKeyEvent(ke);
                        }
                    }
                    ke.consume();
                } else {
                    super.processKeyEvent(ke);
                }

            }
        };

        jt.setName(name);
        jt.setRowHeight(rowHeight);
        jt.setDoubleBuffered(true);
        jt.setSurrendersFocusOnKeystroke(true);
        jt.setCellSelectionEnabled(false);
        jt.setColumnSelectionAllowed(false);
        jt.setRowSelectionAllowed(true);
        jt.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        // jt.setSelectionBackground(new Color(240, 240, 140));
        // jt.setSelectionForeground(java.awt.Color.BLACK);
        return jt;
    }
    /**
     * This method is called after the JTable's models have been created. It is a place holder for any post processing.
     * @param jt
     */
    public static void postModelArrayField(JTable jt){
        TableModel model = jt.getModel();
        if (model instanceof ArrayFieldModel){
            ((ArrayFieldModel)model).resizeTable();
        }
    }
    /**
     * This method is called after the JTable's models have been created. It is a place holder for any post processing.
     * @param jt
     */
    public static void postModelListView(JTable jt){

    }
    /**
     * This method is called after the JTable's models have been created. It is a place holder for any post processing.
     * @param jt
     */
    public static void postModelOutlineField(JTable jt){

    }

    /**
     * Ths method crates an OutlineField with the Array of Columns
     * @param name
     * @param columns
     */
    public static OutlineField newOutlineField(String name, Array_Of_OutlineColumnDesc<OutlineColumnDesc> columns) {
        OutlineField olf = new OutlineField();
        olf.setName(name);
        olf.setColumnList(columns);
        return olf;
    }
}
TOP

Related Classes of DisplayProject.factory.TableFactory

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.