Package org.pentaho.reporting.engine.classic.core.function.sys

Source Code of org.pentaho.reporting.engine.classic.core.function.sys.MetaDataStyleEvaluator$NeedEvalResult

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

import java.awt.Color;
import java.util.Locale;

import org.pentaho.reporting.engine.classic.core.AttributeNames;
import org.pentaho.reporting.engine.classic.core.Band;
import org.pentaho.reporting.engine.classic.core.Element;
import org.pentaho.reporting.engine.classic.core.ElementAlignment;
import org.pentaho.reporting.engine.classic.core.Group;
import org.pentaho.reporting.engine.classic.core.MetaAttributeNames;
import org.pentaho.reporting.engine.classic.core.RootLevelBand;
import org.pentaho.reporting.engine.classic.core.SubReport;
import org.pentaho.reporting.engine.classic.core.filter.types.LabelType;
import org.pentaho.reporting.engine.classic.core.filter.types.ResourceLabelType;
import org.pentaho.reporting.engine.classic.core.function.AbstractElementFormatFunction;
import org.pentaho.reporting.engine.classic.core.function.Expression;
import org.pentaho.reporting.engine.classic.core.function.ExpressionRuntime;
import org.pentaho.reporting.engine.classic.core.function.StructureFunction;
import org.pentaho.reporting.engine.classic.core.layout.output.OutputProcessorMetaData;
import org.pentaho.reporting.engine.classic.core.states.LayoutProcess;
import org.pentaho.reporting.engine.classic.core.style.ElementStyleKeys;
import org.pentaho.reporting.engine.classic.core.style.ElementStyleSheet;
import org.pentaho.reporting.engine.classic.core.style.TextStyleKeys;
import org.pentaho.reporting.engine.classic.core.wizard.AutoGeneratorUtility;
import org.pentaho.reporting.engine.classic.core.wizard.DataAttributeContext;
import org.pentaho.reporting.engine.classic.core.wizard.DataAttributes;
import org.pentaho.reporting.engine.classic.core.wizard.DataSchema;
import org.pentaho.reporting.libraries.base.util.LFUMap;

/**
* Todo: Document Me
*
* @author Thomas Morgner
*/
public class MetaDataStyleEvaluator extends AbstractElementFormatFunction
    implements StructureFunction
{
  private class VolatileDataAttributeContext implements DataAttributeContext
  {
    private VolatileDataAttributeContext()
    {
    }

    public Locale getLocale()
    {
      final ExpressionRuntime expressionRuntime = getRuntime();
      if (expressionRuntime == null)
      {
        throw new IllegalStateException();
      }
      return expressionRuntime.getResourceBundleFactory().getLocale();
    }

    public OutputProcessorMetaData getOutputProcessorMetaData()
    {
      final ExpressionRuntime expressionRuntime = getRuntime();
      if (expressionRuntime == null)
      {
        throw new IllegalStateException();
      }
      return expressionRuntime.getProcessingContext().getOutputProcessorMetaData();
    }
  }

  private static class NeedEvalResult
  {
    private boolean needToRun;
    private long changeTracker;

    private NeedEvalResult(final boolean needToRun, final long changeTracker)
    {
      this.needToRun = needToRun;
      this.changeTracker = changeTracker;
    }

    public boolean isNeedToRun()
    {
      return needToRun;
    }

    public long getChangeTracker()
    {
      return changeTracker;
    }
  }

  private transient VolatileDataAttributeContext attributeContext;
  private transient LFUMap expressionsCache;
  private Boolean legacyMode;

  public MetaDataStyleEvaluator()
  {
    expressionsCache = new LFUMap(500);
  }

  private boolean isLegacyMode()
  {
    if (legacyMode != null)
    {
      return legacyMode.booleanValue();
    }

    if (getRuntime() == null)
    {
      return false;
    }

    //noinspection UnnecessaryBoxing
    legacyMode = Boolean.valueOf("false".equals(getRuntime().getConfiguration().getConfigProperty
        ("org.pentaho.reporting.engine.classic.core.WizardAllowAttributeChangeWithoutStyleChange")));
    return legacyMode.booleanValue();
  }

  /**
   * Returns the dependency level for the expression (controls evaluation order for expressions and functions).
   *
   * @return the level.
   */
  public int getDependencyLevel()
  {
    return LayoutProcess.LEVEL_PAGINATE;
  }

  public int getProcessingPriority()
  {
    return 4000;
  }

  public VolatileDataAttributeContext getAttributeContext()
  {
    if (attributeContext == null)
    {
      attributeContext = new VolatileDataAttributeContext();
    }
    return attributeContext;
  }


  /**
   * Evaluates all style expressions from all elements and updates the style-sheet if needed.
   *
   * @param b the band.
   */
  protected void processRootBand(final Band b)
  {
    final NeedEvalResult needToRun = (NeedEvalResult) expressionsCache.get(b.getObjectID());
    if (needToRun != null)
    {
      if (needToRun.isNeedToRun() == false)
      {
        if (b.getChangeTracker() == needToRun.getChangeTracker())
        {
          return;
        }
      }
    }

    final boolean needToRunVal = processBand(b);
    expressionsCache.put(b.getObjectID(), new NeedEvalResult(needToRunVal, b.getChangeTracker()));
  }


  private boolean processBand(final Band b)
  {
    boolean hasAttrExpressions = evaluateElement(b);

    if (b.isVisible() == false)
    {
      return hasAttrExpressions;
    }

    final Element[] elementBuffer = b.unsafeGetElementArray();
    final int length = elementBuffer.length;
    for (int i = 0; i < length; i++)
    {
      final Element element = elementBuffer[i];
      if (element instanceof Band)
      {
        if (processBand((Band) element))
        {
          hasAttrExpressions = true;
        }
      }
      else
      {
        if (evaluateElement(element))
        {
          hasAttrExpressions = true;
        }
      }
    }

    if (b instanceof RootLevelBand)
    {
      final RootLevelBand rlb = (RootLevelBand) b;
      final SubReport[] reports = rlb.getSubReports();
      for (int i = 0; i < reports.length; i++)
      {
        final SubReport subReport = reports[i];
        if (evaluateElement(subReport))
        {
          hasAttrExpressions = true;
        }
      }
    }
    return hasAttrExpressions;
  }

  protected void processGroup(final Group group)
  {
    evaluateElement(group);
    evaluateElement(group.getBody());
    super.processGroup(group);
  }

  /**
   * Evaluates all defined style-expressions of the given element.
   *
   * @param e the element that should be updated.
   */
  protected boolean evaluateElement(final Element e)
  {
    final DataSchema dataSchema = getRuntime().getDataSchema();

    final Object allowStylingFlag = e.getAttribute(AttributeNames.Wizard.NAMESPACE,
        AttributeNames.Wizard.ALLOW_METADATA_STYLING);
    final Object allowAttributesFlag = e.getAttribute(AttributeNames.Wizard.NAMESPACE,
        AttributeNames.Wizard.ALLOW_METADATA_ATTRIBUTES);
    if (Boolean.TRUE.equals(allowStylingFlag) == false && Boolean.TRUE.equals(allowAttributesFlag) == false)
    {
      // the element prohibits meta-data styling ..
      return false;
    }

    // a flag indicating whether we are dealing with a field or a label.
    String fieldName = (String) e.getAttribute(AttributeNames.Wizard.NAMESPACE, AttributeNames.Wizard.LABEL_FOR);
    if (fieldName == null)
    {
      fieldName = (String) e.getAttribute(AttributeNames.Core.NAMESPACE, AttributeNames.Core.FIELD);
      if (fieldName == null)
      {
        return false;
      }
    }


    // the element always overrides all properties; properties not defined in the meta-data layer
    // will be removed from the stylesheet or attribute collection.

    final DataAttributes attributes = dataSchema.getAttributes(fieldName);
    if (attributes == null)
    {
      return false;
    }

    final MetaDataStyleEvaluator.VolatileDataAttributeContext context = getAttributeContext();

    if (Boolean.TRUE.equals(allowStylingFlag))
    {
      final Boolean bold = (Boolean) attributes.getMetaAttribute
          (MetaAttributeNames.Style.NAMESPACE, MetaAttributeNames.Style.BOLD, Boolean.class, context);
      final Boolean italic = (Boolean) attributes.getMetaAttribute
          (MetaAttributeNames.Style.NAMESPACE, MetaAttributeNames.Style.ITALIC, Boolean.class, context);
      final Boolean strikethrough = (Boolean) attributes.getMetaAttribute
          (MetaAttributeNames.Style.NAMESPACE, MetaAttributeNames.Style.STRIKETHROUGH, Boolean.class, context);
      final Boolean underline = (Boolean) attributes.getMetaAttribute
          (MetaAttributeNames.Style.NAMESPACE, MetaAttributeNames.Style.UNDERLINE, Boolean.class, context);
      final Integer fontSize = (Integer) attributes.getMetaAttribute
          (MetaAttributeNames.Style.NAMESPACE, MetaAttributeNames.Style.FONTSIZE, Integer.class, context);
      final String font = (String) attributes.getMetaAttribute
          (MetaAttributeNames.Style.NAMESPACE, MetaAttributeNames.Style.FONTFAMILY, String.class, context);
      final Color textColor = (Color) attributes.getMetaAttribute
          (MetaAttributeNames.Style.NAMESPACE, MetaAttributeNames.Style.COLOR, Color.class, context);
      final Color bgColor = (Color) attributes.getMetaAttribute
          (MetaAttributeNames.Style.NAMESPACE, MetaAttributeNames.Style.BACKGROUND_COLOR, Color.class, context);
      final ElementAlignment hAlign = (ElementAlignment) attributes.getMetaAttribute
          (MetaAttributeNames.Style.NAMESPACE, MetaAttributeNames.Style.HORIZONTAL_ALIGNMENT,
              ElementAlignment.class, context);
      final ElementAlignment vAlign = (ElementAlignment) attributes.getMetaAttribute
          (MetaAttributeNames.Style.NAMESPACE, MetaAttributeNames.Style.VERTICAL_ALIGNMENT, ElementAlignment.class,
              context);

      final ElementStyleSheet styleSheet = e.getStyle();
      styleSheet.setStyleProperty(ElementStyleKeys.VALIGNMENT, vAlign);
      if (hAlign != null)
      {
        styleSheet.setStyleProperty(ElementStyleKeys.ALIGNMENT, hAlign);
      }
      else if (LabelType.INSTANCE.getMetaData().getName().equals(e.getElementTypeName()) == false &&
          ResourceLabelType.INSTANCE.getMetaData().getName().equals(e.getElementTypeName()) == false)
      {
        styleSheet.setStyleProperty(ElementStyleKeys.ALIGNMENT, computeAlignment(attributes, context));
      }
      styleSheet.setStyleProperty(ElementStyleKeys.BACKGROUND_COLOR, bgColor);
      styleSheet.setStyleProperty(ElementStyleKeys.PAINT, textColor);
      styleSheet.setStyleProperty(TextStyleKeys.FONT, font);
      styleSheet.setStyleProperty(TextStyleKeys.FONTSIZE, fontSize);
      styleSheet.setStyleProperty(TextStyleKeys.UNDERLINED, underline);
      styleSheet.setStyleProperty(TextStyleKeys.BOLD, bold);
      styleSheet.setStyleProperty(TextStyleKeys.STRIKETHROUGH, strikethrough);
      styleSheet.setStyleProperty(TextStyleKeys.ITALIC, italic);
    }

    final boolean legacyMode = isLegacyMode();
    if (Boolean.TRUE.equals(allowAttributesFlag) &&
        ((legacyMode == false) || (Boolean.TRUE.equals(allowStylingFlag))))
    {
      if (LabelType.INSTANCE.getMetaData().getName().equals(e.getElementTypeName()) == false &&
          ResourceLabelType.INSTANCE.getMetaData().getName().equals(e.getElementTypeName()) == false)
      {
        final String format = (String) attributes.getMetaAttribute
            (MetaAttributeNames.Formatting.NAMESPACE, MetaAttributeNames.Formatting.FORMAT,
                String.class, context);
        if (format != null)
        {
          e.setAttribute(AttributeNames.Core.NAMESPACE, AttributeNames.Core.FORMAT_STRING, format);
        }
        else
        {
          final String autoFormat = AutoGeneratorUtility.computeFormatString(attributes, context);
          e.setAttribute(AttributeNames.Core.NAMESPACE, AttributeNames.Core.FORMAT_STRING, autoFormat);
        }
      }
      else
      {
        // only change the static text of labels. We do not touch anything else.
        if ("label".equals(e.getElementTypeName())) // NON-NLS
        {
          final String format = (String) attributes.getMetaAttribute
              (MetaAttributeNames.Formatting.NAMESPACE, MetaAttributeNames.Formatting.LABEL,
                  String.class, context);
          e.setAttribute(AttributeNames.Core.NAMESPACE, AttributeNames.Core.VALUE, format);
        }
      }
    }
    return true;
  }

  private static ElementAlignment computeAlignment(final DataAttributes attributes, final DataAttributeContext context)
  {
    final Class type = (Class) attributes.getMetaAttribute
        (MetaAttributeNames.Core.NAMESPACE, MetaAttributeNames.Core.TYPE, Class.class, context);

    if (Number.class.isAssignableFrom(type))
    {
      return ElementAlignment.RIGHT;
    }
    return ElementAlignment.LEFT;
  }


  /**
   * Clones the expression.  The expression should be reinitialized after the cloning. <P> Expressions maintain no
   * state, cloning is done at the beginning of the report processing to disconnect the expression from any other object
   * space.
   *
   * @return a clone of this expression.
   * @throws CloneNotSupportedException this should never happen.
   */
  public Object clone() throws CloneNotSupportedException
  {
    final MetaDataStyleEvaluator o = (MetaDataStyleEvaluator) super.clone();
    o.attributeContext = null;
    return o;
  }

  public Expression getInstance()
  {
    final MetaDataStyleEvaluator eval = (MetaDataStyleEvaluator) super.getInstance();
    eval.expressionsCache = new LFUMap(500);
    eval.attributeContext = null;
    return eval;
  }
}
TOP

Related Classes of org.pentaho.reporting.engine.classic.core.function.sys.MetaDataStyleEvaluator$NeedEvalResult

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.