1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <limitedformats.hxx>
21 #include <osl/diagnose.h>
22 #include <comphelper/types.hxx>
23 #include <comphelper/extract.hxx>
24 #include <com/sun/star/form/FormComponentType.hpp>
25 #include <com/sun/star/util/NumberFormatsSupplier.hpp>
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::util
;
34 using namespace ::com::sun::star::lang
;
35 using namespace ::com::sun::star::form
;
36 using namespace ::com::sun::star::beans
;
38 sal_Int32
OLimitedFormats::s_nInstanceCount(0);
39 ::osl::Mutex
OLimitedFormats::s_aMutex
;
40 Reference
< XNumberFormatsSupplier
> OLimitedFormats::s_xStandardFormats
;
56 static const Locale
& getLocale(LocaleType _eType
)
58 static const Locale
s_aEnglishUS( "en", "us", OUString() );
59 static const Locale
s_aGerman( "de", "DE", OUString() );
60 static const Locale
s_aSystem( "", "", "" );
74 OSL_FAIL("getLocale: invalid enum value!");
82 const char* pDescription
;
89 static FormatEntry
* lcl_getFormatTable(sal_Int16 nTableId
)
93 case FormComponentType::TIMEFIELD
:
95 static FormatEntry s_aFormats
[] = {
96 { "HH:MM", -1, ltEnglishUS
},
97 { "HH:MM:SS", -1, ltEnglishUS
},
98 { "HH:MM AM/PM", -1, ltEnglishUS
},
99 { "HH:MM:SS AM/PM", -1, ltEnglishUS
},
100 { nullptr, -1, ltSystem
}
104 case FormComponentType::DATEFIELD
:
106 static FormatEntry s_aFormats
[] = {
107 { "T-M-JJ", -1, ltGerman
},
108 { "TT-MM-JJ", -1, ltGerman
},
109 { "TT-MM-JJJJ", -1, ltGerman
},
110 { "NNNNT. MMMM JJJJ", -1, ltGerman
},
112 { "DD/MM/YY", -1, ltEnglishUS
},
113 { "MM/DD/YY", -1, ltEnglishUS
},
114 { "YY/MM/DD", -1, ltEnglishUS
},
115 { "DD/MM/YYYY", -1, ltEnglishUS
},
116 { "MM/DD/YYYY", -1, ltEnglishUS
},
117 { "YYYY/MM/DD", -1, ltEnglishUS
},
119 { "JJ-MM-TT", -1, ltGerman
},
120 { "JJJJ-MM-TT", -1, ltGerman
},
122 { nullptr, -1, ltSystem
}
128 OSL_FAIL("lcl_getFormatTable: invalid id!");
132 OLimitedFormats::OLimitedFormats(const Reference
< XComponentContext
>& _rxContext
, const sal_Int16 _nClassId
)
133 :m_nFormatEnumPropertyHandle(-1)
134 ,m_nTableId(_nClassId
)
136 OSL_ENSURE(_rxContext
.is(), "OLimitedFormats::OLimitedFormats: invalid service factory!");
137 acquireSupplier(_rxContext
);
138 ensureTableInitialized(m_nTableId
);
142 OLimitedFormats::~OLimitedFormats()
148 void OLimitedFormats::ensureTableInitialized(const sal_Int16 _nTableId
)
150 FormatEntry
* pFormatTable
= lcl_getFormatTable(_nTableId
);
151 if (-1 != pFormatTable
->nKey
)
154 ::osl::MutexGuard
aGuard(s_aMutex
);
155 if (-1 != pFormatTable
->nKey
)
158 // initialize the keys
159 Reference
<XNumberFormats
> xStandardFormats
;
160 if (s_xStandardFormats
.is())
161 xStandardFormats
= s_xStandardFormats
->getNumberFormats();
162 OSL_ENSURE(xStandardFormats
.is(), "OLimitedFormats::ensureTableInitialized: don't have a formats supplier!");
164 if (!xStandardFormats
.is())
167 // loop through the table
168 FormatEntry
* pLoopFormats
= pFormatTable
;
169 while (pLoopFormats
->pDescription
)
171 // get the key for the description
172 pLoopFormats
->nKey
= xStandardFormats
->queryKey(
173 OUString::createFromAscii(pLoopFormats
->pDescription
),
174 getLocale(pLoopFormats
->eLocale
),
178 if (-1 == pLoopFormats
->nKey
)
180 pLoopFormats
->nKey
= xStandardFormats
->addNew(
181 OUString::createFromAscii(pLoopFormats
->pDescription
),
182 getLocale(pLoopFormats
->eLocale
)
187 xStandardFormats
->getByKey(pLoopFormats
->nKey
);
189 catch(const Exception
&)
191 OSL_FAIL("OLimitedFormats::ensureTableInitialized: adding the key to the formats collection failed!");
202 void OLimitedFormats::clearTable(const sal_Int16 _nTableId
)
204 ::osl::MutexGuard
aGuard(s_aMutex
);
205 FormatEntry
* pFormats
= lcl_getFormatTable(_nTableId
);
206 FormatEntry
* pResetLoop
= pFormats
;
207 while (pResetLoop
->pDescription
)
209 pResetLoop
->nKey
= -1;
215 void OLimitedFormats::setAggregateSet(const Reference
< XFastPropertySet
>& _rxAggregate
, sal_Int32 _nOriginalPropertyHandle
)
217 // changes (NULL -> not NULL) and (not NULL -> NULL) are allowed
218 OSL_ENSURE(!m_xAggregate
.is() || !_rxAggregate
.is(), "OLimitedFormats::setAggregateSet: already have an aggregate!");
219 OSL_ENSURE(_rxAggregate
.is() || m_xAggregate
.is(), "OLimitedFormats::setAggregateSet: invalid new aggregate!");
221 m_xAggregate
= _rxAggregate
;
222 m_nFormatEnumPropertyHandle
= _nOriginalPropertyHandle
;
224 if (m_xAggregate
.is())
228 m_xAggregate
->getFastPropertyValue(m_nFormatEnumPropertyHandle
);
230 catch(const Exception
&)
232 OSL_FAIL("OLimitedFormats::setAggregateSet: invalid handle!");
239 void OLimitedFormats::getFormatKeyPropertyValue( Any
& _rValue
) const
243 OSL_ENSURE(m_xAggregate
.is() && (-1 != m_nFormatEnumPropertyHandle
), "OLimitedFormats::getFormatKeyPropertyValue: not initialized!");
244 if (!m_xAggregate
.is())
247 // get the aggregate's enum property value
248 Any aEnumPropertyValue
= m_xAggregate
->getFastPropertyValue(m_nFormatEnumPropertyHandle
);
249 sal_Int32 nValue
= -1;
250 ::cppu::enum2int(nValue
, aEnumPropertyValue
);
252 // get the translation table
253 const FormatEntry
* pFormats
= lcl_getFormatTable(m_nTableId
);
255 // seek to the nValue'th entry
256 sal_Int32 nLookup
= 0;
258 (nullptr != pFormats
->pDescription
) && (nLookup
< nValue
);
259 ++pFormats
, ++nLookup
262 OSL_ENSURE(nullptr != pFormats
->pDescription
, "OLimitedFormats::getFormatKeyPropertyValue: did not find the value!");
263 if (pFormats
->pDescription
)
264 _rValue
<<= pFormats
->nKey
;
266 // TODO: should use a standard format for the control type we're working for
270 bool OLimitedFormats::convertFormatKeyPropertyValue(Any
& _rConvertedValue
, Any
& _rOldValue
, const Any
& _rNewValue
)
272 OSL_ENSURE(m_xAggregate
.is() && (-1 != m_nFormatEnumPropertyHandle
), "OLimitedFormats::convertFormatKeyPropertyValue: not initialized!");
274 if (m_xAggregate
.is())
276 // the new format key to set
277 sal_Int32 nNewFormat
= 0;
278 if (!(_rNewValue
>>= nNewFormat
))
279 throw IllegalArgumentException();
281 // get the old (enum) value from the aggregate
282 Any aEnumPropertyValue
= m_xAggregate
->getFastPropertyValue(m_nFormatEnumPropertyHandle
);
283 sal_Int32 nOldEnumValue
= -1;
284 ::cppu::enum2int(nOldEnumValue
, aEnumPropertyValue
);
286 // get the translation table
287 const FormatEntry
* pFormats
= lcl_getFormatTable(m_nTableId
);
290 _rConvertedValue
.clear();
292 // look for the entry with the given format key
293 sal_Int32 nTablePosition
= 0;
295 (nullptr != pFormats
->pDescription
) && (nNewFormat
!= pFormats
->nKey
);
296 ++pFormats
, ++nTablePosition
299 if (nTablePosition
== nOldEnumValue
)
300 _rOldValue
<<= pFormats
->nKey
;
303 bool bFoundIt
= (nullptr != pFormats
->pDescription
);
304 bool bModified
= false;
307 _rConvertedValue
<<= static_cast<sal_Int16
>(nTablePosition
);
308 bModified
= nTablePosition
!= nOldEnumValue
;
311 if (!_rOldValue
.hasValue())
312 { // did not reach the end of the table (means we found nNewFormat)
313 // -> go to the end to ensure that _rOldValue is set
314 while (pFormats
->pDescription
)
316 if (nTablePosition
== nOldEnumValue
)
318 _rOldValue
<<= pFormats
->nKey
;
327 OSL_ENSURE(_rOldValue
.hasValue(), "OLimitedFormats::convertFormatKeyPropertyValue: did not find the old enum value in the table!");
330 { // somebody gave us a format which we can't translate
331 throw IllegalArgumentException("This control supports only a very limited number of formats.", nullptr, 2);
341 void OLimitedFormats::setFormatKeyPropertyValue( const Any
& _rNewValue
)
343 OSL_ENSURE(m_xAggregate
.is() && (-1 != m_nFormatEnumPropertyHandle
), "OLimitedFormats::setFormatKeyPropertyValue: not initialized!");
345 if (m_xAggregate
.is())
346 { // this is to be called after convertFormatKeyPropertyValue, where
347 // we translated the format key into an enum value.
348 // So now we can simply forward this enum value to our aggregate
349 m_xAggregate
->setFastPropertyValue(m_nFormatEnumPropertyHandle
, _rNewValue
);
354 void OLimitedFormats::acquireSupplier(const Reference
< XComponentContext
>& _rxContext
)
356 ::osl::MutexGuard
aGuard(s_aMutex
);
357 if (1 == ++s_nInstanceCount
)
358 { // create the standard formatter
359 s_xStandardFormats
= NumberFormatsSupplier::createWithLocale(_rxContext
, getLocale(ltEnglishUS
));
364 void OLimitedFormats::releaseSupplier()
366 ::osl::MutexGuard
aGuard(s_aMutex
);
367 if (0 == --s_nInstanceCount
)
369 ::comphelper::disposeComponent(s_xStandardFormats
);
370 s_xStandardFormats
= nullptr;
372 clearTable(FormComponentType::TIMEFIELD
);
373 clearTable(FormComponentType::DATEFIELD
);
381 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */