1a1b4a26bSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3a1b4a26bSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4a1b4a26bSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5a1b4a26bSAndrew Rist  * distributed with this work for additional information
6a1b4a26bSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7a1b4a26bSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8a1b4a26bSAndrew Rist  * "License"); you may not use this file except in compliance
9a1b4a26bSAndrew Rist  * with the License.  You may obtain a copy of the License at
10a1b4a26bSAndrew Rist  *
11a1b4a26bSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12a1b4a26bSAndrew Rist  *
13a1b4a26bSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14a1b4a26bSAndrew Rist  * software distributed under the License is distributed on an
15a1b4a26bSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16a1b4a26bSAndrew Rist  * KIND, either express or implied.  See the License for the
17a1b4a26bSAndrew Rist  * specific language governing permissions and limitations
18a1b4a26bSAndrew Rist  * under the License.
19a1b4a26bSAndrew Rist  *
20a1b4a26bSAndrew Rist  *************************************************************/
21a1b4a26bSAndrew Rist 
22a1b4a26bSAndrew Rist 
23cdf0e10cSrcweir package com.sun.star.wizards.reportbuilder.layout;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.awt.FontDescriptor;
26cdf0e10cSrcweir import com.sun.star.awt.Rectangle;
27cdf0e10cSrcweir import com.sun.star.awt.Size;
28cdf0e10cSrcweir import com.sun.star.awt.XControl;
29cdf0e10cSrcweir import com.sun.star.awt.XControlModel;
30cdf0e10cSrcweir import com.sun.star.awt.XLayoutConstrains;
31cdf0e10cSrcweir import com.sun.star.awt.XUnitConversion;
32cdf0e10cSrcweir import com.sun.star.awt.XWindow;
33cdf0e10cSrcweir import com.sun.star.awt.XWindowPeer;
34cdf0e10cSrcweir import com.sun.star.beans.Property;
35cdf0e10cSrcweir import com.sun.star.beans.PropertyAttribute;
36cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
37cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
38cdf0e10cSrcweir import com.sun.star.container.XEnumeration;
39cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
40cdf0e10cSrcweir import com.sun.star.lang.XComponent;
41cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
42cdf0e10cSrcweir import com.sun.star.report.XFixedLine;
43cdf0e10cSrcweir import com.sun.star.report.XFixedText;
44cdf0e10cSrcweir import com.sun.star.report.XFormattedField;
45cdf0e10cSrcweir import com.sun.star.report.XGroup;
46cdf0e10cSrcweir import com.sun.star.report.XGroups;
47cdf0e10cSrcweir import com.sun.star.report.XImageControl;
48cdf0e10cSrcweir import com.sun.star.report.XReportComponent;
49cdf0e10cSrcweir import com.sun.star.report.XReportControlModel;
50cdf0e10cSrcweir import com.sun.star.report.XReportDefinition;
51cdf0e10cSrcweir import com.sun.star.report.XSection;
52cdf0e10cSrcweir import com.sun.star.drawing.XShape;
53cdf0e10cSrcweir import com.sun.star.sdbc.DataType;
54cdf0e10cSrcweir import com.sun.star.style.XStyle;
55cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
56cdf0e10cSrcweir import com.sun.star.util.XNumberFormatTypes;
57cdf0e10cSrcweir import com.sun.star.util.XNumberFormats;
58cdf0e10cSrcweir import com.sun.star.util.XNumberFormatsSupplier;
59cdf0e10cSrcweir import com.sun.star.wizards.common.Configuration;
60cdf0e10cSrcweir import com.sun.star.wizards.common.FileAccess;
61cdf0e10cSrcweir import com.sun.star.wizards.common.Helper;
62cdf0e10cSrcweir import com.sun.star.wizards.common.PropertyNames;
63cdf0e10cSrcweir import com.sun.star.wizards.common.PropertySetHelper;
64cdf0e10cSrcweir import com.sun.star.wizards.common.Resource;
65cdf0e10cSrcweir import com.sun.star.wizards.report.IReportBuilderLayouter;
66cdf0e10cSrcweir import com.sun.star.wizards.report.IReportDefinitionReadAccess;
67cdf0e10cSrcweir import com.sun.star.wizards.ui.UIConsts;
68cdf0e10cSrcweir import java.util.HashMap;
69cdf0e10cSrcweir import java.util.Locale;
70cdf0e10cSrcweir import java.util.logging.Level;
71cdf0e10cSrcweir import java.util.logging.Logger;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir /**
74cdf0e10cSrcweir  * This class is abstract and more like a helper for create different layouts for Report Builder Wizard.
75cdf0e10cSrcweir  * @author ll93751
76cdf0e10cSrcweir  */// TODO: let a little bit place between 2 formatted fields
77cdf0e10cSrcweir // TODO: move all sectionobjects which have a connect to the right max page width to left if there is a orientation change.
78cdf0e10cSrcweir abstract public class ReportBuilderLayouter implements IReportBuilderLayouter
79cdf0e10cSrcweir {
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     private IReportDefinitionReadAccess m_xReportDefinitionReadAccess;
82cdf0e10cSrcweir     private Resource m_aResource;
83cdf0e10cSrcweir     private String[][] m_aSortNames;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir     /**
86cdf0e10cSrcweir      * dispose the ReportBuilderLayouter
87cdf0e10cSrcweir      */
dispose()88cdf0e10cSrcweir     public void dispose()
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir         closeDesignTemplate();
91cdf0e10cSrcweir         m_xReportDefinitionReadAccess = null;
92cdf0e10cSrcweir         // m_xReportBuilderLayouter = null;
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     /**
96cdf0e10cSrcweir      * The Constructor is protected, this is a abstract class, use Tabular or other to create an instance.
97cdf0e10cSrcweir      * @param _xDefinitionAccess
98cdf0e10cSrcweir      * @param _aResource
99cdf0e10cSrcweir      */
ReportBuilderLayouter(IReportDefinitionReadAccess _xDefinitionAccess, Resource _aResource)100cdf0e10cSrcweir     protected ReportBuilderLayouter(IReportDefinitionReadAccess _xDefinitionAccess, Resource _aResource)
101cdf0e10cSrcweir     {
102cdf0e10cSrcweir         m_xReportDefinitionReadAccess = _xDefinitionAccess;
103cdf0e10cSrcweir         m_aResource = _aResource;
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     /**
107cdf0e10cSrcweir      * Access to the current ReportDefinition
108cdf0e10cSrcweir      * @return the ReportDefinition
109cdf0e10cSrcweir      */
getReportDefinition()110cdf0e10cSrcweir     public XReportDefinition getReportDefinition()
111cdf0e10cSrcweir     {
112cdf0e10cSrcweir         // we have to ask our parent for this value
113cdf0e10cSrcweir         return m_xReportDefinitionReadAccess.getReportDefinition();
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir     /**
117cdf0e10cSrcweir      * Access to the global MultiServiceFactory
118cdf0e10cSrcweir      * @return the global Service Factory
119cdf0e10cSrcweir      */
getGlobalMSF()120cdf0e10cSrcweir     private XMultiServiceFactory getGlobalMSF()
121cdf0e10cSrcweir     {
122cdf0e10cSrcweir         return m_xReportDefinitionReadAccess.getGlobalMSF();
123cdf0e10cSrcweir     }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     /**
126cdf0e10cSrcweir      * Layout the complete report new, by all information we know until this time.
127cdf0e10cSrcweir      *
128cdf0e10cSrcweir      * If there are some information less, it is no problem for this function, they will be leaved out.
129cdf0e10cSrcweir      * It is possible to call this function after every change, but be careful, could take a little bit longer.
130cdf0e10cSrcweir      */
layout()131cdf0e10cSrcweir     public synchronized void layout()
132cdf0e10cSrcweir     {
133cdf0e10cSrcweir         try
134cdf0e10cSrcweir         {
135cdf0e10cSrcweir             // we bring the clear and insert methods together, this should be a little bit smoother
136cdf0e10cSrcweir             clearReportHeader();
137cdf0e10cSrcweir             insertReportHeader();
138cdf0e10cSrcweir 
139cdf0e10cSrcweir             clearReportFooter();
140cdf0e10cSrcweir             insertReportFooter();
141cdf0e10cSrcweir 
142cdf0e10cSrcweir             clearPageHeader();
143cdf0e10cSrcweir             insertPageHeader();
144cdf0e10cSrcweir 
145cdf0e10cSrcweir             clearPageFooter();
146cdf0e10cSrcweir             insertPageFooter();
147cdf0e10cSrcweir 
148cdf0e10cSrcweir             clearGroups();
149cdf0e10cSrcweir             int lastGroupPostion = insertGroups();
150cdf0e10cSrcweir 
151cdf0e10cSrcweir             clearDetails();
152cdf0e10cSrcweir             // clearFieldTitles();
153cdf0e10cSrcweir             insertDetailFieldTitles(lastGroupPostion);
154cdf0e10cSrcweir             insertDetailFields();
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir         catch (java.lang.ArrayIndexOutOfBoundsException e)
157cdf0e10cSrcweir         {
158cdf0e10cSrcweir             // could happen, if you change the count of values
159cdf0e10cSrcweir         }
160cdf0e10cSrcweir         catch (java.lang.RuntimeException e)
161cdf0e10cSrcweir         {
162cdf0e10cSrcweir             throw e;
163cdf0e10cSrcweir         }
164cdf0e10cSrcweir         catch (Exception ex)
165cdf0e10cSrcweir         {
166cdf0e10cSrcweir             Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, ex);
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir     }
169cdf0e10cSrcweir     // -------------------------------------------------------------------------
170cdf0e10cSrcweir 
171cdf0e10cSrcweir     /**
172cdf0e10cSrcweir      * Remove all Groups
173cdf0e10cSrcweir      */
clearGroups()174cdf0e10cSrcweir     protected void clearGroups()
175cdf0e10cSrcweir     {
176cdf0e10cSrcweir         final XGroups xGroups = getReportDefinition().getGroups();
177cdf0e10cSrcweir //        int nCount = xGroups.getCount();
178cdf0e10cSrcweir         while (xGroups.hasElements())
179cdf0e10cSrcweir         {
180cdf0e10cSrcweir             try
181cdf0e10cSrcweir             {
182cdf0e10cSrcweir                 xGroups.removeByIndex(0);
183cdf0e10cSrcweir             }
184cdf0e10cSrcweir             catch (com.sun.star.uno.Exception ex)
185cdf0e10cSrcweir             {
186cdf0e10cSrcweir                 Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, ex);
187cdf0e10cSrcweir             }
188cdf0e10cSrcweir         }
189cdf0e10cSrcweir     }
190cdf0e10cSrcweir     // -------------------------------------------------------------------------
191cdf0e10cSrcweir 
192cdf0e10cSrcweir     /**
193cdf0e10cSrcweir      * Remove all fields
194cdf0e10cSrcweir      * @param _xSectionToClear
195cdf0e10cSrcweir      */
emptySection(XSection _xSectionToClear)196cdf0e10cSrcweir     private void emptySection(XSection _xSectionToClear)
197cdf0e10cSrcweir     {
198cdf0e10cSrcweir         if (_xSectionToClear == null)
199cdf0e10cSrcweir         {
200cdf0e10cSrcweir             return;
201cdf0e10cSrcweir         }
202cdf0e10cSrcweir         //      int nCount = _xSectionToClear.getCount();
203cdf0e10cSrcweir         while (_xSectionToClear.hasElements())
204cdf0e10cSrcweir         {
205cdf0e10cSrcweir             try
206cdf0e10cSrcweir             {
207cdf0e10cSrcweir                 final Object aObj = _xSectionToClear.getByIndex(0);
208cdf0e10cSrcweir                 final XShape aShape = UnoRuntime.queryInterface(XShape.class, aObj);
209cdf0e10cSrcweir                 _xSectionToClear.remove(aShape);
210cdf0e10cSrcweir             }
211cdf0e10cSrcweir             catch (com.sun.star.uno.Exception ex)
212cdf0e10cSrcweir             {
213cdf0e10cSrcweir                 Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, ex);
214cdf0e10cSrcweir             }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir         }
217cdf0e10cSrcweir     }
218cdf0e10cSrcweir 
clearDetails()219cdf0e10cSrcweir     private void clearDetails()
220cdf0e10cSrcweir     {
221cdf0e10cSrcweir         final XSection xSection = getReportDefinition().getDetail();
222cdf0e10cSrcweir         emptySection(xSection);
223cdf0e10cSrcweir     }
224cdf0e10cSrcweir //    public void clearFieldTitles()
225cdf0e10cSrcweir //    {
226cdf0e10cSrcweir //    }
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     /**
229cdf0e10cSrcweir      *
230cdf0e10cSrcweir      * @param _nGroupCount
231cdf0e10cSrcweir      * @return the left indent in 1/100mm
232cdf0e10cSrcweir      */
getLeftGroupIndent(int _nGroupCount)233cdf0e10cSrcweir     protected int getLeftGroupIndent(int _nGroupCount)
234cdf0e10cSrcweir     {
235cdf0e10cSrcweir // TODO: Fix values for group indent (1/2cm) only the first 2 groups
236cdf0e10cSrcweir         int nIndent = 0;
237cdf0e10cSrcweir         final int MAX_INDENT = 2;
238cdf0e10cSrcweir         if (_nGroupCount <= MAX_INDENT)
239cdf0e10cSrcweir         {
240cdf0e10cSrcweir             nIndent = _nGroupCount * LayoutConstants.IndentFactorWidth;
241cdf0e10cSrcweir         }
242cdf0e10cSrcweir         else
243cdf0e10cSrcweir         {
244cdf0e10cSrcweir             nIndent = MAX_INDENT * LayoutConstants.IndentFactorWidth;
245cdf0e10cSrcweir         }
246cdf0e10cSrcweir         return nIndent;
247cdf0e10cSrcweir     }
248cdf0e10cSrcweir     /**
249cdf0e10cSrcweir      * Get left page indent.
250cdf0e10cSrcweir      * The left indent is the area on the left side which will no be printed.
251cdf0e10cSrcweir      * The default is 2000 1/100mm what is 2cm of DIN A4.
252cdf0e10cSrcweir      * @return the left indent in 1/100mm
253cdf0e10cSrcweir      */
254cdf0e10cSrcweir     int m_nLeftIndent = -1;
255cdf0e10cSrcweir 
getLeftPageIndent()256cdf0e10cSrcweir     protected int getLeftPageIndent()
257cdf0e10cSrcweir     {
258cdf0e10cSrcweir         if (m_nLeftIndent < 0)
259cdf0e10cSrcweir         {
260cdf0e10cSrcweir             m_nLeftIndent = getFromPageStyles("LeftMargin", 2000);
261cdf0e10cSrcweir         }
262cdf0e10cSrcweir         return m_nLeftIndent;
263cdf0e10cSrcweir     }
264cdf0e10cSrcweir     /**
265cdf0e10cSrcweir      * Get right page indent.
266cdf0e10cSrcweir      * The right indent is the area on the right side which will no be printed.
267cdf0e10cSrcweir      * The default is 2000 1/100mm what is 2cm of DIN A4.
268cdf0e10cSrcweir      * @return the right indent in 1/100mm
269cdf0e10cSrcweir      */
270cdf0e10cSrcweir     int m_nRightIndent = -1;
271cdf0e10cSrcweir 
getRightPageIndent()272cdf0e10cSrcweir     protected int getRightPageIndent()
273cdf0e10cSrcweir     {
274cdf0e10cSrcweir         if (m_nRightIndent < 0)
275cdf0e10cSrcweir         {
276cdf0e10cSrcweir             m_nRightIndent = getFromPageStyles("RightMargin", 2000);
277cdf0e10cSrcweir         }
278cdf0e10cSrcweir         return m_nRightIndent;
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir 
getUsedStyle(String _sStyleName)281cdf0e10cSrcweir     private XStyle getUsedStyle(String _sStyleName)
282cdf0e10cSrcweir     {
283cdf0e10cSrcweir         XStyle aUsedStyle = null;
284cdf0e10cSrcweir         final XNameAccess xNameAccess = getReportDefinition().getStyleFamilies();
285cdf0e10cSrcweir         try
286cdf0e10cSrcweir         {
287cdf0e10cSrcweir             // <Analysecode>
288cdf0e10cSrcweir             // String[] aNames = xNameAccess.getElementNames();
289cdf0e10cSrcweir             // </Analysecode>
290cdf0e10cSrcweir 
291cdf0e10cSrcweir             // get all Page Styles
292cdf0e10cSrcweir             final Object aPageStylesObj = xNameAccess.getByName(_sStyleName);
293cdf0e10cSrcweir             final XNameAccess xContainer = UnoRuntime.queryInterface(XNameAccess.class, aPageStylesObj);
294cdf0e10cSrcweir 
295cdf0e10cSrcweir             // step over all Page Styles, search the one which is in use
296cdf0e10cSrcweir             final String[] aElementNames = xContainer.getElementNames();
297cdf0e10cSrcweir             for (int i = 0; i < aElementNames.length; i++)
298cdf0e10cSrcweir             {
299cdf0e10cSrcweir                 final String sName = aElementNames[i];
300cdf0e10cSrcweir                 final Object aObj = xContainer.getByName(sName);
301cdf0e10cSrcweir                 final XStyle xStyle = UnoRuntime.queryInterface(XStyle.class, aObj);
302cdf0e10cSrcweir                 if (xStyle.isInUse())
303cdf0e10cSrcweir                 {
304cdf0e10cSrcweir                     aUsedStyle = xStyle;
305cdf0e10cSrcweir                     break;
306cdf0e10cSrcweir                 }
307cdf0e10cSrcweir             }
308cdf0e10cSrcweir         }
309cdf0e10cSrcweir         catch (com.sun.star.uno.Exception ex)
310cdf0e10cSrcweir         {
311cdf0e10cSrcweir             Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, ex);
312cdf0e10cSrcweir         }
313cdf0e10cSrcweir         return aUsedStyle;
314cdf0e10cSrcweir     }
315cdf0e10cSrcweir 
getFromPageStyles(String _sStyleName, int _nDefault)316cdf0e10cSrcweir     protected int getFromPageStyles(String _sStyleName, int _nDefault)
317cdf0e10cSrcweir     {
318cdf0e10cSrcweir         int nValue = _nDefault;
319cdf0e10cSrcweir         final XStyle xStyle = getUsedStyle("PageStyles");
320cdf0e10cSrcweir         if (xStyle != null)
321cdf0e10cSrcweir         {
322cdf0e10cSrcweir             // we found the page style which is in use
323cdf0e10cSrcweir             final PropertySetHelper aHelper = new PropertySetHelper(xStyle);
324cdf0e10cSrcweir             nValue = aHelper.getPropertyValueAsInteger(_sStyleName, nValue);
325cdf0e10cSrcweir         }
326cdf0e10cSrcweir         return nValue;
327cdf0e10cSrcweir     }
328cdf0e10cSrcweir 
setToPageStyles(String _sStyleName, Object _aObj)329cdf0e10cSrcweir     protected void setToPageStyles(String _sStyleName, Object _aObj)
330cdf0e10cSrcweir     {
331cdf0e10cSrcweir         final XStyle xStyle = getUsedStyle("PageStyles");
332cdf0e10cSrcweir         if (xStyle != null)
333cdf0e10cSrcweir         {
334cdf0e10cSrcweir             final PropertySetHelper aHelper = new PropertySetHelper(xStyle);
335cdf0e10cSrcweir             aHelper.setPropertyValueDontThrow(_sStyleName, _aObj);
336cdf0e10cSrcweir         }
337cdf0e10cSrcweir     }
338cdf0e10cSrcweir     /**
339cdf0e10cSrcweir      * Get page width. The default is 21000 1/100mm what is 21cm of DIN A4.
340cdf0e10cSrcweir      * @return the Width of the page in 1/100mm
341cdf0e10cSrcweir      */
342cdf0e10cSrcweir     int m_nPageWidth = -1;
343cdf0e10cSrcweir 
getPageWidth()344cdf0e10cSrcweir     protected int getPageWidth()
345cdf0e10cSrcweir     {
346cdf0e10cSrcweir         if (m_nPageWidth < 0)
347cdf0e10cSrcweir         {
348cdf0e10cSrcweir             m_nPageWidth = getFromPageStyles(PropertyNames.PROPERTY_WIDTH, 21000);
349cdf0e10cSrcweir         }
350cdf0e10cSrcweir         return m_nPageWidth;
351cdf0e10cSrcweir     }
352cdf0e10cSrcweir     // -------------------------------------------------------------------------
353cdf0e10cSrcweir 
354cdf0e10cSrcweir     /**
355cdf0e10cSrcweir      * Stores the Group names. To insert/create a report with such group names, call layout()
356cdf0e10cSrcweir      * @param _aGroupNames
357cdf0e10cSrcweir      */
insertGroupNames(String[] _aGroupNames)358cdf0e10cSrcweir     public void insertGroupNames(String[] _aGroupNames)
359cdf0e10cSrcweir     {
360cdf0e10cSrcweir         m_aGroupNames = _aGroupNames;
361cdf0e10cSrcweir     }
362cdf0e10cSrcweir 
insertSortingNames(String[][] _aSortFieldNames)363cdf0e10cSrcweir     public void insertSortingNames(String[][] _aSortFieldNames)
364cdf0e10cSrcweir     {
365cdf0e10cSrcweir         m_aSortNames = _aSortFieldNames;
366cdf0e10cSrcweir     }
367cdf0e10cSrcweir 
copyGroupProperties(int _nGroup)368cdf0e10cSrcweir     protected void copyGroupProperties(int _nGroup)
369cdf0e10cSrcweir     {
370cdf0e10cSrcweir         if (getDesignTemplate() != null)
371cdf0e10cSrcweir         {
372cdf0e10cSrcweir             try
373cdf0e10cSrcweir             {
374cdf0e10cSrcweir                 final XGroups xForeignGroups = getDesignTemplate().getGroups();
375cdf0e10cSrcweir                 if (_nGroup < xForeignGroups.getCount())
376cdf0e10cSrcweir                 {
377cdf0e10cSrcweir                     XGroup xForeignGroup = UnoRuntime.queryInterface(XGroup.class, xForeignGroups.getByIndex(_nGroup));
378cdf0e10cSrcweir                     XSection xForeignGroupSection = xForeignGroup.getHeader();
379cdf0e10cSrcweir 
380cdf0e10cSrcweir                     if (xForeignGroupSection != null)
381cdf0e10cSrcweir                     {
382cdf0e10cSrcweir                         final XGroups xGroups = getReportDefinition().getGroups();
383cdf0e10cSrcweir                         Object aGroup = xGroups.getByIndex(_nGroup);
384cdf0e10cSrcweir                         XGroup xGroup = UnoRuntime.queryInterface(XGroup.class, aGroup);
385cdf0e10cSrcweir                         XSection xGroupSection = xGroup.getHeader();
386cdf0e10cSrcweir 
387cdf0e10cSrcweir                         // copy Properties
388cdf0e10cSrcweir                         copyProperties(xForeignGroupSection, xGroupSection);
389cdf0e10cSrcweir                     }
390cdf0e10cSrcweir                 }
391cdf0e10cSrcweir             }
392cdf0e10cSrcweir             catch (Exception ex)
393cdf0e10cSrcweir             {
394cdf0e10cSrcweir                 Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, ex);
395cdf0e10cSrcweir             }
396cdf0e10cSrcweir         }
397cdf0e10cSrcweir     }
398cdf0e10cSrcweir     // -------------------------------------------------------------------------
399cdf0e10cSrcweir 
insertGroups()400cdf0e10cSrcweir     protected int insertGroups()
401cdf0e10cSrcweir     {
402cdf0e10cSrcweir         final XGroups xGroups = getReportDefinition().getGroups();
403cdf0e10cSrcweir         int lastGroupPosition = -1;
404cdf0e10cSrcweir 
405cdf0e10cSrcweir         if (m_aGroupNames != null)
406cdf0e10cSrcweir         {
407cdf0e10cSrcweir             final int nLeftPageIndent = getLeftPageIndent();
408cdf0e10cSrcweir             final int nLabelWidth = getMaxLabelWidth(); // 3000;
409cdf0e10cSrcweir             final int nUsablePageWidth = getPageWidth() - getLeftPageIndent() - getRightPageIndent() - getLeftGroupIndent(getCountOfGroups());
410cdf0e10cSrcweir             final int nFieldWidth = nUsablePageWidth - nLabelWidth;
411cdf0e10cSrcweir 
412cdf0e10cSrcweir             XGroup aLastGroup = null;
413cdf0e10cSrcweir             // after done with all groups, we need access to the last group, for set property 'KeepTogether' so we remember it.
414cdf0e10cSrcweir 
415cdf0e10cSrcweir             for (int i = 0; i < m_aGroupNames.length; i++)
416cdf0e10cSrcweir             {
417cdf0e10cSrcweir                 lastGroupPosition = i;
418cdf0e10cSrcweir                 final XGroup xGroup = xGroups.createGroup();
419cdf0e10cSrcweir                 aLastGroup = xGroup;
420cdf0e10cSrcweir 
421cdf0e10cSrcweir                 xGroup.setExpression(m_aGroupNames[i]);
422cdf0e10cSrcweir                 xGroup.setHeaderOn(true);
423cdf0e10cSrcweir 
424cdf0e10cSrcweir                 try
425cdf0e10cSrcweir                 {
426cdf0e10cSrcweir                     int nCount = xGroups.getCount();
427cdf0e10cSrcweir                     xGroups.insertByIndex(nCount, xGroup);
428cdf0e10cSrcweir                     final XSection xGroupSection = xGroup.getHeader();
429cdf0e10cSrcweir                     copyGroupProperties(nCount);
430cdf0e10cSrcweir 
431cdf0e10cSrcweir                     Rectangle aRect = new Rectangle();
432cdf0e10cSrcweir                     aRect.X = nLeftPageIndent + getLeftGroupIndent(i);
433cdf0e10cSrcweir                     SectionObject aSO = getDesignTemplate().getGroupLabel(i);
434cdf0e10cSrcweir                     aRect = insertLabel(xGroupSection, getTitleFromFieldName(m_aGroupNames[i]), aRect, nLabelWidth, aSO);
435cdf0e10cSrcweir                     final String sGroupName = convertToFieldName(m_aGroupNames[i]);
436cdf0e10cSrcweir                     aSO = getDesignTemplate().getGroupTextField(i);
437cdf0e10cSrcweir                     aRect = insertFormattedField(xGroupSection, sGroupName, aRect, nFieldWidth, aSO);
438cdf0e10cSrcweir                     int height = aRect.Height;
439cdf0e10cSrcweir 
440cdf0e10cSrcweir                     // draw a line under the label/formattedfield
441cdf0e10cSrcweir                     aRect.X = nLeftPageIndent + getLeftGroupIndent(i);
442cdf0e10cSrcweir                     aRect.Y = aRect.Height;
443cdf0e10cSrcweir                     final int nLineWidth = getPageWidth() - getRightPageIndent() - aRect.X;
444cdf0e10cSrcweir                     final int nLineHeight = LayoutConstants.LineHeight;
445cdf0e10cSrcweir                     insertHorizontalLine(xGroupSection, aRect, nLineWidth, nLineHeight);
446cdf0e10cSrcweir                     xGroupSection.setHeight(height + nLineHeight);
447cdf0e10cSrcweir                 }
448cdf0e10cSrcweir                 catch (com.sun.star.uno.Exception ex)
449cdf0e10cSrcweir                 {
450cdf0e10cSrcweir                     Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, ex);
451cdf0e10cSrcweir                 }
452cdf0e10cSrcweir             }
453cdf0e10cSrcweir 
454cdf0e10cSrcweir             // hold the inner group together
455cdf0e10cSrcweir             if (aLastGroup != null)
456cdf0e10cSrcweir             {
457cdf0e10cSrcweir                 doNotBreakInTable(aLastGroup);
458cdf0e10cSrcweir             }
459cdf0e10cSrcweir         }
460cdf0e10cSrcweir         if (m_aSortNames != null)
461cdf0e10cSrcweir         {
462cdf0e10cSrcweir             for (String[] sortFieldName : m_aSortNames)
463cdf0e10cSrcweir             {
464cdf0e10cSrcweir                 try
465cdf0e10cSrcweir                 {
466cdf0e10cSrcweir                     final XGroup xGroup = xGroups.createGroup();
467cdf0e10cSrcweir                     xGroup.setExpression(sortFieldName[0]);
468cdf0e10cSrcweir                     xGroup.setSortAscending(PropertyNames.ASC.equals(sortFieldName[1]));
469cdf0e10cSrcweir                     xGroup.setHeaderOn(false);
470cdf0e10cSrcweir                     int nCount = xGroups.getCount();
471cdf0e10cSrcweir                     xGroups.insertByIndex(nCount, xGroup);
472cdf0e10cSrcweir                 }
473cdf0e10cSrcweir                 catch (java.lang.Exception ex)
474cdf0e10cSrcweir                 {
475cdf0e10cSrcweir                     Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, ex);
476cdf0e10cSrcweir                 }
477cdf0e10cSrcweir             }
478cdf0e10cSrcweir         }
479cdf0e10cSrcweir         return lastGroupPosition;
480cdf0e10cSrcweir     }
481cdf0e10cSrcweir 
482cdf0e10cSrcweir     // -------------------------------------------------------------------------
483cdf0e10cSrcweir     /**
484cdf0e10cSrcweir      * Give a list off all field title names to insert the field title names, call layout()
485cdf0e10cSrcweir      * @param _aFieldTitleNames
486cdf0e10cSrcweir      */
insertFieldTitles(String[] _aFieldTitleNames)487cdf0e10cSrcweir     public void insertFieldTitles(String[] _aFieldTitleNames)
488cdf0e10cSrcweir     {
489cdf0e10cSrcweir         m_aFieldTitleNames = _aFieldTitleNames;
490cdf0e10cSrcweir     }
491cdf0e10cSrcweir 
492cdf0e10cSrcweir     // -------------------------------------------------------------------------
getTitleFromFieldName(String _sField)493cdf0e10cSrcweir     protected String getTitleFromFieldName(String _sField)
494cdf0e10cSrcweir     {
495cdf0e10cSrcweir         for (int i = 0; i < m_aFieldNames.length; i++)
496cdf0e10cSrcweir         {
497cdf0e10cSrcweir             if (m_aFieldNames[i].equals(_sField))
498cdf0e10cSrcweir             {
499cdf0e10cSrcweir                 return m_aFieldTitleNames[i];
500cdf0e10cSrcweir             }
501cdf0e10cSrcweir         }
502cdf0e10cSrcweir         return PropertyNames.EMPTY_STRING;
503cdf0e10cSrcweir     }
504cdf0e10cSrcweir 
getTypeFromFieldName(String _sField)505cdf0e10cSrcweir     protected int getTypeFromFieldName(String _sField)
506cdf0e10cSrcweir     {
507cdf0e10cSrcweir         for (int i = 0; i < m_aFieldNames.length; i++)
508cdf0e10cSrcweir         {
509cdf0e10cSrcweir             if (m_aFieldNames[i].equals(_sField))
510cdf0e10cSrcweir             {
511cdf0e10cSrcweir                 return m_aFieldTypes[i];
512cdf0e10cSrcweir             }
513cdf0e10cSrcweir         }
514cdf0e10cSrcweir         return 0;
515cdf0e10cSrcweir     }
516cdf0e10cSrcweir 
listContains(String[] _aList, String _aValue)517cdf0e10cSrcweir     protected boolean listContains(String[] _aList, String _aValue)
518cdf0e10cSrcweir     {
519cdf0e10cSrcweir         for (int i = 0; i < _aList.length; i++)
520cdf0e10cSrcweir         {
521cdf0e10cSrcweir             if (_aList[i].equals(_aValue))
522cdf0e10cSrcweir             {
523cdf0e10cSrcweir                 return true;
524cdf0e10cSrcweir             }
525cdf0e10cSrcweir         }
526cdf0e10cSrcweir         return false;
527cdf0e10cSrcweir     }
528cdf0e10cSrcweir     // -------------------------------------------------------------------------
529cdf0e10cSrcweir 
530cdf0e10cSrcweir     /**
531cdf0e10cSrcweir      * Helper to get all field names without the names which are already in the group names
532cdf0e10cSrcweir      * @param _aList
533cdf0e10cSrcweir      * @param _aGetResultFrom
534cdf0e10cSrcweir      * @return
535cdf0e10cSrcweir      */
getNamesWithoutGroupNames(String[] _aList, String[] _aGetResultFrom)536cdf0e10cSrcweir     protected String[] getNamesWithoutGroupNames(String[] _aList, String[] _aGetResultFrom)
537cdf0e10cSrcweir     {
538cdf0e10cSrcweir         if (_aList == null /* || _aGetResultsFrom == null */)
539cdf0e10cSrcweir         {
540cdf0e10cSrcweir             return new String[]
541cdf0e10cSrcweir                     {
542cdf0e10cSrcweir                     }; /* empty list */
543cdf0e10cSrcweir         }
544cdf0e10cSrcweir         if (getCountOfGroups() == 0)
545cdf0e10cSrcweir         {
546cdf0e10cSrcweir             if (_aGetResultFrom != null)
547cdf0e10cSrcweir             {
548cdf0e10cSrcweir                 return _aGetResultFrom;
549cdf0e10cSrcweir             }
550cdf0e10cSrcweir             return _aList;
551cdf0e10cSrcweir         }
552cdf0e10cSrcweir         final int nNewLength = _aList.length - getCountOfGroups();
553cdf0e10cSrcweir         String[] aNewList = new String[nNewLength];
554cdf0e10cSrcweir         int j = 0;
555cdf0e10cSrcweir         for (int i = 0; i < _aList.length; i++)
556cdf0e10cSrcweir         {
557cdf0e10cSrcweir             final String sField = _aList[i];
558cdf0e10cSrcweir             if (listContains(m_aGroupNames, sField))
559cdf0e10cSrcweir             {
560cdf0e10cSrcweir                 continue;
561cdf0e10cSrcweir             }
562cdf0e10cSrcweir             if (_aGetResultFrom != null)
563cdf0e10cSrcweir             {
564cdf0e10cSrcweir                 aNewList[j++] = _aGetResultFrom[i];
565cdf0e10cSrcweir             }
566cdf0e10cSrcweir             else
567cdf0e10cSrcweir             {
568cdf0e10cSrcweir                 aNewList[j++] = sField;
569cdf0e10cSrcweir             }
570cdf0e10cSrcweir             if (j == nNewLength)
571cdf0e10cSrcweir             {
572cdf0e10cSrcweir                 break; // Emergency break, we leave the result list.
573cdf0e10cSrcweir             }
574cdf0e10cSrcweir         }
575cdf0e10cSrcweir         return aNewList;
576cdf0e10cSrcweir     }
577cdf0e10cSrcweir 
578cdf0e10cSrcweir     // -------------------------------------------------------------------------
calculateFieldWidth(int _nLeftIndent, int _nFieldCount)579cdf0e10cSrcweir     protected int calculateFieldWidth(int _nLeftIndent, int _nFieldCount)
580cdf0e10cSrcweir     {
581cdf0e10cSrcweir         int nWidth = 3000;
582cdf0e10cSrcweir         if (_nFieldCount > 0)
583cdf0e10cSrcweir         {
584cdf0e10cSrcweir             nWidth = (getPageWidth() - getLeftPageIndent() - getRightPageIndent() - _nLeftIndent) / _nFieldCount;
585cdf0e10cSrcweir         }
586cdf0e10cSrcweir         return nWidth;
587cdf0e10cSrcweir     }
588cdf0e10cSrcweir 
getFieldTitleNames()589cdf0e10cSrcweir     protected String[] getFieldTitleNames()
590cdf0e10cSrcweir     {
591cdf0e10cSrcweir         return getNamesWithoutGroupNames(m_aFieldNames, m_aFieldTitleNames);
592cdf0e10cSrcweir     }
593cdf0e10cSrcweir     // -------------------------------------------------------------------------
594cdf0e10cSrcweir 
insertDetailFieldTitles(int lastGroupPostion)595cdf0e10cSrcweir     abstract protected void insertDetailFieldTitles(int lastGroupPostion);
596cdf0e10cSrcweir     // -------------------------------------------------------------------------
597cdf0e10cSrcweir 
598cdf0e10cSrcweir     /**
599cdf0e10cSrcweir      * Give a list off all field names to insert the field names, call layout()
600cdf0e10cSrcweir      * @param _aFieldNames
601cdf0e10cSrcweir      */
insertFieldNames(String[] _aFieldNames)602cdf0e10cSrcweir     public void insertFieldNames(String[] _aFieldNames)
603cdf0e10cSrcweir     {
604cdf0e10cSrcweir         m_aFieldNames = _aFieldNames;
605cdf0e10cSrcweir     }
606cdf0e10cSrcweir 
insertFieldTypes(int[] _aFieldTypes)607cdf0e10cSrcweir     public void insertFieldTypes(int[] _aFieldTypes)
608cdf0e10cSrcweir     {
609cdf0e10cSrcweir         m_aFieldTypes = _aFieldTypes;
610cdf0e10cSrcweir     }
611cdf0e10cSrcweir 
insertFieldWidths(int[] _aFieldWidths)612cdf0e10cSrcweir     public void insertFieldWidths(int[] _aFieldWidths)
613cdf0e10cSrcweir     {
614cdf0e10cSrcweir         m_aFieldWidths = _aFieldWidths;
615cdf0e10cSrcweir     }
616cdf0e10cSrcweir 
getCountOfGroups()617cdf0e10cSrcweir     protected int getCountOfGroups()
618cdf0e10cSrcweir     {
619cdf0e10cSrcweir         return ((m_aGroupNames == null) ? 0 : m_aGroupNames.length);
620cdf0e10cSrcweir     }
621cdf0e10cSrcweir 
622cdf0e10cSrcweir     // -------------------------------------------------------------------------
getFieldNames()623cdf0e10cSrcweir     protected String[] getFieldNames()
624cdf0e10cSrcweir     {
625cdf0e10cSrcweir         return getNamesWithoutGroupNames(m_aFieldNames, null);
626cdf0e10cSrcweir     }
627cdf0e10cSrcweir 
insertDetailFields()628cdf0e10cSrcweir     abstract protected void insertDetailFields();
629cdf0e10cSrcweir 
copyDetailProperties()630cdf0e10cSrcweir     protected void copyDetailProperties()
631cdf0e10cSrcweir     {
632cdf0e10cSrcweir         if (getDesignTemplate() != null)
633cdf0e10cSrcweir         {
634cdf0e10cSrcweir             try
635cdf0e10cSrcweir             {
636cdf0e10cSrcweir                 XSection xForeignSection = getDesignTemplate().getDetail();
637cdf0e10cSrcweir                 if (xForeignSection != null)
638cdf0e10cSrcweir                 {
639cdf0e10cSrcweir                     XSection xSection = getReportDefinition().getDetail();
640cdf0e10cSrcweir 
641cdf0e10cSrcweir                     // copy Properties
642cdf0e10cSrcweir                     copyProperties(xForeignSection, xSection);
643cdf0e10cSrcweir                 }
644cdf0e10cSrcweir             }
645cdf0e10cSrcweir             catch (Exception ex)
646cdf0e10cSrcweir             {
647cdf0e10cSrcweir                 Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, ex);
648cdf0e10cSrcweir             }
649cdf0e10cSrcweir         }
650cdf0e10cSrcweir     }
651cdf0e10cSrcweir     // -------------------------------------------------------------------------
652cdf0e10cSrcweir 
insertLabel(XSection _xSection, String _sLabel, Rectangle _aRect, int _nWidth, SectionObject _aSO)653cdf0e10cSrcweir     protected Rectangle insertLabel(XSection _xSection, String _sLabel, Rectangle _aRect, int _nWidth, SectionObject _aSO)
654cdf0e10cSrcweir     {
655cdf0e10cSrcweir         if (_xSection != null)
656cdf0e10cSrcweir         {
657cdf0e10cSrcweir             try
658cdf0e10cSrcweir             {
659cdf0e10cSrcweir                 final Object aFixedText = getMSFofReportDefinition().createInstance("com.sun.star.report.FixedText");
660cdf0e10cSrcweir                 final XFixedText xFixedText = UnoRuntime.queryInterface(XFixedText.class, aFixedText);
661cdf0e10cSrcweir 
662cdf0e10cSrcweir 
663cdf0e10cSrcweir                 int nHeight = LayoutConstants.LabelHeight;        // default height of label is fixed.
664cdf0e10cSrcweir                 if (_aSO != null)
665cdf0e10cSrcweir                 {
666cdf0e10cSrcweir                     if (_aSO instanceof SectionEmptyObject)
667cdf0e10cSrcweir                     {
668cdf0e10cSrcweir                         float fCharWeight = _aSO.getCharWeight(com.sun.star.awt.FontWeight.NORMAL);
669cdf0e10cSrcweir                         if (fCharWeight > 0.1f)
670cdf0e10cSrcweir                         {
671cdf0e10cSrcweir                             xFixedText.setCharWeight(fCharWeight);
672cdf0e10cSrcweir                         }
673cdf0e10cSrcweir                     }
674cdf0e10cSrcweir                     else
675cdf0e10cSrcweir                     {
676cdf0e10cSrcweir // TODO: there seems to be some problems with copy all properties from the design template to the current design
677cdf0e10cSrcweir                         final FontDescriptor aFD = _aSO.getFontDescriptor();
678cdf0e10cSrcweir                         if (aFD != null)
679cdf0e10cSrcweir                         {
680cdf0e10cSrcweir                             xFixedText.setFontDescriptor(aFD);
681cdf0e10cSrcweir                             copyProperties(_aSO.getParent(), xFixedText);
682cdf0e10cSrcweir                         }
683cdf0e10cSrcweir                         nHeight = _aSO.getHeight(LayoutConstants.LabelHeight);
684cdf0e10cSrcweir                     }
685cdf0e10cSrcweir                 }
686cdf0e10cSrcweir                 xFixedText.setLabel(_sLabel);
687cdf0e10cSrcweir 
688cdf0e10cSrcweir                 xFixedText.setPositionX(_aRect.X);
689cdf0e10cSrcweir                 xFixedText.setPositionY(_aRect.Y);
690cdf0e10cSrcweir 
691cdf0e10cSrcweir                 // Width will calculate from outside.
692cdf0e10cSrcweir                 // We have to set, because there exist no right default (0)
693cdf0e10cSrcweir                 xFixedText.setWidth(_nWidth);
694cdf0e10cSrcweir                 _aRect.X += _nWidth;
695cdf0e10cSrcweir                 xFixedText.setHeight(nHeight);
696cdf0e10cSrcweir                 _xSection.add(xFixedText);
697cdf0e10cSrcweir             }
698cdf0e10cSrcweir             catch (com.sun.star.uno.Exception ex)
699cdf0e10cSrcweir             {
700cdf0e10cSrcweir                 Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, ex);
701cdf0e10cSrcweir             }
702cdf0e10cSrcweir         }
703cdf0e10cSrcweir         return _aRect;
704cdf0e10cSrcweir     }
705cdf0e10cSrcweir     // -------------------------------------------------------------------------
706cdf0e10cSrcweir 
convertToFieldName(String _sElementName)707cdf0e10cSrcweir     protected String convertToFieldName(String _sElementName)
708cdf0e10cSrcweir     {
709cdf0e10cSrcweir         final StringBuffer aDataField = new StringBuffer(32);
710cdf0e10cSrcweir         aDataField.append("field:[").append(_sElementName).append(']');
711cdf0e10cSrcweir         return aDataField.toString();
712cdf0e10cSrcweir 
713cdf0e10cSrcweir     }
714cdf0e10cSrcweir 
convertFromFieldName(String _sName)715cdf0e10cSrcweir     protected String convertFromFieldName(String _sName)
716cdf0e10cSrcweir     {
717cdf0e10cSrcweir         if (_sName.startsWith("field:["))
718cdf0e10cSrcweir         {
719cdf0e10cSrcweir             int nCloseBrace = _sName.lastIndexOf("]");
720cdf0e10cSrcweir             return _sName.substring(7, nCloseBrace).trim();
721cdf0e10cSrcweir         }
722cdf0e10cSrcweir         return _sName;
723cdf0e10cSrcweir     }
724cdf0e10cSrcweir     // -------------------------------------------------------------------------
725cdf0e10cSrcweir 
726cdf0e10cSrcweir     /**
727cdf0e10cSrcweir      * Insert a already formatted field name into a given section
728cdf0e10cSrcweir      *
729cdf0e10cSrcweir      * Use 'convertToFieldName(dbfield)' to convert a dbfield name in the right.
730cdf0e10cSrcweir      *
731cdf0e10cSrcweir      * @param _xSection        in which section the formatted field will store
732cdf0e10cSrcweir      * @param _sFormattedfield as String a dbfield or an other function
733cdf0e10cSrcweir      * @param _aRect           .X, .Y are the absolute position (1/100mm) where the formatted field will set
734cdf0e10cSrcweir      * @param _nWidth          the width of the field in 1/100mm
735cdf0e10cSrcweir      * @param _aSO
736cdf0e10cSrcweir      * @return a new Rectangle with the new Rect.X position, Rect.Y will not change.
737cdf0e10cSrcweir      */
insertFormattedField(XSection _xSection, String _sFormattedfield, Rectangle _aRect, int _nWidth, SectionObject _aSO)738cdf0e10cSrcweir     protected Rectangle insertFormattedField(XSection _xSection, String _sFormattedfield, Rectangle _aRect, int _nWidth, SectionObject _aSO)
739cdf0e10cSrcweir     {
740cdf0e10cSrcweir         return insertFormattedField(_xSection, _sFormattedfield, _aRect, _nWidth, _aSO, (short) com.sun.star.awt.TextAlign.LEFT);
741cdf0e10cSrcweir     }
742cdf0e10cSrcweir 
insertFormattedField(XSection _xSection, String _sFormattedfield, Rectangle _aRect, int _nWidth, SectionObject _aSO, short _nAlignment)743cdf0e10cSrcweir     protected Rectangle insertFormattedField(XSection _xSection, String _sFormattedfield, Rectangle _aRect, int _nWidth, SectionObject _aSO, short _nAlignment)
744cdf0e10cSrcweir     {
745cdf0e10cSrcweir         if (_xSection != null)
746cdf0e10cSrcweir         {
747cdf0e10cSrcweir             try
748cdf0e10cSrcweir             {
749cdf0e10cSrcweir                 Object aField;
750cdf0e10cSrcweir                 int nHeight = LayoutConstants.FormattedFieldHeight;
751cdf0e10cSrcweir 
752cdf0e10cSrcweir                 int nType = getTypeFromFieldName(convertFromFieldName(_sFormattedfield));
753cdf0e10cSrcweir                 if (nType == DataType.BINARY
754cdf0e10cSrcweir                         || nType == DataType.VARBINARY
755cdf0e10cSrcweir                         || nType == DataType.LONGVARBINARY)
756cdf0e10cSrcweir                 {
757cdf0e10cSrcweir                     aField = getMSFofReportDefinition().createInstance("com.sun.star.report.ImageControl");
758cdf0e10cSrcweir                     nHeight = LayoutConstants.BinaryHeight;
759cdf0e10cSrcweir                 }
760cdf0e10cSrcweir                 else
761cdf0e10cSrcweir                 {
762cdf0e10cSrcweir                     aField = getMSFofReportDefinition().createInstance("com.sun.star.report.FormattedField");
763cdf0e10cSrcweir                     nHeight = LayoutConstants.FormattedFieldHeight;
764cdf0e10cSrcweir                     if (nType == DataType.LONGVARCHAR) /* memo */
765cdf0e10cSrcweir 
766cdf0e10cSrcweir                     {
767cdf0e10cSrcweir                         nHeight = LayoutConstants.MemoFieldHeight; // special case for memo
768cdf0e10cSrcweir                     }
769cdf0e10cSrcweir                 }
770cdf0e10cSrcweir                 _aRect.Height = nHeight;
771cdf0e10cSrcweir 
772cdf0e10cSrcweir                 final XReportControlModel xReportControlModel = UnoRuntime.queryInterface(XReportControlModel.class, aField);
773cdf0e10cSrcweir                 if (xReportControlModel != null)
774cdf0e10cSrcweir                 {
775cdf0e10cSrcweir                     // #i86907# not documented right in idl description.
776cdf0e10cSrcweir                     xReportControlModel.setDataField(_sFormattedfield);
777cdf0e10cSrcweir                     if (_aSO != null)
778cdf0e10cSrcweir                     {
779cdf0e10cSrcweir                         // TODO: there seems to be some problems with copy all properties from the design template to the current design
780cdf0e10cSrcweir                         final FontDescriptor aFD = _aSO.getFontDescriptor();
781cdf0e10cSrcweir                         if (aFD != null)
782cdf0e10cSrcweir                         {
783cdf0e10cSrcweir                             xReportControlModel.setFontDescriptor(aFD);
784cdf0e10cSrcweir                             copyProperties(_aSO.getParent(), xReportControlModel);
785cdf0e10cSrcweir                         }
786cdf0e10cSrcweir                         nHeight = _aSO.getHeight(nHeight);
787cdf0e10cSrcweir                     }
788cdf0e10cSrcweir                     xReportControlModel.setPositionX(_aRect.X);
789cdf0e10cSrcweir                     xReportControlModel.setPositionY(_aRect.Y);
790cdf0e10cSrcweir                     xReportControlModel.setWidth(_nWidth);
791cdf0e10cSrcweir                     _aRect.X += _nWidth;
792cdf0e10cSrcweir                     xReportControlModel.setHeight(nHeight);
793cdf0e10cSrcweir 
794cdf0e10cSrcweir                     if (nType == DataType.BINARY
795cdf0e10cSrcweir                             || nType == DataType.VARBINARY
796cdf0e10cSrcweir                             || nType == DataType.LONGVARBINARY)
797cdf0e10cSrcweir                     {
798cdf0e10cSrcweir                         // aField = getMSFofReportDefinition().createInstance("com.sun.star.report.ImageControl");
799cdf0e10cSrcweir                         final XImageControl xImageControl = UnoRuntime.queryInterface(XImageControl.class, xReportControlModel);
800cdf0e10cSrcweir                         if (xImageControl != null)
801cdf0e10cSrcweir                         {
802cdf0e10cSrcweir                             // xImageControl.setScaleImage(true);
803cdf0e10cSrcweir 
804*b6dc695eSAriel Constenla-Haile                             xImageControl.setScaleMode(com.sun.star.awt.ImageScaleMode.ISOTROPIC);
805cdf0e10cSrcweir                         }
806cdf0e10cSrcweir                     }
807cdf0e10cSrcweir                     else
808cdf0e10cSrcweir                     {
809cdf0e10cSrcweir                         try
810cdf0e10cSrcweir                         {
811cdf0e10cSrcweir                             xReportControlModel.setParaAdjust(_nAlignment);
812cdf0e10cSrcweir                             // if (nType == DataType.LONGVARCHAR)
813cdf0e10cSrcweir                             // {
814cdf0e10cSrcweir                             //     xReportControlModel.???
815cdf0e10cSrcweir                             // }
816cdf0e10cSrcweir                         }
817cdf0e10cSrcweir                         catch (com.sun.star.beans.UnknownPropertyException ex)
818cdf0e10cSrcweir                         {
819cdf0e10cSrcweir                             Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, ex);
820cdf0e10cSrcweir                         }
821cdf0e10cSrcweir                     }
822cdf0e10cSrcweir                     // spezial case rpt:now() (default date format)
823cdf0e10cSrcweir                     if (_sFormattedfield.equals("rpt:now()"))
824cdf0e10cSrcweir                     {
825cdf0e10cSrcweir                         final XFormattedField xFormattedField = UnoRuntime.queryInterface(XFormattedField.class, xReportControlModel);
826cdf0e10cSrcweir 
827cdf0e10cSrcweir                         XNumberFormatsSupplier x = xFormattedField.getFormatsSupplier();
828cdf0e10cSrcweir                         XNumberFormats xFormats = x.getNumberFormats();
829cdf0e10cSrcweir                         XNumberFormatTypes x3 = UnoRuntime.queryInterface(XNumberFormatTypes.class, xFormats);
830cdf0e10cSrcweir                         Locale.getDefault();
831cdf0e10cSrcweir                         com.sun.star.lang.Locale aLocale = new com.sun.star.lang.Locale();
832cdf0e10cSrcweir                         aLocale.Country = Locale.getDefault().getCountry();
833cdf0e10cSrcweir                         aLocale.Language = Locale.getDefault().getLanguage();
834cdf0e10cSrcweir 
835cdf0e10cSrcweir                         int nFormat = x3.getStandardFormat(com.sun.star.util.NumberFormat.DATE, aLocale);
836cdf0e10cSrcweir                         xFormattedField.setFormatKey(nFormat);
837cdf0e10cSrcweir                     }
838cdf0e10cSrcweir                     _xSection.add(xReportControlModel);
839cdf0e10cSrcweir                 }
840cdf0e10cSrcweir             }
841cdf0e10cSrcweir             catch (com.sun.star.uno.Exception ex)
842cdf0e10cSrcweir             {
843cdf0e10cSrcweir                 Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, ex);
844cdf0e10cSrcweir             }
845cdf0e10cSrcweir         }
846cdf0e10cSrcweir         return _aRect;
847cdf0e10cSrcweir     }
848cdf0e10cSrcweir 
849cdf0e10cSrcweir     // -------------------------------------------------------------------------
850cdf0e10cSrcweir // TODO: check with Query, this code will not work with Queries
setTableName(int _aType, String _sTableName)851cdf0e10cSrcweir     public void setTableName(int _aType, String _sTableName)
852cdf0e10cSrcweir     {
853cdf0e10cSrcweir         m_aCommandType = _aType;
854cdf0e10cSrcweir         m_sTableName = _sTableName;
855cdf0e10cSrcweir 
856cdf0e10cSrcweir         // getReportDefinition().setCommandType(com.sun.star.sdb.CommandType.TABLE);
857cdf0e10cSrcweir         getReportDefinition().setCommandType(_aType);
858cdf0e10cSrcweir         getReportDefinition().setCommand(_sTableName);
859cdf0e10cSrcweir     }    // -------------------------------------------------------------------------
860cdf0e10cSrcweir     protected XMultiServiceFactory m_xMSF;
861cdf0e10cSrcweir 
getMSFofReportDefinition()862cdf0e10cSrcweir     protected XMultiServiceFactory getMSFofReportDefinition()
863cdf0e10cSrcweir     {
864cdf0e10cSrcweir         if (m_xMSF == null)
865cdf0e10cSrcweir         {
866cdf0e10cSrcweir             m_xMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, getReportDefinition());
867cdf0e10cSrcweir         }
868cdf0e10cSrcweir         return m_xMSF;
869cdf0e10cSrcweir     }
870cdf0e10cSrcweir     // -------------------------------------------------------------------------
871cdf0e10cSrcweir 
insertVerticalLine(XSection _xSection, Rectangle _aRect, int _nWidth, int _nHeight)872cdf0e10cSrcweir     protected Rectangle insertVerticalLine(XSection _xSection, Rectangle _aRect, int _nWidth, int _nHeight)
873cdf0e10cSrcweir     {
874cdf0e10cSrcweir         return insertLine(_xSection, _aRect, _nWidth, _nHeight, 1);
875cdf0e10cSrcweir     }
876cdf0e10cSrcweir 
insertHorizontalLine(XSection _xSection, Rectangle _aRect, int _nWidth, int _nHeight)877cdf0e10cSrcweir     protected Rectangle insertHorizontalLine(XSection _xSection, Rectangle _aRect, int _nWidth, int _nHeight)
878cdf0e10cSrcweir     {
879cdf0e10cSrcweir         return insertLine(_xSection, _aRect, _nWidth, _nHeight, 0);
880cdf0e10cSrcweir     }
881cdf0e10cSrcweir 
insertLine(XSection _xSection, Rectangle _aRect, int _nWidth, int _nHeight, int _nOrientation)882cdf0e10cSrcweir     protected Rectangle insertLine(XSection _xSection, Rectangle _aRect, int _nWidth, int _nHeight, int _nOrientation)
883cdf0e10cSrcweir     {
884cdf0e10cSrcweir         if (_xSection != null)
885cdf0e10cSrcweir         {
886cdf0e10cSrcweir             try
887cdf0e10cSrcweir             {
888cdf0e10cSrcweir                 final Object aFixedLine = getMSFofReportDefinition().createInstance("com.sun.star.report.FixedLine");
889cdf0e10cSrcweir                 final XFixedLine xFixedLine = UnoRuntime.queryInterface(XFixedLine.class, aFixedLine);
890cdf0e10cSrcweir 
891cdf0e10cSrcweir                 xFixedLine.setOrientation(_nOrientation);
892cdf0e10cSrcweir                 // TODO: line width is fixed
893cdf0e10cSrcweir                 xFixedLine.setLineWidth(8);
894cdf0e10cSrcweir 
895cdf0e10cSrcweir                 xFixedLine.setPositionX(_aRect.X);
896cdf0e10cSrcweir                 xFixedLine.setPositionY(_aRect.Y);
897cdf0e10cSrcweir 
898cdf0e10cSrcweir                 xFixedLine.setWidth(_nWidth);
899cdf0e10cSrcweir                 _aRect.X += _nWidth;
900cdf0e10cSrcweir                 xFixedLine.setHeight(_nHeight);
901cdf0e10cSrcweir                 _xSection.add(xFixedLine);
902cdf0e10cSrcweir             }
903cdf0e10cSrcweir             catch (com.sun.star.uno.Exception ex)
904cdf0e10cSrcweir             {
905cdf0e10cSrcweir                 Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, ex);
906cdf0e10cSrcweir             }
907cdf0e10cSrcweir         }
908cdf0e10cSrcweir         return _aRect;
909cdf0e10cSrcweir     }
910cdf0e10cSrcweir     // -------------------------------------------------------------------------
911cdf0e10cSrcweir 
clearReportHeader()912cdf0e10cSrcweir     protected void clearReportHeader()
913cdf0e10cSrcweir     {
914cdf0e10cSrcweir         XSection xSection;
915cdf0e10cSrcweir         try
916cdf0e10cSrcweir         {
917cdf0e10cSrcweir             if (getReportDefinition().getReportHeaderOn())
918cdf0e10cSrcweir             {
919cdf0e10cSrcweir                 xSection = getReportDefinition().getReportHeader();
920cdf0e10cSrcweir                 emptySection(xSection);
921cdf0e10cSrcweir             }
922cdf0e10cSrcweir         }
923cdf0e10cSrcweir         catch (com.sun.star.container.NoSuchElementException ex)
924cdf0e10cSrcweir         {
925cdf0e10cSrcweir             Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, ex);
926cdf0e10cSrcweir         }
927cdf0e10cSrcweir     }
928cdf0e10cSrcweir 
insertReportHeader()929cdf0e10cSrcweir     protected void insertReportHeader()
930cdf0e10cSrcweir     {
931cdf0e10cSrcweir         if (getDesignTemplate() != null)
932cdf0e10cSrcweir         {
933cdf0e10cSrcweir             if (getDesignTemplate().getReportHeaderOn())
934cdf0e10cSrcweir             {
935cdf0e10cSrcweir                 // copy all Section information from Page Header to our Header
936cdf0e10cSrcweir                 try
937cdf0e10cSrcweir                 {
938cdf0e10cSrcweir                     XSection xForeignSection = getDesignTemplate().getReportHeader();
939cdf0e10cSrcweir 
940cdf0e10cSrcweir                     if (xForeignSection != null)
941cdf0e10cSrcweir                     {
942cdf0e10cSrcweir                         getReportDefinition().setReportHeaderOn(true);
943cdf0e10cSrcweir                         XSection xSection = getReportDefinition().getReportHeader();
944cdf0e10cSrcweir 
945cdf0e10cSrcweir                         // copy Sections
946cdf0e10cSrcweir                         copySection(xForeignSection, xSection);
947cdf0e10cSrcweir                     }
948cdf0e10cSrcweir                 }
949cdf0e10cSrcweir                 catch (Exception e)
950cdf0e10cSrcweir                 {
951cdf0e10cSrcweir                     Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
952cdf0e10cSrcweir                 }
953cdf0e10cSrcweir             }
954cdf0e10cSrcweir             else
955cdf0e10cSrcweir             {
956cdf0e10cSrcweir                 // we won't a page header
957cdf0e10cSrcweir                 getReportDefinition().setReportHeaderOn(false);
958cdf0e10cSrcweir             }
959cdf0e10cSrcweir         }
960cdf0e10cSrcweir     }
961cdf0e10cSrcweir 
clearReportFooter()962cdf0e10cSrcweir     protected void clearReportFooter()
963cdf0e10cSrcweir     {
964cdf0e10cSrcweir         XSection xSection;
965cdf0e10cSrcweir         try
966cdf0e10cSrcweir         {
967cdf0e10cSrcweir             if (getReportDefinition().getReportFooterOn())
968cdf0e10cSrcweir             {
969cdf0e10cSrcweir                 xSection = getReportDefinition().getReportFooter();
970cdf0e10cSrcweir                 emptySection(xSection);
971cdf0e10cSrcweir             }
972cdf0e10cSrcweir         }
973cdf0e10cSrcweir         catch (com.sun.star.container.NoSuchElementException e)
974cdf0e10cSrcweir         {
975cdf0e10cSrcweir             Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
976cdf0e10cSrcweir         }
977cdf0e10cSrcweir     }
978cdf0e10cSrcweir 
insertReportFooter()979cdf0e10cSrcweir     protected void insertReportFooter()
980cdf0e10cSrcweir     {
981cdf0e10cSrcweir         if (getDesignTemplate() != null)
982cdf0e10cSrcweir         {
983cdf0e10cSrcweir             if (getDesignTemplate().getReportFooterOn())
984cdf0e10cSrcweir             {
985cdf0e10cSrcweir                 // copy all Section information from Page Header to our Header
986cdf0e10cSrcweir                 try
987cdf0e10cSrcweir                 {
988cdf0e10cSrcweir                     XSection xForeignSection = getDesignTemplate().getReportFooter();
989cdf0e10cSrcweir 
990cdf0e10cSrcweir                     if (xForeignSection != null)
991cdf0e10cSrcweir                     {
992cdf0e10cSrcweir                         getReportDefinition().setReportFooterOn(true);
993cdf0e10cSrcweir                         XSection xSection = getReportDefinition().getReportFooter();
994cdf0e10cSrcweir 
995cdf0e10cSrcweir                         // copy Sections
996cdf0e10cSrcweir                         copySection(xForeignSection, xSection);
997cdf0e10cSrcweir                     }
998cdf0e10cSrcweir                 }
999cdf0e10cSrcweir                 catch (Exception e)
1000cdf0e10cSrcweir                 {
1001cdf0e10cSrcweir                     Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
1002cdf0e10cSrcweir                 }
1003cdf0e10cSrcweir             }
1004cdf0e10cSrcweir             else
1005cdf0e10cSrcweir             {
1006cdf0e10cSrcweir                 // we won't a page header
1007cdf0e10cSrcweir                 getReportDefinition().setReportFooterOn(false);
1008cdf0e10cSrcweir             }
1009cdf0e10cSrcweir         }
1010cdf0e10cSrcweir     }
1011cdf0e10cSrcweir     // -------------------------------------------------------------------------
1012cdf0e10cSrcweir 
clearPageHeader()1013cdf0e10cSrcweir     protected void clearPageHeader()
1014cdf0e10cSrcweir     {
1015cdf0e10cSrcweir         XSection xSection;
1016cdf0e10cSrcweir         try
1017cdf0e10cSrcweir         {
1018cdf0e10cSrcweir             if (getReportDefinition().getPageHeaderOn())
1019cdf0e10cSrcweir             {
1020cdf0e10cSrcweir                 xSection = getReportDefinition().getPageHeader();
1021cdf0e10cSrcweir                 emptySection(xSection);
1022cdf0e10cSrcweir             }
1023cdf0e10cSrcweir         }
1024cdf0e10cSrcweir         catch (com.sun.star.container.NoSuchElementException e)
1025cdf0e10cSrcweir         {
1026cdf0e10cSrcweir             Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
1027cdf0e10cSrcweir         }
1028cdf0e10cSrcweir     }
1029cdf0e10cSrcweir 
1030cdf0e10cSrcweir //    protected static String getDateString(String _sFormat)
1031cdf0e10cSrcweir //        {
1032cdf0e10cSrcweir //            GregorianCalendar aCalendar = new GregorianCalendar();
1033cdf0e10cSrcweir //            StringBuffer aBuf = new StringBuffer();
1034cdf0e10cSrcweir //
1035cdf0e10cSrcweir //            Locale aLocale = new Locale("en","US");
1036cdf0e10cSrcweir //            SimpleDateFormat aFormat = new SimpleDateFormat(_sFormat, aLocale);
1037cdf0e10cSrcweir //            aBuf = aFormat.format(aCalendar.getTime(), aBuf, new FieldPosition(0) );
1038cdf0e10cSrcweir //            // DebugHelper.writeInfo("Date: " + aBuf.toString());
1039cdf0e10cSrcweir //            return aBuf.toString();
1040cdf0e10cSrcweir //    }
1041cdf0e10cSrcweir //    protected String getCurrentDate()
1042cdf0e10cSrcweir //    {
1043cdf0e10cSrcweir //        Calendar aCalendar = Calendar.getInstance();
1044cdf0e10cSrcweir ////        aCalendar.setTimeInMillis(1202382900000L);
1045cdf0e10cSrcweir ////        aCalendar.
1046cdf0e10cSrcweir //        Date aDate = new Date();
1047cdf0e10cSrcweir //        String sDate = DateFormat.getDateInstance().format(aDate);
1048cdf0e10cSrcweir //        // String aCalStr = aCalendar.toString();
1049cdf0e10cSrcweir //        return sDate;
1050cdf0e10cSrcweir //        //
1051cdf0e10cSrcweir ////        Date aDate = new Date();
1052cdf0e10cSrcweir ////        aDate.setSeconds(0);
1053cdf0e10cSrcweir ////        aDate.setMinutes(15);
1054cdf0e10cSrcweir ////        aDate.setHours(12);
1055cdf0e10cSrcweir ////        // aDate.setMonth(2);
1056cdf0e10cSrcweir ////        // aDate.setYear(2008);
1057cdf0e10cSrcweir ////        // aDate.setDay(7);
1058cdf0e10cSrcweir ////        long nTime = aDate.getTime();
1059cdf0e10cSrcweir ////        Long aLong = new Long(nTime);
1060cdf0e10cSrcweir ////        String aStr = aLong.toString();
1061cdf0e10cSrcweir ////
1062cdf0e10cSrcweir ////        Date aNewDate = new Date(1202382900000L);
1063cdf0e10cSrcweir //////         aNewDate.
1064cdf0e10cSrcweir ////        String aDateStr = aNewDate.toString();
1065cdf0e10cSrcweir //////         Datetime aNewTime = new Time(1202382900);
1066cdf0e10cSrcweir //////         String aTimeStr = aNewTime.toString();
1067cdf0e10cSrcweir ////
1068cdf0e10cSrcweir //
1069cdf0e10cSrcweir //    }
clearPageFooter()1070cdf0e10cSrcweir     protected void clearPageFooter()
1071cdf0e10cSrcweir     {
1072cdf0e10cSrcweir         XSection xSection;
1073cdf0e10cSrcweir         try
1074cdf0e10cSrcweir         {
1075cdf0e10cSrcweir             if (getReportDefinition().getPageFooterOn())
1076cdf0e10cSrcweir             {
1077cdf0e10cSrcweir                 xSection = getReportDefinition().getPageFooter();
1078cdf0e10cSrcweir                 emptySection(xSection);
1079cdf0e10cSrcweir             }
1080cdf0e10cSrcweir         }
1081cdf0e10cSrcweir         catch (com.sun.star.container.NoSuchElementException e)
1082cdf0e10cSrcweir         {
1083cdf0e10cSrcweir             Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
1084cdf0e10cSrcweir         }
1085cdf0e10cSrcweir     }
1086cdf0e10cSrcweir 
setPageOrientation(int _nOrientation)1087cdf0e10cSrcweir     public void setPageOrientation(int _nOrientation)
1088cdf0e10cSrcweir     {
1089cdf0e10cSrcweir         final int nWidth = getFromPageStyles(PropertyNames.PROPERTY_WIDTH, 0);
1090cdf0e10cSrcweir         final int nHeight = getFromPageStyles(PropertyNames.PROPERTY_HEIGHT, 0);
1091cdf0e10cSrcweir 
1092cdf0e10cSrcweir         if (com.sun.star.wizards.report.ReportLayouter.SOOPTLANDSCAPE == _nOrientation)
1093cdf0e10cSrcweir         {
1094cdf0e10cSrcweir             setToPageStyles("IsLandscape", Boolean.TRUE);
1095cdf0e10cSrcweir             if (nWidth < nHeight)
1096cdf0e10cSrcweir             {
1097cdf0e10cSrcweir                 setToPageStyles(PropertyNames.PROPERTY_WIDTH, new Integer(nHeight));
1098cdf0e10cSrcweir                 setToPageStyles(PropertyNames.PROPERTY_HEIGHT, new Integer(nWidth));
1099cdf0e10cSrcweir             }
1100cdf0e10cSrcweir         }
1101cdf0e10cSrcweir         else
1102cdf0e10cSrcweir         {
1103cdf0e10cSrcweir             setToPageStyles("IsLandscape", Boolean.FALSE);
1104cdf0e10cSrcweir             if (nHeight < nWidth)
1105cdf0e10cSrcweir             {
1106cdf0e10cSrcweir                 setToPageStyles(PropertyNames.PROPERTY_WIDTH, new Integer(nHeight));
1107cdf0e10cSrcweir                 setToPageStyles(PropertyNames.PROPERTY_HEIGHT, new Integer(nWidth));
1108cdf0e10cSrcweir             }
1109cdf0e10cSrcweir         }
1110cdf0e10cSrcweir         // dirty the PageWidth
1111cdf0e10cSrcweir         m_nPageWidth = -1;
1112cdf0e10cSrcweir     }
1113cdf0e10cSrcweir 
1114cdf0e10cSrcweir     /**
1115cdf0e10cSrcweir      * Returns the width and height of a given string (_sText) in 1/100mm drawn in the given font descriptor.
1116cdf0e10cSrcweir      * TODO: This function is a performance leak, we could store already calculated values in a map, to build a cache. Access should be much faster then.
1117cdf0e10cSrcweir      *
1118cdf0e10cSrcweir      * @param _sText
1119cdf0e10cSrcweir      * @param _aFont
1120cdf0e10cSrcweir      * @return width of given text in 1/100mm
1121cdf0e10cSrcweir      */
getPreferredSize(String _sText, FontDescriptor _aFont)1122cdf0e10cSrcweir     Size getPreferredSize(String _sText, FontDescriptor _aFont)
1123cdf0e10cSrcweir     {
1124cdf0e10cSrcweir         Size aSizeMM_100TH = new Size(0, 0);
1125cdf0e10cSrcweir         try
1126cdf0e10cSrcweir         {
1127cdf0e10cSrcweir             // Object aControlContainer = getGlobalMSF().createInstance("com.sun.star.awt.UnoControlContainer");
1128cdf0e10cSrcweir             // XControlContainer xControlContainer = (XControlContainer)UnoRuntime.queryInterface(XControlContainer.class, aControlContainer);
1129cdf0e10cSrcweir 
1130cdf0e10cSrcweir             final Object aFixedTextModel = getGlobalMSF().createInstance("com.sun.star.awt.UnoControlFixedTextModel");
1131cdf0e10cSrcweir             final XControlModel xFixedTextModel = UnoRuntime.queryInterface(XControlModel.class, aFixedTextModel);
1132cdf0e10cSrcweir 
1133cdf0e10cSrcweir             final PropertySetHelper aPropertySetHelper = new PropertySetHelper(xFixedTextModel);
1134cdf0e10cSrcweir //          aPropertySetHelper.showProperties();
1135cdf0e10cSrcweir             aPropertySetHelper.setPropertyValueDontThrow(PropertyNames.FONT_DESCRIPTOR, _aFont);
1136cdf0e10cSrcweir 
1137cdf0e10cSrcweir             final Object aUnoCtrlFixedText = getGlobalMSF().createInstance("com.sun.star.awt.UnoControlFixedText");
1138cdf0e10cSrcweir //            XServiceInfo xServiceInfo2 = (XServiceInfo)UnoRuntime.queryInterface(XServiceInfo.class, aUnoCtrlFixedText);
1139cdf0e10cSrcweir //            String[] sServices2 = xServiceInfo2.getSupportedServiceNames();
1140cdf0e10cSrcweir 
1141cdf0e10cSrcweir             final XWindow xWindow = UnoRuntime.queryInterface(XWindow.class, aUnoCtrlFixedText);
1142cdf0e10cSrcweir             xWindow.setVisible(false);
1143cdf0e10cSrcweir 
1144cdf0e10cSrcweir             final XControl xControl = UnoRuntime.queryInterface(XControl.class, aUnoCtrlFixedText);
1145cdf0e10cSrcweir             xControl.setModel(xFixedTextModel);
1146cdf0e10cSrcweir 
1147cdf0e10cSrcweir             final com.sun.star.awt.XFixedText xFixedText = UnoRuntime.queryInterface(com.sun.star.awt.XFixedText.class, aUnoCtrlFixedText);
1148cdf0e10cSrcweir             xFixedText.setText(_sText);
1149cdf0e10cSrcweir 
1150cdf0e10cSrcweir             final XLayoutConstrains xLayoutConstraints = UnoRuntime.queryInterface(XLayoutConstrains.class, aUnoCtrlFixedText);
1151cdf0e10cSrcweir             final Size aSizeInPixel = xLayoutConstraints.getPreferredSize();
1152cdf0e10cSrcweir 
1153cdf0e10cSrcweir             final XWindowPeer xPeerOfReportDefinition = UnoRuntime.queryInterface(XWindowPeer.class, getReportDefinition().getCurrentController().getFrame().getComponentWindow());
1154cdf0e10cSrcweir             xControl.createPeer(null, xPeerOfReportDefinition);
1155cdf0e10cSrcweir 
1156cdf0e10cSrcweir             final XWindowPeer x = xControl.getPeer();
1157cdf0e10cSrcweir 
1158cdf0e10cSrcweir             final XUnitConversion xConversion = UnoRuntime.queryInterface(XUnitConversion.class, x);
1159cdf0e10cSrcweir             aSizeMM_100TH = xConversion.convertSizeToLogic(aSizeInPixel, com.sun.star.util.MeasureUnit.MM_100TH);
1160cdf0e10cSrcweir             // xToolkit.createScreenCompatibleDevice(_nWidth, _nWidth).
1161cdf0e10cSrcweir             // XWindow x = getReportDefinition().getCurrentController().getFrame().getContainerWindow();
1162cdf0e10cSrcweir             // Object aObj = _xSection.getParent();
1163cdf0e10cSrcweir 
1164cdf0e10cSrcweir             // we don't need the created objects any longer
1165cdf0e10cSrcweir             final XComponent xFixedTextDeleter = UnoRuntime.queryInterface(XComponent.class, xFixedText);
1166cdf0e10cSrcweir             xFixedTextDeleter.dispose();
1167cdf0e10cSrcweir 
1168cdf0e10cSrcweir             final XComponent xFixedTextModelDeleter = UnoRuntime.queryInterface(XComponent.class, aFixedTextModel);
1169cdf0e10cSrcweir             xFixedTextModelDeleter.dispose();
1170cdf0e10cSrcweir         }
1171cdf0e10cSrcweir         catch (Exception e)
1172cdf0e10cSrcweir         {
1173cdf0e10cSrcweir             Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
1174cdf0e10cSrcweir         }
1175cdf0e10cSrcweir         return aSizeMM_100TH;
1176cdf0e10cSrcweir     }
1177cdf0e10cSrcweir 
getTableName()1178cdf0e10cSrcweir     protected String getTableName()
1179cdf0e10cSrcweir     {
1180cdf0e10cSrcweir         if (m_sTableName != null)
1181cdf0e10cSrcweir         {
1182cdf0e10cSrcweir             return m_sTableName;
1183cdf0e10cSrcweir         }
1184cdf0e10cSrcweir         return PropertyNames.EMPTY_STRING;
1185cdf0e10cSrcweir     }
1186cdf0e10cSrcweir 
getUserNameFromConfiguration()1187cdf0e10cSrcweir     protected String getUserNameFromConfiguration()
1188cdf0e10cSrcweir     {
1189cdf0e10cSrcweir         String sFirstName = PropertyNames.EMPTY_STRING;
1190cdf0e10cSrcweir         String sLastName = PropertyNames.EMPTY_STRING;
1191cdf0e10cSrcweir         try
1192cdf0e10cSrcweir         {
1193cdf0e10cSrcweir             Object oProdNameAccess = Configuration.getConfigurationRoot(getGlobalMSF(), "org.openoffice.UserProfile/Data", false);
1194cdf0e10cSrcweir             sFirstName = (String) Helper.getUnoObjectbyName(oProdNameAccess, "givenname");
1195cdf0e10cSrcweir             sLastName = (String) Helper.getUnoObjectbyName(oProdNameAccess, "sn");
1196cdf0e10cSrcweir         }
1197cdf0e10cSrcweir         catch (Exception e)
1198cdf0e10cSrcweir         {
1199cdf0e10cSrcweir             Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
1200cdf0e10cSrcweir         }
1201cdf0e10cSrcweir         return sFirstName + PropertyNames.SPACE + sLastName;
1202cdf0e10cSrcweir     }
1203cdf0e10cSrcweir 
1204cdf0e10cSrcweir     /**
1205cdf0e10cSrcweir      * Helper function, to copy all not read only properties of _xFromSection to _xToSection
1206cdf0e10cSrcweir      * @param _xFromSection
1207cdf0e10cSrcweir      * @param _xToSection
1208cdf0e10cSrcweir      */
copyProperties(Object _aFrom, Object _aTo)1209cdf0e10cSrcweir     private void copyProperties(Object _aFrom, Object _aTo)
1210cdf0e10cSrcweir     {
1211cdf0e10cSrcweir         XPropertySet xFrom = UnoRuntime.queryInterface(XPropertySet.class, _aFrom);
1212cdf0e10cSrcweir         XPropertySet xTo = UnoRuntime.queryInterface(XPropertySet.class, _aTo);
1213cdf0e10cSrcweir 
1214cdf0e10cSrcweir 
1215cdf0e10cSrcweir         XPropertySetInfo xForeignPropInfo = xFrom.getPropertySetInfo();
1216cdf0e10cSrcweir         XPropertySetInfo xSectionPropInfo = xTo.getPropertySetInfo();
1217cdf0e10cSrcweir         Property[] aAllProperties = xForeignPropInfo.getProperties();
1218cdf0e10cSrcweir         for (int i = 0; i < aAllProperties.length; i++)
1219cdf0e10cSrcweir         {
1220cdf0e10cSrcweir             String sPropertyName = aAllProperties[i].Name;
1221cdf0e10cSrcweir             if (xSectionPropInfo.hasPropertyByName(sPropertyName))
1222cdf0e10cSrcweir             {
1223cdf0e10cSrcweir                 try
1224cdf0e10cSrcweir                 {
1225cdf0e10cSrcweir                     Property aDestProp = xForeignPropInfo.getPropertyByName(sPropertyName);
1226cdf0e10cSrcweir                     if ((aDestProp.Attributes & PropertyAttribute.READONLY) == 0)
1227cdf0e10cSrcweir                     {
1228cdf0e10cSrcweir                         xTo.setPropertyValue(sPropertyName, xFrom.getPropertyValue(sPropertyName));
1229cdf0e10cSrcweir                     }
1230cdf0e10cSrcweir                 }
1231cdf0e10cSrcweir                 catch (Exception e)
1232cdf0e10cSrcweir                 {
1233cdf0e10cSrcweir                     Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
1234cdf0e10cSrcweir                 }
1235cdf0e10cSrcweir             }
1236cdf0e10cSrcweir         }
1237cdf0e10cSrcweir     }
1238cdf0e10cSrcweir 
1239cdf0e10cSrcweir     /**
1240cdf0e10cSrcweir      * Helper Funktion to copy the whole content of _xFromSection to the _xToSection
1241cdf0e10cSrcweir      * @param _xFromSection
1242cdf0e10cSrcweir      * @param _xToSection
1243cdf0e10cSrcweir      */
copySection(XSection _xFromSection, XSection _xToSection)1244cdf0e10cSrcweir     private void copySection(XSection _xFromSection, XSection _xToSection)
1245cdf0e10cSrcweir     {
1246cdf0e10cSrcweir         copyProperties(_xFromSection, _xToSection);
1247cdf0e10cSrcweir 
1248cdf0e10cSrcweir         try
1249cdf0e10cSrcweir         {
1250cdf0e10cSrcweir             XEnumeration xEnum = _xFromSection.createEnumeration();
1251cdf0e10cSrcweir             while (xEnum.hasMoreElements())
1252cdf0e10cSrcweir             {
1253cdf0e10cSrcweir                 Object aEnumObj = xEnum.nextElement();
1254cdf0e10cSrcweir                 XReportComponent aComponent = UnoRuntime.queryInterface(XReportComponent.class, aEnumObj);
1255cdf0e10cSrcweir 
1256cdf0e10cSrcweir                 // XCloneable aClone = (XCloneable)UnoRuntime.queryInterface(XCloneable.class, aEnumObj);
1257cdf0e10cSrcweir                 if (aComponent != null)
1258cdf0e10cSrcweir                 {
1259cdf0e10cSrcweir                     Object aClone = aComponent.createClone();
1260cdf0e10cSrcweir                     if (aClone != null)
1261cdf0e10cSrcweir                     {
1262cdf0e10cSrcweir                         XShape aShape = UnoRuntime.queryInterface(XShape.class, aClone);
1263cdf0e10cSrcweir 
1264cdf0e10cSrcweir                         // normally 'createClone' will create a real clone of the component,
1265cdf0e10cSrcweir                         // but there seems some problems, we have to controll.
1266cdf0e10cSrcweir                         copyProperties(aComponent, aClone);
1267cdf0e10cSrcweir 
1268cdf0e10cSrcweir                         // aShape.setPosition(aComponent.getPosition());
1269cdf0e10cSrcweir                         // aShape.setSize(aComponent.getSize());
1270cdf0e10cSrcweir                         _xToSection.add(aShape);
1271cdf0e10cSrcweir                     }
1272cdf0e10cSrcweir                 }
1273cdf0e10cSrcweir             }
1274cdf0e10cSrcweir         }
1275cdf0e10cSrcweir         catch (Exception e)
1276cdf0e10cSrcweir         {
1277cdf0e10cSrcweir             Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
1278cdf0e10cSrcweir         }
1279cdf0e10cSrcweir         // String sName = xForeignSection.getName();
1280cdf0e10cSrcweir         // int dummy = 0;
1281cdf0e10cSrcweir     }
1282cdf0e10cSrcweir 
insertPageHeader()1283cdf0e10cSrcweir     protected void insertPageHeader()
1284cdf0e10cSrcweir     {
1285cdf0e10cSrcweir         if (getDesignTemplate() != null)
1286cdf0e10cSrcweir         {
1287cdf0e10cSrcweir             if (getDesignTemplate().getPageHeaderOn())
1288cdf0e10cSrcweir             {
1289cdf0e10cSrcweir                 // copy all Section information from Page Header to our Header
1290cdf0e10cSrcweir                 try
1291cdf0e10cSrcweir                 {
1292cdf0e10cSrcweir                     XSection xForeignSection = getDesignTemplate().getPageHeader();
1293cdf0e10cSrcweir 
1294cdf0e10cSrcweir                     if (xForeignSection != null)
1295cdf0e10cSrcweir                     {
1296cdf0e10cSrcweir                         getReportDefinition().setPageHeaderOn(true);
1297cdf0e10cSrcweir                         XSection xSection = getReportDefinition().getPageHeader();
1298cdf0e10cSrcweir 
1299cdf0e10cSrcweir                         // copy Sections
1300cdf0e10cSrcweir                         copySection(xForeignSection, xSection);
1301cdf0e10cSrcweir                     }
1302cdf0e10cSrcweir                 }
1303cdf0e10cSrcweir                 catch (Exception e)
1304cdf0e10cSrcweir                 {
1305cdf0e10cSrcweir                     Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
1306cdf0e10cSrcweir                 }
1307cdf0e10cSrcweir                 }
1308cdf0e10cSrcweir             else
1309cdf0e10cSrcweir             {
1310cdf0e10cSrcweir                 // we won't a page header
1311cdf0e10cSrcweir                 // getReportDefinition().setPageHeaderOn(true);
1312cdf0e10cSrcweir                 getReportDefinition().setPageHeaderOn(false);
1313cdf0e10cSrcweir             }
1314cdf0e10cSrcweir         }
1315cdf0e10cSrcweir         else
1316cdf0e10cSrcweir         {
1317cdf0e10cSrcweir             if (getReportDefinition() == null)
1318cdf0e10cSrcweir             {
1319cdf0e10cSrcweir                 return;
1320cdf0e10cSrcweir             }
1321cdf0e10cSrcweir             // there is no foreign report definition
1322cdf0e10cSrcweir             // TODO: #i86902# rpt:Title() out of the document
1323cdf0e10cSrcweir 
1324cdf0e10cSrcweir             // TODO: #i86902# rpt:Author() can't set with something like rpt:author()
1325cdf0e10cSrcweir             // TODO: #i86902# more fieldnames need.
1326cdf0e10cSrcweir             final String sTitleTitle = getResource().getResText(UIConsts.RID_REPORT + 86); // "Title:"
1327cdf0e10cSrcweir             final String sTitle = getTableName(); // "Default title, this is a first draft report generated by the new report wizard.";
1328cdf0e10cSrcweir             final String sAuthorTitle = getResource().getResText(UIConsts.RID_REPORT + 87); // "Author:"
1329cdf0e10cSrcweir             final String sAuthor = getUserNameFromConfiguration(); // "You";     // rpt:fieldvalue();
1330cdf0e10cSrcweir             final String sDateTitle = getResource().getResText(UIConsts.RID_REPORT + 88); // "Date:"
1331cdf0e10cSrcweir             // TODO: #i86911# Date: we need to set the style of the date.
1332cdf0e10cSrcweir             final String sDate = "rpt:now()"; // getDateString("EEE, d MMM yyyy HH:mm:ss zzzz");
1333cdf0e10cSrcweir 
1334cdf0e10cSrcweir             try
1335cdf0e10cSrcweir             {
1336cdf0e10cSrcweir                 getReportDefinition().setPageHeaderOn(true);
1337cdf0e10cSrcweir                 XSection xSection = null;
1338cdf0e10cSrcweir                 xSection = getReportDefinition().getPageHeader();
1339cdf0e10cSrcweir 
1340cdf0e10cSrcweir                 Rectangle aRect = new Rectangle();
1341cdf0e10cSrcweir                 aRect.X = getLeftPageIndent();
1342cdf0e10cSrcweir                 SectionObject aSOLabel = SectionEmptyObject.create();
1343cdf0e10cSrcweir                 aSOLabel.setFontToBold();
1344cdf0e10cSrcweir                 aRect.Y = aSOLabel.getHeight(LayoutConstants.LabelHeight);
1345cdf0e10cSrcweir 
1346cdf0e10cSrcweir                 final int nWidth = 3000;
1347cdf0e10cSrcweir 
1348cdf0e10cSrcweir                 aRect = insertLabel(xSection, sTitleTitle, aRect, nWidth, aSOLabel);
1349cdf0e10cSrcweir 
1350cdf0e10cSrcweir                 final int nTitleWidth = getPageWidth() - getLeftPageIndent() - getRightPageIndent() - 3000;
1351cdf0e10cSrcweir                 // aRect = insertFormattedField(xSection, "rpt:Title()", aRect, nTitleWidth);
1352cdf0e10cSrcweir                 aRect = insertLabel(xSection, sTitle, aRect, nTitleWidth, aSOLabel);
1353cdf0e10cSrcweir 
1354cdf0e10cSrcweir                 aRect.Y += aSOLabel.getHeight(LayoutConstants.LabelHeight) + LayoutConstants.LineHeight;
1355cdf0e10cSrcweir 
1356cdf0e10cSrcweir                 aRect.X = getLeftPageIndent();
1357cdf0e10cSrcweir                 aRect = insertLabel(xSection, sAuthorTitle, aRect, nWidth, aSOLabel);
1358cdf0e10cSrcweir                 // aRect = insertFormattedField(xSection, "rpt:Author()", aRect, nWidth);
1359cdf0e10cSrcweir                 aRect = insertLabel(xSection, sAuthor, aRect, nTitleWidth, aSOLabel);
1360cdf0e10cSrcweir 
1361cdf0e10cSrcweir                 aRect.Y += aSOLabel.getHeight(LayoutConstants.LabelHeight);
1362cdf0e10cSrcweir 
1363cdf0e10cSrcweir                 aRect.X = getLeftPageIndent();
1364cdf0e10cSrcweir                 aRect = insertLabel(xSection, sDateTitle, aRect, nWidth, aSOLabel);
1365cdf0e10cSrcweir                 // aRect = insertFormattedField(xSection, "rpt:Date()", aRect, nWidth);
1366cdf0e10cSrcweir                 aRect = insertFormattedField(xSection, sDate, aRect, nTitleWidth, aSOLabel);
1367cdf0e10cSrcweir 
1368cdf0e10cSrcweir                 aRect.Y += aSOLabel.getHeight(LayoutConstants.FormattedFieldHeight) + LayoutConstants.LineHeight;
1369cdf0e10cSrcweir 
1370cdf0e10cSrcweir                 // draw a line under the label/formattedfield
1371cdf0e10cSrcweir                 aRect.X = getLeftPageIndent();
1372cdf0e10cSrcweir                 final int nLineWidth = getPageWidth() - getRightPageIndent() - aRect.X;
1373cdf0e10cSrcweir                 final int nLineHeight = LayoutConstants.LineHeight;
1374cdf0e10cSrcweir                 insertHorizontalLine(xSection, aRect, nLineWidth, nLineHeight);
1375cdf0e10cSrcweir 
1376cdf0e10cSrcweir                 aRect.Y += nLineHeight;
1377cdf0e10cSrcweir 
1378cdf0e10cSrcweir                 xSection.setHeight(aRect.Y);
1379cdf0e10cSrcweir             }
1380cdf0e10cSrcweir             catch (com.sun.star.uno.Exception e)
1381cdf0e10cSrcweir             {
1382cdf0e10cSrcweir                 Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
1383cdf0e10cSrcweir             }
1384cdf0e10cSrcweir         }
1385cdf0e10cSrcweir     }
1386cdf0e10cSrcweir 
insertPageFooter()1387cdf0e10cSrcweir     protected void insertPageFooter()
1388cdf0e10cSrcweir     {
1389cdf0e10cSrcweir         if (getDesignTemplate() != null)
1390cdf0e10cSrcweir         {
1391cdf0e10cSrcweir             if (getDesignTemplate().getPageFooterOn())
1392cdf0e10cSrcweir             {
1393cdf0e10cSrcweir                 try
1394cdf0e10cSrcweir                 {
1395cdf0e10cSrcweir                     XSection xForeignSection = getDesignTemplate().getPageFooter();
1396cdf0e10cSrcweir 
1397cdf0e10cSrcweir                     if (xForeignSection != null)
1398cdf0e10cSrcweir                     {
1399cdf0e10cSrcweir                         getReportDefinition().setPageFooterOn(true);
1400cdf0e10cSrcweir                         XSection xSection = getReportDefinition().getPageFooter();
1401cdf0e10cSrcweir 
1402cdf0e10cSrcweir                         // copy Sections
1403cdf0e10cSrcweir                         copySection(xForeignSection, xSection);
1404cdf0e10cSrcweir                     }
1405cdf0e10cSrcweir                 }
1406cdf0e10cSrcweir                 catch (Exception e)
1407cdf0e10cSrcweir                 {
1408cdf0e10cSrcweir                     Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
1409cdf0e10cSrcweir                 }
1410cdf0e10cSrcweir             }
1411cdf0e10cSrcweir             else
1412cdf0e10cSrcweir             {
1413cdf0e10cSrcweir                 // getReportDefinition().setPageFooterOn(true);
1414cdf0e10cSrcweir                 getReportDefinition().setPageFooterOn(false);
1415cdf0e10cSrcweir             }
1416cdf0e10cSrcweir         }
1417cdf0e10cSrcweir         else
1418cdf0e10cSrcweir         {
1419cdf0e10cSrcweir             if (getReportDefinition() == null)
1420cdf0e10cSrcweir             {
1421cdf0e10cSrcweir                 return;
1422cdf0e10cSrcweir             }
1423cdf0e10cSrcweir 
1424cdf0e10cSrcweir             // TODO: how should we arrive this code (set page and pagecount in the middle of the page footer)
1425cdf0e10cSrcweir             // If there exists a design template, don't use it.
1426cdf0e10cSrcweir 
1427cdf0e10cSrcweir             // we don't have a default report definition
1428cdf0e10cSrcweir             final String sPageOf = getResource().getResText(UIConsts.RID_REPORT + 89); // 'Page #page# of #count#'
1429cdf0e10cSrcweir 
1430cdf0e10cSrcweir             // Convert
1431cdf0e10cSrcweir             // 'Page #page# of #count#'
1432cdf0e10cSrcweir             // to something like
1433cdf0e10cSrcweir             // '\"Page \" & PageNumber() & \" of \" & PageCount()'
1434cdf0e10cSrcweir             // due to the fact that is is not fixed, where #page# or #count# occurs, we make it
1435cdf0e10cSrcweir             // a little bit trickier.
1436cdf0e10cSrcweir             // we first surround the string with double quotes,
1437cdf0e10cSrcweir             // second, replace the #...#
1438cdf0e10cSrcweir             // last, we remove double 'double quotes'.
1439cdf0e10cSrcweir             final String sSurroundDoubleQuotes = "\"" + sPageOf + "\"";
1440cdf0e10cSrcweir             final String sPageNumber = sSurroundDoubleQuotes.replaceAll("#page#", "\" & PageNumber() & \"");
1441cdf0e10cSrcweir             final String sPageCount = sPageNumber.replaceAll("#count#", "\" & PageCount() & \"");
1442cdf0e10cSrcweir             final String sNoLastUnusedQuotes = sPageCount.replaceAll(" & \\\"\\\"", PropertyNames.EMPTY_STRING);
1443cdf0e10cSrcweir             final String sNoFirstUnusedQuotes = sNoLastUnusedQuotes.replaceAll("\\\"\\\" & ", PropertyNames.EMPTY_STRING);
1444cdf0e10cSrcweir 
1445cdf0e10cSrcweir             final int nUsablePageWidth = getPageWidth() - getLeftPageIndent() - getRightPageIndent();
1446cdf0e10cSrcweir 
1447cdf0e10cSrcweir             try
1448cdf0e10cSrcweir             {
1449cdf0e10cSrcweir                 getReportDefinition().setPageFooterOn(true);
1450cdf0e10cSrcweir                 XSection xSection = null;
1451cdf0e10cSrcweir                 xSection = getReportDefinition().getPageFooter();
1452cdf0e10cSrcweir 
1453cdf0e10cSrcweir                 Rectangle aRect = new Rectangle();
1454cdf0e10cSrcweir                 aRect.X = getLeftPageIndent();
1455cdf0e10cSrcweir 
1456cdf0e10cSrcweir                 // draw a line over the label/formattedfield
1457cdf0e10cSrcweir                 final int nLineWidth = getPageWidth() - getRightPageIndent() - aRect.X;
1458cdf0e10cSrcweir                 final int nLineHeight = LayoutConstants.LineHeight;
1459cdf0e10cSrcweir                 insertHorizontalLine(xSection, aRect, nLineWidth, nLineHeight);
1460cdf0e10cSrcweir 
1461cdf0e10cSrcweir                 aRect.Y += nLineHeight;
1462cdf0e10cSrcweir                 aRect.Y += LayoutConstants.LabelHeight;
1463cdf0e10cSrcweir 
1464cdf0e10cSrcweir                 aRect.X = getLeftPageIndent();
1465cdf0e10cSrcweir 
1466cdf0e10cSrcweir                 aRect = insertFormattedField(xSection, "rpt:" + sNoFirstUnusedQuotes, aRect, nUsablePageWidth, null, (short) com.sun.star.awt.TextAlign.CENTER);
1467cdf0e10cSrcweir 
1468cdf0e10cSrcweir                 aRect.Y += LayoutConstants.FormattedFieldHeight + LayoutConstants.LineHeight;
1469cdf0e10cSrcweir                 xSection.setHeight(aRect.Y);
1470cdf0e10cSrcweir             }
1471cdf0e10cSrcweir             catch (Exception e)
1472cdf0e10cSrcweir             {
1473cdf0e10cSrcweir                 Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
1474cdf0e10cSrcweir             }
1475cdf0e10cSrcweir         }
1476cdf0e10cSrcweir     }
1477cdf0e10cSrcweir 
getResource()1478cdf0e10cSrcweir     protected Resource getResource()
1479cdf0e10cSrcweir     {
1480cdf0e10cSrcweir         return m_aResource;
1481cdf0e10cSrcweir     }
1482cdf0e10cSrcweir     protected int m_aCommandType; // Table or Query
1483cdf0e10cSrcweir     protected String m_sTableName;
1484cdf0e10cSrcweir     protected String[] m_aGroupNames;
1485cdf0e10cSrcweir     protected String[] m_aFieldNames;
1486cdf0e10cSrcweir     protected String[] m_aFieldTitleNames;
1487cdf0e10cSrcweir     protected int[] m_aFieldWidths;
1488cdf0e10cSrcweir     protected int[] m_aFieldTypes;
1489cdf0e10cSrcweir     // protected int[]    m_nLeftIndent;
1490cdf0e10cSrcweir     private DesignTemplate m_xDesignTemplate = null;
1491cdf0e10cSrcweir 
initializeData(IReportBuilderLayouter _aOther)1492cdf0e10cSrcweir     public void initializeData(IReportBuilderLayouter _aOther)
1493cdf0e10cSrcweir     {
1494cdf0e10cSrcweir         if (_aOther instanceof ReportBuilderLayouter)
1495cdf0e10cSrcweir         {
1496cdf0e10cSrcweir             final ReportBuilderLayouter aOther = (ReportBuilderLayouter) _aOther;
1497cdf0e10cSrcweir             m_aCommandType = aOther.m_aCommandType;
1498cdf0e10cSrcweir             m_sTableName = aOther.m_sTableName;
1499cdf0e10cSrcweir             m_aGroupNames = aOther.m_aGroupNames;
1500cdf0e10cSrcweir             m_aFieldNames = aOther.m_aFieldNames;
1501cdf0e10cSrcweir             m_aFieldTitleNames = aOther.m_aFieldTitleNames;
1502cdf0e10cSrcweir             m_aFieldWidths = aOther.m_aFieldWidths;
1503cdf0e10cSrcweir             m_aFieldTypes = aOther.m_aFieldTypes;
1504cdf0e10cSrcweir             // m_nLeftIndent = aOther.m_nLeftIndent;
1505cdf0e10cSrcweir             m_xDesignTemplate = aOther.m_xDesignTemplate;
1506cdf0e10cSrcweir 
1507cdf0e10cSrcweir             // dirty PageWidth
1508cdf0e10cSrcweir             m_nPageWidth = -1;
1509cdf0e10cSrcweir         }
1510cdf0e10cSrcweir     }
1511cdf0e10cSrcweir 
1512cdf0e10cSrcweir     /**
1513cdf0e10cSrcweir      * Get the maximal label width of all labels
1514cdf0e10cSrcweir      * @return the width in 1/100mm
1515cdf0e10cSrcweir      */
getMaxLabelWidth()1516cdf0e10cSrcweir     protected int getMaxLabelWidth()
1517cdf0e10cSrcweir     {
1518cdf0e10cSrcweir         int nWidth = 0;
1519cdf0e10cSrcweir         final String[] aFieldTitles = m_aFieldTitleNames; // we want all Field Titles here // getFieldTitleNames();
1520cdf0e10cSrcweir         for (int i = 0; i < aFieldTitles.length; i++)
1521cdf0e10cSrcweir         {
1522cdf0e10cSrcweir             final String sLabel = aFieldTitles[i];
1523cdf0e10cSrcweir             nWidth = Math.max(nWidth, getLabelWidth(sLabel));
1524cdf0e10cSrcweir         }
1525cdf0e10cSrcweir         for (int i = 0; i < m_aGroupNames.length; i++)
1526cdf0e10cSrcweir         {
1527cdf0e10cSrcweir             final String sGroupName = m_aGroupNames[i];
1528cdf0e10cSrcweir             final SectionObject a = getDesignTemplate().getGroupLabel(i);
1529cdf0e10cSrcweir             final FontDescriptor aFD = a.getFontDescriptor();
1530cdf0e10cSrcweir             nWidth = Math.max(nWidth, getLabelWidth(sGroupName, aFD));
1531cdf0e10cSrcweir         }
1532cdf0e10cSrcweir 
1533cdf0e10cSrcweir         if (nWidth == 0)
1534cdf0e10cSrcweir         {
1535cdf0e10cSrcweir             nWidth = 3000;
1536cdf0e10cSrcweir         }
1537cdf0e10cSrcweir         else
1538cdf0e10cSrcweir         {
1539cdf0e10cSrcweir             nWidth += 500;
1540cdf0e10cSrcweir         }
1541cdf0e10cSrcweir         return nWidth;
1542cdf0e10cSrcweir     }
1543cdf0e10cSrcweir 
1544cdf0e10cSrcweir     /**
1545cdf0e10cSrcweir      * Get width of a given string (Label) in 1/100mm
1546cdf0e10cSrcweir      * @param _sLabel
1547cdf0e10cSrcweir      * @return the width in 1/100mm
1548cdf0e10cSrcweir      */
getLabelWidth(String _sLabel)1549cdf0e10cSrcweir     protected int getLabelWidth(String _sLabel)
1550cdf0e10cSrcweir     {
1551cdf0e10cSrcweir         return getLabelWidth(_sLabel, 0.0f, 0.0f);
1552cdf0e10cSrcweir     }
1553cdf0e10cSrcweir     XFixedText m_aFixedTextHelper = null;
1554cdf0e10cSrcweir     HashMap m_aLabelWidthMap;
1555cdf0e10cSrcweir 
getLabelWidth(String _sLabel, FontDescriptor _aFD)1556cdf0e10cSrcweir     protected int getLabelWidth(String _sLabel, FontDescriptor _aFD)
1557cdf0e10cSrcweir     {
1558cdf0e10cSrcweir         float fCharWeight = 0.0f;
1559cdf0e10cSrcweir         float fCharHeight = 0.0f;
1560cdf0e10cSrcweir         if (_aFD != null)
1561cdf0e10cSrcweir         {
1562cdf0e10cSrcweir             fCharWeight = _aFD.Weight;
1563cdf0e10cSrcweir             fCharHeight = _aFD.Height;
1564cdf0e10cSrcweir         }
1565cdf0e10cSrcweir         return getLabelWidth(_sLabel, fCharWeight, fCharHeight);
1566cdf0e10cSrcweir     }
1567cdf0e10cSrcweir 
getLabelWidth(String _sLabel, float _nCharWeight, float _nCharHeight)1568cdf0e10cSrcweir     protected int getLabelWidth(String _sLabel, float _nCharWeight, float _nCharHeight)
1569cdf0e10cSrcweir     {
1570cdf0e10cSrcweir         int nWidth = 0;
1571cdf0e10cSrcweir 
1572cdf0e10cSrcweir         if (m_aLabelWidthMap == null)
1573cdf0e10cSrcweir         {
1574cdf0e10cSrcweir             m_aLabelWidthMap = new HashMap();
1575cdf0e10cSrcweir         }
1576cdf0e10cSrcweir         // At first, try to get the Width out of a HashMap (Cache)
1577cdf0e10cSrcweir         StringBuffer aKey = new StringBuffer(40);
1578cdf0e10cSrcweir         final String sKey = aKey.append(_sLabel).append(_nCharWeight).append(_nCharHeight).toString();
1579cdf0e10cSrcweir         if (m_aLabelWidthMap.containsKey(sKey))
1580cdf0e10cSrcweir         {
1581cdf0e10cSrcweir             final Object aWidth = m_aLabelWidthMap.get(sKey);
1582cdf0e10cSrcweir             final Integer aIntegerWidth = (Integer) aWidth;
1583cdf0e10cSrcweir             nWidth = aIntegerWidth.intValue();
1584cdf0e10cSrcweir         }
1585cdf0e10cSrcweir         else
1586cdf0e10cSrcweir         {
1587cdf0e10cSrcweir             try
1588cdf0e10cSrcweir             {
1589cdf0e10cSrcweir                 if (m_aFixedTextHelper == null)
1590cdf0e10cSrcweir                 {
1591cdf0e10cSrcweir                     final Object aFixedText = getMSFofReportDefinition().createInstance("com.sun.star.report.FixedText");
1592cdf0e10cSrcweir                     m_aFixedTextHelper = UnoRuntime.queryInterface(XFixedText.class, aFixedText);
1593cdf0e10cSrcweir                 }
1594cdf0e10cSrcweir 
1595cdf0e10cSrcweir                 m_aFixedTextHelper.setLabel(_sLabel);
1596cdf0e10cSrcweir                 if (_nCharWeight > 0.1f)
1597cdf0e10cSrcweir                 {
1598cdf0e10cSrcweir                     m_aFixedTextHelper.setCharWeight(_nCharWeight);
1599cdf0e10cSrcweir                 }
1600cdf0e10cSrcweir                 if (_nCharHeight > 0.1f)
1601cdf0e10cSrcweir                 {
1602cdf0e10cSrcweir                     m_aFixedTextHelper.setCharHeight(_nCharHeight);
1603cdf0e10cSrcweir                 }
1604cdf0e10cSrcweir 
1605cdf0e10cSrcweir                 final FontDescriptor xFont = m_aFixedTextHelper.getFontDescriptor();
1606cdf0e10cSrcweir                 final Size aSize = getPreferredSize(_sLabel, xFont);
1607cdf0e10cSrcweir                 nWidth = aSize.Width;
1608cdf0e10cSrcweir                 // cache the found width
1609cdf0e10cSrcweir                 m_aLabelWidthMap.put(sKey, new Integer(nWidth));
1610cdf0e10cSrcweir             }
1611cdf0e10cSrcweir             catch (com.sun.star.uno.Exception e)
1612cdf0e10cSrcweir             {
1613cdf0e10cSrcweir                 Logger.getLogger(ReportBuilderLayouter.class.getName()).log(Level.SEVERE, null, e);
1614cdf0e10cSrcweir             }
1615cdf0e10cSrcweir         }
1616cdf0e10cSrcweir         return nWidth;
1617cdf0e10cSrcweir     }
1618cdf0e10cSrcweir 
doNotBreakInTable(Object _xSectionOrGroup)1619cdf0e10cSrcweir     protected void doNotBreakInTable(Object _xSectionOrGroup)
1620cdf0e10cSrcweir     {
1621cdf0e10cSrcweir //         try
1622cdf0e10cSrcweir //         {
1623cdf0e10cSrcweir //            _xSection.setKeepTogether(true);
1624cdf0e10cSrcweir         final PropertySetHelper aHelper = new PropertySetHelper(_xSectionOrGroup);
1625cdf0e10cSrcweir         aHelper.setPropertyValueDontThrow("KeepTogether", Boolean.TRUE);
1626cdf0e10cSrcweir //        }
1627cdf0e10cSrcweir //        catch (com.sun.star.uno.Exception e)
1628cdf0e10cSrcweir //        {
1629cdf0e10cSrcweir //            // Exception not set, but not really from interest.
1630cdf0e10cSrcweir //        }
1631cdf0e10cSrcweir     }
1632cdf0e10cSrcweir 
getDesignTemplate()1633cdf0e10cSrcweir     protected DesignTemplate getDesignTemplate()
1634cdf0e10cSrcweir     {
1635cdf0e10cSrcweir         if (m_xDesignTemplate == null)
1636cdf0e10cSrcweir         {
1637cdf0e10cSrcweir             // initialise the report definition.
1638cdf0e10cSrcweir             String sDefaultHeaderLayout = m_xReportDefinitionReadAccess.getDefaultHeaderLayout();
1639cdf0e10cSrcweir             loadAndSetBackgroundTemplate(sDefaultHeaderLayout);
1640cdf0e10cSrcweir 
1641cdf0e10cSrcweir             // copy all functions from the design template to the current report definition
1642cdf0e10cSrcweir //            XFunctions xOtherFunctions = m_xDesignTemplate.getReportDefinition().getFunctions();
1643cdf0e10cSrcweir //            final int nFunctionCount = xOtherFunctions.getCount();
1644cdf0e10cSrcweir //            for (int i=0;i<nFunctionCount;i++)
1645cdf0e10cSrcweir //            {
1646cdf0e10cSrcweir //                try
1647cdf0e10cSrcweir //                {
1648cdf0e10cSrcweir //                    Object aOtherFunction = xOtherFunctions.getByIndex(i);
1649cdf0e10cSrcweir //                    XFunctions xFunctions = getReportDefinition().getFunctions();
1650cdf0e10cSrcweir //                    XFunction xFunction = xFunctions.createFunction();
1651cdf0e10cSrcweir //
1652cdf0e10cSrcweir //                    copyProperties(aOtherFunction, xFunction);
1653cdf0e10cSrcweir //                    xFunctions.insertByIndex(xFunctions.getCount(), xFunction);
1654cdf0e10cSrcweir //                }
1655cdf0e10cSrcweir //                catch (com.sun.star.lang.IllegalArgumentException e)
1656cdf0e10cSrcweir //                {
1657cdf0e10cSrcweir //                }
1658cdf0e10cSrcweir //                catch (IndexOutOfBoundsException ex)
1659cdf0e10cSrcweir //                {
1660cdf0e10cSrcweir //                    ex.printStackTrace();
1661cdf0e10cSrcweir //                }
1662cdf0e10cSrcweir //                catch (WrappedTargetException ex)
1663cdf0e10cSrcweir //                {
1664cdf0e10cSrcweir //                    ex.printStackTrace();
1665cdf0e10cSrcweir //                }
1666cdf0e10cSrcweir //
1667cdf0e10cSrcweir //            }
1668cdf0e10cSrcweir 
1669cdf0e10cSrcweir         }
1670cdf0e10cSrcweir         return m_xDesignTemplate;
1671cdf0e10cSrcweir     }
1672cdf0e10cSrcweir 
1673cdf0e10cSrcweir     /**
1674cdf0e10cSrcweir      * If there already exists a foreign report definition, which we use to get the layout from
1675cdf0e10cSrcweir      * close it.
1676cdf0e10cSrcweir      * Veto is not allowed here.
1677cdf0e10cSrcweir      */
closeDesignTemplate()1678cdf0e10cSrcweir     private void closeDesignTemplate()
1679cdf0e10cSrcweir     {
1680cdf0e10cSrcweir         if (m_xDesignTemplate != null)
1681cdf0e10cSrcweir         {
1682cdf0e10cSrcweir             m_xDesignTemplate.close();
1683cdf0e10cSrcweir             m_xDesignTemplate = null;
1684cdf0e10cSrcweir         }
1685cdf0e10cSrcweir     }
1686cdf0e10cSrcweir 
1687cdf0e10cSrcweir     /**
1688cdf0e10cSrcweir      * load the given string as a template and use it's content to paint the other
1689cdf0e10cSrcweir      * @param LayoutTemplatePath
1690cdf0e10cSrcweir      */
loadAndSetBackgroundTemplate(String LayoutTemplatePath)1691cdf0e10cSrcweir     public void loadAndSetBackgroundTemplate(String LayoutTemplatePath)
1692cdf0e10cSrcweir     {
1693cdf0e10cSrcweir         closeDesignTemplate();
1694cdf0e10cSrcweir 
1695cdf0e10cSrcweir         String sName = FileAccess.getFilename(LayoutTemplatePath);
1696cdf0e10cSrcweir         if (sName.toLowerCase().equals("default.otr_")
1697cdf0e10cSrcweir                 || LayoutTemplatePath.equals("DefaultLayoutOfHeaders"))
1698cdf0e10cSrcweir         {
1699cdf0e10cSrcweir             // this is the default layout, we don't want to have a layout for this.
1700cdf0e10cSrcweir         }
1701cdf0e10cSrcweir         else
1702cdf0e10cSrcweir         {
1703cdf0e10cSrcweir             XMultiServiceFactory xMSF = getGlobalMSF();
1704cdf0e10cSrcweir             m_xDesignTemplate = DesignTemplate.create(xMSF, LayoutTemplatePath);
1705cdf0e10cSrcweir         }
1706cdf0e10cSrcweir     }
1707cdf0e10cSrcweir }
1708