Package DisplayProject.table

Source Code of DisplayProject.table.ArrayFieldCellRenderer

/*
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.GridBagConstraints;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import DisplayProject.ArrayColumn;
import DisplayProject.Constants;
import DisplayProject.GridCell;
import DisplayProject.GridField;
import DisplayProject.GridFieldLayout;
import DisplayProject.actions.PendingAction;


/**
* Cell renderer to wrap components in a JPanel to stop them sizing to parent.
*
* @see ArrayFieldCellEditor
*
* @author Craig Mitchell
* @since 24/08/2007
*/
public class ArrayFieldCellRenderer implements TableCellRenderer {
    private TableCellRenderer defaultRenderer;
    private GridField panelWrapper;
    private GridBagConstraints gbc;
    private Color bgColor = null;
    private Color fgColor = null;

    /**
     * The list hold all the actions that are required to get this renderer back into a "clean" state
     */
    protected ArrayList<PendingAction> _actionsForRevert = new ArrayList<PendingAction>();

    /**
     * If use screen row is true, the BodyGrid refers to actual visible screen cells,
     * which stay constant as the user scrolls through a set of larger data. If this
     * value is false, the BodyGrid refers to the actual mapped data row (so you can
     * easily highlight the 3rd row of data, for example) The default is true, because
     * this was the Forte default
     */
    protected boolean useScreenRow = true;

    public ArrayFieldCellRenderer(TableCellRenderer pRenderer) {
        this.defaultRenderer = pRenderer;
        this.panelWrapper = new GridField(); // CraigM:15/10/2008 - Switched from being a JPanel to a GridField.
        this.panelWrapper.setOpaque(false);
        this.panelWrapper.setLayout(new GridFieldLayout());
        this.panelWrapper.setHeightPolicy(Constants.SP_TO_PARENT);
        this.panelWrapper.setWidthPolicy(Constants.SP_TO_PARENT);
        this.panelWrapper.setCellGravity(Constants.CG_CENTER);
        this.gbc = new GridBagConstraints();
        this.gbc.gridx = 0;
        this.gbc.gridy = 0;
        this.gbc.weightx = 1;
        this.gbc.weighty = 1;
        this.gbc.anchor = GridBagConstraints.CENTER;
    }

    public boolean isUseScreenRow() {
        return useScreenRow;
    }

    public void setUseScreenRow(boolean useScreenRow) {
        this.useScreenRow = useScreenRow;
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
     
        // First, roll back any pending actions
        for (PendingAction action : _actionsForRevert) {
            action.performAction();
        }
        this._actionsForRevert.clear();

        // Wrap the component in a panel to stop it sizing to parent
        this.panelWrapper.removeAll();
        Component comp = this.defaultRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        // TF:29 Oct 2008:Removed the body grid noise
        // The body grid always has the first visible row in the table as the first row, so we need to set up an index
//        Point loc = table.getVisibleRect().getLocation();
//        int firstRow = (this.useScreenRow ? table.rowAtPoint(loc) : 0);
//        int firstCol = (this.useScreenRow ? table.columnAtPoint(loc) : 0);

        // get the list of actions to apply to this element of the body grid
//        BodyGrid grid = BodyGrid.get(table);
//        if (grid != null) {
//          int realColumn = table.convertColumnIndexToModel(column);
//            ArrayList<ReversableAction> actions = grid.getActionsForCell(row-firstRow, realCcolumn-firstCol);
//            if (actions != null) {
//              synchronized (actions) {
//                  for (ReversableAction action : actions) {
//                      PendingAction reverse = action.applyActionTo(comp);
//                      if (reverse != null) {
//                          _actionsForRevert.add(reverse);
//                      }
//                  }
//              }
//            }
//        }
        //PM:22/11/07 bg colour from colour change
        if (this.bgColor != null) {
            if (isSelected) {
                comp.setBackground(table.getSelectionBackground());
            }
            else {
                comp.setBackground(this.bgColor);
            }
        }
        if (this.fgColor != null) {
            if (isSelected) {
                comp.setForeground(table.getSelectionForeground());
            }
            else {
                comp.setForeground(this.fgColor);
            }
        }
        this.gbc.fill = ArrayFieldCellHelper.getGBCFill(comp);
        // TF:21/10/2008:Changed this to use grid cells to obey the width and height policies
        GridCell cell = new GridCell(this.gbc);
        if (comp instanceof JComponent) {
          GridCell oldCell = GridCell.get((JComponent)comp);
          cell.setWidthPolicy(oldCell.getWidthPolicy());
          cell.setHeightPolicy(oldCell.getHeightPolicy());
        }
        // this.panelWrapper.add(comp, this.gbc);
        this.panelWrapper.add(comp, cell);

      // CraigM:16/10/2008 - Check the array column is wide enough to fit us
        // TODO CM: The ArrayField shouldn't be too small, however it sometime is.  Need to work out why.
      int columnWidth = table.getColumnModel().getColumn(column).getMinWidth();
      Dimension minSize = comp.getMinimumSize();
      int ourWidth = minSize.width;
      if (columnWidth < ourWidth) {
        ((ArrayColumn)table.getColumnModel().getColumn(column)).setAssignedWidth(ourWidth);
        ((ArrayColumn)table.getColumnModel().getColumn(column)).setMinWidth(ourWidth);
      }
     
      // TF:29 Oct 2008:The array height should always be the same size as this renderer min size
      if (minSize.height + table.getRowMargin() > table.getRowHeight()) {
        table.setRowHeight(minSize.height + table.getRowMargin());
      }
     
      // TF:14/12/2009:DET-143:Added in handling the tool tip text
      if (comp instanceof JComponent) {
        this.panelWrapper.setToolTipText(((JComponent)comp).getToolTipText());
      }
     
        return this.panelWrapper;
    }

    public TableCellRenderer getRenderer() {
        return this.defaultRenderer;
    }

    public void setBackground(Color c) {
        bgColor = c;
// This isn't needed anymore.  CraigM: 22/03/2008
//        if (this.defaultRenderer instanceof FormattedCellRenderer)
//        ((FormattedCellRenderer)this.defaultRenderer).setDefaultBackground(c);
        if (this.defaultRenderer instanceof FormattedCellRenderer) {
            ((FormattedCellRenderer)this.defaultRenderer).getDataFieldOriginalComponent().setBackground(c);
        }
        else if (this.defaultRenderer instanceof ComboBoxCellRenderer) {
            ((ComboBoxCellRenderer)this.defaultRenderer).getComboBox().setBackground(c);
        }
        else if (this.defaultRenderer instanceof CheckBoxCellRenderer) {
            ((CheckBoxCellRenderer)this.defaultRenderer).getCheckBox().setBackground(c);
        }
    }

    public void setForeground(Color c) {
        fgColor = c;
    }
}
TOP

Related Classes of DisplayProject.table.ArrayFieldCellRenderer

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.