/*
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.DropListModel;
import DisplayProject.controls.AutoResizingComboBox;
import Framework.Array_Of_ListElement;
import Framework.IntegerData;
import Framework.ListElement;
import Framework.TextData;
/**
* This class is used to display combo boxes in JTables. Each column in the JTable that has
* a combobox template must use this class as the cell editor. Otherwise, the combo box wil
* not base it's initial drop-down value on the current value in the cell, but rather on the
* last value that was edited in that column. This is a problem for the user.
* @author faulkest
*
*/
@SuppressWarnings("serial")
public class ListFieldCellEditor extends DefaultCellEditor implements ArrayFieldTableCellEditor {
/*
* this is a table of values to cater for code that directly manipulates
* the element list per row
*/
private transient List<Array_Of_ListElement<ListElement>> rowElements;
private Array_Of_ListElement<ListElement> defaultElements;
public ListFieldCellEditor(AutoResizingComboBox comboBox) {
super(comboBox);
if (comboBox.getModel() instanceof DropListModel) {
this.defaultElements = ((DropListModel)comboBox.getModel()).getElementList();
}
}
/**
* Overridden method. This is overridden to correct the value in the combo box prior to
* displaying it's value. Initially the value will be set to the last edited value for
* any row in that column, but we want to set it to the current value of the current row
* in this column.
*
* @see DefaultCellEditor
*/
public Component getTableCellEditorComponent(JTable table,
Object value,
boolean isSelected,
int row,
int 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) {
AutoResizingComboBox cb = (AutoResizingComboBox)result;
if (cb.getModel() instanceof DropListModel){
DropListModel model = (DropListModel)cb.getModel();
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 = this.defaultElements;
}
cb.setModel(new DropListModel(elements, cb));
if (value instanceof Integer) {
model.setIntegerValue(((Integer)value).intValue());
}
else if (value instanceof IntegerData) {
model.setIntegerValue(((IntegerData)value).intValue());
}
else if (value instanceof TextData) {
model.setTextValue((TextData)value);
}
else if (value instanceof String) {
model.setTextValue(new TextData((String)value));
}
else {
model.setObjectValue(value);
}
}
}
return result;
}
//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);
}
public void removeRowElements(int row){
if (rowElements == null){
return;
}
rowElements.remove(row);
}
}