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: ReportContextImpl.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.data;
033    
034    import java.util.HashMap;
035    
036    import org.jfree.formula.FormulaContext;
037    import org.jfree.report.flow.ReportContext;
038    import org.jfree.report.flow.ReportStructureRoot;
039    import org.jfree.report.flow.layoutprocessor.LayoutControllerFactory;
040    import org.jfree.report.i18n.ResourceBundleFactory;
041    
042    /**
043     * Creation-Date: 20.11.2006, 12:19:34
044     *
045     * @author Thomas Morgner
046     */
047    public class ReportContextImpl implements ReportContext
048    {
049      private static class DataCarrier
050      {
051        private boolean locked;
052        private Object value;
053    
054        private DataCarrier(final Object value)
055        {
056          this.value = value;
057        }
058    
059        public void lock ()
060        {
061          locked = true;
062        }
063    
064        public boolean isLocked()
065        {
066          return locked;
067        }
068    
069        public Object getValue()
070        {
071          return value;
072        }
073    
074        public void setValue(final Object value)
075        {
076          this.value = value;
077        }
078      }
079    
080      private HashMap backend;
081      private String exportDescriptor;
082      private FormulaContext formulaContext;
083      private LayoutControllerFactory layoutControllerFactory;
084      private ResourceBundleFactory resourceBundleFactory;
085      private ReportStructureRoot reportStructureRoot;
086    
087      public ReportContextImpl()
088      {
089        backend = new HashMap();
090      }
091    
092      public void setReportStructureRoot(final ReportStructureRoot reportStructureRoot)
093      {
094        this.reportStructureRoot = reportStructureRoot;
095      }
096    
097      public ReportStructureRoot getReportStructureRoot()
098      {
099        return reportStructureRoot;
100      }
101    
102      public String getExportDescriptor()
103      {
104        return exportDescriptor;
105      }
106    
107      public void setExportDescriptor(final String exportDescriptor)
108      {
109        this.exportDescriptor = exportDescriptor;
110      }
111    
112      public FormulaContext getFormulaContext()
113      {
114        return formulaContext;
115      }
116    
117      public void setFormulaContext(final FormulaContext formulaContext)
118      {
119        this.formulaContext = formulaContext;
120      }
121    
122      public LayoutControllerFactory getLayoutControllerFactory()
123      {
124        return layoutControllerFactory;
125      }
126    
127      public void setLayoutControllerFactory(final LayoutControllerFactory layoutControllerFactory)
128      {
129        this.layoutControllerFactory = layoutControllerFactory;
130      }
131    
132      public void setAttribute(final Object key, final Object value)
133      {
134        final DataCarrier dc = (DataCarrier) backend.get(key);
135        if (dc == null)
136        {
137          if (value == null)
138          {
139            return;
140          }
141    
142          final DataCarrier ndc = new DataCarrier(value);
143          backend.put (key, ndc);
144          return;
145        }
146    
147        if (dc.isLocked())
148        {
149          throw new IllegalStateException("Context-Entry is locked.");
150        }
151        dc.setValue (value);
152      }
153    
154      public void setSystemAttribute(final Object key, final Object value)
155      {
156        final DataCarrier dc = (DataCarrier) backend.get(key);
157        if (dc == null)
158        {
159          if (value == null)
160          {
161            return;
162          }
163    
164          final DataCarrier ndc = new DataCarrier(value);
165          ndc.lock();
166          backend.put (key, ndc);
167          return;
168        }
169    
170        if (dc.isLocked())
171        {
172          throw new IllegalStateException("Context-Entry is locked.");
173        }
174        dc.setValue (value);
175      }
176    
177      public Object getAttribute(final Object key)
178      {
179        final DataCarrier dc = (DataCarrier) backend.get(key);
180        if (dc == null)
181        {
182          return null;
183        }
184        return dc.getValue();
185      }
186    
187      public boolean isSystemAttribute(final Object key)
188      {
189        final DataCarrier dc = (DataCarrier) backend.get(key);
190        if (dc == null)
191        {
192          return false;
193        }
194        return dc.isLocked();
195      }
196    
197      public ResourceBundleFactory getResourceBundleFactory()
198      {
199        return resourceBundleFactory;
200      }
201    
202      public void setResourceBundleFactory(final ResourceBundleFactory resourceBundleFactory)
203      {
204        this.resourceBundleFactory = resourceBundleFactory;
205      }
206    }