Line data Source code
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 "Filter.hxx"
21 : #include "FormComponent.hxx"
22 : #include "frm_module.hxx"
23 : #include "frm_resource.hrc"
24 : #include "frm_resource.hxx"
25 : #include "property.hrc"
26 : #include "property.hxx"
27 :
28 : #include <com/sun/star/awt/VclWindowPeerAttribute.hpp>
29 : #include <com/sun/star/awt/XCheckBox.hpp>
30 : #include <com/sun/star/awt/XComboBox.hpp>
31 : #include <com/sun/star/awt/XListBox.hpp>
32 : #include <com/sun/star/awt/XRadioButton.hpp>
33 : #include <com/sun/star/awt/XVclWindowPeer.hpp>
34 : #include <com/sun/star/beans/NamedValue.hpp>
35 : #include <com/sun/star/container/XChild.hpp>
36 : #include <com/sun/star/container/XIndexAccess.hpp>
37 : #include <com/sun/star/container/XNamed.hpp>
38 : #include <com/sun/star/form/FormComponentType.hpp>
39 : #include <com/sun/star/sdb/BooleanComparisonMode.hpp>
40 : #include <com/sun/star/sdb/ErrorMessageDialog.hpp>
41 : #include <com/sun/star/sdb/XColumn.hpp>
42 : #include <com/sun/star/sdb/XSQLQueryComposerFactory.hpp>
43 : #include <com/sun/star/sdbc/DataType.hpp>
44 : #include <com/sun/star/sdbc/XRowSet.hpp>
45 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
46 : #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
47 : #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
48 : #include <com/sun/star/util/NumberFormatter.hpp>
49 : #include <com/sun/star/awt/XItemList.hpp>
50 :
51 : #include <comphelper/numbers.hxx>
52 : #include <comphelper/processfactory.hxx>
53 : #include <comphelper/property.hxx>
54 : #include <cppuhelper/supportsservice.hxx>
55 : #include <connectivity/dbconversion.hxx>
56 : #include <connectivity/dbtools.hxx>
57 : #include <connectivity/formattedcolumnvalue.hxx>
58 : #include <connectivity/predicateinput.hxx>
59 : #include <rtl/ustrbuf.hxx>
60 : #include <toolkit/helper/vclunohelper.hxx>
61 : #include <tools/diagnose_ex.h>
62 : #include <unotools/localedatawrapper.hxx>
63 : #include <vcl/stdtext.hxx>
64 : #include <vcl/svapp.hxx>
65 : #include <tools/wintypes.hxx>
66 :
67 :
68 17 : extern "C" void SAL_CALL createRegistryInfo_OFilterControl()
69 : {
70 17 : static ::frm::OMultiInstanceAutoRegistration< ::frm::OFilterControl > aAutoRegistration;
71 17 : }
72 :
73 :
74 : namespace frm
75 : {
76 :
77 :
78 : using namespace ::com::sun::star::uno;
79 : using namespace ::com::sun::star::awt;
80 : using namespace ::com::sun::star::lang;
81 : using namespace ::com::sun::star::beans;
82 : using namespace ::com::sun::star::sdb;
83 : using namespace ::com::sun::star::sdbc;
84 : using namespace ::com::sun::star::sdbcx;
85 : using namespace ::com::sun::star::util;
86 : using namespace ::com::sun::star::form;
87 : using namespace ::com::sun::star::container;
88 : using namespace ::com::sun::star::ui::dialogs;
89 :
90 : using namespace ::connectivity;
91 :
92 :
93 : // OFilterControl
94 :
95 :
96 0 : OFilterControl::OFilterControl( const Reference< XComponentContext >& _rxORB )
97 : :UnoControl()
98 : ,m_aTextListeners( *this )
99 : ,m_xContext( _rxORB )
100 : ,m_aParser( _rxORB )
101 : ,m_nControlClass( FormComponentType::TEXTFIELD )
102 : ,m_bFilterList( sal_False )
103 : ,m_bMultiLine( sal_False )
104 0 : ,m_bFilterListFilled( sal_False )
105 : {
106 0 : }
107 :
108 :
109 0 : sal_Bool OFilterControl::ensureInitialized( )
110 : {
111 0 : if ( !m_xField.is() )
112 : {
113 : OSL_FAIL( "OFilterControl::ensureInitialized: improperly initialized: no field!" );
114 0 : return sal_False;
115 : }
116 :
117 0 : if ( !m_xConnection.is() )
118 : {
119 : OSL_FAIL( "OFilterControl::ensureInitialized: improperly initialized: no connection!" );
120 0 : return sal_False;
121 : }
122 :
123 0 : if ( !m_xFormatter.is() )
124 : {
125 : // we can create one from the connection, if it's an SDB connection
126 :
127 0 : Reference< XNumberFormatsSupplier > xFormatSupplier = ::dbtools::getNumberFormats( m_xConnection, true, m_xContext );
128 :
129 0 : if ( xFormatSupplier.is() )
130 : {
131 0 : m_xFormatter.set(NumberFormatter::create(m_xContext), UNO_QUERY_THROW );
132 0 : m_xFormatter->attachNumberFormatsSupplier( xFormatSupplier );
133 0 : }
134 : }
135 0 : if ( !m_xFormatter.is() )
136 : {
137 : OSL_FAIL( "OFilterControl::ensureInitialized: no number formatter!" );
138 : // no fallback anymore
139 0 : return sal_False;
140 : }
141 :
142 0 : return sal_True;
143 : }
144 :
145 :
146 0 : Any SAL_CALL OFilterControl::queryAggregation( const Type & rType ) throw(RuntimeException, std::exception)
147 : {
148 0 : Any aRet = UnoControl::queryAggregation( rType);
149 0 : if(!aRet.hasValue())
150 0 : aRet = OFilterControl_BASE::queryInterface(rType);
151 :
152 0 : return aRet;
153 : }
154 :
155 :
156 0 : OUString OFilterControl::GetComponentServiceName()
157 : {
158 0 : OUString aServiceName;
159 0 : switch (m_nControlClass)
160 : {
161 : case FormComponentType::RADIOBUTTON:
162 0 : aServiceName = "radiobutton";
163 0 : break;
164 : case FormComponentType::CHECKBOX:
165 0 : aServiceName = "checkbox";
166 0 : break;
167 : case FormComponentType::COMBOBOX:
168 0 : aServiceName = "combobox";
169 0 : break;
170 : case FormComponentType::LISTBOX:
171 0 : aServiceName = "listbox";
172 0 : break;
173 : default:
174 0 : if (m_bMultiLine)
175 0 : aServiceName = "MultiLineEdit";
176 : else
177 0 : aServiceName = "Edit";
178 : }
179 0 : return aServiceName;
180 : }
181 :
182 : // XComponent
183 :
184 0 : void OFilterControl::dispose() throw( RuntimeException, std::exception )
185 : {
186 0 : EventObject aEvt(*this);
187 0 : m_aTextListeners.disposeAndClear( aEvt );
188 0 : UnoControl::dispose();
189 0 : }
190 :
191 :
192 0 : void OFilterControl::createPeer( const Reference< XToolkit > & rxToolkit, const Reference< XWindowPeer > & rParentPeer ) throw(RuntimeException, std::exception)
193 : {
194 0 : UnoControl::createPeer( rxToolkit, rParentPeer );
195 :
196 : try
197 : {
198 0 : Reference< XVclWindowPeer > xVclWindow( getPeer(), UNO_QUERY_THROW );
199 0 : switch ( m_nControlClass )
200 : {
201 : case FormComponentType::CHECKBOX:
202 : {
203 : // checkboxes always have a tristate-mode
204 0 : xVclWindow->setProperty( PROPERTY_TRISTATE, makeAny( sal_Bool( sal_True ) ) );
205 0 : xVclWindow->setProperty( PROPERTY_STATE, makeAny( sal_Int32( TRISTATE_INDET ) ) );
206 :
207 0 : Reference< XCheckBox > xBox( getPeer(), UNO_QUERY_THROW );
208 0 : xBox->addItemListener( this );
209 :
210 : }
211 0 : break;
212 :
213 : case FormComponentType::RADIOBUTTON:
214 : {
215 0 : xVclWindow->setProperty( PROPERTY_STATE, makeAny( sal_Int32( TRISTATE_FALSE ) ) );
216 :
217 0 : Reference< XRadioButton > xRadio( getPeer(), UNO_QUERY_THROW );
218 0 : xRadio->addItemListener( this );
219 : }
220 0 : break;
221 :
222 : case FormComponentType::LISTBOX:
223 : {
224 0 : Reference< XListBox > xListBox( getPeer(), UNO_QUERY_THROW );
225 0 : xListBox->addItemListener( this );
226 : }
227 : // no break
228 :
229 : case FormComponentType::COMBOBOX:
230 : {
231 0 : xVclWindow->setProperty(PROPERTY_AUTOCOMPLETE, makeAny( sal_Bool( sal_True ) ) );
232 : }
233 : // no break
234 :
235 : default:
236 : {
237 0 : Reference< XWindow > xWindow( getPeer(), UNO_QUERY );
238 0 : xWindow->addFocusListener( this );
239 :
240 0 : Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
241 0 : if (xText.is())
242 0 : xText->setMaxTextLen(0);
243 : }
244 0 : break;
245 : }
246 :
247 0 : OControl::initFormControlPeer( getPeer() );
248 :
249 : // filter controls are _never_ readonly
250 0 : Reference< XPropertySet > xModel( getModel(), UNO_QUERY_THROW );
251 0 : Reference< XPropertySetInfo > xModelPSI( xModel->getPropertySetInfo(), UNO_SET_THROW );
252 0 : if ( xModelPSI->hasPropertyByName( PROPERTY_READONLY ) )
253 0 : xVclWindow->setProperty( PROPERTY_READONLY, makeAny( sal_Bool( sal_False ) ) );
254 : }
255 0 : catch( const Exception& )
256 : {
257 : DBG_UNHANDLED_EXCEPTION();
258 : }
259 :
260 0 : if (m_bFilterList)
261 0 : m_bFilterListFilled = sal_False;
262 0 : }
263 :
264 :
265 0 : void OFilterControl::PrepareWindowDescriptor( WindowDescriptor& rDescr )
266 : {
267 0 : if (m_bFilterList)
268 0 : rDescr.WindowAttributes |= VclWindowPeerAttribute::DROPDOWN;
269 0 : }
270 :
271 :
272 0 : void OFilterControl::ImplSetPeerProperty( const OUString& rPropName, const Any& rVal )
273 : {
274 : // these properties are ignored
275 0 : if (rPropName == PROPERTY_TEXT ||
276 0 : rPropName == PROPERTY_STATE)
277 0 : return;
278 :
279 0 : UnoControl::ImplSetPeerProperty( rPropName, rVal );
280 : }
281 :
282 : // XEventListener
283 :
284 0 : void SAL_CALL OFilterControl::disposing(const EventObject& Source) throw( RuntimeException, std::exception )
285 : {
286 0 : UnoControl::disposing(Source);
287 0 : }
288 :
289 : // XItemListener
290 :
291 0 : void SAL_CALL OFilterControl::itemStateChanged( const ItemEvent& rEvent ) throw(RuntimeException, std::exception)
292 : {
293 0 : OUStringBuffer aText;
294 0 : switch (m_nControlClass)
295 : {
296 : case FormComponentType::CHECKBOX:
297 : {
298 0 : if ( ( rEvent.Selected == TRISTATE_TRUE ) || ( rEvent.Selected == TRISTATE_FALSE ) )
299 : {
300 0 : sal_Int32 nBooleanComparisonMode = ::dbtools::DatabaseMetaData( m_xConnection ).getBooleanComparisonMode();
301 :
302 0 : bool bSelected = ( rEvent.Selected == TRISTATE_TRUE );
303 :
304 0 : OUString sExpressionMarker( "$expression$" );
305 : ::dbtools::getBooleanComparisonPredicate(
306 : sExpressionMarker,
307 : bSelected,
308 : nBooleanComparisonMode,
309 : aText
310 0 : );
311 :
312 0 : OUString sText( aText.makeStringAndClear() );
313 0 : sal_Int32 nMarkerPos( sText.indexOf( sExpressionMarker ) );
314 : OSL_ENSURE( nMarkerPos == 0, "OFilterControl::itemStateChanged: unsupported boolean comparison mode!" );
315 : // If this assertion fails, then getBoleanComparisonPredicate created a predicate which
316 : // does not start with the expression we gave it. The only known case is when
317 : // the comparison mode is ACCESS_COMPAT, and the value is TRUE. In this case,
318 : // the expression is rather complex.
319 : // Well, so this is a known issue - the filter controls (and thus the form based filter)
320 : // do not work with boolean MS Access fields.
321 : // To fix this, we would probably have to revert here to always return "1" or "0" as normalized
322 : // filter, and change our client code to properly translate this (which could be some effort).
323 0 : if ( nMarkerPos == 0 )
324 0 : aText.append( sText.copy( sExpressionMarker.getLength() ) );
325 : else
326 : {
327 : // fallback
328 0 : aText.appendAscii( bSelected ? "1" : "0" );
329 0 : }
330 : }
331 : }
332 0 : break;
333 :
334 : case FormComponentType::LISTBOX:
335 : {
336 : try
337 : {
338 0 : const Reference< XItemList > xItemList( getModel(), UNO_QUERY_THROW );
339 0 : OUString sItemText( xItemList->getItemText( rEvent.Selected ) );
340 :
341 0 : const MapString2String::const_iterator itemPos = m_aDisplayItemToValueItem.find( sItemText );
342 0 : if ( itemPos != m_aDisplayItemToValueItem.end() )
343 : {
344 0 : sItemText = itemPos->second;
345 0 : if ( !sItemText.isEmpty() )
346 : {
347 0 : ::dbtools::OPredicateInputController aPredicateInput( m_xContext, m_xConnection, getParseContext() );
348 0 : OUString sErrorMessage;
349 0 : OSL_VERIFY( aPredicateInput.normalizePredicateString( sItemText, m_xField, &sErrorMessage ) );
350 : }
351 : }
352 0 : aText.append( sItemText );
353 : }
354 0 : catch( const Exception& )
355 : {
356 : DBG_UNHANDLED_EXCEPTION();
357 : }
358 : }
359 0 : break;
360 :
361 : case FormComponentType::RADIOBUTTON:
362 : {
363 0 : if ( rEvent.Selected == TRISTATE_TRUE )
364 0 : aText.append( ::comphelper::getString( Reference< XPropertySet >( getModel(), UNO_QUERY )->getPropertyValue( PROPERTY_REFVALUE ) ) );
365 : }
366 0 : break;
367 : }
368 :
369 0 : OUString sText( aText.makeStringAndClear() );
370 0 : if ( m_aText.compareTo( sText ) )
371 : {
372 0 : m_aText = sText;
373 0 : TextEvent aEvt;
374 0 : aEvt.Source = *this;
375 0 : ::cppu::OInterfaceIteratorHelper aIt( m_aTextListeners );
376 0 : while( aIt.hasMoreElements() )
377 0 : ((XTextListener *)aIt.next())->textChanged( aEvt );
378 0 : }
379 0 : }
380 :
381 :
382 0 : void OFilterControl::implInitFilterList()
383 : {
384 0 : if ( !ensureInitialized( ) )
385 : // already asserted in ensureInitialized
386 0 : return;
387 :
388 : // ensure the cursor and the statement are disposed as soon as we leave
389 0 : ::utl::SharedUNOComponent< XResultSet > xListCursor;
390 0 : ::utl::SharedUNOComponent< XStatement > xStatement;
391 :
392 : try
393 : {
394 0 : m_bFilterListFilled = sal_True;
395 :
396 0 : if ( !m_xField.is() )
397 0 : return;
398 :
399 0 : OUString sFieldName;
400 0 : m_xField->getPropertyValue( PROPERTY_NAME ) >>= sFieldName;
401 :
402 : // here we need a table to which the field belongs to
403 0 : const Reference< XChild > xModelAsChild( getModel(), UNO_QUERY_THROW );
404 0 : const Reference< XRowSet > xForm( xModelAsChild->getParent(), UNO_QUERY_THROW );
405 0 : const Reference< XPropertySet > xFormProps( xForm, UNO_QUERY_THROW );
406 :
407 : // create a query composer
408 0 : Reference< XColumnsSupplier > xSuppColumns;
409 0 : xFormProps->getPropertyValue("SingleSelectQueryComposer") >>= xSuppColumns;
410 :
411 0 : const Reference< XConnection > xConnection( ::dbtools::getConnection( xForm ), UNO_SET_THROW );
412 0 : const Reference< XNameAccess > xFieldNames( xSuppColumns->getColumns(), UNO_SET_THROW );
413 0 : if ( !xFieldNames->hasByName( sFieldName ) )
414 0 : return;
415 0 : OUString sRealFieldName, sTableName;
416 0 : const Reference< XPropertySet > xComposerFieldProps( xFieldNames->getByName( sFieldName ), UNO_QUERY_THROW );
417 0 : xComposerFieldProps->getPropertyValue( PROPERTY_REALNAME ) >>= sRealFieldName;
418 0 : xComposerFieldProps->getPropertyValue( PROPERTY_TABLENAME ) >>= sTableName;
419 :
420 : // obtain the table of the field
421 0 : const Reference< XTablesSupplier > xSuppTables( xSuppColumns, UNO_QUERY_THROW );
422 0 : const Reference< XNameAccess > xTablesNames( xSuppTables->getTables(), UNO_SET_THROW );
423 0 : const Reference< XNamed > xNamedTable( xTablesNames->getByName( sTableName ), UNO_QUERY_THROW );
424 0 : sTableName = xNamedTable->getName();
425 :
426 : // create a statement selecting all values for the given field
427 0 : OUStringBuffer aStatement;
428 :
429 0 : const Reference< XDatabaseMetaData > xMeta( xConnection->getMetaData(), UNO_SET_THROW );
430 0 : const OUString sQuoteChar = xMeta->getIdentifierQuoteString();
431 :
432 0 : aStatement.appendAscii( "SELECT DISTINCT " );
433 0 : aStatement.append( sQuoteChar );
434 0 : aStatement.append( sRealFieldName );
435 0 : aStatement.append( sQuoteChar );
436 :
437 : // if the field had an alias in our form's statement, give it this alias in the new statement, too
438 0 : if ( !sFieldName.isEmpty() && ( sFieldName != sRealFieldName ) )
439 : {
440 0 : aStatement.appendAscii(" AS ");
441 0 : aStatement.append( sQuoteChar );
442 0 : aStatement.append( sFieldName );
443 0 : aStatement.append( sQuoteChar );
444 : }
445 :
446 0 : aStatement.appendAscii( " FROM " );
447 :
448 0 : OUString sCatalog, sSchema, sTable;
449 0 : ::dbtools::qualifiedNameComponents( xMeta, sTableName, sCatalog, sSchema, sTable, ::dbtools::eInDataManipulation );
450 0 : aStatement.append( ::dbtools::composeTableNameForSelect( xConnection, sCatalog, sSchema, sTable ) );
451 :
452 : // execute the statement
453 0 : xStatement.reset( xConnection->createStatement() );
454 0 : const OUString sSelectStatement( aStatement.makeStringAndClear( ) );
455 0 : xListCursor.reset( xStatement->executeQuery( sSelectStatement ) );
456 :
457 : // retrieve the one column which we take the values from
458 0 : const Reference< XColumnsSupplier > xSupplyCols( xListCursor, UNO_QUERY_THROW );
459 0 : const Reference< XIndexAccess > xFields( xSupplyCols->getColumns(), UNO_QUERY_THROW );
460 0 : const Reference< XPropertySet > xDataField( xFields->getByIndex(0), UNO_QUERY_THROW );
461 :
462 : // ensure the values will be formatted according to the field format
463 0 : const ::dbtools::FormattedColumnValue aFormatter( m_xFormatter, xDataField );
464 :
465 0 : ::std::vector< OUString > aProposals;
466 0 : aProposals.reserve(16);
467 :
468 0 : while ( xListCursor->next() && ( aProposals.size() < size_t( SHRT_MAX ) ) )
469 : {
470 0 : const OUString sCurrentValue = aFormatter.getFormattedValue();
471 0 : aProposals.push_back( sCurrentValue );
472 0 : }
473 :
474 : // fill the list items into our peer
475 0 : Sequence< OUString> aStringSeq( aProposals.size() );
476 0 : ::std::copy( aProposals.begin(), aProposals.end(), aStringSeq.getArray() );
477 :
478 0 : const Reference< XComboBox > xComboBox( getPeer(), UNO_QUERY_THROW );
479 0 : xComboBox->addItems( aStringSeq, 0 );
480 :
481 : // set the drop down line count to something reasonable
482 0 : const sal_Int16 nLineCount = ::std::min( sal_Int16( 16 ), sal_Int16( aStringSeq.getLength() ) );
483 0 : xComboBox->setDropDownLineCount( nLineCount );
484 : }
485 0 : catch( const Exception& )
486 : {
487 : DBG_UNHANDLED_EXCEPTION();
488 0 : }
489 : }
490 :
491 : // XFocusListener
492 :
493 0 : void SAL_CALL OFilterControl::focusGained(const FocusEvent& /*e*/) throw( RuntimeException, std::exception )
494 : {
495 : // should we fill the combobox?
496 0 : if (m_bFilterList && !m_bFilterListFilled)
497 0 : implInitFilterList();
498 0 : }
499 :
500 :
501 0 : void SAL_CALL OFilterControl::focusLost(const FocusEvent& /*e*/) throw( RuntimeException, std::exception )
502 : {
503 0 : }
504 :
505 :
506 0 : sal_Bool SAL_CALL OFilterControl::commit() throw(RuntimeException, std::exception)
507 : {
508 0 : if ( !ensureInitialized( ) )
509 : // already asserted in ensureInitialized
510 0 : return sal_True;
511 :
512 0 : OUString aText;
513 0 : switch (m_nControlClass)
514 : {
515 : case FormComponentType::TEXTFIELD:
516 : case FormComponentType::COMBOBOX:
517 : {
518 0 : Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
519 0 : if (xText.is())
520 0 : aText = xText->getText();
521 0 : } break;
522 : default:
523 0 : return sal_True;
524 : }
525 0 : if (m_aText.compareTo(aText))
526 : {
527 : // check the text with the SQL-Parser
528 0 : OUString aNewText(aText);
529 0 : aNewText = aNewText.trim();
530 0 : if ( !aNewText.isEmpty() )
531 : {
532 0 : ::dbtools::OPredicateInputController aPredicateInput( m_xContext, m_xConnection, getParseContext() );
533 0 : OUString sErrorMessage;
534 0 : if ( !aPredicateInput.normalizePredicateString( aNewText, m_xField, &sErrorMessage ) )
535 : {
536 : // display the error and outta here
537 0 : SQLContext aError;
538 0 : aError.Message = FRM_RES_STRING( RID_STR_SYNTAXERROR );
539 0 : aError.Details = sErrorMessage;
540 0 : displayException( aError );
541 0 : return sal_False;
542 0 : }
543 : }
544 :
545 0 : setText(aNewText);
546 0 : TextEvent aEvt;
547 0 : aEvt.Source = *this;
548 0 : ::cppu::OInterfaceIteratorHelper aIt( m_aTextListeners );
549 0 : while( aIt.hasMoreElements() )
550 0 : static_cast< XTextListener* >( aIt.next() )->textChanged( aEvt );
551 : }
552 0 : return sal_True;
553 : }
554 :
555 : // XTextComponent
556 :
557 0 : void SAL_CALL OFilterControl::addTextListener(const Reference< XTextListener > & l) throw(RuntimeException, std::exception)
558 : {
559 0 : m_aTextListeners.addInterface( l );
560 0 : }
561 :
562 :
563 0 : void SAL_CALL OFilterControl::removeTextListener(const Reference< XTextListener > & l) throw(RuntimeException, std::exception)
564 : {
565 0 : m_aTextListeners.removeInterface( l );
566 0 : }
567 :
568 :
569 0 : void SAL_CALL OFilterControl::setText( const OUString& aText ) throw(RuntimeException, std::exception)
570 : {
571 0 : if ( !ensureInitialized( ) )
572 : // already asserted in ensureInitialized
573 0 : return;
574 :
575 0 : switch (m_nControlClass)
576 : {
577 : case FormComponentType::CHECKBOX:
578 : {
579 0 : Reference< XVclWindowPeer > xVclWindow( getPeer(), UNO_QUERY );
580 0 : if (xVclWindow.is())
581 : {
582 0 : Any aValue;
583 0 : if ( aText == "1"
584 0 : || aText.equalsIgnoreAsciiCase("TRUE")
585 0 : || aText.equalsIgnoreAsciiCase("IS TRUE")
586 : )
587 : {
588 0 : aValue <<= (sal_Int32)TRISTATE_TRUE;
589 : }
590 0 : else if ( aText == "0" || aText.equalsIgnoreAsciiCase("FALSE") )
591 : {
592 0 : aValue <<= (sal_Int32)TRISTATE_FALSE;
593 : }
594 : else
595 0 : aValue <<= (sal_Int32)TRISTATE_INDET;
596 :
597 0 : m_aText = aText;
598 0 : xVclWindow->setProperty( PROPERTY_STATE, aValue );
599 0 : }
600 0 : } break;
601 : case FormComponentType::RADIOBUTTON:
602 : {
603 0 : Reference< XVclWindowPeer > xVclWindow( getPeer(), UNO_QUERY );
604 0 : if (xVclWindow.is())
605 : {
606 0 : OUString aRefText = ::comphelper::getString(com::sun::star::uno::Reference< XPropertySet > (getModel(), UNO_QUERY)->getPropertyValue(PROPERTY_REFVALUE));
607 0 : Any aValue;
608 0 : if (aText == aRefText)
609 0 : aValue <<= (sal_Int32)TRISTATE_TRUE;
610 : else
611 0 : aValue <<= (sal_Int32)TRISTATE_FALSE;
612 0 : m_aText = aText;
613 0 : xVclWindow->setProperty(PROPERTY_STATE, aValue);
614 0 : }
615 0 : } break;
616 : case FormComponentType::LISTBOX:
617 : {
618 0 : Reference< XListBox > xListBox( getPeer(), UNO_QUERY );
619 0 : if (xListBox.is())
620 : {
621 0 : m_aText = aText;
622 0 : MapString2String::const_iterator itemPos = m_aDisplayItemToValueItem.find( m_aText );
623 0 : if ( itemPos == m_aDisplayItemToValueItem.end() )
624 : {
625 0 : const bool isQuoted = ( m_aText.getLength() > 1 )
626 0 : && ( m_aText[0] == '\'' )
627 0 : && ( m_aText[ m_aText.getLength() - 1 ] == '\'' );
628 0 : if ( isQuoted )
629 : {
630 0 : m_aText = m_aText.copy( 1, m_aText.getLength() - 2 );
631 0 : itemPos = m_aDisplayItemToValueItem.find( m_aText );
632 : }
633 : }
634 :
635 : OSL_ENSURE( ( itemPos != m_aDisplayItemToValueItem.end() ) || m_aText.isEmpty(),
636 : "OFilterControl::setText: this text is not in my display list!" );
637 0 : if ( itemPos == m_aDisplayItemToValueItem.end() )
638 0 : m_aText = "";
639 :
640 0 : if ( m_aText.isEmpty() )
641 : {
642 0 : while ( xListBox->getSelectedItemPos() >= 0 )
643 : {
644 0 : xListBox->selectItemPos( xListBox->getSelectedItemPos(), sal_False );
645 : }
646 : }
647 : else
648 : {
649 0 : xListBox->selectItem( m_aText, sal_True );
650 : }
651 0 : }
652 : }
653 0 : break;
654 :
655 : default:
656 : {
657 0 : Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
658 0 : if (xText.is())
659 : {
660 0 : m_aText = aText;
661 0 : xText->setText(aText);
662 0 : }
663 : }
664 : }
665 : }
666 :
667 :
668 0 : void SAL_CALL OFilterControl::insertText( const ::com::sun::star::awt::Selection& rSel, const OUString& aText ) throw(::com::sun::star::uno::RuntimeException, std::exception)
669 : {
670 0 : Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
671 0 : if (xText.is())
672 : {
673 0 : xText->insertText(rSel, aText);
674 0 : m_aText = xText->getText();
675 0 : }
676 0 : }
677 :
678 :
679 0 : OUString SAL_CALL OFilterControl::getText() throw(RuntimeException, std::exception)
680 : {
681 0 : return m_aText;
682 : }
683 :
684 :
685 0 : OUString SAL_CALL OFilterControl::getSelectedText( void ) throw(RuntimeException, std::exception)
686 : {
687 0 : OUString aSelected;
688 0 : Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
689 0 : if (xText.is())
690 0 : aSelected = xText->getSelectedText();
691 :
692 0 : return aSelected;
693 : }
694 :
695 :
696 0 : void SAL_CALL OFilterControl::setSelection( const ::com::sun::star::awt::Selection& aSelection ) throw(::com::sun::star::uno::RuntimeException, std::exception)
697 : {
698 0 : Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
699 0 : if (xText.is())
700 0 : xText->setSelection( aSelection );
701 0 : }
702 :
703 :
704 0 : ::com::sun::star::awt::Selection SAL_CALL OFilterControl::getSelection( void ) throw(::com::sun::star::uno::RuntimeException, std::exception)
705 : {
706 0 : ::com::sun::star::awt::Selection aSel;
707 0 : Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
708 0 : if (xText.is())
709 0 : aSel = xText->getSelection();
710 0 : return aSel;
711 : }
712 :
713 :
714 0 : sal_Bool SAL_CALL OFilterControl::isEditable( void ) throw(RuntimeException, std::exception)
715 : {
716 0 : Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
717 0 : return xText.is() && xText->isEditable();
718 : }
719 :
720 :
721 0 : void SAL_CALL OFilterControl::setEditable( sal_Bool bEditable ) throw(RuntimeException, std::exception)
722 : {
723 0 : Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
724 0 : if (xText.is())
725 0 : xText->setEditable(bEditable);
726 0 : }
727 :
728 :
729 0 : sal_Int16 SAL_CALL OFilterControl::getMaxTextLen() throw(RuntimeException, std::exception)
730 : {
731 0 : Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
732 0 : return xText.is() ? xText->getMaxTextLen() : 0;
733 : }
734 :
735 :
736 0 : void SAL_CALL OFilterControl::setMaxTextLen( sal_Int16 nLength ) throw(RuntimeException, std::exception)
737 : {
738 0 : Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
739 0 : if (xText.is())
740 0 : xText->setMaxTextLen(nLength);
741 0 : }
742 :
743 :
744 0 : void OFilterControl::displayException( const ::com::sun::star::sdb::SQLContext& _rExcept )
745 : {
746 : try
747 : {
748 0 : Reference< XExecutableDialog > xErrorDialog = ErrorMessageDialog::create( m_xContext, "", m_xMessageParent, makeAny(_rExcept));
749 0 : xErrorDialog->execute();
750 : }
751 0 : catch( const Exception& )
752 : {
753 : DBG_UNHANDLED_EXCEPTION();
754 : }
755 0 : }
756 :
757 :
758 0 : void SAL_CALL OFilterControl::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException, std::exception)
759 : {
760 0 : const Any* pArguments = aArguments.getConstArray();
761 0 : const Any* pArgumentsEnd = pArguments + aArguments.getLength();
762 :
763 0 : PropertyValue aProp;
764 0 : NamedValue aValue;
765 0 : const OUString* pName = NULL;
766 0 : const Any* pValue = NULL;
767 0 : Reference< XPropertySet > xControlModel;
768 :
769 0 : if (aArguments.getLength() == 3
770 0 : && (aArguments[0] >>= m_xMessageParent)
771 0 : && (aArguments[1] >>= m_xFormatter)
772 0 : && (aArguments[2] >>= xControlModel))
773 : {
774 0 : initControlModel(xControlModel);
775 : }
776 0 : else for ( ; pArguments != pArgumentsEnd; ++pArguments )
777 : {
778 : // we recognize PropertyValues and NamedValues
779 0 : if ( *pArguments >>= aProp )
780 : {
781 0 : pName = &aProp.Name;
782 0 : pValue = &aProp.Value;
783 : }
784 0 : else if ( *pArguments >>= aValue )
785 : {
786 0 : pName = &aValue.Name;
787 0 : pValue = &aValue.Value;
788 : }
789 : else
790 : {
791 : OSL_FAIL( "OFilterControl::initialize: unrecognized argument!" );
792 0 : continue;
793 : }
794 :
795 0 : if ( pName->equalsAscii( "MessageParent" ) )
796 : {
797 : // the message parent
798 0 : *pValue >>= m_xMessageParent;
799 : OSL_ENSURE( m_xMessageParent.is(), "OFilterControl::initialize: invalid MessageParent!" );
800 : }
801 0 : else if ( pName->equalsAscii( "NumberFormatter" ) )
802 : {
803 : // the number format. This argument is optional.
804 0 : *pValue >>= m_xFormatter;
805 : OSL_ENSURE( m_xFormatter.is(), "OFilterControl::initialize: invalid NumberFormatter!" );
806 : }
807 0 : else if ( pName->equalsAscii( "ControlModel" ) )
808 : {
809 : // the control model for which we act as filter control
810 0 : if ( !(*pValue >>= xControlModel ) )
811 : {
812 : OSL_FAIL( "OFilterControl::initialize: invalid control model argument!" );
813 0 : continue;
814 : }
815 0 : initControlModel(xControlModel);
816 : }
817 0 : }
818 0 : }
819 :
820 0 : void OFilterControl::initControlModel(Reference< XPropertySet >& xControlModel)
821 : {
822 0 : if ( !xControlModel.is() )
823 : {
824 : OSL_FAIL( "OFilterControl::initialize: invalid control model argument!" );
825 0 : return;
826 : }
827 : // some properties which are "derived" from the control model we're working for
828 :
829 : // the field
830 0 : m_xField.clear();
831 : OSL_ENSURE( ::comphelper::hasProperty( PROPERTY_BOUNDFIELD, xControlModel ), "OFilterControl::initialize: control model needs a bound field property!" );
832 0 : xControlModel->getPropertyValue( PROPERTY_BOUNDFIELD ) >>= m_xField;
833 :
834 :
835 : // filter list and control class
836 0 : m_bFilterList = ::comphelper::hasProperty( PROPERTY_FILTERPROPOSAL, xControlModel ) && ::comphelper::getBOOL( xControlModel->getPropertyValue( PROPERTY_FILTERPROPOSAL ) );
837 0 : if ( m_bFilterList )
838 0 : m_nControlClass = FormComponentType::COMBOBOX;
839 : else
840 : {
841 0 : sal_Int16 nClassId = ::comphelper::getINT16( xControlModel->getPropertyValue( PROPERTY_CLASSID ) );
842 0 : switch (nClassId)
843 : {
844 : case FormComponentType::CHECKBOX:
845 : case FormComponentType::RADIOBUTTON:
846 : case FormComponentType::LISTBOX:
847 : case FormComponentType::COMBOBOX:
848 0 : m_nControlClass = nClassId;
849 0 : if ( FormComponentType::LISTBOX == nClassId )
850 : {
851 0 : Sequence< OUString > aDisplayItems;
852 0 : OSL_VERIFY( xControlModel->getPropertyValue( PROPERTY_STRINGITEMLIST ) >>= aDisplayItems );
853 0 : Sequence< OUString > aValueItems;
854 0 : OSL_VERIFY( xControlModel->getPropertyValue( PROPERTY_VALUE_SEQ ) >>= aValueItems );
855 : OSL_ENSURE( aDisplayItems.getLength() == aValueItems.getLength(), "OFilterControl::initialize: inconsistent item lists!" );
856 0 : for ( sal_Int32 i=0; i < ::std::min( aDisplayItems.getLength(), aValueItems.getLength() ); ++i )
857 0 : m_aDisplayItemToValueItem[ aDisplayItems[i] ] = aValueItems[i];
858 : }
859 0 : break;
860 : default:
861 0 : m_bMultiLine = ::comphelper::hasProperty( PROPERTY_MULTILINE, xControlModel ) && ::comphelper::getBOOL( xControlModel->getPropertyValue( PROPERTY_MULTILINE ) );
862 0 : m_nControlClass = FormComponentType::TEXTFIELD;
863 0 : break;
864 : }
865 : }
866 :
867 :
868 : // the connection meta data for the form which we're working for
869 0 : Reference< XChild > xModel( xControlModel, UNO_QUERY );
870 0 : Reference< XRowSet > xForm;
871 0 : if ( xModel.is() )
872 0 : xForm = xForm.query( xModel->getParent() );
873 0 : m_xConnection = ::dbtools::getConnection( xForm );
874 0 : OSL_ENSURE( m_xConnection.is(), "OFilterControl::initialize: unable to determine the form's connection!" );
875 : }
876 :
877 0 : OUString SAL_CALL OFilterControl::getImplementationName( ) throw (RuntimeException, std::exception)
878 : {
879 0 : return getImplementationName_Static();
880 : }
881 :
882 0 : sal_Bool SAL_CALL OFilterControl::supportsService( const OUString& ServiceName ) throw (RuntimeException, std::exception)
883 : {
884 0 : return cppu::supportsService(this, ServiceName);
885 : }
886 :
887 0 : Sequence< OUString > SAL_CALL OFilterControl::getSupportedServiceNames( ) throw (RuntimeException, std::exception)
888 : {
889 0 : return getSupportedServiceNames_Static();
890 : }
891 :
892 34 : OUString SAL_CALL OFilterControl::getImplementationName_Static()
893 : {
894 34 : return OUString( "com.sun.star.comp.forms.OFilterControl" );
895 : }
896 :
897 17 : Sequence< OUString > SAL_CALL OFilterControl::getSupportedServiceNames_Static()
898 : {
899 17 : Sequence< OUString > aNames( 2 );
900 17 : aNames[ 0 ] = "com.sun.star.form.control.FilterControl";
901 17 : aNames[ 1 ] = "com.sun.star.awt.UnoControl";
902 17 : return aNames;
903 : }
904 :
905 :
906 0 : Reference< XInterface > SAL_CALL OFilterControl::Create( const Reference< XMultiServiceFactory >& _rxFactory )
907 : {
908 0 : return static_cast< XServiceInfo* >( new OFilterControl( comphelper::getComponentContext(_rxFactory) ) );
909 : }
910 :
911 :
912 : } // namespace frm
913 :
914 :
915 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|