Source for org.jfree.report.flow.layoutprocessor.ContentElementLayoutController

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: ContentElementLayoutController.java 2725 2007-04-01 18:49:29Z taqua $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: 
  32: package org.jfree.report.flow.layoutprocessor;
  33: 
  34: import org.jfree.report.DataFlags;
  35: import org.jfree.report.DataSourceException;
  36: import org.jfree.report.ReportDataFactoryException;
  37: import org.jfree.report.ReportProcessingException;
  38: import org.jfree.report.data.DefaultDataFlags;
  39: import org.jfree.report.expressions.Expression;
  40: import org.jfree.report.flow.FlowController;
  41: import org.jfree.report.flow.LayoutExpressionRuntime;
  42: import org.jfree.report.flow.ReportContext;
  43: import org.jfree.report.flow.ReportTarget;
  44: import org.jfree.report.structure.ContentElement;
  45: import org.jfree.report.structure.Node;
  46: 
  47: /**
  48:  * Creation-Date: 24.11.2006, 15:06:56
  49:  *
  50:  * @author Thomas Morgner
  51:  */
  52: public class ContentElementLayoutController extends ElementLayoutController
  53: {
  54:   public ContentElementLayoutController()
  55:   {
  56:   }
  57: 
  58:   protected LayoutController processContent(final ReportTarget target)
  59:       throws DataSourceException, ReportProcessingException, ReportDataFactoryException
  60:   {
  61: 
  62:     final Node node = getElement();
  63:     final FlowController flowController = getFlowController();
  64:     final LayoutExpressionRuntime er =
  65:         LayoutControllerUtil.getExpressionRuntime(flowController, node);
  66:     final ContentElement element = (ContentElement) node;
  67:     final Expression ex = element.getValueExpression();
  68:     final Object value;
  69: 
  70:     if (ex != null)
  71:     {
  72:       try
  73:       {
  74:         ex.setRuntime(er);
  75:         value = ex.computeValue();
  76:       }
  77:       finally
  78:       {
  79:         ex.setRuntime(null);
  80:       }
  81:     }
  82:     else
  83:     {
  84:       value = null;
  85:     }
  86: 
  87:     // This should be a very rare case indeed ..
  88:     if (value instanceof DataFlags)
  89:     {
  90:       target.processContent((DataFlags) value);
  91: 
  92:       final ContentElementLayoutController derived = (ContentElementLayoutController) clone();
  93:       derived.setProcessingState(ElementLayoutController.FINISHING);
  94:       derived.setFlowController(flowController);
  95:       return derived;
  96:     }
  97: 
  98:     if (value instanceof Node)
  99:     {
 100:       // we explictly allow structural content here.
 101:       // As this might be a very expensive thing, if we
 102:       // keep it in a single state, we continue on a separate state.
 103:       final Node valueNode = (Node) value;
 104:       valueNode.updateParent(node.getParent());
 105:       final ReportContext reportContext = flowController.getReportContext();
 106:       final LayoutControllerFactory layoutControllerFactory =
 107:           reportContext.getLayoutControllerFactory();
 108: 
 109:       // actually, this is the same as if the element were a
 110:       // child element of a section. The only difference is
 111:       // that there can be only one child, and that there is no
 112:       // direct parent-child direction.
 113: 
 114:       final ContentElementLayoutController derived =
 115:           (ContentElementLayoutController) clone();
 116:       derived.setProcessingState(ElementLayoutController.WAITING_FOR_JOIN);
 117:       derived.setFlowController(flowController);
 118: 
 119:       return layoutControllerFactory.create
 120:           (flowController, valueNode, derived);
 121:     }
 122: 
 123:     if (ex != null)
 124:     {
 125:       target.processContent (new DefaultDataFlags(ex.getName(), value, true));
 126:     }
 127: 
 128:     final ContentElementLayoutController derived = (ContentElementLayoutController) clone();
 129:     derived.setProcessingState(ElementLayoutController.FINISHING);
 130:     derived.setFlowController(flowController);
 131:     return derived;
 132:   }
 133: 
 134:   /**
 135:    * Joins with a delegated process flow. This is generally called from a child
 136:    * flow and should *not* (I mean it!) be called from outside. If you do,
 137:    * you'll suffer.
 138:    *
 139:    * @param flowController the flow controller of the parent.
 140:    * @return the joined layout controller that incorperates all changes from
 141:    * the delegate.
 142:    */
 143:   public LayoutController join(final FlowController flowController)
 144:   {
 145:     final ContentElementLayoutController derived = (ContentElementLayoutController) clone();
 146:     derived.setProcessingState(ElementLayoutController.FINISHING);
 147:     derived.setFlowController(flowController);
 148:     return derived;
 149:   }
 150: }