/*
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;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import DisplayProject.actions.ArrayVisibleRows;
import DisplayProject.actions.ReversableAction;
import DisplayProject.table.ArrayFieldCellEditor;
import DisplayProject.table.ArrayFieldCellRenderer;
import Framework.DynamicArray;
/**
* A class that encapsulates widget information from cells within a JTable
* @author Tim
*
*/
public class BodyGrid implements CompoundField {
public static final String cBODY_KEY = "qq_BodyGrid";
protected JTable table;
private List<List<BodyGridCell>> cells = null;
private int columns = 0;
private int rows = -1;
public BodyGrid(JTable pTable) {
this.table = pTable;
pTable.putClientProperty(cBODY_KEY, this);
if (pTable.getColumnModel() instanceof ArrayColumnModel) {
columns = ((ArrayColumnModel)pTable.getColumnModel()).getRealColumnCount();
} else {
columns = this.table.getColumnModel().getColumnCount();
}
rows = ArrayVisibleRows.get(pTable);
}
public static BodyGrid get(JTable pTable) {
BodyGrid grid = (BodyGrid)pTable.getClientProperty(cBODY_KEY);
if (grid == null) {
grid = new BodyGrid(pTable);
}
return grid;
}
/**
* Get the cell for a particular row and column. <i><b>Note that the row and column
* parameters are 1-based array values, not 0-based array values!</b></i>
* @param pRow
* @param pColumn
* @param pCreate
* @return
*/
protected BodyGridCell getCell(int pRow, int pColumn, boolean pCreate) {
if (cells == null) {
if (pCreate) {
cells = new DynamicArray<List<BodyGridCell>>();
}
else {
return null;
}
}
List<BodyGridCell> colList = cells.get(pRow);
if (colList == null) {
if (pCreate) {
colList = new DynamicArray<BodyGridCell>();
cells.set(pRow, colList);
}
else {
return null;
}
}
BodyGridCell cell = colList.get(pColumn);
if (cell == null) {
if (pCreate) {
cell = new BodyGridCell(pRow, pColumn, this);
colList.set(pColumn, cell);
}
}
return cell;
}
/**
* Returns the cell render for this cell
* @param pRow one based
* @param pColumn one based
* @return
*/
public JComponent getChildInCell(int pRow, int pColumn) {
return getChildInCell(pRow, pColumn, false);
}
/**
* Returns the cell render, by default, or cell editor for this cell
* @param pRow one based
* @param pColumn one based
* @param getEditor if this is set to true, the editor will be returned in place of the renderer
* @return
*/
public JComponent getChildInCell(int pRow, int pColumn, boolean getEditor) {
pRow -= 1;
pColumn -=1;
ArrayColumnModel model = (ArrayColumnModel)this.table.getColumnModel();
TableCellEditor editor = model.getRealColumn(pColumn).getCellEditor();
TableCellRenderer renderer = model.getRealColumn(pColumn).getCellRenderer();
if (editor instanceof ArrayFieldCellEditor) {
editor = ((ArrayFieldCellEditor)editor).getEditor();
}
if (renderer instanceof ArrayFieldCellRenderer) {
renderer = ((ArrayFieldCellRenderer)renderer).getRenderer();
}
JComponent c;
if (getEditor) {
// Not used anymore. CraigM: 27/02/2008
// if (editor instanceof ComponentCellEditor) {
// c = (JComponent)((ComponentCellEditor)editor).getComponent();
// }
// else if (editor instanceof DefaultCellEditor) {
if (editor instanceof DefaultCellEditor) {
c = (JComponent)((DefaultCellEditor)editor).getComponent();
}
else {
//c = (JComponent)editor.getTableCellEditorComponent(this.table, this.table.getValueAt(pRow, pColumn), false, pRow, pColumn);
c = (JComponent)editor.getTableCellEditorComponent(this.table, this.table.getModel().getValueAt(pRow, pColumn), false, pRow, pColumn);
}
} else {
// Not used anymore. CraigM: 27/02/2008
// if (renderer instanceof ComponentCellRenderer) {
// c = (JComponent)((ComponentCellRenderer)renderer).getTableColumnTemplate();
// }
// else {
//c = (JComponent)renderer.getTableCellRendererComponent(this.table, this.table.getValueAt(pRow, pColumn), false, false, pRow, pColumn);
c = (JComponent)renderer.getTableCellRendererComponent(this.table, this.table.getModel().getValueAt(pRow, pColumn), false, false, pRow, pColumn);
// }
}
BodyGridCell cell = this.getCell(pRow, pColumn, true);
cell.setTemplate(c);
return c;
}
public JComponent[] getComponents() {
JComponent[] _Result = new JComponent[this.getComponentCount()];
int index = 0;
for (int row=0; row<this.getRows(); row++) {
for (int col=0; col<this.getColumns(); col++) {
_Result[index] = this.getChildInCell(row+1, col+1);
index++;
}
}
return _Result;
}
public int getComponentCount() {
return this.getRows() * this.getColumns();
}
/**
* Get a list of the reversable actions for this cell. Note that the row and column are 0-based offsets, not 1-based.
* @param pRow
* @param pColumn
* @return
*/
public List<ReversableAction> getActionsForCell(int pRow, int pColumn) {
BodyGridCell c = this.getCell(pRow, pColumn, false);
return (c == null) ? null : c.getPendingActions();
}
/**
* @return Returns the columns.
*/
public int getColumns() {
return columns;
}
/**
* @return Returns the rows.
*/
public int getRows() {
return rows;
}
/**
* Return the underlying table
*/
public JTable getTable() {
return table;
}
/**
* Sets the column justify weight
* @param column
* @param weight
*/
public void setColumnJustifyWeight(int column, byte weight) {
// CraigM:24/06/2008 - Use the real column
ArrayColumnModel model = (ArrayColumnModel)table.getColumnModel();
ArrayColumn ac = model.getRealColumn(column-1);
ac.setColumnWeight(weight);
}
/**
* Sets the column justify weight
* @param column
* @param weight
*/
public void setColumnJustifyWeight(int column, float weight) {
// CraigM:24/06/2008 - Use the real column
ArrayColumnModel model = (ArrayColumnModel)table.getColumnModel();
ArrayColumn ac = model.getRealColumn(column-1);
ac.setColumnWeight(weight);
}
}