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 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chart2.hxx"
26 #include "NumberFormatterWrapper.hxx"
27 #include "macros.hxx"
28 #include <comphelper/processfactory.hxx>
29 // header for class SvNumberFormatsSupplierObj
30 #include <svl/numuno.hxx>
31 // header for class SvNumberformat
32 #include <svl/zformat.hxx>
33 #include <tools/color.hxx>
34 #include <i18npool/mslangid.hxx>
35 #include <tools/debug.hxx>
36 #include <com/sun/star/util/DateTime.hpp>
37 
38 //.............................................................................
39 namespace chart
40 {
41 //.............................................................................
42 using namespace ::com::sun::star;
43 
FixedNumberFormatter(const uno::Reference<util::XNumberFormatsSupplier> & xSupplier,sal_Int32 nNumberFormatKey)44 FixedNumberFormatter::FixedNumberFormatter(
45                 const uno::Reference< util::XNumberFormatsSupplier >& xSupplier
46                 , sal_Int32 nNumberFormatKey )
47             : m_aNumberFormatterWrapper(xSupplier)
48             , m_nNumberFormatKey( nNumberFormatKey )
49 {
50 }
51 
~FixedNumberFormatter()52 FixedNumberFormatter::~FixedNumberFormatter()
53 {
54 }
55 
56 /*
57 sal_Int32 FixedNumberFormatter::getTextAndColor( double fUnscaledValueForText, rtl::OUString& rLabel ) const
58 {
59     sal_Int32 nLabelColor = Color(COL_BLUE).GetColor(); //@todo get this from somewheres
60     rLabel = getFormattedString( fUnscaledValueForText, nLabelColor );
61     return nLabelColor;
62 }
63 */
64 
getFormattedString(double fValue,sal_Int32 & rLabelColor,bool & rbColorChanged) const65 rtl::OUString FixedNumberFormatter::getFormattedString( double fValue, sal_Int32& rLabelColor, bool& rbColorChanged ) const
66 {
67     return m_aNumberFormatterWrapper.getFormattedString(
68         m_nNumberFormatKey, fValue, rLabelColor, rbColorChanged );
69 }
70 
71 //-----------------------------------------------------------------------
72 //-----------------------------------------------------------------------
73 //-----------------------------------------------------------------------
74 
NumberFormatterWrapper(const uno::Reference<util::XNumberFormatsSupplier> & xSupplier)75 NumberFormatterWrapper::NumberFormatterWrapper( const uno::Reference< util::XNumberFormatsSupplier >& xSupplier )
76                     : m_xNumberFormatsSupplier(xSupplier)
77                     , m_pNumberFormatter(NULL)
78 
79 {
80     uno::Reference<beans::XPropertySet> xProp(m_xNumberFormatsSupplier,uno::UNO_QUERY);
81     rtl::OUString sNullDate( RTL_CONSTASCII_USTRINGPARAM("NullDate"));
82     if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(sNullDate) )
83         m_aNullDate = xProp->getPropertyValue(sNullDate);
84     SvNumberFormatsSupplierObj* pSupplierObj = SvNumberFormatsSupplierObj::getImplementation( xSupplier );
85 	if( pSupplierObj )
86 		m_pNumberFormatter = pSupplierObj->GetNumberFormatter();
87     DBG_ASSERT(m_pNumberFormatter,"need a numberformatter");
88 }
89 
~NumberFormatterWrapper()90 NumberFormatterWrapper::~NumberFormatterWrapper()
91 {
92 }
93 
getSvNumberFormatter() const94 SvNumberFormatter* NumberFormatterWrapper::getSvNumberFormatter() const
95 {
96     return m_pNumberFormatter;
97 }
98 
getNullDate() const99 Date NumberFormatterWrapper::getNullDate() const
100 {
101     sal_uInt16 nYear = 1899,nDay = 30,nMonth = 12;
102     Date aRet(nDay,nMonth,nYear);
103 
104     util::DateTime aUtilDate;
105     if( m_aNullDate.hasValue() && (m_aNullDate >>= aUtilDate) )
106     {
107         aRet = Date(aUtilDate.Day,aUtilDate.Month,aUtilDate.Year);
108     }
109     else if( m_pNumberFormatter )
110     {
111         Date* pDate = m_pNumberFormatter->GetNullDate();
112         if( pDate )
113             aRet = *pDate;
114     }
115     return aRet;
116 }
117 
getFormattedString(sal_Int32 nNumberFormatKey,double fValue,sal_Int32 & rLabelColor,bool & rbColorChanged) const118 rtl::OUString NumberFormatterWrapper::getFormattedString(
119     sal_Int32 nNumberFormatKey, double fValue, sal_Int32& rLabelColor, bool& rbColorChanged ) const
120 {
121     String aText;
122     Color* pTextColor = NULL;
123     if( !m_pNumberFormatter )
124     {
125         DBG_ERROR("Need a NumberFormatter");
126         return aText;
127     }
128     // i99104 handle null date correctly
129     sal_uInt16 nYear = 1899,nDay = 30,nMonth = 12;
130     if ( m_aNullDate.hasValue() )
131     {
132         Date* pDate = m_pNumberFormatter->GetNullDate();
133         if ( pDate )
134         {
135             nYear = pDate->GetYear();
136             nMonth = pDate->GetMonth();
137             nDay = pDate->GetDay();
138         } // if ( pDate )
139         util::DateTime aNewNullDate;
140         m_aNullDate >>= aNewNullDate;
141         m_pNumberFormatter->ChangeNullDate(aNewNullDate.Day,aNewNullDate.Month,aNewNullDate.Year);
142     }
143     m_pNumberFormatter->GetOutputString(
144         fValue, nNumberFormatKey, aText, &pTextColor);
145     if ( m_aNullDate.hasValue() )
146     {
147         m_pNumberFormatter->ChangeNullDate(nDay,nMonth,nYear);
148     }
149     rtl::OUString aRet( aText );
150 
151     if(pTextColor)
152     {
153         rbColorChanged = true;
154         rLabelColor = pTextColor->GetColor();
155     }
156     else
157         rbColorChanged = false;
158 
159     return aRet;
160 }
161 
162 // to get the language type use MsLangId::convertLocaleToLanguage( rNumberFormat.aLocale )
163 
164 /*
165     uno::Reference< i18n::XNumberFormatCode > xNumberFormatCode(
166 		m_xCC->getServiceManager()->createInstanceWithContext( C2U(
167         "com.sun.star.i18n.NumberFormatMapper" ), m_xCC ), uno::UNO_QUERY );
168 
169     i18n::NumberFormatCode aNumberFormatCode = xNumberFormatCode->getDefault (
170         i18n::KNumberFormatType::MEDIUM,
171         i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER,
172         aLocale );
173 
174     uno::Sequence< i18n::NumberFormatCode > aListOfNumberFormatCode = xNumberFormatCode->getAllFormatCode(
175         i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER,
176         aLocale );
177 
178     i18n::NumberFormatCode aNumberFormatCode0 = aListOfNumberFormatCode[0];
179     i18n::NumberFormatCode aNumberFormatCode1 = aListOfNumberFormatCode[1];
180 */
181 
182 //.............................................................................
183 } //namespace chart
184 //.............................................................................
185