Package org.pentaho.reporting.engine.classic.extensions.modules.sbarcodes

Source Code of org.pentaho.reporting.engine.classic.extensions.modules.sbarcodes.BarcodeWrapper

/*
* 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.extensions.modules.sbarcodes;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import net.sourceforge.barbecue.Barcode;
import net.sourceforge.barbecue.output.OutputException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.reporting.engine.classic.core.ResourceBundleFactory;
import org.pentaho.reporting.engine.classic.core.imagemap.ImageMap;
import org.pentaho.reporting.engine.classic.core.layout.LayoutSupport;
import org.pentaho.reporting.engine.classic.core.style.ElementStyleKeys;
import org.pentaho.reporting.engine.classic.core.style.StyleSheet;
import org.pentaho.reporting.engine.classic.core.style.TextStyleKeys;
import org.pentaho.reporting.engine.classic.core.util.ReportDrawable;
import org.pentaho.reporting.libraries.base.config.Configuration;

/**
* This class is used to wrap a <i>barbecue</i> barcode element in order to offer the method {@link
* BarcodeWrapper#draw(java.awt.Graphics2D, java.awt.geom.Rectangle2D)} which is requested by the reporting engine for
* automatic support using <i>drawable-field</i>s.
*/
public class BarcodeWrapper implements ReportDrawable
{
  private static final Log logger = LogFactory.getLog(BarcodeWrapper.class);
  private static final Color ALPHA = new Color(255, 255, 255, 0);
  private Barcode barcode;
  private Dimension preferredSize;

  public BarcodeWrapper(final Barcode barcode)
  {
    if (barcode == null)
    {
      throw new IllegalArgumentException("Barcode to wrap must not be null");
    }
    this.barcode = barcode;
  }

  public boolean isPreserveAspectRatio()
  {
    return true;
  }

  public Dimension getPreferredSize()
  {
    if (preferredSize == null)
    {
      preferredSize = barcode.getSize();
    }
    return preferredSize;
  }

  public void draw(final Graphics2D g2, final Rectangle2D bounds)
  {
    final Graphics2D gr2 = (Graphics2D) g2.create();
    try
    {
      gr2.clip(bounds);
      barcode.draw(gr2, (int) bounds.getX(), (int) bounds.getY());
    }
    catch (OutputException e)
    {
      logger.error("Unable to draw barcode element", e);
    }
    finally
    {
      gr2.dispose();
    }

  }

  /**
   * Provides the Layout-Support of the current report processor to the drawable. The layout-support can be used to
   * compute text-layouts.
   *
   * @param layoutSupport the layout support.
   * @deprecated This method will be removed. If you need to do layouting, use the Graphics2D context. It should be more
   *             accurate than what we supply here.
   */
  public void setLayoutSupport(final LayoutSupport layoutSupport)
  {

  }

  /**
   * Provides the current report configuration of the current report process to the drawable. The report configuration
   * can be used to configure the drawing process through the report.
   *
   * @param config the report configuration.
   */
  public void setConfiguration(final Configuration config)
  {

  }

  /**
   * Provides the computed stylesheet of the report element that contained this drawable. The stylesheet is immutable.
   *
   * @param style the stylesheet.
   */
  public void setStyleSheet(final StyleSheet style)
  {
    if (style != null)
    {
      final String fontName = (String) style.getStyleProperty(TextStyleKeys.FONT);
      final int fontSize = style.getIntStyleProperty(TextStyleKeys.FONTSIZE, 0);
      final boolean bold = style.getBooleanStyleProperty(TextStyleKeys.BOLD);
      final boolean italics = style.getBooleanStyleProperty(TextStyleKeys.ITALIC);
      final Color foregroundColor = (Color) style.getStyleProperty(ElementStyleKeys.PAINT);
      final Color backgroundColor = (Color) style.getStyleProperty(ElementStyleKeys.BACKGROUND_COLOR);
      if (fontName != null && fontSize > 0)
      {
        int fontstyle = Font.PLAIN;
        if (bold)
        {
          fontstyle |= Font.BOLD;
        }
        if (italics)
        {
          fontstyle |= Font.ITALIC;
        }

        barcode.setFont(new Font(fontName, fontstyle, fontSize));
      }
      if (foregroundColor != null)
      {
        barcode.setForeground(foregroundColor);
      }
      if (backgroundColor != null)
      {
        barcode.setBackground(backgroundColor);
        barcode.setOpaque(backgroundColor.getAlpha() == 255);
      }
      else
      {
        barcode.setBackground(ALPHA);
        barcode.setOpaque(false);
      }
    }
  }

  /**
   * Defines the resource-bundle factory that can be used to localize the drawing process.
   *
   * @param bundleFactory the resource-bundle factory.
   */
  public void setResourceBundleFactory(final ResourceBundleFactory bundleFactory)
  {

  }

  /**
   * Returns an optional image-map for the entry.
   *
   * @param bounds the bounds for which the image map is computed.
   * @return the computed image-map or null if there is no image-map available.
   */
  public ImageMap getImageMap(final Rectangle2D bounds)
  {
    return null;
  }
}
TOP

Related Classes of org.pentaho.reporting.engine.classic.extensions.modules.sbarcodes.BarcodeWrapper

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.