1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package com.sun.star.wizards.reportbuilder.layout;
24 
25 import com.sun.star.beans.PropertyValue;
26 // import com.sun.star.container.NoSuchElementException;
27 // import com.sun.star.lang.IndexOutOfBoundsException;
28 // import com.sun.star.lang.WrappedTargetException;
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.report.XFixedText;
31 import com.sun.star.report.XFormattedField;
32 import com.sun.star.report.XGroup;
33 import com.sun.star.report.XGroups;
34 import com.sun.star.report.XReportDefinition;
35 import com.sun.star.report.XSection;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.wizards.common.Properties;
38 import com.sun.star.wizards.common.PropertyNames;
39 
40 /**
41  *
42  * @author ll93751
43  */
44 public class DesignTemplate
45 {
46 
47     private XReportDefinition m_xReportDefinition;
48     XMultiServiceFactory m_xGlobalMSF;
49     String m_sFilename;
50 
DesignTemplate()51     private DesignTemplate()
52     {
53     }
54     // -------------------------------------------------------------------------
55     // All functions from XReportDefinition, we use
56     // -------------------------------------------------------------------------
getGroups()57     XGroups getGroups()
58     {
59         return m_xReportDefinition.getGroups();
60     }
61 
getDetail()62     XSection getDetail()
63     {
64         return m_xReportDefinition.getDetail();
65     }
66 
getReportHeaderOn()67     boolean getReportHeaderOn()
68     {
69         return m_xReportDefinition.getReportHeaderOn();
70     }
71 
getReportHeader()72     XSection getReportHeader() throws com.sun.star.container.NoSuchElementException
73     {
74         return m_xReportDefinition.getReportHeader();
75     }
76 
getReportFooterOn()77     boolean getReportFooterOn()
78     {
79         return m_xReportDefinition.getReportFooterOn();
80     }
81 
getReportFooter()82     XSection getReportFooter() throws com.sun.star.container.NoSuchElementException
83     {
84         return m_xReportDefinition.getReportFooter();
85     }
86 
getPageHeaderOn()87     boolean getPageHeaderOn()
88     {
89         return m_xReportDefinition.getPageHeaderOn();
90     }
91 
getPageHeader()92     XSection getPageHeader() throws com.sun.star.container.NoSuchElementException
93     {
94         return m_xReportDefinition.getPageHeader();
95     }
96 
getPageFooterOn()97     boolean getPageFooterOn()
98     {
99         return m_xReportDefinition.getPageFooterOn();
100     }
101 
getPageFooter()102     XSection getPageFooter() throws com.sun.star.container.NoSuchElementException
103     {
104         return m_xReportDefinition.getPageFooter();
105     }    // -------------------------------------------------------------------------
106     private SectionObject m_aDetailLabel;
107     private SectionObject m_aDetailTextField;
108     private SectionObject[] m_aGroupLabel;
109     private SectionObject[] m_aGroupTextField;
110 
getDetailLabel()111     SectionObject getDetailLabel()
112     {
113         if (m_aDetailLabel == null)
114         {
115             return SectionEmptyObject.create();
116         }
117         return m_aDetailLabel;
118     }
119 
getDetailTextField()120     SectionObject getDetailTextField()
121     {
122         if (m_aDetailTextField == null)
123         {
124             return SectionEmptyObject.create();
125         }
126         return m_aDetailTextField;
127     }
128 
getGroupTextField(int _nGroup)129     SectionObject getGroupTextField(int _nGroup)
130     {
131         return getGroupSectionObject(m_aGroupTextField, _nGroup);
132     }
133 
getGroupLabel(int _nGroup)134     SectionObject getGroupLabel(int _nGroup)
135     {
136         return getGroupSectionObject(m_aGroupLabel, _nGroup);
137     }
138 
getGroupSectionObject(SectionObject _aGroupList[], int _nGroup)139     private SectionObject getGroupSectionObject(SectionObject _aGroupList[], int _nGroup)
140     {
141         SectionObject a = null;
142         if (_nGroup < _aGroupList.length)
143         {
144             a = _aGroupList[_nGroup];
145         }
146         else
147         {
148             // if there are not enough groups, the last one will win
149             if (_aGroupList.length > 0)
150             {
151                 a = _aGroupList[_aGroupList.length - 1];
152             }
153         }
154         // we show if the group is null
155         if (a == null)
156         {
157             a = SectionEmptyObject.create();
158             // a empty group has to use bold font
159             a.setPropertyValue("CharWeight", new Float(com.sun.star.awt.FontWeight.BOLD));
160         }
161         return a;
162     }
163 
164     // we analyse the loaded ReportDefinition, we want to know the FontDescriptor of all XSections
analyseReportDefinition()165     private void analyseReportDefinition()
166     {
167         final XSection xDetailSection = m_xReportDefinition.getDetail();
168         final int nDetailCount = xDetailSection.getCount();
169         for (int i = 0; i < nDetailCount; i++)
170         {
171             try
172             {
173                 Object aObj = xDetailSection.getByIndex(i);
174                 // is aObj a label
175                 // is aObj a textfield
176                 // m_aDetailLabel_FD
177                 // m_aDetailTextField_FD
178                 XFixedText aFixedText = UnoRuntime.queryInterface(XFixedText.class, aObj);
179                 if (aFixedText != null &&
180                         m_aDetailLabel == null)
181                 {
182                     m_aDetailLabel = SectionLabel.create(aFixedText);
183                 }
184                 else
185                 {
186                     XFormattedField aFormattedField = UnoRuntime.queryInterface(XFormattedField.class, aObj);
187                     if (aFormattedField != null &&
188                             m_aDetailTextField == null)
189                     {
190                         m_aDetailTextField = SectionTextField.create(aFormattedField);
191                     }
192                 }
193                 int dummy = 0;
194             }
195             catch (com.sun.star.lang.IndexOutOfBoundsException ex)
196             {
197             }
198             catch (com.sun.star.lang.WrappedTargetException ex)
199             {
200             }
201         }
202 
203         final XGroups xGroups = m_xReportDefinition.getGroups();
204         final int nGroupCount = xGroups.getCount();
205         // create a m_aGroupLabel_FD[]
206         // create a m_aGroupTextField_FD[]
207         m_aGroupLabel = new SectionObject[nGroupCount];
208         m_aGroupTextField = new SectionObject[nGroupCount];
209 
210         for (int nGroup = 0; nGroup < nGroupCount; nGroup++)
211         {
212             try
213             {
214                 Object aGroup = xGroups.getByIndex(nGroup);
215                 XGroup xGroup = UnoRuntime.queryInterface(XGroup.class, aGroup);
216                 XSection xGroupSection = xGroup.getHeader();
217 
218                 final int nCount = xGroupSection.getCount();
219                 for (int i = 0; i < nCount; i++)
220                 {
221                     try
222                     {
223                         Object aObj = xGroupSection.getByIndex(i);
224                         XFixedText aFixedText = UnoRuntime.queryInterface(XFixedText.class, aObj);
225                         // is aObj a label
226                         // is aObj a textfield
227                         if (aFixedText != null &&
228                                 m_aGroupLabel[nGroup] == null)
229                         {
230                             m_aGroupLabel[nGroup] = SectionLabel.create(aFixedText);
231                         }
232                         else
233                         {
234                             XFormattedField aFormattedField = UnoRuntime.queryInterface(XFormattedField.class, aObj);
235                             if (aFormattedField != null &&
236                                     m_aGroupTextField[nGroup] == null)
237                             {
238                                 m_aGroupTextField[nGroup] = SectionTextField.create(aFormattedField);
239                             }
240                         }
241                         int dummy = 0;
242                     }
243                     catch (com.sun.star.lang.IndexOutOfBoundsException ex)
244                     {
245                     }
246                     catch (com.sun.star.lang.WrappedTargetException ex)
247                     {
248                     }
249                 }
250             }
251             catch (com.sun.star.container.NoSuchElementException ex)
252             {
253             }
254             catch (com.sun.star.lang.IndexOutOfBoundsException ex)
255             {
256             }
257             catch (com.sun.star.lang.WrappedTargetException ex)
258             {
259             }
260         }
261     }
262 
263     /**
264      * close our current ReportDefinition
265      */
close()266     void close()
267     {
268         try
269         {
270             m_xReportDefinition.close(true);
271         }
272         catch (com.sun.star.util.CloseVetoException e)
273         {
274         }
275     }
276 
277     /**
278      * create a new DesignTemplate by try to load a otr file from the given path.
279      * Internally we store the loaded ReportDefinition.
280      *
281      * @param _xMSF
282      * @param _sPath
283      * @return
284      */
create(XMultiServiceFactory _xMSF, String _sPath)285     public static DesignTemplate create(XMultiServiceFactory _xMSF, String _sPath)
286     {
287         DesignTemplate a = new DesignTemplate();
288         try
289         {
290             a.load(_xMSF, _sPath);
291         }
292         catch (com.sun.star.lang.WrappedTargetRuntimeException e)
293         {
294             // this should not happen
295             a = null;
296             throw new java.lang.RuntimeException(e.getMessage());
297         }
298         catch (com.sun.star.uno.Exception e)
299         {
300             a = null;
301         }
302         catch (Exception e)
303         {
304             a = null;
305         }
306         return a;
307     }
308 
load(XMultiServiceFactory _xMSF, String _sPath)309     private void load(XMultiServiceFactory _xMSF, String _sPath) throws com.sun.star.uno.Exception
310     {
311         m_xGlobalMSF = _xMSF;
312         m_sFilename = _sPath;
313         final Object aObj = _xMSF.createInstance("com.sun.star.report.ReportDefinition");
314         m_xReportDefinition = UnoRuntime.queryInterface(XReportDefinition.class, aObj);
315 
316         PropertyValue[] aLoadProps = new PropertyValue[2];
317         aLoadProps[0] = Properties.createProperty(PropertyNames.URL, _sPath);
318         aLoadProps[1] = Properties.createProperty(PropertyNames.READ_ONLY, Boolean.TRUE);
319 
320         m_xReportDefinition.load(aLoadProps);
321         analyseReportDefinition();
322     }
323 
getReportDefinition()324     public XReportDefinition getReportDefinition()
325     {
326         return m_xReportDefinition;
327     }
328 }
329