1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_chart2.hxx"
30 #include "NumberFormatterWrapper.hxx"
31 #include "macros.hxx"
32 #include <comphelper/processfactory.hxx>
33 // header for class SvNumberFormatsSupplierObj
34 #include <svl/numuno.hxx>
35 // header for class SvNumberformat
36 #include <svl/zformat.hxx>
37 #include <tools/color.hxx>
38 #include <i18npool/mslangid.hxx>
39 #include <tools/debug.hxx>
40 #include <com/sun/star/util/DateTime.hpp>
41 
42 //.............................................................................
43 namespace chart
44 {
45 //.............................................................................
46 using namespace ::com::sun::star;
47 
48 FixedNumberFormatter::FixedNumberFormatter(
49                 const uno::Reference< util::XNumberFormatsSupplier >& xSupplier
50                 , sal_Int32 nNumberFormatKey )
51             : m_aNumberFormatterWrapper(xSupplier)
52             , m_nNumberFormatKey( nNumberFormatKey )
53 {
54 }
55 
56 FixedNumberFormatter::~FixedNumberFormatter()
57 {
58 }
59 
60 /*
61 sal_Int32 FixedNumberFormatter::getTextAndColor( double fUnscaledValueForText, rtl::OUString& rLabel ) const
62 {
63     sal_Int32 nLabelColor = Color(COL_BLUE).GetColor(); //@todo get this from somewheres
64     rLabel = getFormattedString( fUnscaledValueForText, nLabelColor );
65     return nLabelColor;
66 }
67 */
68 
69 rtl::OUString FixedNumberFormatter::getFormattedString( double fValue, sal_Int32& rLabelColor, bool& rbColorChanged ) const
70 {
71     return m_aNumberFormatterWrapper.getFormattedString(
72         m_nNumberFormatKey, fValue, rLabelColor, rbColorChanged );
73 }
74 
75 //-----------------------------------------------------------------------
76 //-----------------------------------------------------------------------
77 //-----------------------------------------------------------------------
78 
79 NumberFormatterWrapper::NumberFormatterWrapper( const uno::Reference< util::XNumberFormatsSupplier >& xSupplier )
80                     : m_xNumberFormatsSupplier(xSupplier)
81                     , m_pNumberFormatter(NULL)
82 
83 {
84     uno::Reference<beans::XPropertySet> xProp(m_xNumberFormatsSupplier,uno::UNO_QUERY);
85     rtl::OUString sNullDate( RTL_CONSTASCII_USTRINGPARAM("NullDate"));
86     if ( xProp.is() && xProp->getPropertySetInfo()->hasPropertyByName(sNullDate) )
87         m_aNullDate = xProp->getPropertyValue(sNullDate);
88     SvNumberFormatsSupplierObj* pSupplierObj = SvNumberFormatsSupplierObj::getImplementation( xSupplier );
89 	if( pSupplierObj )
90 		m_pNumberFormatter = pSupplierObj->GetNumberFormatter();
91     DBG_ASSERT(m_pNumberFormatter,"need a numberformatter");
92 }
93 
94 NumberFormatterWrapper::~NumberFormatterWrapper()
95 {
96 }
97 
98 SvNumberFormatter* NumberFormatterWrapper::getSvNumberFormatter() const
99 {
100     return m_pNumberFormatter;
101 }
102 
103 Date NumberFormatterWrapper::getNullDate() const
104 {
105     sal_uInt16 nYear = 1899,nDay = 30,nMonth = 12;
106     Date aRet(nDay,nMonth,nYear);
107 
108     util::DateTime aUtilDate;
109     if( m_aNullDate.hasValue() && (m_aNullDate >>= aUtilDate) )
110     {
111         aRet = Date(aUtilDate.Day,aUtilDate.Month,aUtilDate.Year);
112     }
113     else if( m_pNumberFormatter )
114     {
115         Date* pDate = m_pNumberFormatter->GetNullDate();
116         if( pDate )
117             aRet = *pDate;
118     }
119     return aRet;
120 }
121 
122 rtl::OUString NumberFormatterWrapper::getFormattedString(
123     sal_Int32 nNumberFormatKey, double fValue, sal_Int32& rLabelColor, bool& rbColorChanged ) const
124 {
125     String aText;
126     Color* pTextColor = NULL;
127     if( !m_pNumberFormatter )
128     {
129         DBG_ERROR("Need a NumberFormatter");
130         return aText;
131     }
132     // i99104 handle null date correctly
133     sal_uInt16 nYear = 1899,nDay = 30,nMonth = 12;
134     if ( m_aNullDate.hasValue() )
135     {
136         Date* pDate = m_pNumberFormatter->GetNullDate();
137         if ( pDate )
138         {
139             nYear = pDate->GetYear();
140             nMonth = pDate->GetMonth();
141             nDay = pDate->GetDay();
142         } // if ( pDate )
143         util::DateTime aNewNullDate;
144         m_aNullDate >>= aNewNullDate;
145         m_pNumberFormatter->ChangeNullDate(aNewNullDate.Day,aNewNullDate.Month,aNewNullDate.Year);
146     }
147     m_pNumberFormatter->GetOutputString(
148         fValue, nNumberFormatKey, aText, &pTextColor);
149     if ( m_aNullDate.hasValue() )
150     {
151         m_pNumberFormatter->ChangeNullDate(nDay,nMonth,nYear);
152     }
153     rtl::OUString aRet( aText );
154 
155     if(pTextColor)
156     {
157         rbColorChanged = true;
158         rLabelColor = pTextColor->GetColor();
159     }
160     else
161         rbColorChanged = false;
162 
163     return aRet;
164 }
165 
166 // to get the language type use MsLangId::convertLocaleToLanguage( rNumberFormat.aLocale )
167 
168 /*
169     uno::Reference< i18n::XNumberFormatCode > xNumberFormatCode(
170 		m_xCC->getServiceManager()->createInstanceWithContext( C2U(
171         "com.sun.star.i18n.NumberFormatMapper" ), m_xCC ), uno::UNO_QUERY );
172 
173     i18n::NumberFormatCode aNumberFormatCode = xNumberFormatCode->getDefault (
174         i18n::KNumberFormatType::MEDIUM,
175         i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER,
176         aLocale );
177 
178     uno::Sequence< i18n::NumberFormatCode > aListOfNumberFormatCode = xNumberFormatCode->getAllFormatCode(
179         i18n::KNumberFormatUsage::SCIENTIFIC_NUMBER,
180         aLocale );
181 
182     i18n::NumberFormatCode aNumberFormatCode0 = aListOfNumberFormatCode[0];
183     i18n::NumberFormatCode aNumberFormatCode1 = aListOfNumberFormatCode[1];
184 */
185 
186 //.............................................................................
187 } //namespace chart
188 //.............................................................................
189