001    /**
002     * ========================================
003     * JFreeReport : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/
007     *
008     * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: AbstractLayoutController.java 3525 2007-10-16 11:43:48Z tmorgner $
027     * ------------
028     * (C) Copyright 2000-2005, by Object Refinery Limited.
029     * (C) Copyright 2005-2007, by Pentaho Corporation.
030     */
031    
032    package org.jfree.report.flow.layoutprocessor;
033    
034    import org.jfree.report.flow.FlowController;
035    import org.jfree.report.DataSourceException;
036    import org.jfree.report.ReportDataFactoryException;
037    import org.jfree.report.ReportProcessingException;
038    import org.jfree.util.Log;
039    
040    /**
041     * Todo: Document me!
042     *
043     * @author Thomas Morgner
044     * @since 05.03.2007
045     */
046    public abstract class AbstractLayoutController implements LayoutController
047    {
048      private FlowController flowController;
049      private LayoutController parent;
050      private Object node;
051      private boolean initialized;
052    
053      protected AbstractLayoutController()
054      {
055      }
056    
057      /**
058       * Retrieves the parent of this layout controller. This allows childs to query
059       * their context.
060       *
061       * @return the layout controller's parent to <code>null</code> if there is no
062       * parent.
063       */
064      public LayoutController getParent()
065      {
066        return parent;
067      }
068    
069    
070      /**
071       * Initializes the layout controller. This method is called exactly once. It
072       * is the creators responsibility to call this method.
073       * <p/>
074       * Calling initialize after the first advance must result in a
075       * IllegalStateException.
076       *
077       * @param node           the currently processed object or layout node.
078       * @param flowController the current flow controller.
079       * @param parent         the parent layout controller that was responsible for
080       *                       instantiating this controller.
081       * @throws DataSourceException        if there was a problem reading data from
082       *                                    the datasource.
083       * @throws ReportProcessingException  if there was a general problem during
084       *                                    the report processing.
085       * @throws ReportDataFactoryException if a query failed.
086       */
087      public void initialize(final Object node, final FlowController flowController,
088                             final LayoutController parent)
089          throws DataSourceException, ReportDataFactoryException,
090          ReportProcessingException
091      {
092        if (initialized == true)
093        {
094          throw new IllegalStateException();
095        }
096    
097        this.initialized = true;
098        this.node = node;
099        this.flowController = flowController;
100        this.parent = parent;
101      }
102    
103      public Object clone()
104      {
105        try
106        {
107          return super.clone();
108        }
109        catch (CloneNotSupportedException e)
110        {
111          Log.error ("Clone failed where it shouldn't.", e);
112          throw new IllegalStateException("Clone failed?");
113        }
114      }
115    
116    
117      public FlowController getFlowController()
118      {
119        return flowController;
120      }
121    
122    
123      public Object getNode()
124      {
125        return node;
126      }
127    
128    
129      public boolean isInitialized()
130      {
131        return initialized;
132      }
133    
134      /**
135       * Derives a copy of this controller that is suitable to perform a
136       * precomputation.
137       *
138       * @param fc
139       * @return
140       */
141      public LayoutController createPrecomputeInstance(final FlowController fc)
142      {
143        final AbstractLayoutController lc = (AbstractLayoutController) clone();
144        lc.flowController = fc;
145        return lc;
146      }
147    }