Package DisplayProject.table

Source Code of DisplayProject.table.ListFieldCellRenderer

/*
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 java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.table.DefaultTableCellRenderer;

import DisplayProject.RadioList;
import Framework.ListElement;

/**
* This class renders the selected value of a ListField in a cell of a JTable.
* If the data value does not match an item in the list, the first item is rendered.
* If there are no items in the list, the cell is rendered with an empty string
*/
public class ListFieldCellRenderer extends DefaultTableCellRenderer implements PropertyChangeListener{
    /**
     *
     */
    private static final long serialVersionUID = 85649056313752887L;
    private List<ListElement> elements = null;
    private JComboBox cb = null;
    private RadioList rl = null;
    public ListFieldCellRenderer() {
        super();
    }
    public ListFieldCellRenderer(List<ListElement> elements) {
        super();
        this.elements = elements;
    }
    public ListFieldCellRenderer(RadioList rl){
        super();
        this.rl = rl;
        fireChange();
        this.rl.getModel().addListDataListener(new ListDataListener() {
            public void contentsChanged(ListDataEvent e) {
                ListFieldCellRenderer.this.fireChange();
            }
            public void intervalAdded(ListDataEvent e) {
                ListFieldCellRenderer.this.fireChange();
            }
            public void intervalRemoved(ListDataEvent e) {
                ListFieldCellRenderer.this.fireChange();
            }
        });
    }
    public ListFieldCellRenderer(JComboBox cb) {
        super();
        this.cb = cb;
        fireChange();
        this.cb.getModel().addListDataListener(new ListDataListener() {
            public void contentsChanged(ListDataEvent e) {
                ListFieldCellRenderer.this.fireChange();
            }
            public void intervalAdded(ListDataEvent e) {
                ListFieldCellRenderer.this.fireChange();
            }
            public void intervalRemoved(ListDataEvent e) {
                ListFieldCellRenderer.this.fireChange();
            }
        });
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
                                                    boolean isSelected, boolean hasFocus, int row, int column) {
        Component comp =  super.getTableCellRendererComponent(table, value, isSelected,
                hasFocus, row, column);
        JLabel jl = (JLabel)comp;
        if (value instanceof Number){
            ListElement target = getElementForValue((Number) value);

            if (target == null
                && elements.size() > 0)
                target = ((ListElement)elements.get(0));

            jl.setText((target == null) ? "" : target.toString());

        }
        else
            jl.setText((value == null) ? "" : value.toString());

        jl.setHorizontalAlignment(getHorizontalAlignment());
        return comp;

    }

    public void fireChange() {
        if (cb != null) {
            this.elements = getElements(cb.getModel());
        }
        else if (rl != null) {
            this.elements = getElements(rl.getModel());
        }
    }

    public void propertyChange(PropertyChangeEvent arg0) {
        fireChange();
    }

    private List<ListElement> getElements(ListModel model)
    {
        List<ListElement> array = new ArrayList<ListElement>();
        for (int i=0; i < model.getSize(); i++) {
            array.add((ListElement)model.getElementAt(i));
        }
        return array;
    }


    public ListElement getElementForValue(Number value) {
      for (ListElement le : elements) {
            if (le.getIntegerValue() == value.intValue())
                return le;
        }
        return null;
    }
}
TOP

Related Classes of DisplayProject.table.ListFieldCellRenderer

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.