/*
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.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import DisplayProject.Constants;
import DisplayProject.DropListModel;
import DisplayProject.GridCell;
import DisplayProject.UIutils;
import DisplayProject.controls.FillInField;
import Framework.Array_Of_ListElement;
import Framework.IntegerData;
import Framework.ListElement;
import Framework.TextData;
/**
* Implementation of the FillInField cell renderer. We create a new FillInField so we don't
* interfere with the FillInFieldCellEditor.
*
* @author Craig Mitchell
* @since 31/08/2007
*
* Added default background colour
* Added simple rendering of the editor value
*/
public class FillInFieldCellRenderer implements TableCellRenderer, ArrayFieldEmptyCellRenderer, StateColourForRowDisplayer {
private FillInField fifOriginal;
private FillInField fifCloned;
private FillInField fifPainter;
/*
* this is a table of values to cater for code that directly manipulates
* the element list per row
*/
protected Color background = UIutils.White;
protected Color foreground = UIutils.Black;
private StateColourForRow stateColour;
/**
* Create a new rendered whose displayed elements come from the passed parameter
* @param pFIF - the combo box on which this renderer is based.
*/
public FillInFieldCellRenderer(FillInField pFIF) {
try {
this.fifOriginal = pFIF;
this.fifCloned = (FillInField)(pFIF.getClass().newInstance());
ArrayFieldCellHelper.setUpCellRenderer(pFIF, this.fifCloned); // CraigM: 28/03/2008
if (pFIF.getBackground() != null)
this.background = pFIF.getBackground();
if (pFIF.getForeground() != null)
this.foreground = pFIF.getForeground();
this.fifCloned.setModel(new DropListModel(new Array_Of_ListElement<ListElement>(), this.fifCloned));
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public FillInFieldCellRenderer(FillInField pFIF, Color background, Color foreground) {
this(pFIF);
this.background = background;
this.foreground = foreground;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
// TF:02/11/2009:Since we re-use the same component to paint all the row elements, we must
// reset the value every time this method is called. If the passed value is null, then we need
// to set the item to the null value)
if (value == null) {
this.fifCloned.getEditor().setItem(value);
}
else {
if (value instanceof Number || value instanceof IntegerData) {
// Scan through the list to find a match for this number
int num = 0;
// TF:02/11/2009:Changed this logic so we don't skip the colour and size setting at the end of the method
boolean found = false;
if (value instanceof Number) {
num = ((Number)value).intValue();
}
else {
IntegerData anInt = (IntegerData)value;
if (anInt.isNull()) {
// TF:02/11/2009:If we're null, we need to set the item to N/A (the default string)
this.fifCloned.getEditor().setItem(anInt.toString());
found = true;
}
else {
num = anInt.intValue();
}
}
// this.fif.setSelectedItem(null);
if (!found) {
int limit = this.fifCloned.getModel().getSize();
for (int i = 0; i < limit; i++) {
Object o = this.fifCloned.getModel().getElementAt(i);
if (o instanceof ListElement) {
if (((ListElement)o).getIntegerValue() == num) {
this.fifCloned.getEditor().setItem(((ListElement)o).getTextValue().toString());
found = true;
}
}
}
}
// TF:02/11/2009:Removed this IF test so an empty fillin field can still have the value set properly
if (!found) {
// if (this.fifCloned.getModel().getSize() > 0) {
this.fifCloned.getEditor().setItem(value);
// }
}
}
//PM:17/8/07 handle text correctly
else if (value instanceof String || value instanceof TextData){
this.fifCloned.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
this.fifCloned.getEditor().setItem(value);
}
}
// CraigM:06/11/2008 - Set the correct size of the FillInField (copied from ComboBoxCellRenderer)
this.fifCloned.setSize(this.fifOriginal.getWidth(), this.fifOriginal.getHeight());
if (GridCell.get(this.fifOriginal).getWidthPolicy() == Constants.SP_EXPLICIT) {
this.fifCloned.setPreferredSize(new Dimension(this.fifOriginal.getWidth(), this.fifOriginal.getHeight()));
}
else if (GridCell.get(this.fifOriginal).getWidthPolicy() == Constants.SP_NATURAL) {
this.fifCloned.setPreferredSize(this.fifOriginal.getMinimumSize());
}
else {
this.fifCloned.setPreferredSize(this.fifOriginal.getPreferredSize());
}
this.fifCloned.setMinimumSize(this.fifCloned.getPreferredSize());
// TF:02/12/2009:DET-138:Explicitly set the colour of the editor to allow it to be overridden in the subclass
this.fifCloned.getEditor().getEditorComponent().setBackground(this.fifOriginal.getEditor().getEditorComponent().getBackground());
this.fifCloned.getEditor().getEditorComponent().setForeground(this.fifOriginal.getEditor().getEditorComponent().getForeground());
//PM:28/9/07 allow for row highlight
if (isSelected) {
this.fifCloned.setForeground(table.getSelectionForeground());
this.fifCloned.setBackground(table.getSelectionBackground());
}
else {
this.fifCloned.setForeground(this.getForegroundForRow(row));
this.fifCloned.setBackground(this.getBackgroundForRow(row));
}
// TF:14/12/2009:DET-143:Allowed the tool tip information to show
this.fifCloned.setToolTipText(this.fifOriginal.getToolTipText());
return this.fifCloned;
}
// TF:10/02/2009:added customisation point for cell background colour
private Color getBackgroundForRow(int row) {
if (this.stateColour != null){
return this.stateColour.getBackgroundForRow(row);
}
else {
return this.background;
}
}
// TF:10/02/2009::added customisation point for cell foreground colour
private Color getForegroundForRow(int row) {
if (this.stateColour != null){
return this.stateColour.getForegroundForRow(row);
}
else {
return this.foreground;
}
}
/**
* gets the default background colour
*/
public Color getBackground() {
return background;
}
/**
* sets the default background colour
*/
public void setBackground(Color background) {
this.background = background;
}
/**
* gets the default foreground colour
*/
public Color getForeground() {
return foreground;
}
/**
* sets the default foreground colour
*/
public void setForeground(Color foreground) {
this.foreground = foreground;
}
/**
* CraigM:20/01/2009 - Create a buffered image of an empty FillInField.
*/
public BufferedImage getImage(int width) {
int height = this.fifOriginal.getMinimumSize().height;
// TF:27/11/2009:Changed this to use the passed width
// int width = this.fifOriginal.getMinimumSize().width;
if (this.fifPainter == null) {
this.fifPainter = new FillInField();
}
// TF:02/12/2009:Set the colours based on the current fillin field.
this.fifPainter.setForeground(this.fifOriginal.getForeground());
this.fifPainter.setBackground(this.fifOriginal.getBackground());
return ArrayFieldCellHelper.createBufferedImage(this.fifPainter, width, height);
}
public void setStateColour(StateColourForRow stateColour) {
this.stateColour = stateColour;
}
}