Package org.pentaho.reporting.engine.classic.core.modules.output.table.base

Source Code of org.pentaho.reporting.engine.classic.core.modules.output.table.base.CellBackground

/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2001 - 2009 Object Refinery Ltd, Pentaho Corporation and Contributors..  All rights reserved.
*/

package org.pentaho.reporting.engine.classic.core.modules.output.table.base;

import java.awt.Color;
import java.util.ArrayList;

import org.pentaho.reporting.engine.classic.core.ReportAttributeMap;
import org.pentaho.reporting.engine.classic.core.layout.model.BorderCorner;
import org.pentaho.reporting.engine.classic.core.layout.model.BorderEdge;
import org.pentaho.reporting.engine.classic.core.metadata.ElementType;

/**
* Todo: Document Me
*
* @author Thomas Morgner
*/
public class CellBackground
{
  private ReportAttributeMap attributes;
  private ArrayList collectedColors;
  private Color backgroundColor;
  private ArrayList anchors;

  private BorderEdge top;
  private BorderEdge left;
  private BorderEdge bottom;
  private BorderEdge right;

  private BorderCorner topLeft;
  private BorderCorner topRight;
  private BorderCorner bottomLeft;
  private BorderCorner bottomRight;
  private boolean origin;
  private ElementType elementType;
  private static final String[] EMPTY_ANCHORS = new String[0];

  public CellBackground()
  {
    this.top = BorderEdge.EMPTY;
    this.left = BorderEdge.EMPTY;
    this.bottom = BorderEdge.EMPTY;
    this.right = BorderEdge.EMPTY;
    this.attributes = new ReportAttributeMap();

    this.topLeft = new BorderCorner(0, 0);
    this.topRight = new BorderCorner(0, 0);
    this.bottomLeft = new BorderCorner(0, 0);
    this.bottomRight = new BorderCorner(0, 0);
  }

  public boolean isOrigin()
  {
    return origin;
  }

  public void setOrigin(final boolean origin)
  {
    this.origin = origin;
  }

  public void addAttributes(final ReportAttributeMap attrs)
  {
    if (attrs == null)
    {
      throw new NullPointerException();
    }

    final String[] namespaces = attrs.getNameSpaces();
    for (int i = 0; i < namespaces.length; i++)
    {
      final String namespace = namespaces[i];
      final String[] names = attrs.getNames(namespace);
      for (int j = 0; j < names.length; j++)
      {
        final String name = names[j];
        final Object value = attrs.getAttribute(namespace, name);
        if (value != null)
        {
          this.attributes.setAttribute(namespace, name, value);
        }
      }
    }
  }

  public ReportAttributeMap getAttributes()
  {
    return attributes;
  }

  public void addBackground(final Color color)
  {
    if (color == null)
    {
      return;
    }
    if (color.getAlpha() == 0)
    {
      // fully transparent ..
      return;
    }

    // common special case: Only one background color defined ..
    if (backgroundColor == null)
    {
      backgroundColor = color;
      return;
    }

    if (color.getAlpha() == 255)
    {
      // fully opaque, so
      backgroundColor = color;
      if (collectedColors != null)
      {
        collectedColors.clear();
      }
      return;
    }

    if (collectedColors == null)
    {
      collectedColors = new ArrayList();
    }
    if (collectedColors.isEmpty())
    {
      collectedColors.add(backgroundColor);
    }

    collectedColors.add(color);
  }

  public Color getBackgroundColor()
  {
    if (backgroundColor != null && backgroundColor.getAlpha() == 255)
    {
      return backgroundColor;
    }

    if (collectedColors == null)
    {
      return backgroundColor;
    }
    if (collectedColors.isEmpty() == false)
    {
      Color retval = null;
      final int colorCount = collectedColors.size();
      for (int i = 0; i < colorCount; i++)
      {
        final Color c = (Color) collectedColors.get(i);
        if (retval == null)
        {
          retval = c;
        }
        else
        {
          retval = addColor(retval, c);
        }
      }
      return retval;
    }
    return backgroundColor;
  }

  /**
   * Adds two colors, the result is the mixed color of the base color and the paint color.
   *
   * @param base  the base color
   * @param paint the overlay color
   * @return the merged colors.
   */
  private static Color addColor(final Color base, final Color paint)
  {
    if (paint.getAlpha() == 255)
    {
      return paint;
    }
    if (paint.getAlpha() == 0)
    {
      return base;
    }

    final double baseAlpha = (base.getAlpha() / 255.0);
    final double paintAlpha = (paint.getAlpha() / 255.0);
    final double effectiveAlpha = 1.0 - baseAlpha * paintAlpha;

    final double deltaAlpha = 1.0 - effectiveAlpha;
    final int red = (int)
        (base.getRed() * deltaAlpha + paint.getRed() * effectiveAlpha);
    final int green = (int)
        (base.getGreen() * deltaAlpha + paint.getGreen() * effectiveAlpha);
    final int blue = (int)
        (base.getBlue() * deltaAlpha + paint.getBlue() * effectiveAlpha);
    return new Color(red, green, blue, (int) (effectiveAlpha * 255.0));
  }

  public BorderEdge getTop()
  {
    return top;
  }

  public void setTop(final BorderEdge top)
  {
    if (top == null)
    {
      throw new NullPointerException();
    }
    this.top = top;
  }

  public BorderEdge getLeft()
  {
    return left;
  }

  public void setLeft(final BorderEdge left)
  {
    if (left == null)
    {
      throw new NullPointerException();
    }
    this.left = left;
  }

  public BorderEdge getBottom()
  {
    return bottom;
  }

  public void setBottom(final BorderEdge edge)
  {
    if (edge == null)
    {
      throw new NullPointerException();
    }
    this.bottom = edge;
  }

  public BorderEdge getRight()
  {
    return right;
  }

  public void setRight(final BorderEdge edge)
  {
    if (edge == null)
    {
      throw new NullPointerException();
    }
    this.right = edge;
  }

  public void setTopLeft(final BorderCorner topLeft)
  {
    if (topLeft == null)
    {
      throw new NullPointerException();
    }
    this.topLeft = topLeft;
  }

  public void setTopRight(final BorderCorner topRight)
  {
    if (topRight == null)
    {
      throw new NullPointerException();
    }
    this.topRight = topRight;
  }

  public void setBottomLeft(final BorderCorner bottomLeft)
  {
    if (bottomLeft == null)
    {
      throw new NullPointerException();
    }
    this.bottomLeft = bottomLeft;
  }

  public void setBottomRight(final BorderCorner bottomRight)
  {
    if (bottomRight == null)
    {
      throw new NullPointerException();
    }
    this.bottomRight = bottomRight;
  }

  public BorderCorner getTopLeft()
  {
    return topLeft;
  }

  public BorderCorner getTopRight()
  {
    return topRight;
  }

  public BorderCorner getBottomLeft()
  {
    return bottomLeft;
  }

  public BorderCorner getBottomRight()
  {
    return bottomRight;
  }

  public void addAnchor(final String anchor)
  {
    if (anchor == null)
    {
      return;
    }
    if (anchors == null)
    {
      anchors = new ArrayList();
    }
    anchors.add(anchor);
  }

  public String[] getAnchors()
  {
    if (anchors == null)
    {
      return EMPTY_ANCHORS;
    }
    return (String[]) anchors.toArray(new String[anchors.size()]);
  }

  public void addElementType(final ElementType type)
  {
    if (type == null)
    {
      throw new NullPointerException();
    }
    this.elementType = type;
  }

  public ElementType getElementType()
  {
    return elementType;
  }
}
TOP

Related Classes of org.pentaho.reporting.engine.classic.core.modules.output.table.base.CellBackground

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.