1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 package org.libreoffice.report.pentaho;
19 
20 import com.sun.star.lib.uno.helper.PropertySetMixin;
21 import com.sun.star.lib.uno.helper.WeakBase;
22 import com.sun.star.report.meta.XFunctionDescription;
23 import com.sun.star.uno.Type;
24 import com.sun.star.uno.XComponentContext;
25 
26 import java.util.Locale;
27 import java.util.MissingResourceException;
28 
29 import org.pentaho.reporting.libraries.formula.DefaultFormulaContext;
30 import org.pentaho.reporting.libraries.formula.function.FunctionCategory;
31 import org.pentaho.reporting.libraries.formula.function.FunctionRegistry;
32 
33 public final class StarFunctionCategory extends WeakBase
34         implements com.sun.star.report.meta.XFunctionCategory
35 {
36 
37     private final XComponentContext m_xContext;
38     private final PropertySetMixin m_prophlp;
39     // attributes
40     private final int m_Number;
41     private final FunctionCategory category;
42     private final FunctionRegistry functionRegistry;
43     private final String functions[];
44     private final DefaultFormulaContext defaultContext;
45     private final Locale defaultLocale;
46 
StarFunctionCategory(DefaultFormulaContext defaultContext, final XComponentContext context, final FunctionRegistry functionRegistry, final int _number, final FunctionCategory category)47     public StarFunctionCategory(DefaultFormulaContext defaultContext, final XComponentContext context, final FunctionRegistry functionRegistry, final int _number, final FunctionCategory category)
48     {
49         this.defaultContext = defaultContext;
50         m_xContext = context;
51         m_Number = _number;
52         this.category = category;
53         this.functionRegistry = functionRegistry;
54         Locale locale;
55         try
56         {
57             category.getDisplayName(defaultContext.getLocalizationContext().getLocale());
58             locale = defaultContext.getLocalizationContext().getLocale();
59         }
60         catch (MissingResourceException e)
61         {
62             locale = Locale.ENGLISH;
63             try
64             {
65                 category.getDisplayName(locale);
66             }
67             catch (MissingResourceException e2)
68             {
69             }
70         }
71         this.defaultLocale = locale;
72 
73         functions = functionRegistry.getFunctionNamesByCategory(category);
74         // use the last parameter of the PropertySetMixin constructor
75         // for your optional attributes if necessary. See the documentation
76         // of the PropertySetMixin helper for further information.
77         // Ensure that your attributes are initialized correctly!
78         m_prophlp = new PropertySetMixin(m_xContext, this,
79                 new Type(com.sun.star.report.meta.XFunctionCategory.class), null);
80     }
81 
82     // com.sun.star.beans.XPropertySet:
getPropertySetInfo()83     public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()
84     {
85         return m_prophlp.getPropertySetInfo();
86     }
87 
setPropertyValue(String aPropertyName, Object aValue)88     public void setPropertyValue(String aPropertyName, Object aValue) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.beans.PropertyVetoException, com.sun.star.lang.IllegalArgumentException, com.sun.star.lang.WrappedTargetException
89     {
90         m_prophlp.setPropertyValue(aPropertyName, aValue);
91     }
92 
getPropertyValue(String aPropertyName)93     public Object getPropertyValue(String aPropertyName) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
94     {
95         return m_prophlp.getPropertyValue(aPropertyName);
96     }
97 
addPropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)98     public void addPropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
99     {
100         m_prophlp.addPropertyChangeListener(aPropertyName, xListener);
101     }
102 
removePropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)103     public void removePropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
104     {
105         m_prophlp.removePropertyChangeListener(aPropertyName, xListener);
106     }
107 
addVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener)108     public void addVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
109     {
110         m_prophlp.addVetoableChangeListener(aPropertyName, xListener);
111     }
112 
removeVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener)113     public void removeVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
114     {
115         m_prophlp.removeVetoableChangeListener(aPropertyName, xListener);
116     }
117 
118     // com.sun.star.container.XElementAccess:
getElementType()119     public com.sun.star.uno.Type getElementType()
120     {
121         return new com.sun.star.uno.Type(XFunctionDescription.class);
122     }
123 
hasElements()124     public boolean hasElements()
125     {
126         return functions.length != 0;
127     }
128 
129     // com.sun.star.container.XIndexAccess:
getCount()130     public int getCount()
131     {
132         return functions.length;
133     }
134 
getByIndex(int Index)135     public Object getByIndex(int Index) throws com.sun.star.lang.IndexOutOfBoundsException, com.sun.star.lang.WrappedTargetException
136     {
137         return getFunction(Index);
138     }
139 
140     // com.sun.star.report.meta.XFunctionCategory:
getNumber()141     public int getNumber()
142     {
143         return m_Number;
144     }
145 
getName()146     public String getName()
147     {
148         try
149         {
150             return category.getDisplayName(defaultLocale);
151         }
152         catch(Exception ex)
153         {
154         }
155         return "Missing category for number " + m_Number;
156     }
157 
getFunction(int position)158     public com.sun.star.report.meta.XFunctionDescription getFunction(int position) throws com.sun.star.lang.IndexOutOfBoundsException, com.sun.star.lang.WrappedTargetException
159     {
160         if (position >= functions.length)
161         {
162             throw new IndexOutOfBoundsException();
163         }
164         return new StarFunctionDescription(defaultContext, m_xContext, this, functionRegistry.getMetaData(functions[position]));
165     }
166 }
167