LibreOffice Module forms (master) 1
limitedformats.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <limitedformats.hxx>
21#include <osl/diagnose.h>
22#include <comphelper/types.hxx>
24#include <com/sun/star/form/FormComponentType.hpp>
25#include <com/sun/star/util/NumberFormatsSupplier.hpp>
26
27
28namespace frm
29{
30
31
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;
37
39 ::osl::Mutex OLimitedFormats::s_aMutex;
40 Reference< XNumberFormatsSupplier > OLimitedFormats::s_xStandardFormats;
41
42
43 //=
44
45 namespace {
46
47 enum LocaleType
48 {
49 ltEnglishUS,
50 ltGerman,
51 ltSystem
52 };
53
54 }
55
56 static const Locale& getLocale(LocaleType _eType)
57 {
58 static const Locale s_aEnglishUS( "en", "us", OUString() );
59 static const Locale s_aGerman( "de", "DE", OUString() );
60 static const Locale s_aSystem( "", "", "" );
61
62 switch (_eType)
63 {
64 case ltEnglishUS:
65 return s_aEnglishUS;
66
67 case ltGerman:
68 return s_aGerman;
69
70 case ltSystem:
71 return s_aSystem;
72 }
73
74 OSL_FAIL("getLocale: invalid enum value!");
75 return s_aSystem;
76 }
77
78 namespace {
79
80 struct FormatEntry
81 {
82 const char* pDescription;
83 sal_Int32 nKey;
84 LocaleType eLocale;
85 };
86
87 }
88
89 static FormatEntry* lcl_getFormatTable(sal_Int16 nTableId)
90 {
91 switch (nTableId)
92 {
93 case FormComponentType::TIMEFIELD:
94 {
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 }
101 };
102 return s_aFormats;
103 }
104 case FormComponentType::DATEFIELD:
105 {
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 },
111
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 },
118
119 { "JJ-MM-TT", -1, ltGerman },
120 { "JJJJ-MM-TT", -1, ltGerman },
121
122 { nullptr, -1, ltSystem }
123 };
124 return s_aFormats;
125 }
126 }
127
128 OSL_FAIL("lcl_getFormatTable: invalid id!");
129 return nullptr;
130 }
131
132 OLimitedFormats::OLimitedFormats(const Reference< XComponentContext >& _rxContext, const sal_Int16 _nClassId)
133 :m_nFormatEnumPropertyHandle(-1)
134 ,m_nTableId(_nClassId)
135 {
136 OSL_ENSURE(_rxContext.is(), "OLimitedFormats::OLimitedFormats: invalid service factory!");
137 acquireSupplier(_rxContext);
139 }
140
141
143 {
145 }
146
147
148 void OLimitedFormats::ensureTableInitialized(const sal_Int16 _nTableId)
149 {
150 FormatEntry* pFormatTable = lcl_getFormatTable(_nTableId);
151 if (-1 != pFormatTable->nKey)
152 return;
153
154 ::osl::MutexGuard aGuard(s_aMutex);
155 if (-1 != pFormatTable->nKey)
156 return;
157
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!");
163
164 if (!xStandardFormats.is())
165 return;
166
167 // loop through the table
168 FormatEntry* pLoopFormats = pFormatTable;
169 while (pLoopFormats->pDescription)
170 {
171 // get the key for the description
172 pLoopFormats->nKey = xStandardFormats->queryKey(
173 OUString::createFromAscii(pLoopFormats->pDescription),
174 getLocale(pLoopFormats->eLocale),
175 false
176 );
177
178 if (-1 == pLoopFormats->nKey)
179 {
180 pLoopFormats->nKey = xStandardFormats->addNew(
181 OUString::createFromAscii(pLoopFormats->pDescription),
182 getLocale(pLoopFormats->eLocale)
183 );
184#ifdef DBG_UTIL
185 try
186 {
187 xStandardFormats->getByKey(pLoopFormats->nKey);
188 }
189 catch(const Exception&)
190 {
191 OSL_FAIL("OLimitedFormats::ensureTableInitialized: adding the key to the formats collection failed!");
192 }
193#endif
194 }
195
196 // next
197 ++pLoopFormats;
198 }
199 }
200
201
202 void OLimitedFormats::clearTable(const sal_Int16 _nTableId)
203 {
204 ::osl::MutexGuard aGuard(s_aMutex);
205 FormatEntry* pFormats = lcl_getFormatTable(_nTableId);
206 FormatEntry* pResetLoop = pFormats;
207 while (pResetLoop->pDescription)
208 {
209 pResetLoop->nKey = -1;
210 ++pResetLoop;
211 }
212 }
213
214
215 void OLimitedFormats::setAggregateSet(const Reference< XFastPropertySet >& _rxAggregate, sal_Int32 _nOriginalPropertyHandle)
216 {
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!");
220
221 m_xAggregate = _rxAggregate;
222 m_nFormatEnumPropertyHandle = _nOriginalPropertyHandle;
223#ifdef DBG_UTIL
224 if (m_xAggregate.is())
225 {
226 try
227 {
228 m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
229 }
230 catch(const Exception&)
231 {
232 OSL_FAIL("OLimitedFormats::setAggregateSet: invalid handle!");
233 }
234 }
235#endif
236 }
237
238
240 {
241 _rValue.clear();
242
243 OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::getFormatKeyPropertyValue: not initialized!");
244 if (!m_xAggregate.is())
245 return;
246
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);
251
252 // get the translation table
253 const FormatEntry* pFormats = lcl_getFormatTable(m_nTableId);
254
255 // seek to the nValue'th entry
256 sal_Int32 nLookup = 0;
257 for ( ;
258 (nullptr != pFormats->pDescription) && (nLookup < nValue);
259 ++pFormats, ++nLookup
260 )
261 ;
262 OSL_ENSURE(nullptr != pFormats->pDescription, "OLimitedFormats::getFormatKeyPropertyValue: did not find the value!");
263 if (pFormats->pDescription)
264 _rValue <<= pFormats->nKey;
265
266 // TODO: should use a standard format for the control type we're working for
267 }
268
269
270 bool OLimitedFormats::convertFormatKeyPropertyValue(Any& _rConvertedValue, Any& _rOldValue, const Any& _rNewValue)
271 {
272 OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::convertFormatKeyPropertyValue: not initialized!");
273
274 if (!m_xAggregate)
275 return false;
276
277 // the new format key to set
278 sal_Int32 nNewFormat = 0;
279 if (!(_rNewValue >>= nNewFormat))
280 throw IllegalArgumentException();
281
282 // get the old (enum) value from the aggregate
283 Any aEnumPropertyValue = m_xAggregate->getFastPropertyValue(m_nFormatEnumPropertyHandle);
284 sal_Int32 nOldEnumValue = -1;
285 ::cppu::enum2int(nOldEnumValue, aEnumPropertyValue);
286
287 // get the translation table
288 const FormatEntry* pFormats = lcl_getFormatTable(m_nTableId);
289
290 _rOldValue.clear();
291 _rConvertedValue.clear();
292
293 // look for the entry with the given format key
294 sal_Int32 nTablePosition = 0;
295 for ( ;
296 (nullptr != pFormats->pDescription) && (nNewFormat != pFormats->nKey);
297 ++pFormats, ++nTablePosition
298 )
299 {
300 if (nTablePosition == nOldEnumValue)
301 _rOldValue <<= pFormats->nKey;
302 }
303
304 bool bFoundIt = (nullptr != pFormats->pDescription);
305 bool bModified = false;
306 if (bFoundIt)
307 {
308 _rConvertedValue <<= static_cast<sal_Int16>(nTablePosition);
309 bModified = nTablePosition != nOldEnumValue;
310 }
311
312 if (!_rOldValue.hasValue())
313 { // did not reach the end of the table (means we found nNewFormat)
314 // -> go to the end to ensure that _rOldValue is set
315 while (pFormats->pDescription)
316 {
317 if (nTablePosition == nOldEnumValue)
318 {
319 _rOldValue <<= pFormats->nKey;
320 break;
321 }
322
323 ++pFormats;
324 ++nTablePosition;
325 }
326 }
327
328 OSL_ENSURE(_rOldValue.hasValue(), "OLimitedFormats::convertFormatKeyPropertyValue: did not find the old enum value in the table!");
329
330 if (!bFoundIt)
331 { // somebody gave us a format which we can't translate
332 throw IllegalArgumentException("This control supports only a very limited number of formats.", nullptr, 2);
333 }
334
335 return bModified;
336 }
337
338
339 void OLimitedFormats::setFormatKeyPropertyValue( const Any& _rNewValue )
340 {
341 OSL_ENSURE(m_xAggregate.is() && (-1 != m_nFormatEnumPropertyHandle), "OLimitedFormats::setFormatKeyPropertyValue: not initialized!");
342
343 if (m_xAggregate.is())
344 { // this is to be called after convertFormatKeyPropertyValue, where
345 // we translated the format key into an enum value.
346 // So now we can simply forward this enum value to our aggregate
347 m_xAggregate->setFastPropertyValue(m_nFormatEnumPropertyHandle, _rNewValue);
348 }
349 }
350
351
352 void OLimitedFormats::acquireSupplier(const Reference< XComponentContext >& _rxContext)
353 {
354 ::osl::MutexGuard aGuard(s_aMutex);
355 if (1 == ++s_nInstanceCount)
356 { // create the standard formatter
357 s_xStandardFormats = NumberFormatsSupplier::createWithLocale(_rxContext, getLocale(ltEnglishUS));
358 }
359 }
360
361
363 {
364 ::osl::MutexGuard aGuard(s_aMutex);
365 if (0 == --s_nInstanceCount)
366 {
367 ::comphelper::disposeComponent(s_xStandardFormats);
368 s_xStandardFormats = nullptr;
369
370 clearTable(FormComponentType::TIMEFIELD);
371 clearTable(FormComponentType::DATEFIELD);
372 }
373 }
374
375
376} // namespace frm
377
378
379/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::beans::XFastPropertySet > m_xAggregate
static void clearTable(const sal_Int16 _nTableId)
static sal_Int32 s_nInstanceCount
sal_Int32 m_nFormatEnumPropertyHandle
void setAggregateSet(const css::uno::Reference< css::beans::XFastPropertySet > &_rxAggregate, sal_Int32 _nOriginalPropertyHandle)
void getFormatKeyPropertyValue(css::uno::Any &_rValue) const
void setFormatKeyPropertyValue(const css::uno::Any &_rNewValue)
OLimitedFormats(const css::uno::Reference< css::uno::XComponentContext > &_rxContext, const sal_Int16 _nClassId)
ctor
bool convertFormatKeyPropertyValue(css::uno::Any &_rConvertedValue, css::uno::Any &_rOldValue, const css::uno::Any &_rNewValue)
static css::uno::Reference< css::util::XNumberFormatsSupplier > s_xStandardFormats
void acquireSupplier(const css::uno::Reference< css::uno::XComponentContext > &_rxContext)
const sal_Int16 m_nTableId
static void ensureTableInitialized(const sal_Int16 _nTableId)
sal_Int16 nValue
@ Exception
ListBox is a bit confusing / different from other form components, so here are a few notes:
Definition: BaseListBox.hxx:25
static FormatEntry * lcl_getFormatTable(sal_Int16 nTableId)
static const Locale & getLocale(LocaleType _eType)