update credits
[LibreOffice.git] / forms / source / misc / limitedformats.cxx
blob7054db2403bbaac96b682639838c6715d3312437
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
28 //.........................................................................
29 namespace frm
31 //.........................................................................
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;
43 //=====================================================================
44 //=
45 //=====================================================================
46 //---------------------------------------------------------------------
47 enum LocaleType
49 ltEnglishUS,
50 ltGerman,
51 ltSystem
54 //---------------------------------------------------------------------
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 );
62 switch (_eType)
64 case ltEnglishUS:
65 return s_aEnglishUS;
67 case ltGerman:
68 return s_aGerman;
70 case ltSystem:
71 return s_aSystem;
74 OSL_FAIL("getLocale: invalid enum value!");
75 return s_aSystem;
78 //---------------------------------------------------------------------
79 struct FormatEntry
81 const sal_Char* pDescription;
82 sal_Int32 nKey;
83 LocaleType eLocale;
86 //---------------------------------------------------------------------
87 static const FormatEntry* lcl_getFormatTable(sal_Int16 nTableId)
89 switch (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 { NULL, -1, ltSystem }
100 // don't switch this table here to const. The compiler could be tempted to really place this
101 // in a non-writeable segment, but we want to fill in the format keys later ....
102 return s_aFormats;
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 { NULL, -1, ltSystem }
124 return s_aFormats;
128 OSL_FAIL("lcl_getFormatTable: invalid id!");
129 return NULL;
132 //=====================================================================
133 //= OLimitedFormats
134 //=====================================================================
135 //---------------------------------------------------------------------
136 OLimitedFormats::OLimitedFormats(const Reference< XComponentContext >& _rxContext, const sal_Int16 _nClassId)
137 :m_nFormatEnumPropertyHandle(-1)
138 ,m_nTableId(_nClassId)
140 OSL_ENSURE(_rxContext.is(), "OLimitedFormats::OLimitedFormats: invalid service factory!");
141 acquireSupplier(_rxContext);
142 ensureTableInitialized(m_nTableId);
145 //---------------------------------------------------------------------
146 OLimitedFormats::~OLimitedFormats()
148 releaseSupplier();
151 //---------------------------------------------------------------------
152 void OLimitedFormats::ensureTableInitialized(const sal_Int16 _nTableId)
154 const FormatEntry* pFormatTable = lcl_getFormatTable(_nTableId);
155 if (-1 == pFormatTable->nKey)
157 ::osl::MutexGuard aGuard(s_aMutex);
158 if (-1 == pFormatTable->nKey)
160 // initialize the keys
161 Reference<XNumberFormats> xStandardFormats;
162 if (s_xStandardFormats.is())
163 xStandardFormats = s_xStandardFormats->getNumberFormats();
164 OSL_ENSURE(xStandardFormats.is(), "OLimitedFormats::ensureTableInitialized: don't have a formats supplier!");
166 if (xStandardFormats.is())
168 // loop through the table
169 FormatEntry* pLoopFormats = const_cast<FormatEntry*>(pFormatTable);
170 while (pLoopFormats->pDescription)
172 // get the key for the description
173 pLoopFormats->nKey = xStandardFormats->queryKey(
174 OUString::createFromAscii(pLoopFormats->pDescription),
175 getLocale(pLoopFormats->eLocale),
176 sal_False
179 if (-1 == pLoopFormats->nKey)
181 pLoopFormats->nKey = xStandardFormats->addNew(
182 OUString::createFromAscii(pLoopFormats->pDescription),
183 getLocale(pLoopFormats->eLocale)
185 #ifdef DBG_UTIL
188 xStandardFormats->getByKey(pLoopFormats->nKey);
190 catch(const Exception&)
192 OSL_FAIL("OLimitedFormats::ensureTableInitialized: adding the key to the formats collection failed!");
194 #endif
197 // next
198 ++pLoopFormats;
205 //---------------------------------------------------------------------
206 void OLimitedFormats::clearTable(const sal_Int16 _nTableId)
208 ::osl::MutexGuard aGuard(s_aMutex);
209 const FormatEntry* pFormats = lcl_getFormatTable(_nTableId);
210 FormatEntry* pResetLoop = const_cast<FormatEntry*>(pFormats);
211 while (pResetLoop->pDescription)
213 pResetLoop->nKey = -1;
214 ++pResetLoop;
218 //---------------------------------------------------------------------
219 void OLimitedFormats::setAggregateSet(const Reference< XFastPropertySet >& _rxAggregate, sal_Int32 _nOriginalPropertyHandle)
221 // changes (NULL -> not NULL) and (not NULL -> NULL) are allowed
222 OSL_ENSURE(!m_xAggregate.is() || !_rxAggregate.is(), "OLimitedFormats::setAggregateSet: already have an aggregate!");
223 OSL_ENSURE(_rxAggregate.is() || m_xAggregate.is(), "OLimitedFormats::setAggregateSet: invalid new aggregate!");
225 m_xAggregate = _rxAggregate;
226 m_nFormatEnumPropertyHandle = _nOriginalPropertyHandle;
227 #ifdef DBG_UTIL
228 if (m_xAggregate.is())
232 m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
234 catch(const Exception&)
236 OSL_FAIL("OLimitedFormats::setAggregateSet: invalid handle!");
239 #endif
242 //---------------------------------------------------------------------
243 void OLimitedFormats::getFormatKeyPropertyValue( Any& _rValue ) const
245 _rValue.clear();
247 OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::getFormatKeyPropertyValue: not initialized!");
248 if (m_xAggregate.is())
250 // get the aggregate's enum property value
251 Any aEnumPropertyValue = m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
252 sal_Int32 nValue = -1;
253 ::cppu::enum2int(nValue, aEnumPropertyValue);
255 // get the translation table
256 const FormatEntry* pFormats = lcl_getFormatTable(m_nTableId);
258 // seek to the nValue'th entry
259 sal_Int32 nLookup = 0;
260 for ( ;
261 (NULL != pFormats->pDescription) && (nLookup < nValue);
262 ++pFormats, ++nLookup
265 OSL_ENSURE(NULL != pFormats->pDescription, "OLimitedFormats::getFormatKeyPropertyValue: did not find the value!");
266 if (pFormats->pDescription)
267 _rValue <<= pFormats->nKey;
270 // TODO: should use a standard format for the control type we're working for
273 //---------------------------------------------------------------------
274 sal_Bool OLimitedFormats::convertFormatKeyPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rNewValue)
276 OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::convertFormatKeyPropertyValue: not initialized!");
278 if (m_xAggregate.is())
280 // the new format key to set
281 sal_Int32 nNewFormat = 0;
282 if (!(_rNewValue >>= nNewFormat))
283 throw IllegalArgumentException();
285 // get the old (enum) value from the aggregate
286 Any aEnumPropertyValue = m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
287 sal_Int32 nOldEnumValue = -1;
288 ::cppu::enum2int(nOldEnumValue, aEnumPropertyValue);
290 // get the translation table
291 const FormatEntry* pFormats = lcl_getFormatTable(m_nTableId);
293 _rOldValue.clear();
294 _rConvertedValue.clear();
296 // look for the entry with the given format key
297 sal_Int32 nTablePosition = 0;
298 for ( ;
299 (NULL != pFormats->pDescription) && (nNewFormat != pFormats->nKey);
300 ++pFormats, ++nTablePosition
303 if (nTablePosition == nOldEnumValue)
304 _rOldValue <<= pFormats->nKey;
307 sal_Bool bFoundIt = (NULL != pFormats->pDescription);
308 sal_Bool bModified = sal_False;
309 if (bFoundIt)
311 _rConvertedValue <<= (sal_Int16)nTablePosition;
312 bModified = nTablePosition != nOldEnumValue;
315 if (!_rOldValue.hasValue())
316 { // did not reach the end of the table (means we found nNewFormat)
317 // -> go to the end to ensure that _rOldValue is set
318 while (pFormats->pDescription)
320 if (nTablePosition == nOldEnumValue)
322 _rOldValue <<= pFormats->nKey;
323 break;
326 ++pFormats;
327 ++nTablePosition;
331 OSL_ENSURE(_rOldValue.hasValue(), "OLimitedFormats::convertFormatKeyPropertyValue: did not find the old enum value in the table!");
333 if (!bFoundIt)
334 { // somebody gave us an format which we can't translate
335 OUString sMessage ("This control supports only a very limited number of formats.");
336 throw IllegalArgumentException(sMessage, NULL, 2);
339 return bModified;
342 return sal_False;
345 //---------------------------------------------------------------------
346 void OLimitedFormats::setFormatKeyPropertyValue( const Any& _rNewValue )
348 OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::setFormatKeyPropertyValue: not initialized!");
350 if (m_xAggregate.is())
351 { // this is to be called after convertFormatKeyPropertyValue, where
352 // we translated the format key into a enum value.
353 // So now we can simply forward this enum value to our aggreate
354 m_xAggregate->setFastPropertyValue(m_nFormatEnumPropertyHandle, _rNewValue);
358 //---------------------------------------------------------------------
359 void OLimitedFormats::acquireSupplier(const Reference< XComponentContext >& _rxContext)
361 ::osl::MutexGuard aGuard(s_aMutex);
362 if (1 == ++s_nInstanceCount)
363 { // create the standard formatter
364 s_xStandardFormats = NumberFormatsSupplier::createWithLocale(_rxContext, getLocale(ltEnglishUS));
368 //---------------------------------------------------------------------
369 void OLimitedFormats::releaseSupplier()
371 ::osl::MutexGuard aGuard(s_aMutex);
372 if (0 == --s_nInstanceCount)
374 ::comphelper::disposeComponent(s_xStandardFormats);
375 s_xStandardFormats = NULL;
377 clearTable(FormComponentType::TIMEFIELD);
378 clearTable(FormComponentType::DATEFIELD);
382 //.........................................................................
383 } // namespace frm
384 //.........................................................................
386 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */