Package DisplayProject.table

Source Code of DisplayProject.table.ListElementCellEditor

/*
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.table;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultCellEditor;
import javax.swing.JTable;

import DisplayProject.ArrayColumnModel;
import DisplayProject.ArrayFieldModel;
import DisplayProject.DropListModel;
import DisplayProject.controls.ArrayField;
import DisplayProject.controls.AutoResizingComboBox;
import Framework.Array_Of_ListElement;
import Framework.DataValue;
import Framework.EventManager;
import Framework.IntegerData;
import Framework.ListElement;
import Framework.TextData;

/**
* This class is a cell editor that knows how to handle ListElements, such as DropLists and FillInFields
*/
@SuppressWarnings("serial")
public class ListElementCellEditor extends DefaultCellEditor implements ArrayFieldTableCellEditor
{
    private Class<?> editorColumnClass;
    private Component editor;
    /*
     * this is a table of values to cater for code that directly manipulates
     * the element list per row
     */
    protected transient List<Array_Of_ListElement<ListElement>> rowElements;
    protected transient ArrayField table;
    protected transient int row;
    protected transient int column;

    public ListElementCellEditor(AutoResizingComboBox comboBox) {
        super(comboBox);
        this.editor = comboBox;
    }
   
    // Override to make sure the current value is the selected item, instead of
    // the default behaviour where the last selected value is used.
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
    {
        // TF:24/11/07: During the creation of the table editor we should not fire after value change
        // events, yet setting the text value or the selected item will have this side effect. We
        // must disable this first.
        try {
            EventManager.disableEventPosting();

            if (this.table == null || (this.table != table)){
                if (table instanceof ArrayField)
                    this.table = (ArrayField)table;
            }
            this.row = row;
            this.column = column;
            //PM:14/11/07 check if we have valid row and column
            if (row >-1 && column > -1){
                if (table.getColumnModel() instanceof ArrayColumnModel){ //this ensures that we get the real column, not the displayed column index
                    int col = ((ArrayColumnModel)table.getColumnModel()).getColumn(column).getModelIndex();
                    editorColumnClass = ((ArrayFieldModel)table.getModel()).getColumnClass(col);
                } else {
                    editorColumnClass = table.getModel().getColumnClass(column);
                }
            }
            Component result = super.getTableCellEditorComponent(table, value, isSelected, row, column);
         // AD:26/6/2008 Change JComboBox to AutoResizingComboBox to reduce casting later
            if (result instanceof AutoResizingComboBox)
            {
                final AutoResizingComboBox cb = (AutoResizingComboBox) result;
                cb.setComponentPopupMenu(table.getComponentPopupMenu());
                // Tf:26/9/07:Removed this as it's not needed, providing we don't un-popup the drop list
                // in the arrayField when the editor activate mouse click gets re-posted to the editor
                // On a mouse click, we want to show the drop list.  CraigM 02/09/2007.
    //            cb.addMouseListener(new MouseAdapter() {
    //                @Override
    //                public void mousePressed(MouseEvent e) {
    //                    SwingUtilities.invokeLater(new Runnable() {
    //                        public void run() {
    //                            Integer state = (Integer)cb.getClientProperty("qq_state");
    //
    //                            if (state == Constants.FS_UPDATE) {
    //                                cb.setPopupVisible(true);
    //                            }
    //                        }
    //                    });
    //                }
    //            });

                Array_Of_ListElement<ListElement> elements = null;
                if (rowElements != null){
                    //PM:10/8/07
                    /*
                     * customize the element list per row, if there is
                     * no customisation for the row
                     * use the default
                     */
                    elements = rowElements.get(row);
                }
                if (elements == null){
                    //PM:17/7/07 add a DropListModel with the correct data
                    elements =((DropListModel)cb.getModel()).getElementList();
                }
                cb.setModel(new DropListModel(elements, cb));

                if (value instanceof DataValue && ((DataValue)value).isNull())
                    return result;

                for (int i = 0; i < cb.getModel().getSize(); i++)
                {
                    ListElement element = (ListElement) cb.getModel().getElementAt(i);
                    if (value instanceof Number || value instanceof IntegerData)
                    {
                        int num;
                        if (value instanceof Number)
                            num = ((Number)value).intValue();
                        else
                            num = ((IntegerData)value).intValue();

                        if (num == element.getIntegerValue())
                        {
                            cb.getModel().setSelectedItem(element);
                            return result;
                        }
                    }
                    else {
                        if (value != null) {
                            String aString = value.toString();
                            if (aString.equals(element.getTextValue().toString())) {
                                cb.getModel().setSelectedItem(element);
                                return result;
                            }
                        }
                    }
                }

                if (cb.getModel().getSize() > 0 && !cb.isEditable()) {
                    ListElement element = (ListElement)cb.getModel().getElementAt(0);

                    if (row >-1 && column > -1) {
                        // Convert the column to the column model index, incase they have removed some columns.  CraigM: 01/02/3008.
                        int modelIndex = table.getColumnModel().getColumn(column).getModelIndex();
                        table.getModel().setValueAt(this.getListElementValue(element),row, modelIndex);
                    }
                    cb.setSelectedIndex(0);
                } else if (value != null){
                    String aString = value.toString();
                    cb.getModel().setSelectedItem(aString);
                }
            }
            return result;
        }
        finally {
            EventManager.enableEventPosting();
        }
    }

    public Object getCellEditorValue()
    {
        //Object o = super.getCellEditorValue();

        Object o = ((AutoResizingComboBox)this.editor).getSelectedItem();

        if (o != null && o instanceof ListElement) {
            ListElement element = (ListElement) super.getCellEditorValue();
            return this.getListElementValue(element);
        }
        else {
            // This can happen for example on fill-in fields, where they are strings
            return o;
        }
    }

    /**
     * Get the value of the passed list element as an object of the appropriate type. The appropriate
     * type depends on the class this column in the table is mapped to.
     * @param element
     */
    public Object getListElementValue(ListElement element) {
        return getListElementValue(element, editorColumnClass);
       
    }

    public static Object getListElementValue(ListElement element, Class<?> elementClass) {
        if (element == null || elementClass == null) {
            return null;
        }
        if (Integer.class.equals(elementClass)) {
            return new Integer(element.getIntegerValue());
        }
        else if (IntegerData.class.isAssignableFrom(elementClass)) {
            try {
                IntegerData newValue = (IntegerData)elementClass.newInstance();
                newValue.setValue(element.getIntegerValue());
                return newValue;
            }
            catch (Exception e) {
                // Shouldn't get here, let's keep the compiler happy
                return new IntegerData(element.getIntegerValue());
            }
        }
        else if (TextData.class.isAssignableFrom(elementClass)) {
            try {
                TextData newValue = (TextData)elementClass.newInstance();
                newValue.setValue(element.getTextValue());
                return newValue;
            }
            catch (Exception e) {
                // Shouldn't get here, let's keep the compiler happy
                return element == null ? null : element.getTextValue();
            }
        }

        else if (elementClass.equals(String.class)) {
            return element.getTextValue().toString();
        }

        return null;
    }

    //PM:10/8/07
    /**
     * adds an array of ListElements to a specific row
     * @param row
     * @param elements
     */
    public void addRowElements(int row, Array_Of_ListElement<ListElement> elements){
        if (rowElements == null){
            rowElements = new ArrayList<Array_Of_ListElement<ListElement>>();
        }
        rowElements.add(row, elements);
    }

    /**
     * Remove as the array of list elements from the specified row
     * @param row
     */
    public void removeRowElements(int row){
        if (rowElements != null){
            rowElements.remove(row);
        }
    }

    /**
     * Sets the list elements to be used in a DropList/FillInField in a row in
     * an ArrayField.
     * @param row
     * @param elements
     */
    public void setRowElements(int row, Array_Of_ListElement<ListElement> elements){
        if (rowElements == null){
            rowElements = new ArrayList<Array_Of_ListElement<ListElement>>();
        }
        rowElements.set(row, elements);
    }
}
TOP

Related Classes of DisplayProject.table.ListElementCellEditor

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.