Package DisplayProject.controls

Source Code of DisplayProject.controls.OutlineFieldCellRenderer

/*
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.controls;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeNode;

import DisplayProject.Array_Of_OutlineColumnDesc;
import DisplayProject.Constants;
import DisplayProject.DisplayNode;
import DisplayProject.OutlineColumnDesc;
import DisplayProject.factory.TableFactory;

/**
* A cell renderer for OutlineField that knows how to render columns, and take into account
* the tree controls.
*
* @author Craig Mitchell
* @since 23/04/2007.
*/
public class OutlineFieldCellRenderer implements TreeCellRenderer {
    private OutlineField parent;
    private boolean hasRowHighlights;
    private DefaultTreeCellRenderer defaultTreeRenderer;
    private JPanel panel;
    private OutlineFieldJLabel[] cachedJLabels;

    public OutlineFieldCellRenderer(OutlineField pParent) {
        this.parent = pParent;
        this.hasRowHighlights = true;
        this.defaultTreeRenderer = new DefaultTreeCellRenderer();
        this.panel = new JPanel();
        this.panel.setLayout(new GridBagLayout());
        this.cachedJLabels = null;
    }

    /**
     * Return how many parents before we hit the root node.
     *
     * @param pNode
     */
    private int getNumParents(TreeNode pNode) {
        int _Result = 0;

        pNode = pNode.getParent();

        while (pNode != null) {
            pNode = pNode.getParent();
            _Result++;
        }

        // If the root is not visible, then we have one less parent
        if (this.parent.getTree().isRootVisible() == false) {
            _Result--;
        }

        return _Result;
    }

    private static final double FACTOR = 0.85;
    /**
     * Creates a new <code>Color</code> that is a darker version of this
     * <code>Color</code>.
     * <p>
     * This method applies an arbitrary scale factor to each of the three RGB
     * components of this <code>Color</code> to create a darker version of
     * this <code>Color</code>.  Although <code>brighter</code> and
     * <code>darker</code> are inverse operations, the results of a series
     * of invocations of these two methods might be inconsistent because
     * of rounding errors.
     * @return  a new <code>Color</code> object that is
     *                    a darker version of this <code>Color</code>.
     * @see        java.awt.Color#brighter
     * @since      JDK1.0
     */
    public Color darker(Color color) {
      return new Color(Math.max((int)(color.getRed()  *FACTOR), 0),
          Math.max((int)(color.getGreen()*FACTOR), 0),
          Math.max((int)(color.getBlue() *FACTOR), 0));
    }


    public Component getTreeCellRendererComponent(
            JTree tree,
            Object value,
            boolean selected,
            boolean expanded,
            boolean leaf,
            int row,
            boolean hasFocus) {

        if (this.parent.getColumnSizes() != null && value instanceof DisplayNode) {

            // Cache the labels for performance
            if (this.cachedJLabels == null || this.cachedJLabels.length != this.parent.getNumVisibleColumns()) {
                this.panel.removeAll();

                this.cachedJLabels = new OutlineFieldJLabel[this.parent.getColumnSizes().length];

                for (int col=0; col<this.parent.getColumnSizes().length; col++) {
                    this.cachedJLabels[col] = new OutlineFieldJLabel();
                    this.cachedJLabels[col].setFont(this.parent.getFont());

                    GridBagConstraints gbc = new GridBagConstraints();
                    gbc.gridx = col;
                    gbc.gridy = 0;
                    gbc.anchor = GridBagConstraints.WEST;
                    // TF:04/02/2009:JCT-628: The data in Forte is slightly closer than the font size,
                    // so adjust the offset with a bottom of -1 to match this. This will also match the -1
                    // in the row height in getMinimumSize() in the outline field.
                    gbc.insets = new Insets(0,0,-1,OutlineField.cCOLUMN_SPACING);
                    this.panel.add(this.cachedJLabels[col], gbc);
                }
            }
            else {
              // CraigM:18/06/2008 - Swing needs to be told that sometime it will need to change the size of the panel.
              this.panel.invalidate();
            }

            int firstIndentCol = 0;

            // Work out what is our first indented column. It's the one with the firstIndent property
            // set true, or the first one after that if that column is invisible.
            Array_Of_OutlineColumnDesc<OutlineColumnDesc> columnList = this.parent.getColumnList(false);
            boolean found = false;
            for (int col=0; col<columnList.size(); col++) {
                OutlineColumnDesc colDec = columnList.get(col);
                if (colDec.getIsFirstIndent()) {
                    found = true;
                }
                if (colDec.getState() != Constants.FS_INVISIBLE && found) {
                  firstIndentCol = col;
                  break;
                }
            }

            // Layout the data into a grid, setting the correct width of each data item to
            // match the column width.
            int col = 0;
            for (int realCol=0; realCol<columnList.size(); realCol++) {
              OutlineColumnDesc colDec = columnList.get(realCol);
              if (colDec.getState() != Constants.FS_INVISIBLE) {
                  Object data = this.parent.getData((DisplayNode)value,realCol);
                  this.parent.setJLabel(data, this.cachedJLabels[col]);
 
                  // Set the column alignment
                  switch (colDec.getAlignment()) {
                      case Constants.TA_RIGHT:
                          this.cachedJLabels[col].setHorizontalAlignment(SwingConstants.RIGHT);
                          break;
                      case Constants.TA_CENTER:
                          this.cachedJLabels[col].setHorizontalAlignment(SwingConstants.CENTER);
                          break;
                      default:
                          this.cachedJLabels[col].setHorizontalAlignment(SwingConstants.LEFT);
                  }
 
                  if (this.hasRowHighlights) {
                      if (selected) {
                          if (hasFocus) {
                              this.cachedJLabels[col].setForeground(this.defaultTreeRenderer.getTextSelectionColor());
                              this.panel.setBackground(TableFactory.HIGHLIGHT_COLOUR_FOCUS);
                          }
                          else {
                            // TF:Mar 10, 2010:Changed this to use a color close to the background colour
                              this.cachedJLabels[col].setForeground(this.defaultTreeRenderer.getTextNonSelectionColor());
  //                            this.panel.setBackground(TableFactory.HIGHLIGHT_COLOUR_NO_FOCUS);
                              this.panel.setBackground(darker(parent.getBackground()));
                          }
                      }
                      else {
                          this.cachedJLabels[col].setForeground(this.defaultTreeRenderer.getTextNonSelectionColor());
  //                      //this.panel.setBackground(parent.getBackground()); // CraigM:16/12/2008 - Set the background colour based on the OutlineField
                          this.panel.setBackground(null);//PM:12/01/2009: when not selected, make it inherit the colour
                      }
                  }
                  else {
                      this.cachedJLabels[col].setForeground(this.defaultTreeRenderer.getTextNonSelectionColor()); // CraigM 14/11/2007
                      this.panel.setBackground(Color.white);
                  }
 
                  if (col == firstIndentCol && firstIndentCol > 0) {
                      int width = this.parent.getColumnSizes()[firstIndentCol];
                      for (int i=0; i<firstIndentCol; i++) {
                          width += this.parent.getColumnSizes()[i];
                          width -= this.cachedJLabels[i].getPreferredSize().width;
                          if (i==0) {
                              width -= (this.getNumParents((TreeNode)value) * OutlineField.cTREE_CONTROL_WIDTH);
                          }
                      }
                      this.cachedJLabels[col].setPreferredSize(new Dimension(width, this.cachedJLabels[col].getPreferredSizeCalc().height));
                  }
                  else if (col >= firstIndentCol) {
                      int width = this.parent.getColumnSizes()[col];
                      if (col==0) {
                          width -= (this.getNumParents((TreeNode)value) * OutlineField.cTREE_CONTROL_WIDTH);
                      }
                      this.cachedJLabels[col].setPreferredSize(new Dimension(width, this.cachedJLabels[col].getPreferredSizeCalc().height));
                  }
                  col++;
              }
            }
        }

        return panel;
    }

    public void clearCache() {
        this.cachedJLabels = null;
    }

    public void setHasRowHighlights(boolean pDoIt) {
        this.hasRowHighlights = pDoIt;
    }

    public boolean getHasRowHighlights() {
        return this.hasRowHighlights;
    }
}
TOP

Related Classes of DisplayProject.controls.OutlineFieldCellRenderer

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.