Package DisplayProject.table

Source Code of DisplayProject.table.RadioListCellEditor

/*
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 java.awt.Component;

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

import DisplayProject.RadioList;
import DisplayProject.RadioListModel;
import Framework.IntegerData;
import Framework.ListElement;
import Framework.NumericData;
import Framework.TextData;

/**
* Cell Editor for a {@link DisplayProject.RadioList} in an {@link DisplayProject.controls.ArrayField}.
*/
@SuppressWarnings("serial")
public class RadioListCellEditor extends DefaultCellEditor implements ArrayFieldTableCellEditor {
//    private static final int MT_INTEGER = 1;
//    private static final int MT_STRING = 2;
//    private static final int MT_INTEGERDATA = 3;
//    private static final int MT_TEXTDATA = 4;
    protected final RadioList rl;
    // Handled other types like Integer Nullable
//    protected int mapType = MT_INTEGER;
    protected Class<?> mapType = null;

    public RadioListCellEditor(final RadioList rl) {
        super(new JTextField());
        editorComponent = rl;
        this.rl = rl;
        this.clickCountToStart = 1;
        delegate = new EditorDelegate() {
            public void setValue(Object value) {
                if (value != null) {
                    mapType = value.getClass();
                    if (value instanceof Number) {
                        rl.setIntegerValue(((Number)value).intValue());
                        //mapType = MT_INTEGER;
                    } else if (value instanceof String) {
                        rl.setTextValue(value.toString());
                        //mapType = MT_STRING;
                    } else if (value instanceof NumericData) {
                        // Cater for null values. CraigM. 07/08/2007
                        if (((NumericData)value).isNull()) {
                            rl.clearSelection();
                        }
                        else {
                            rl.setIntegerValue(((IntegerData)value).intValue());
                        }
                        //mapType = MT_INTEGERDATA;
                    } else if (value instanceof TextData) {
                        rl.setTextValue(value.toString());
                        //mapType = MT_TEXTDATA;
                    }
                }
            }
            public Object getCellEditorValue() {
                return (ListElement)rl.getSelectedValue();
            }

        };
        //rl.addActionListener(delegate);
    }

    public RadioList getRadioList() {
        return rl;
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        Component result = super.getTableCellEditorComponent(table, value, isSelected, row, column);
        if (result instanceof RadioList && isSelected) {
            RadioList rl = (RadioList)result;

            // CraigM:05/08/2008 - Java changes the colours for us, so don't do this.
//            if (isSelected)
//            {
//                rl.setForeground(table.getSelectionForeground());
//                rl.setBackground(table.getSelectionBackground());
//            }
//            else
//            {
//                rl.setBackground(table.getBackground()); //PM:27/9/07
//                rl.setForeground(table.getForeground()); //PM:27/9/07
//            }

            if (value instanceof Integer)
                ((RadioListModel)rl.getModel()).setIntegerValue(((Integer)value).intValue());
            else if (value instanceof IntegerData)
                // Cater for null values. CraigM. 07/08/2007
                if (((IntegerData)value).isNull()) {
                    rl.clearSelection();
                }
                else {
                    ((RadioListModel)rl.getModel()).setIntegerValue(((IntegerData)value).intValue());
                }
            else if (value instanceof TextData)
                ((RadioListModel)rl.getModel()).setTextValue((TextData)value);
            else if (value instanceof String)
                ((RadioListModel)rl.getModel()).setTextValue(new TextData((String)value));
            else
                ((RadioListModel)rl.getModel()).setObjectValue(value);
        }
        return result;
    }

    public Object getCellEditorValue()
    {
        ListElement element = (ListElement) super.getCellEditorValue();
        return this.getListElementValue(element);
    }

    // MI2007: Set the list element value properly, based on the expected type and the type passed in.
    public Object getListElementValue(ListElement element) {
        if (element == null) {
            return null;
        }
        if (Integer.class.equals(this.mapType)) {
            return new Integer(element.getIntegerValue());
        }
        else if (IntegerData.class.isAssignableFrom(mapType)) {
            try {
                IntegerData newValue = (IntegerData)mapType.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(mapType))
            try {
                TextData newValue = (TextData)mapType.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 (mapType.equals(String.class)) {
            return element.getTextValue().toString();
        }

        return null;
    }
}
TOP

Related Classes of DisplayProject.table.RadioListCellEditor

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.