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 "services.hxx"
22 #include <osl/diagnose.h>
23 #include <comphelper/types.hxx>
24 #include <comphelper/extract.hxx>
25 #include <com/sun/star/form/FormComponentType.hpp>
26 #include <com/sun/star/util/NumberFormatsSupplier.hpp>
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::util
;
35 using namespace ::com::sun::star::lang
;
36 using namespace ::com::sun::star::form
;
37 using namespace ::com::sun::star::beans
;
39 sal_Int32
OLimitedFormats::s_nInstanceCount(0);
40 ::osl::Mutex
OLimitedFormats::s_aMutex
;
41 Reference
< XNumberFormatsSupplier
> OLimitedFormats::s_xStandardFormats
;
55 static const Locale
& getLocale(LocaleType _eType
)
57 static const Locale
s_aEnglishUS( OUString("en"), OUString("us"), OUString() );
58 static const Locale
s_aGerman( OUString("de"), OUString("DE"), OUString() );
59 static const OUString s_sEmptyString
;
60 static const Locale
s_aSystem( s_sEmptyString
, s_sEmptyString
, s_sEmptyString
);
74 OSL_FAIL("getLocale: invalid enum value!");
81 const sal_Char
* pDescription
;
87 static FormatEntry
* lcl_getFormatTable(sal_Int16 nTableId
)
91 case FormComponentType::TIMEFIELD
:
93 static FormatEntry s_aFormats
[] = {
94 { "HH:MM", -1, ltEnglishUS
},
95 { "HH:MM:SS", -1, ltEnglishUS
},
96 { "HH:MM AM/PM", -1, ltEnglishUS
},
97 { "HH:MM:SS AM/PM", -1, ltEnglishUS
},
98 { nullptr, -1, ltSystem
}
102 case FormComponentType::DATEFIELD
:
104 static FormatEntry s_aFormats
[] = {
105 { "T-M-JJ", -1, ltGerman
},
106 { "TT-MM-JJ", -1, ltGerman
},
107 { "TT-MM-JJJJ", -1, ltGerman
},
108 { "NNNNT. MMMM JJJJ", -1, ltGerman
},
110 { "DD/MM/YY", -1, ltEnglishUS
},
111 { "MM/DD/YY", -1, ltEnglishUS
},
112 { "YY/MM/DD", -1, ltEnglishUS
},
113 { "DD/MM/YYYY", -1, ltEnglishUS
},
114 { "MM/DD/YYYY", -1, ltEnglishUS
},
115 { "YYYY/MM/DD", -1, ltEnglishUS
},
117 { "JJ-MM-TT", -1, ltGerman
},
118 { "JJJJ-MM-TT", -1, ltGerman
},
120 { nullptr, -1, ltSystem
}
126 OSL_FAIL("lcl_getFormatTable: invalid id!");
130 OLimitedFormats::OLimitedFormats(const Reference
< XComponentContext
>& _rxContext
, const sal_Int16 _nClassId
)
131 :m_nFormatEnumPropertyHandle(-1)
132 ,m_nTableId(_nClassId
)
134 OSL_ENSURE(_rxContext
.is(), "OLimitedFormats::OLimitedFormats: invalid service factory!");
135 acquireSupplier(_rxContext
);
136 ensureTableInitialized(m_nTableId
);
140 OLimitedFormats::~OLimitedFormats()
146 void OLimitedFormats::ensureTableInitialized(const sal_Int16 _nTableId
)
148 FormatEntry
* pFormatTable
= lcl_getFormatTable(_nTableId
);
149 if (-1 == pFormatTable
->nKey
)
151 ::osl::MutexGuard
aGuard(s_aMutex
);
152 if (-1 == pFormatTable
->nKey
)
154 // initialize the keys
155 Reference
<XNumberFormats
> xStandardFormats
;
156 if (s_xStandardFormats
.is())
157 xStandardFormats
= s_xStandardFormats
->getNumberFormats();
158 OSL_ENSURE(xStandardFormats
.is(), "OLimitedFormats::ensureTableInitialized: don't have a formats supplier!");
160 if (xStandardFormats
.is())
162 // loop through the table
163 FormatEntry
* pLoopFormats
= pFormatTable
;
164 while (pLoopFormats
->pDescription
)
166 // get the key for the description
167 pLoopFormats
->nKey
= xStandardFormats
->queryKey(
168 OUString::createFromAscii(pLoopFormats
->pDescription
),
169 getLocale(pLoopFormats
->eLocale
),
173 if (-1 == pLoopFormats
->nKey
)
175 pLoopFormats
->nKey
= xStandardFormats
->addNew(
176 OUString::createFromAscii(pLoopFormats
->pDescription
),
177 getLocale(pLoopFormats
->eLocale
)
182 xStandardFormats
->getByKey(pLoopFormats
->nKey
);
184 catch(const Exception
&)
186 OSL_FAIL("OLimitedFormats::ensureTableInitialized: adding the key to the formats collection failed!");
200 void OLimitedFormats::clearTable(const sal_Int16 _nTableId
)
202 ::osl::MutexGuard
aGuard(s_aMutex
);
203 FormatEntry
* pFormats
= lcl_getFormatTable(_nTableId
);
204 FormatEntry
* pResetLoop
= pFormats
;
205 while (pResetLoop
->pDescription
)
207 pResetLoop
->nKey
= -1;
213 void OLimitedFormats::setAggregateSet(const Reference
< XFastPropertySet
>& _rxAggregate
, sal_Int32 _nOriginalPropertyHandle
)
215 // changes (NULL -> not NULL) and (not NULL -> NULL) are allowed
216 OSL_ENSURE(!m_xAggregate
.is() || !_rxAggregate
.is(), "OLimitedFormats::setAggregateSet: already have an aggregate!");
217 OSL_ENSURE(_rxAggregate
.is() || m_xAggregate
.is(), "OLimitedFormats::setAggregateSet: invalid new aggregate!");
219 m_xAggregate
= _rxAggregate
;
220 m_nFormatEnumPropertyHandle
= _nOriginalPropertyHandle
;
222 if (m_xAggregate
.is())
226 m_xAggregate
->getFastPropertyValue(m_nFormatEnumPropertyHandle
);
228 catch(const Exception
&)
230 OSL_FAIL("OLimitedFormats::setAggregateSet: invalid handle!");
237 void OLimitedFormats::getFormatKeyPropertyValue( Any
& _rValue
) const
241 OSL_ENSURE(m_xAggregate
.is() && (-1 != m_nFormatEnumPropertyHandle
), "OLimitedFormats::getFormatKeyPropertyValue: not initialized!");
242 if (m_xAggregate
.is())
244 // get the aggregate's enum property value
245 Any aEnumPropertyValue
= m_xAggregate
->getFastPropertyValue(m_nFormatEnumPropertyHandle
);
246 sal_Int32 nValue
= -1;
247 ::cppu::enum2int(nValue
, aEnumPropertyValue
);
249 // get the translation table
250 const FormatEntry
* pFormats
= lcl_getFormatTable(m_nTableId
);
252 // seek to the nValue'th entry
253 sal_Int32 nLookup
= 0;
255 (nullptr != pFormats
->pDescription
) && (nLookup
< nValue
);
256 ++pFormats
, ++nLookup
259 OSL_ENSURE(nullptr != pFormats
->pDescription
, "OLimitedFormats::getFormatKeyPropertyValue: did not find the value!");
260 if (pFormats
->pDescription
)
261 _rValue
<<= pFormats
->nKey
;
264 // TODO: should use a standard format for the control type we're working for
268 bool OLimitedFormats::convertFormatKeyPropertyValue(Any
& _rConvertedValue
, Any
& _rOldValue
, const Any
& _rNewValue
)
270 OSL_ENSURE(m_xAggregate
.is() && (-1 != m_nFormatEnumPropertyHandle
), "OLimitedFormats::convertFormatKeyPropertyValue: not initialized!");
272 if (m_xAggregate
.is())
274 // the new format key to set
275 sal_Int32 nNewFormat
= 0;
276 if (!(_rNewValue
>>= nNewFormat
))
277 throw IllegalArgumentException();
279 // get the old (enum) value from the aggregate
280 Any aEnumPropertyValue
= m_xAggregate
->getFastPropertyValue(m_nFormatEnumPropertyHandle
);
281 sal_Int32 nOldEnumValue
= -1;
282 ::cppu::enum2int(nOldEnumValue
, aEnumPropertyValue
);
284 // get the translation table
285 const FormatEntry
* pFormats
= lcl_getFormatTable(m_nTableId
);
288 _rConvertedValue
.clear();
290 // look for the entry with the given format key
291 sal_Int32 nTablePosition
= 0;
293 (nullptr != pFormats
->pDescription
) && (nNewFormat
!= pFormats
->nKey
);
294 ++pFormats
, ++nTablePosition
297 if (nTablePosition
== nOldEnumValue
)
298 _rOldValue
<<= pFormats
->nKey
;
301 bool bFoundIt
= (nullptr != pFormats
->pDescription
);
302 bool bModified
= false;
305 _rConvertedValue
<<= (sal_Int16
)nTablePosition
;
306 bModified
= nTablePosition
!= nOldEnumValue
;
309 if (!_rOldValue
.hasValue())
310 { // did not reach the end of the table (means we found nNewFormat)
311 // -> go to the end to ensure that _rOldValue is set
312 while (pFormats
->pDescription
)
314 if (nTablePosition
== nOldEnumValue
)
316 _rOldValue
<<= pFormats
->nKey
;
325 OSL_ENSURE(_rOldValue
.hasValue(), "OLimitedFormats::convertFormatKeyPropertyValue: did not find the old enum value in the table!");
328 { // somebody gave us a format which we can't translate
329 OUString
sMessage ("This control supports only a very limited number of formats.");
330 throw IllegalArgumentException(sMessage
, nullptr, 2);
340 void OLimitedFormats::setFormatKeyPropertyValue( const Any
& _rNewValue
)
342 OSL_ENSURE(m_xAggregate
.is() && (-1 != m_nFormatEnumPropertyHandle
), "OLimitedFormats::setFormatKeyPropertyValue: not initialized!");
344 if (m_xAggregate
.is())
345 { // this is to be called after convertFormatKeyPropertyValue, where
346 // we translated the format key into an enum value.
347 // So now we can simply forward this enum value to our aggregate
348 m_xAggregate
->setFastPropertyValue(m_nFormatEnumPropertyHandle
, _rNewValue
);
353 void OLimitedFormats::acquireSupplier(const Reference
< XComponentContext
>& _rxContext
)
355 ::osl::MutexGuard
aGuard(s_aMutex
);
356 if (1 == ++s_nInstanceCount
)
357 { // create the standard formatter
358 s_xStandardFormats
= NumberFormatsSupplier::createWithLocale(_rxContext
, getLocale(ltEnglishUS
));
363 void OLimitedFormats::releaseSupplier()
365 ::osl::MutexGuard
aGuard(s_aMutex
);
366 if (0 == --s_nInstanceCount
)
368 ::comphelper::disposeComponent(s_xStandardFormats
);
369 s_xStandardFormats
= nullptr;
371 clearTable(FormComponentType::TIMEFIELD
);
372 clearTable(FormComponentType::DATEFIELD
);
380 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */