update dev300-m58
[ooovba.git] / forms / source / component / Filter.cxx
blob1bf3e8410369468d8c3890a3072c9ee1ed27a3d2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Filter.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_forms.hxx"
35 #include "Filter.hxx"
36 #include "FormComponent.hxx"
37 #include "frm_module.hxx"
38 #include "frm_resource.hrc"
39 #include "frm_resource.hxx"
40 #include "property.hrc"
41 #include "property.hxx"
43 /** === begin UNO includes === **/
44 #include <com/sun/star/awt/VclWindowPeerAttribute.hpp>
45 #include <com/sun/star/awt/XCheckBox.hpp>
46 #include <com/sun/star/awt/XComboBox.hpp>
47 #include <com/sun/star/awt/XListBox.hpp>
48 #include <com/sun/star/awt/XRadioButton.hpp>
49 #include <com/sun/star/awt/XVclWindowPeer.hpp>
50 #include <com/sun/star/beans/NamedValue.hpp>
51 #include <com/sun/star/container/XChild.hpp>
52 #include <com/sun/star/container/XIndexAccess.hpp>
53 #include <com/sun/star/container/XNamed.hpp>
54 #include <com/sun/star/form/FormComponentType.hpp>
55 #include <com/sun/star/sdb/BooleanComparisonMode.hpp>
56 #include <com/sun/star/sdb/XColumn.hpp>
57 #include <com/sun/star/sdb/XSQLQueryComposerFactory.hpp>
58 #include <com/sun/star/sdbc/DataType.hpp>
59 #include <com/sun/star/sdbc/XRowSet.hpp>
60 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
61 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
62 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
63 /** === end UNO includes === **/
65 #include <comphelper/numbers.hxx>
66 #include <comphelper/property.hxx>
67 #include <connectivity/dbconversion.hxx>
68 #include <connectivity/dbtools.hxx>
69 #include <connectivity/predicateinput.hxx>
70 #include <rtl/ustrbuf.hxx>
71 #include <toolkit/helper/vclunohelper.hxx>
72 #include <tools/diagnose_ex.h>
73 #include <unotools/localedatawrapper.hxx>
74 #include <vcl/stdtext.hxx>
75 #include <vcl/svapp.hxx>
76 #include <vcl/wintypes.hxx>
78 //--------------------------------------------------------------------------
79 extern "C" void SAL_CALL createRegistryInfo_OFilterControl()
81 static ::frm::OMultiInstanceAutoRegistration< ::frm::OFilterControl > aAutoRegistration;
84 //.........................................................................
85 namespace frm
87 //.........................................................................
89 using namespace ::com::sun::star::uno;
90 using namespace ::com::sun::star::awt;
91 using namespace ::com::sun::star::lang;
92 using namespace ::com::sun::star::beans;
93 using namespace ::com::sun::star::sdb;
94 using namespace ::com::sun::star::sdbc;
95 using namespace ::com::sun::star::sdbcx;
96 using namespace ::com::sun::star::util;
97 using namespace ::com::sun::star::form;
98 using namespace ::com::sun::star::container;
99 using namespace ::com::sun::star::ui::dialogs;
101 using namespace ::connectivity;
103 //=====================================================================
104 // OFilterControl
105 //=====================================================================
106 //---------------------------------------------------------------------
107 OFilterControl::OFilterControl( const Reference< XMultiServiceFactory >& _rxORB )
108 :m_aTextListeners( *this )
109 ,m_xORB( _rxORB )
110 ,m_aParser( _rxORB )
111 ,m_nControlClass( FormComponentType::TEXTFIELD )
112 ,m_bFilterList( sal_False )
113 ,m_bMultiLine( sal_False )
114 ,m_bFilterListFilled( sal_False )
118 //---------------------------------------------------------------------
119 sal_Bool OFilterControl::ensureInitialized( )
121 if ( !m_xField.is() )
123 OSL_ENSURE( sal_False, "OFilterControl::ensureInitialized: improperly initialized: no field!" );
124 return sal_False;
127 if ( !m_xConnection.is() )
129 OSL_ENSURE( sal_False, "OFilterControl::ensureInitialized: improperly initialized: no connection!" );
130 return sal_False;
133 if ( !m_xFormatter.is() )
135 // we can create one from the connection, if it's an SDB connection
136 Reference< XNumberFormatsSupplier > xFormatSupplier = ::dbtools::getNumberFormats( m_xConnection, sal_True, m_xORB );
138 if ( xFormatSupplier.is() )
140 m_xFormatter = m_xFormatter.query(
141 m_xORB->createInstance( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.NumberFormatter" ) ) ) );
142 if ( m_xFormatter.is() )
143 m_xFormatter->attachNumberFormatsSupplier( xFormatSupplier );
146 if ( !m_xFormatter.is() )
148 OSL_ENSURE( sal_False, "OFilterControl::ensureInitialized: no number formatter!" );
149 // no fallback anymore
150 return sal_False;
153 return sal_True;
156 //---------------------------------------------------------------------
157 Any SAL_CALL OFilterControl::queryAggregation( const Type & rType ) throw(RuntimeException)
159 Any aRet = UnoControl::queryAggregation( rType);
160 if(!aRet.hasValue())
161 aRet = OFilterControl_BASE::queryInterface(rType);
163 return aRet;
166 //------------------------------------------------------------------
167 ::rtl::OUString OFilterControl::GetComponentServiceName()
169 ::rtl::OUString aServiceName;
170 switch (m_nControlClass)
172 case FormComponentType::RADIOBUTTON:
173 aServiceName = ::rtl::OUString::createFromAscii("radiobutton");
174 break;
175 case FormComponentType::CHECKBOX:
176 aServiceName = ::rtl::OUString::createFromAscii("checkbox");
177 break;
178 case FormComponentType::COMBOBOX:
179 aServiceName = ::rtl::OUString::createFromAscii("combobox");
180 break;
181 case FormComponentType::LISTBOX:
182 aServiceName = ::rtl::OUString::createFromAscii("listbox");
183 break;
184 default:
185 if (m_bMultiLine)
186 aServiceName = ::rtl::OUString::createFromAscii("MultiLineEdit");
187 else
188 aServiceName = ::rtl::OUString::createFromAscii("Edit");
190 return aServiceName;
193 // XComponent
194 //---------------------------------------------------------------------
195 void OFilterControl::dispose() throw( RuntimeException )
197 EventObject aEvt(*this);
198 m_aTextListeners.disposeAndClear( aEvt );
199 UnoControl::dispose();
202 //---------------------------------------------------------------------
203 void OFilterControl::createPeer( const Reference< XToolkit > & rxToolkit, const Reference< XWindowPeer > & rParentPeer ) throw(RuntimeException)
205 UnoControl::createPeer( rxToolkit, rParentPeer );
209 Reference< XVclWindowPeer > xVclWindow( getPeer(), UNO_QUERY_THROW );
210 switch ( m_nControlClass )
212 case FormComponentType::CHECKBOX:
214 // checkboxes always have a tristate-mode
215 xVclWindow->setProperty( PROPERTY_TRISTATE, makeAny( sal_Bool( sal_True ) ) );
216 xVclWindow->setProperty( PROPERTY_STATE, makeAny( sal_Int32( STATE_DONTKNOW ) ) );
218 Reference< XCheckBox > xBox( getPeer(), UNO_QUERY_THROW );
219 xBox->addItemListener( this );
222 break;
224 case FormComponentType::RADIOBUTTON:
226 xVclWindow->setProperty( PROPERTY_STATE, makeAny( sal_Int32( STATE_NOCHECK ) ) );
228 Reference< XRadioButton > xRadio( getPeer(), UNO_QUERY_THROW );
229 xRadio->addItemListener( this );
231 break;
233 case FormComponentType::LISTBOX:
235 Reference< XListBox > xListBox( getPeer(), UNO_QUERY_THROW );
236 xListBox->addItemListener( this );
238 // no break
240 case FormComponentType::COMBOBOX:
242 xVclWindow->setProperty(PROPERTY_AUTOCOMPLETE, makeAny( sal_Bool( sal_True ) ) );
244 // no break
246 default:
248 Reference< XWindow > xWindow( getPeer(), UNO_QUERY );
249 xWindow->addFocusListener( this );
251 Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
252 if (xText.is())
253 xText->setMaxTextLen(0);
255 break;
258 OControl::initFormControlPeer( getPeer() );
260 // filter controls are _never_ readonly
261 // #107013# - 2002-02-03 - fs@openoffice.org
262 Reference< XPropertySet > xModel( getModel(), UNO_QUERY_THROW );
263 Reference< XPropertySetInfo > xModelPSI( xModel->getPropertySetInfo(), UNO_SET_THROW );
264 if ( xModelPSI->hasPropertyByName( PROPERTY_READONLY ) )
265 xVclWindow->setProperty( PROPERTY_READONLY, makeAny( sal_Bool( sal_False ) ) );
267 catch( const Exception& )
269 DBG_UNHANDLED_EXCEPTION();
272 if (m_bFilterList)
273 m_bFilterListFilled = sal_False;
276 //---------------------------------------------------------------------
277 void OFilterControl::PrepareWindowDescriptor( WindowDescriptor& rDescr )
279 if (m_bFilterList)
280 rDescr.WindowAttributes |= VclWindowPeerAttribute::DROPDOWN;
283 //---------------------------------------------------------------------
284 void OFilterControl::ImplSetPeerProperty( const ::rtl::OUString& rPropName, const Any& rVal )
286 // these properties are ignored
287 if (rPropName == PROPERTY_TEXT ||
288 rPropName == PROPERTY_STATE)
289 return;
291 UnoControl::ImplSetPeerProperty( rPropName, rVal );
294 // XEventListener
295 //---------------------------------------------------------------------
296 void SAL_CALL OFilterControl::disposing(const EventObject& Source) throw( RuntimeException )
298 UnoControl::disposing(Source);
301 // XItemListener
302 //---------------------------------------------------------------------
303 void SAL_CALL OFilterControl::itemStateChanged( const ItemEvent& rEvent ) throw(RuntimeException)
305 ::rtl::OUStringBuffer aText;
306 switch (m_nControlClass)
308 case FormComponentType::CHECKBOX:
310 if ( ( rEvent.Selected == STATE_CHECK ) || ( rEvent.Selected == STATE_NOCHECK ) )
312 sal_Int32 nBooleanComparisonMode = ::dbtools::DatabaseMetaData( m_xConnection ).getBooleanComparisonMode();
314 bool bSelected = ( rEvent.Selected == STATE_CHECK );
316 ::rtl::OUString sExpressionMarker( RTL_CONSTASCII_USTRINGPARAM( "$expression$" ) );
317 ::dbtools::getBoleanComparisonPredicate(
318 sExpressionMarker,
319 bSelected,
320 nBooleanComparisonMode,
321 aText
324 ::rtl::OUString sText( aText.makeStringAndClear() );
325 sal_Int32 nMarkerPos( sText.indexOf( sExpressionMarker ) );
326 OSL_ENSURE( nMarkerPos == 0, "OFilterControl::itemStateChanged: unsupported boolean comparison mode!" );
327 // If this assertion fails, then getBoleanComparisonPredicate created a predicate which
328 // does not start with the expression we gave it. The only known case is when
329 // the comparison mode is ACCESS_COMPAT, and the value is TRUE. In this case,
330 // the expression is rather complex.
331 // Well, so this is a known issue - the filter controls (and thus the form based filter)
332 // do not work with boolean MS Access fields.
333 // To fix this, we would probably have to revert here to always return "1" or "0" as normalized
334 // filter, and change our client code to properly translate this (which could be some effort).
335 if ( nMarkerPos == 0 )
336 aText.append( sText.copy( sExpressionMarker.getLength() ) );
337 else
339 // fallback
340 aText.appendAscii( bSelected ? "1" : "0" );
344 break;
346 case FormComponentType::LISTBOX:
348 Sequence< ::rtl::OUString> aValueSelection;
349 Reference< XPropertySet > aPropertyPointer(getModel(), UNO_QUERY);
350 aPropertyPointer->getPropertyValue(PROPERTY_VALUE_SEQ) >>= aValueSelection;
351 if (rEvent.Selected <= aValueSelection.getLength())
352 aText.append( aValueSelection[ rEvent.Selected ] );
354 break;
356 case FormComponentType::RADIOBUTTON:
358 if ( rEvent.Selected == STATE_CHECK )
359 aText.append( ::comphelper::getString( Reference< XPropertySet >( getModel(), UNO_QUERY )->getPropertyValue( PROPERTY_REFVALUE ) ) );
361 break;
364 ::rtl::OUString sText( aText.makeStringAndClear() );
365 if ( m_aText.compareTo( sText ) )
367 m_aText = sText;
368 TextEvent aEvt;
369 aEvt.Source = *this;
370 ::cppu::OInterfaceIteratorHelper aIt( m_aTextListeners );
371 while( aIt.hasMoreElements() )
372 ((XTextListener *)aIt.next())->textChanged( aEvt );
376 //---------------------------------------------------------------------
377 void OFilterControl::implInitFilterList()
379 if ( !ensureInitialized( ) )
380 // already asserted in ensureInitialized
381 return;
383 // declare here for later disposal
384 Reference< XResultSet > xListCursor;
385 Reference< XStatement > xStatement;
389 m_bFilterListFilled = sal_True;
391 Reference< XPropertySet > xSet(getModel(), UNO_QUERY);
392 if (xSet.is() && m_xField.is())
394 ::rtl::OUString sName;
395 m_xField->getPropertyValue(PROPERTY_NAME) >>= sName;
397 // here we need a table to which the field belongs to
398 Reference< XChild > xModelAsChild( xSet, UNO_QUERY );
399 Reference< XRowSet > xForm( xModelAsChild->getParent(), UNO_QUERY );
400 Reference< XPropertySet > xFormAsSet( xForm, UNO_QUERY );
402 // Connection holen
403 Reference< XConnection > xConnection;
404 if ( xForm.is() )
405 xConnection = ::dbtools::getConnection( xForm );
406 Reference< XSQLQueryComposerFactory > xFactory( xConnection, UNO_QUERY );
407 OSL_ENSURE( xFactory.is() && xFormAsSet.is(), "OFilterControl::implInitFilterList: invalid form or invalid connection!" );
408 if ( !xFactory.is() || !xFormAsSet.is() )
409 return;
411 // create a query composer
412 Reference< XSQLQueryComposer > xComposer = xFactory->createQueryComposer();
413 OSL_ENSURE( xComposer.is() , "OFilterControl::implInitFilterList: invalid query composer!" );
414 if ( !xComposer.is() )
415 return;
417 // set the statement on the composer, ...
418 ::rtl::OUString sStatement;
419 xFormAsSet->getPropertyValue( PROPERTY_ACTIVECOMMAND ) >>= sStatement;
420 xComposer->setQuery( sStatement );
422 // ... and ask it for the involved tables and queries
423 Reference< XTablesSupplier > xSuppTables( xComposer, UNO_QUERY );
424 Reference< XColumnsSupplier > xSuppColumns( xComposer, UNO_QUERY );
426 Reference< XNameAccess > xFieldNames;
427 if ( xSuppColumns.is() ) xFieldNames = xSuppColumns->getColumns();
428 Reference< XNameAccess > xTablesNames;
429 if ( xSuppTables.is() ) xTablesNames = xSuppTables->getTables();
431 if ( !xFieldNames.is() || !xTablesNames.is() )
433 OSL_ENSURE( sal_False, "OFilterControl::implInitFilterList: invalid query composer (no fields or no tables supplied)!" );
434 return;
437 // search the field
438 Reference< XPropertySet > xComposerFieldAsSet;
439 if ( xFieldNames->hasByName( sName ) )
440 xFieldNames->getByName( sName ) >>= xComposerFieldAsSet;
442 if ( xComposerFieldAsSet.is()
443 && ::comphelper::hasProperty( PROPERTY_TABLENAME, xComposerFieldAsSet )
444 && ::comphelper::hasProperty( PROPERTY_REALNAME, xComposerFieldAsSet )
447 ::rtl::OUString sFieldName, sTableName;
448 xComposerFieldAsSet->getPropertyValue(PROPERTY_REALNAME) >>= sFieldName;
449 xComposerFieldAsSet->getPropertyValue(PROPERTY_TABLENAME) >>= sTableName;
451 // no possibility to create a select statement
452 // looking for the complete table name
453 if (!xTablesNames->hasByName(sTableName))
454 return;
456 // this is the tablename
457 Reference< XNamed > xName;
458 xTablesNames->getByName(sTableName) >>= xName;
459 OSL_ENSURE(xName.is(),"No XName interface!");
460 sTableName = xName->getName();
462 // ein Statement aufbauen und abschicken als query
463 // Access to the connection
465 Reference< XColumn > xDataField;
467 Reference< XDatabaseMetaData > xMeta = xConnection->getMetaData();
468 ::rtl::OUString aQuote = xMeta->getIdentifierQuoteString();
469 ::rtl::OUStringBuffer aStatement;
470 aStatement.appendAscii( "SELECT DISTINCT " );
471 aStatement.append( ::dbtools::quoteName( aQuote, sName ) );
473 if ( sFieldName.getLength() && ( sName != sFieldName ) )
475 aStatement.appendAscii(" AS ");
476 aStatement.append( ::dbtools::quoteName(aQuote, sFieldName) );
479 aStatement.appendAscii( " FROM " );
481 ::rtl::OUString sCatalog, sSchema, sTable;
482 ::dbtools::qualifiedNameComponents( xMeta, sTableName, sCatalog, sSchema, sTable, ::dbtools::eInDataManipulation );
483 aStatement.append( ::dbtools::composeTableNameForSelect( xConnection, sCatalog, sSchema, sTable ) );
485 ::rtl::OUString sSelectStatement( aStatement.makeStringAndClear( ) );
486 xStatement = xConnection->createStatement();
487 xListCursor = xStatement->executeQuery( sSelectStatement );
489 Reference< XColumnsSupplier > xSupplyCols(xListCursor, UNO_QUERY);
490 Reference< XIndexAccess > xFields;
491 if (xSupplyCols.is())
492 xFields = Reference< XIndexAccess > (xSupplyCols->getColumns(), UNO_QUERY);
493 if (xFields.is())
494 xFields->getByIndex(0) >>= xDataField;
495 if (!xDataField.is())
496 return;
499 sal_Int16 i = 0;
500 ::std::vector< ::rtl::OUString> aStringList;
501 aStringList.reserve(16);
502 ::rtl::OUString aStr;
504 ::com::sun::star::util::Date aNullDate( ::dbtools::DBTypeConversion::getStandardDate() );
505 sal_Int32 nFormatKey = 0;
508 m_xFormatter->getNumberFormatsSupplier()->getNumberFormatSettings()->getPropertyValue(::rtl::OUString::createFromAscii("NullDate"))
509 >>= aNullDate;
510 nFormatKey = ::comphelper::getINT32(m_xField->getPropertyValue(PROPERTY_FORMATKEY));
512 catch(const Exception&)
517 sal_Int16 nKeyType = ::comphelper::getNumberFormatType(m_xFormatter->getNumberFormatsSupplier()->getNumberFormats(), nFormatKey);
518 while ( xListCursor->next() && ( i++ < SHRT_MAX) )
520 aStr = ::dbtools::DBTypeConversion::getValue(xDataField, m_xFormatter, aNullDate, nFormatKey, nKeyType);
521 aStringList.push_back(aStr);
524 Sequence< ::rtl::OUString> aStringSeq(aStringList.size());
525 ::rtl::OUString* pustrStrings = aStringSeq.getArray();
526 for (i = 0; i < (sal_Int16)aStringList.size(); ++i)
527 pustrStrings[i] = aStringList[i];
529 Reference< XComboBox > xComboBox( getPeer(), UNO_QUERY);
530 if ( xComboBox.is() )
532 xComboBox->addItems(aStringSeq, 0);
533 // set the drop down line count
534 sal_Int16 nLineCount = ::std::min( (sal_Int16)10, (sal_Int16)aStringSeq.getLength() );
535 xComboBox->setDropDownLineCount( nLineCount );
540 catch( const Exception& )
542 DBG_UNHANDLED_EXCEPTION();
545 ::comphelper::disposeComponent( xListCursor );
546 ::comphelper::disposeComponent( xStatement );
549 // XFocusListener
550 //---------------------------------------------------------------------
551 void SAL_CALL OFilterControl::focusGained(const FocusEvent& /*e*/) throw( RuntimeException )
553 // should we fill the combobox?
554 if (m_bFilterList && !m_bFilterListFilled)
555 implInitFilterList();
558 //---------------------------------------------------------------------
559 void SAL_CALL OFilterControl::focusLost(const FocusEvent& /*e*/) throw( RuntimeException )
563 //---------------------------------------------------------------------
564 sal_Bool SAL_CALL OFilterControl::commit() throw(RuntimeException)
566 if ( !ensureInitialized( ) )
567 // already asserted in ensureInitialized
568 return sal_True;
570 ::rtl::OUString aText;
571 switch (m_nControlClass)
573 case FormComponentType::TEXTFIELD:
574 case FormComponentType::COMBOBOX:
576 Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
577 if (xText.is())
578 aText = xText->getText();
579 } break;
580 default:
581 return sal_True;
583 if (m_aText.compareTo(aText))
585 // check the text with the SQL-Parser
586 ::rtl::OUString aNewText(aText);
587 aNewText.trim();
588 if ( aNewText.getLength() )
590 ::dbtools::OPredicateInputController aPredicateInput( m_xORB, m_xConnection, getParseContext() );
591 ::rtl::OUString sErrorMessage;
592 if ( !aPredicateInput.normalizePredicateString( aNewText, m_xField, &sErrorMessage ) )
594 // display the error and outta here
595 SQLContext aError;
596 aError.Message = String( FRM_RES_STRING( RID_STR_SYNTAXERROR ) );
597 aError.Details = sErrorMessage;
598 displayException( aError );
599 return sal_False;
603 setText(aNewText);
604 TextEvent aEvt;
605 aEvt.Source = *this;
606 ::cppu::OInterfaceIteratorHelper aIt( m_aTextListeners );
607 while( aIt.hasMoreElements() )
608 static_cast< XTextListener* >( aIt.next() )->textChanged( aEvt );
610 return sal_True;
613 // XTextComponent
614 //---------------------------------------------------------------------
615 void SAL_CALL OFilterControl::addTextListener(const Reference< XTextListener > & l) throw(RuntimeException)
617 m_aTextListeners.addInterface( l );
620 //---------------------------------------------------------------------
621 void SAL_CALL OFilterControl::removeTextListener(const Reference< XTextListener > & l) throw(RuntimeException)
623 m_aTextListeners.removeInterface( l );
626 //---------------------------------------------------------------------
627 void SAL_CALL OFilterControl::setText( const ::rtl::OUString& aText ) throw(RuntimeException)
629 if ( !ensureInitialized( ) )
630 // already asserted in ensureInitialized
631 return;
633 switch (m_nControlClass)
635 case FormComponentType::CHECKBOX:
637 Reference< XVclWindowPeer > xVclWindow( getPeer(), UNO_QUERY );
638 if (xVclWindow.is())
640 Any aValue;
641 if ( aText.equalsAscii( "1" )
642 || aText.equalsIgnoreAsciiCaseAscii( "TRUE" )
643 || aText.equalsIgnoreAsciiCaseAscii( "IS TRUE" )
646 aValue <<= (sal_Int32)STATE_CHECK;
648 else if ( aText.equalsAscii( "0" )
649 || aText.equalsIgnoreAsciiCaseAscii( "FALSE" )
652 aValue <<= (sal_Int32)STATE_NOCHECK;
654 else
655 aValue <<= (sal_Int32)STATE_DONTKNOW;
657 m_aText = aText;
658 xVclWindow->setProperty( PROPERTY_STATE, aValue );
660 } break;
661 case FormComponentType::RADIOBUTTON:
663 Reference< XVclWindowPeer > xVclWindow( getPeer(), UNO_QUERY );
664 if (xVclWindow.is())
666 ::rtl::OUString aRefText = ::comphelper::getString(com::sun::star::uno::Reference< XPropertySet > (getModel(), UNO_QUERY)->getPropertyValue(PROPERTY_REFVALUE));
667 Any aValue;
668 if (aText == aRefText)
669 aValue <<= (sal_Int32)STATE_CHECK;
670 else
671 aValue <<= (sal_Int32)STATE_NOCHECK;
672 m_aText = aText;
673 xVclWindow->setProperty(PROPERTY_STATE, aValue);
675 } break;
676 case FormComponentType::LISTBOX:
678 Reference< XListBox > xListBox( getPeer(), UNO_QUERY );
679 if (xListBox.is())
681 m_aText = aText;
682 xListBox->selectItem(m_aText, sal_True);
684 } break;
685 default:
687 Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
688 if (xText.is())
690 m_aText = aText;
691 xText->setText(aText);
697 //---------------------------------------------------------------------
698 void SAL_CALL OFilterControl::insertText( const ::com::sun::star::awt::Selection& rSel, const ::rtl::OUString& aText ) throw(::com::sun::star::uno::RuntimeException)
700 Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
701 if (xText.is())
703 xText->insertText(rSel, aText);
704 m_aText = xText->getText();
708 //---------------------------------------------------------------------
709 ::rtl::OUString SAL_CALL OFilterControl::getText() throw(RuntimeException)
711 return m_aText;
714 //---------------------------------------------------------------------
715 ::rtl::OUString SAL_CALL OFilterControl::getSelectedText( void ) throw(RuntimeException)
717 ::rtl::OUString aSelected;
718 Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
719 if (xText.is())
720 aSelected = xText->getSelectedText();
722 return aSelected;
725 //---------------------------------------------------------------------
726 void SAL_CALL OFilterControl::setSelection( const ::com::sun::star::awt::Selection& aSelection ) throw(::com::sun::star::uno::RuntimeException)
728 Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
729 if (xText.is())
730 xText->setSelection( aSelection );
733 //---------------------------------------------------------------------
734 ::com::sun::star::awt::Selection SAL_CALL OFilterControl::getSelection( void ) throw(::com::sun::star::uno::RuntimeException)
736 ::com::sun::star::awt::Selection aSel;
737 Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
738 if (xText.is())
739 aSel = xText->getSelection();
740 return aSel;
743 //---------------------------------------------------------------------
744 sal_Bool SAL_CALL OFilterControl::isEditable( void ) throw(RuntimeException)
746 Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
747 return xText.is() && xText->isEditable();
750 //---------------------------------------------------------------------
751 void SAL_CALL OFilterControl::setEditable( sal_Bool bEditable ) throw(RuntimeException)
753 Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
754 if (xText.is())
755 xText->setEditable(bEditable);
758 //---------------------------------------------------------------------
759 sal_Int16 SAL_CALL OFilterControl::getMaxTextLen() throw(RuntimeException)
761 Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
762 return xText.is() ? xText->getMaxTextLen() : 0;
765 //---------------------------------------------------------------------
766 void SAL_CALL OFilterControl::setMaxTextLen( sal_Int16 nLength ) throw(RuntimeException)
768 Reference< XTextComponent > xText( getPeer(), UNO_QUERY );
769 if (xText.is())
770 xText->setMaxTextLen(nLength);
773 //---------------------------------------------------------------------
774 void OFilterControl::displayException( const ::com::sun::star::sdb::SQLContext& _rExcept )
778 Sequence< Any > aArgs(2);
779 aArgs[0] <<= PropertyValue(::rtl::OUString::createFromAscii("SQLException"), 0, makeAny( _rExcept ), PropertyState_DIRECT_VALUE);
780 aArgs[1] <<= PropertyValue(::rtl::OUString::createFromAscii("ParentWindow"), 0, makeAny( m_xMessageParent ), PropertyState_DIRECT_VALUE);
782 static ::rtl::OUString s_sDialogServiceName = ::rtl::OUString::createFromAscii( "com.sun.star.sdb.ErrorMessageDialog" );
783 Reference< XExecutableDialog > xErrorDialog( m_xORB->createInstanceWithArguments( s_sDialogServiceName, aArgs ), UNO_QUERY );
784 if ( xErrorDialog.is() )
785 xErrorDialog->execute();
786 else
788 Window* pMessageParent = VCLUnoHelper::GetWindow( m_xMessageParent );
789 ShowServiceNotAvailableError( pMessageParent, s_sDialogServiceName, sal_True );
792 catch( const Exception& )
794 OSL_ENSURE( sal_False, "displayException: could not display the error message!" );
798 //---------------------------------------------------------------------
799 void SAL_CALL OFilterControl::initialize( const Sequence< Any >& aArguments ) throw (Exception, RuntimeException)
801 const Any* pArguments = aArguments.getConstArray();
802 const Any* pArgumentsEnd = pArguments + aArguments.getLength();
804 PropertyValue aProp;
805 NamedValue aValue;
806 const ::rtl::OUString* pName = NULL;
807 const Any* pValue = NULL;
809 for ( ; pArguments != pArgumentsEnd; ++pArguments )
811 // we recognize PropertyValues and NamedValues
812 if ( *pArguments >>= aProp )
814 pName = &aProp.Name;
815 pValue = &aProp.Value;
817 else if ( *pArguments >>= aValue )
819 pName = &aValue.Name;
820 pValue = &aValue.Value;
822 else
824 DBG_ERROR( "OFilterControl::initialize: unrecognized argument!" );
825 continue;
828 if ( 0 == pName->compareToAscii( "MessageParent" ) )
830 // the message parent
831 *pValue >>= m_xMessageParent;
832 OSL_ENSURE( m_xMessageParent.is(), "OFilterControl::initialize: invalid MessageParent!" );
834 else if ( 0 == pName->compareToAscii( "NumberFormatter" ) )
836 // the number format. This argument is optional.
837 *pValue >>= m_xFormatter;
838 OSL_ENSURE( m_xFormatter.is(), "OFilterControl::initialize: invalid NumberFormatter!" );
840 else if ( 0 == pName->compareToAscii( "ControlModel" ) )
842 // the control model for which we act as filter control
843 Reference< XPropertySet > xControlModel;
844 if ( !(*pValue >>= xControlModel ) || !xControlModel.is() )
846 OSL_ENSURE( sal_False, "OFilterControl::initialize: invalid control model argument!" );
847 continue;
850 // some properties which are "derived" from the control model we're working for
851 // ...................................................
852 // the field
853 m_xField.clear();
854 OSL_ENSURE( ::comphelper::hasProperty( PROPERTY_BOUNDFIELD, xControlModel ), "OFilterControl::initialize: control model needs a bound field property!" );
855 xControlModel->getPropertyValue( PROPERTY_BOUNDFIELD ) >>= m_xField;
857 // ...................................................
858 // filter list and control class
859 m_bFilterList = ::comphelper::hasProperty( PROPERTY_FILTERPROPOSAL, xControlModel ) && ::comphelper::getBOOL( xControlModel->getPropertyValue( PROPERTY_FILTERPROPOSAL ) );
860 if ( m_bFilterList )
861 m_nControlClass = FormComponentType::COMBOBOX;
862 else
864 sal_Int16 nClassId = ::comphelper::getINT16( xControlModel->getPropertyValue( PROPERTY_CLASSID ) );
865 switch (nClassId)
867 case FormComponentType::CHECKBOX:
868 case FormComponentType::RADIOBUTTON:
869 case FormComponentType::LISTBOX:
870 case FormComponentType::COMBOBOX:
871 m_nControlClass = nClassId;
872 break;
873 default:
874 m_bMultiLine = ::comphelper::hasProperty( PROPERTY_MULTILINE, xControlModel ) && ::comphelper::getBOOL( xControlModel->getPropertyValue( PROPERTY_MULTILINE ) );
875 m_nControlClass = FormComponentType::TEXTFIELD;
876 break;
880 // ...................................................
881 // the connection meta data for the form which we're working for
882 Reference< XChild > xModel( xControlModel, UNO_QUERY );
883 Reference< XRowSet > xForm;
884 if ( xModel.is() )
885 xForm = xForm.query( xModel->getParent() );
886 m_xConnection = ::dbtools::getConnection( xForm );
887 OSL_ENSURE( m_xConnection.is(), "OFilterControl::initialize: unable to determine the form's connection!" );
892 //---------------------------------------------------------------------
893 ::rtl::OUString SAL_CALL OFilterControl::getImplementationName( ) throw (RuntimeException)
895 return getImplementationName_Static();
898 //---------------------------------------------------------------------
899 sal_Bool SAL_CALL OFilterControl::supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException)
901 Sequence< ::rtl::OUString > aSupported( getSupportedServiceNames() );
902 const ::rtl::OUString* pArray = aSupported.getConstArray();
903 for( sal_Int32 i = 0; i < aSupported.getLength(); ++i, ++pArray )
904 if( pArray->equals( ServiceName ) )
905 return sal_True;
906 return sal_False;
909 //---------------------------------------------------------------------
910 Sequence< ::rtl::OUString > SAL_CALL OFilterControl::getSupportedServiceNames( ) throw (RuntimeException)
912 return getSupportedServiceNames_Static();
915 //---------------------------------------------------------------------
916 ::rtl::OUString SAL_CALL OFilterControl::getImplementationName_Static()
918 return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.forms.OFilterControl" ) );
921 //---------------------------------------------------------------------
922 Sequence< ::rtl::OUString > SAL_CALL OFilterControl::getSupportedServiceNames_Static()
924 Sequence< ::rtl::OUString > aNames( 2 );
925 aNames[ 0 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.form.control.FilterControl" ) );
926 aNames[ 1 ] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.awt.UnoControl" ) );
927 return aNames;
930 //---------------------------------------------------------------------
931 Reference< XInterface > SAL_CALL OFilterControl::Create( const Reference< XMultiServiceFactory >& _rxFactory )
933 return static_cast< XServiceInfo* >( new OFilterControl( _rxFactory ) );
936 //.........................................................................
937 } // namespace frm
938 //.........................................................................