/*
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.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import DisplayProject.Constants;
import DisplayProject.DropListModel;
import DisplayProject.GridCell;
import DisplayProject.UIutils;
import DisplayProject.controls.AutoResizingComboBox;
import DisplayProject.controls.DropList;
import Framework.Array_Of_ListElement;
import Framework.DynamicArray;
import Framework.IntegerData;
import Framework.ListElement;
import Framework.TextData;
/**
* This implementation of a CellRenderer displays the cell as a droplist, the value of which is the
* value of the underlying combobox. Since the renderer is just used to populate the control and
* not actually edit it, the combobox has an empty list, and only the selected value is used to
* mimic the real value. Then, when the combobox is clicked to select it's dropdown, it turns into
* an editor which actually stores the real values.
* @author tfaulkes
*
*/
@SuppressWarnings("serial")
public class ComboBoxCellRenderer extends DefaultTableCellRenderer implements ArrayFieldEmptyCellRenderer {
/**
* The underlying combobox. This stores the list of elements to be displayed
* AD:26/6/2008 Change JComboBox to AutoResizingComboBox to reduce casting later
*/
protected AutoResizingComboBox comboBox;
protected DropList comboBoxPainter;
/*
* this is a table of values to cater for code that directly manipulates
* the element list per row
*/
protected transient DynamicArray<Array_Of_ListElement<ListElement>> rowElements;
protected Color defaultBackground = UIutils.White;
protected Color defaultforeground = UIutils.Black;
/**
* Create a new rendered whose displayed elements come from the passed parameter
* @param c - the combo box on which this renderer is based.
*/
public ComboBoxCellRenderer(AutoResizingComboBox c)
{
super();
this.comboBox = c;
}
public ComboBoxCellRenderer(AutoResizingComboBox c, Color background, Color foreground)
{
super();
this.comboBox = c;
this.defaultBackground = background;
this.defaultforeground = foreground;
}
/**
* Obtain the component used to render this cell. In this case it will be a combobox.
* If the combobox is based on an integer type (and is therefore non-editable) the
* appropriate element will be displayed, or an empty combobox. Otherwise the passed
* value will be displayed within the combobox.
*/
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col)
{
AutoResizingComboBox aComboBox = null;
try {
aComboBox = (AutoResizingComboBox)this.comboBox.getClass().newInstance();
}
catch (InstantiationException e) {
aComboBox = new AutoResizingComboBox();
}
catch (IllegalAccessException e) {
aComboBox = new AutoResizingComboBox();
}
ArrayFieldCellHelper.setUpCellRenderer(this.comboBox, aComboBox); // CraigM: 28/03/2008
// TF:12/11/07:Added the sizing in of a combo box.
aComboBox.setSize(this.comboBox.getWidth(), this.comboBox.getHeight());
if (GridCell.get(this.comboBox).getWidthPolicy() == Constants.SP_EXPLICIT) {
aComboBox.setPreferredSize(new Dimension(this.comboBox.getWidth(), this.comboBox.getHeight()));
}
// CraigM:06/11/2008 - If sized natural, size to minimum (this is calculated based on the contents of the list)
else if (GridCell.get(this.comboBox).getWidthPolicy() == Constants.SP_NATURAL) {
aComboBox.setPreferredSize(this.comboBox.getMinimumSize());
}
else {
aComboBox.setPreferredSize(this.comboBox.getPreferredSize());
}
aComboBox.setMinimumSize(aComboBox.getPreferredSize());
aComboBox.setName(table.getName() + "." + row + "." + col);
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 = (Array_Of_ListElement<ListElement>)rowElements.get(row);
}
if (elements == null){
//PM:17/7/07 add a DropListModel with the correct data
elements =((DropListModel)this.comboBox.getModel()).getElementList();
}
aComboBox.setModel(new DropListModel(elements, aComboBox));
// TF:17/07/07: Added check to ensure it's editable
aComboBox.setEditable(this.comboBox.isEditable());
if (isSelected)
{
aComboBox.setForeground(table.getSelectionForeground());
aComboBox.setBackground(table.getSelectionBackground());
}
else
{
// TF:19/01/2010:DET-140:We want the background colour to be the colour of the template.
// aComboBox.setForeground(this.defaultforeground);
// aComboBox.setBackground(this.defaultBackground);
aComboBox.setForeground(this.comboBox.getForeground());
aComboBox.setBackground(this.comboBox.getBackground());
}
if (value != null)
{
if (value instanceof Number || value instanceof IntegerData)
{
// Scan through the list to find a match for this number
int num;
if (value instanceof Number) {
num = ((Number)value).intValue();
}
else {
IntegerData anInt = (IntegerData)value;
if (anInt.isNull()) {
return aComboBox;
}
else {
num = anInt.intValue();
}
}
aComboBox.setSelectedItem(null);
int limit = aComboBox.getModel().getSize(); // KM changed this.comboBox to aComboBox
for (int i = 0; i < limit; i++) {
Object o = aComboBox.getModel().getElementAt(i); // KM changed this.comboBox to aComboBox
if (o instanceof ListElement) {
if (((ListElement)o).getIntegerValue() == num)
{
//PM:9/8/07 selecting the correct value
aComboBox.setSelectedIndex(i);
return aComboBox;
}
}
}
// TF:04/09/2009:Changed this to be aComboBox. See JIRA DET-114
if (aComboBox.getModel().getSize() > 0)
{
//PM:17/7/07 added test to cater for FillInField
//Do not select the value if the item does not exist
if (!aComboBox.isEditable()){
// aComboBox.addItem(comboBox.getModel().getElementAt(0));
aComboBox.setSelectedIndex(0);
} else {
aComboBox.getEditor().setItem(value);
}
}
}
//PM:17/8/07 handle text correctly
else if (value instanceof String || value instanceof TextData){
aComboBox.setSelectedItem(null);
int limit = aComboBox.getModel().getSize(); // KM changed this.comboBox to aComboBox
for (int i = 0; i < limit; i++) {
Object o = aComboBox.getModel().getElementAt(i); // KM changed this.comboBox to aComboBox
if (o instanceof ListElement) {
if (((ListElement)o).getTextValue().toString().equalsIgnoreCase(value.toString()))
{
aComboBox.setSelectedIndex(i);
return aComboBox;
}
}
}
// TF:04/09/2009:Changed this to be aComboBox. See JIRA DET-114
if (aComboBox.getModel().getSize() > 0)
{
if (!aComboBox.isEditable()){
aComboBox.setSelectedIndex(0);
} else {
aComboBox.getEditor().setItem(value);
}
}
}
else
{
//PM:17/7/07 added test to cater for FillInField
//Do not select the value if the item does not exist
if (!aComboBox.isEditable()){
//TF:17/07/07 We cannot add a value easily, just assume it's in the list
// aComboBox.addItem(value);
aComboBox.setSelectedItem(value);
} else {
aComboBox.getEditor().setItem(value);
}
}
}
return aComboBox;
}
public AutoResizingComboBox getComboBox(){
return this.comboBox;
}
//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 DynamicArray<Array_Of_ListElement<ListElement>>(Array_Of_ListElement.class);
}
rowElements.add(row, elements);
}
public void removeRowElements(int row){
if (rowElements == null){
return;
}
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 DynamicArray<Array_Of_ListElement<ListElement>>(Array_Of_ListElement.class);
}
rowElements.set(row, elements);
}
/**
* gets the default background colour
*/
public Color getDefaultBackground() {
return defaultBackground;
}
/**
* sets the default background colour
*/
public void setDefaultBackground(Color background) {
this.defaultBackground = background;
}
/**
* gets the default foreground colour
*/
public Color getDefaultForeground() {
return defaultforeground;
}
/**
* sets the default foreground colour
*/
public void setDefaultForeground(Color foreground) {
this.defaultforeground = foreground;
}
/**
* CraigM:20/01/2009 - Create a buffered image of an empty DropList.
*/
public BufferedImage getImage(int width) {
int height = this.comboBox.getMinimumSize().height;
// TF:27/11/2009:Changed this to use the passed width
// int width = this.comboBox.getMinimumSize().width;
if (this.comboBoxPainter == null) {
this.comboBoxPainter = new DropList();
}
// TF:02/12/2009:Set the colours based on the current fillin field.
this.comboBoxPainter.setForeground(this.comboBox.getForeground());
this.comboBoxPainter.setBackground(this.comboBox.getBackground());
return ArrayFieldCellHelper.createBufferedImage(this.comboBoxPainter, width, height);
}
}