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.report.pentaho;
24 
25 import com.sun.star.lib.uno.helper.PropertySetMixin;
26 import com.sun.star.lib.uno.helper.WeakBase;
27 import com.sun.star.report.meta.XFunctionDescription;
28 import com.sun.star.uno.Type;
29 import com.sun.star.uno.XComponentContext;
30 
31 import java.util.Locale;
32 import java.util.MissingResourceException;
33 
34 import org.pentaho.reporting.libraries.formula.DefaultFormulaContext;
35 import org.pentaho.reporting.libraries.formula.function.FunctionCategory;
36 import org.pentaho.reporting.libraries.formula.function.FunctionRegistry;
37 
38 public final class StarFunctionCategory extends WeakBase
39         implements com.sun.star.report.meta.XFunctionCategory
40 {
41 
42     private final XComponentContext m_xContext;
43     private final PropertySetMixin m_prophlp;
44     // attributes
45     private final int m_Number;
46     private final FunctionCategory category;
47     private final FunctionRegistry functionRegistry;
48     private final String functions[];
49     private final DefaultFormulaContext defaultContext;
50     private final Locale defaultLocale;
51 
StarFunctionCategory(DefaultFormulaContext defaultContext, final XComponentContext context, final FunctionRegistry functionRegistry, final int _number, final FunctionCategory category)52     public StarFunctionCategory(DefaultFormulaContext defaultContext, final XComponentContext context, final FunctionRegistry functionRegistry, final int _number, final FunctionCategory category)
53     {
54         this.defaultContext = defaultContext;
55         m_xContext = context;
56         m_Number = _number;
57         this.category = category;
58         this.functionRegistry = functionRegistry;
59         Locale locale;
60         try
61         {
62             category.getDisplayName(defaultContext.getLocalizationContext().getLocale());
63             locale = defaultContext.getLocalizationContext().getLocale();
64         }
65         catch (MissingResourceException e)
66         {
67             locale = Locale.ENGLISH;
68             try
69             {
70                 category.getDisplayName(locale);
71             }
72             catch (MissingResourceException e2)
73             {
74             }
75         }
76         this.defaultLocale = locale;
77 
78         functions = functionRegistry.getFunctionNamesByCategory(category);
79         // use the last parameter of the PropertySetMixin constructor
80         // for your optional attributes if necessary. See the documentation
81         // of the PropertySetMixin helper for further information.
82         // Ensure that your attributes are initialized correctly!
83         m_prophlp = new PropertySetMixin(m_xContext, this,
84                 new Type(com.sun.star.report.meta.XFunctionCategory.class), null);
85     }
86 
87     // com.sun.star.beans.XPropertySet:
getPropertySetInfo()88     public com.sun.star.beans.XPropertySetInfo getPropertySetInfo()
89     {
90         return m_prophlp.getPropertySetInfo();
91     }
92 
setPropertyValue(String aPropertyName, Object aValue)93     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
94     {
95         m_prophlp.setPropertyValue(aPropertyName, aValue);
96     }
97 
getPropertyValue(String aPropertyName)98     public Object getPropertyValue(String aPropertyName) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
99     {
100         return m_prophlp.getPropertyValue(aPropertyName);
101     }
102 
addPropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)103     public void addPropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
104     {
105         m_prophlp.addPropertyChangeListener(aPropertyName, xListener);
106     }
107 
removePropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener)108     public void removePropertyChangeListener(String aPropertyName, com.sun.star.beans.XPropertyChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
109     {
110         m_prophlp.removePropertyChangeListener(aPropertyName, xListener);
111     }
112 
addVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener)113     public void addVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
114     {
115         m_prophlp.addVetoableChangeListener(aPropertyName, xListener);
116     }
117 
removeVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener)118     public void removeVetoableChangeListener(String aPropertyName, com.sun.star.beans.XVetoableChangeListener xListener) throws com.sun.star.beans.UnknownPropertyException, com.sun.star.lang.WrappedTargetException
119     {
120         m_prophlp.removeVetoableChangeListener(aPropertyName, xListener);
121     }
122 
123     // com.sun.star.container.XElementAccess:
getElementType()124     public com.sun.star.uno.Type getElementType()
125     {
126         return new com.sun.star.uno.Type(XFunctionDescription.class);
127     }
128 
hasElements()129     public boolean hasElements()
130     {
131         return functions.length != 0;
132     }
133 
134     // com.sun.star.container.XIndexAccess:
getCount()135     public int getCount()
136     {
137         return functions.length;
138     }
139 
getByIndex(int Index)140     public Object getByIndex(int Index) throws com.sun.star.lang.IndexOutOfBoundsException, com.sun.star.lang.WrappedTargetException
141     {
142         return getFunction(Index);
143     }
144 
145     // com.sun.star.report.meta.XFunctionCategory:
getNumber()146     public int getNumber()
147     {
148         return m_Number;
149     }
150 
getName()151     public String getName()
152     {
153         try
154         {
155             return category.getDisplayName(defaultLocale);
156         }
157         catch(Exception ex)
158         {
159 
160         }
161         return "Missing category for number " + m_Number;
162     }
163 
getFunction(int position)164     public com.sun.star.report.meta.XFunctionDescription getFunction(int position) throws com.sun.star.lang.IndexOutOfBoundsException, com.sun.star.lang.WrappedTargetException
165     {
166         if (position >= functions.length)
167         {
168             throw new IndexOutOfBoundsException();
169         }
170         return new StarFunctionDescription(defaultContext, m_xContext, this, functionRegistry.getMetaData(functions[position]));
171     }
172 }
173