xref: /aoo42x/main/forms/source/component/Pattern.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_forms.hxx"
30 
31 #include "Pattern.hxx"
32 
33 /** === begin UNO includes === **/
34 /** === end UNO includes === **/
35 
36 //.........................................................................
37 namespace frm
38 {
39 //.........................................................................
40 
41     /** === begin UNO using === **/
42     using ::com::sun::star::uno::Reference;
43     using ::com::sun::star::lang::XMultiServiceFactory;
44     using ::com::sun::star::uno::Sequence;
45     using ::com::sun::star::uno::Type;
46     using ::com::sun::star::beans::Property;
47     using ::com::sun::star::uno::Exception;
48     using ::com::sun::star::uno::XInterface;
49     using ::com::sun::star::uno::Any;
50     using ::com::sun::star::uno::makeAny;
51     using ::com::sun::star::sdbc::XRowSet;
52     using ::com::sun::star::uno::UNO_QUERY;
53 	/** === end UNO using === **/
54     namespace FormComponentType = ::com::sun::star::form::FormComponentType;
55 
56 //==================================================================
57 // OPatternControl
58 //==================================================================
59 //------------------------------------------------------------------
60 OPatternControl::OPatternControl(const Reference<XMultiServiceFactory>& _rxFactory)
61     :OBoundControl(_rxFactory, VCL_CONTROL_PATTERNFIELD)
62 {
63 }
64 
65 //------------------------------------------------------------------
66 InterfaceRef SAL_CALL OPatternControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
67 {
68     return *(new OPatternControl(_rxFactory));
69 }
70 
71 //------------------------------------------------------------------------------
72 Sequence<Type> OPatternControl::_getTypes()
73 {
74     return OBoundControl::_getTypes();
75 }
76 
77 //------------------------------------------------------------------------------
78 StringSequence OPatternControl::getSupportedServiceNames() throw()
79 {
80     StringSequence aSupported = OBoundControl::getSupportedServiceNames();
81     aSupported.realloc(aSupported.getLength() + 1);
82 
83     ::rtl::OUString*pArray = aSupported.getArray();
84     pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_PATTERNFIELD;
85     return aSupported;
86 }
87 
88 //==================================================================
89 // OPatternModel
90 //==================================================================
91 //------------------------------------------------------------------
92 InterfaceRef SAL_CALL OPatternModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
93 {
94     return *(new OPatternModel(_rxFactory));
95 }
96 
97 //------------------------------------------------------------------------------
98 Sequence<Type> OPatternModel::_getTypes()
99 {
100     return OEditBaseModel::_getTypes();
101 }
102 
103 //------------------------------------------------------------------
104 DBG_NAME( OPatternModel )
105 //------------------------------------------------------------------
106 OPatternModel::OPatternModel(const Reference<XMultiServiceFactory>& _rxFactory)
107     :OEditBaseModel( _rxFactory, VCL_CONTROLMODEL_PATTERNFIELD, FRM_SUN_CONTROL_PATTERNFIELD, sal_False, sal_False )
108                                     // use the old control name for compytibility reasons
109 {
110 	DBG_CTOR( OPatternModel, NULL );
111 
112     m_nClassId = FormComponentType::PATTERNFIELD;
113 	initValueProperty( PROPERTY_TEXT, PROPERTY_ID_TEXT );
114 }
115 
116 //------------------------------------------------------------------
117 OPatternModel::OPatternModel( const OPatternModel* _pOriginal, const Reference<XMultiServiceFactory>& _rxFactory )
118     :OEditBaseModel( _pOriginal, _rxFactory )
119 {
120 	DBG_CTOR( OPatternModel, NULL );
121 }
122 
123 //------------------------------------------------------------------
124 OPatternModel::~OPatternModel()
125 {
126 	DBG_DTOR( OPatternModel, NULL );
127 }
128 
129 // XCloneable
130 //------------------------------------------------------------------------------
131 IMPLEMENT_DEFAULT_CLONING( OPatternModel )
132 
133 // XServiceInfo
134 //------------------------------------------------------------------------------
135 StringSequence SAL_CALL OPatternModel::getSupportedServiceNames() throw()
136 {
137     StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
138     aSupported.realloc(aSupported.getLength() + 2);
139 
140     ::rtl::OUString*pArray = aSupported.getArray();
141     pArray[aSupported.getLength()-2] = FRM_SUN_COMPONENT_DATABASE_PATTERNFIELD;
142     pArray[aSupported.getLength()-1] = FRM_SUN_COMPONENT_PATTERNFIELD;
143     return aSupported;
144 }
145 
146 
147 //------------------------------------------------------------------------------
148 void OPatternModel::describeFixedProperties( Sequence< Property >& _rProps ) const
149 {
150     BEGIN_DESCRIBE_PROPERTIES( 4, OEditBaseModel )
151         DECL_PROP2(DEFAULT_TEXT,    ::rtl::OUString,    BOUND, MAYBEDEFAULT);
152         DECL_BOOL_PROP1(EMPTY_IS_NULL,                  BOUND);
153         DECL_PROP1(TABINDEX,        sal_Int16,          BOUND);
154         DECL_PROP2(FILTERPROPOSAL,  sal_Bool,           BOUND, MAYBEDEFAULT);
155     END_DESCRIBE_PROPERTIES();
156 }
157 
158 //------------------------------------------------------------------------------
159 ::rtl::OUString SAL_CALL OPatternModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException)
160 {
161     return FRM_COMPONENT_PATTERNFIELD;  // old (non-sun) name for compatibility !
162 }
163 
164 //------------------------------------------------------------------------------
165 sal_Bool OPatternModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
166 {
167     Any aNewValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
168 
169     if ( aNewValue != m_aLastKnownValue )
170     {
171         ::rtl::OUString sNewValue;
172         aNewValue >>= sNewValue;
173 
174 		if  (   !aNewValue.hasValue()
175             ||  (   !sNewValue.getLength()      // an empty string
176                 &&  m_bEmptyIsNull              // which should be interpreted as NULL
177                 )
178             )
179         {
180             m_xColumnUpdate->updateNull();
181         }
182         else
183         {
184             OSL_ENSURE( m_pFormattedValue.get(), "OPatternModel::commitControlValueToDbColumn: no value helper!" );
185             if ( !m_pFormattedValue.get() )
186                 return sal_False;
187 
188             if ( !m_pFormattedValue->setFormattedValue( sNewValue ) )
189                 return sal_False;
190         }
191 
192         m_aLastKnownValue = aNewValue;
193     }
194 
195     return sal_True;
196 }
197 
198 //------------------------------------------------------------------------------
199 void OPatternModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
200 {
201     OEditBaseModel::onConnectedDbColumn( _rxForm );
202 
203     Reference< XPropertySet > xField( getField() );
204     if ( !xField.is() )
205         return;
206 
207     m_pFormattedValue.reset( new ::dbtools::FormattedColumnValue( getContext(), Reference< XRowSet >( _rxForm, UNO_QUERY ), xField ) );
208 }
209 
210 //------------------------------------------------------------------------------
211 void OPatternModel::onDisconnectedDbColumn()
212 {
213     OEditBaseModel::onDisconnectedDbColumn();
214     m_pFormattedValue.reset();
215 }
216 
217 // XPropertyChangeListener
218 //------------------------------------------------------------------------------
219 Any OPatternModel::translateDbColumnToControlValue()
220 {
221     OSL_PRECOND( m_pFormattedValue.get(), "OPatternModel::translateDbColumnToControlValue: no value helper!" );
222 
223     if ( m_pFormattedValue.get() )
224     {
225         ::rtl::OUString sValue( m_pFormattedValue->getFormattedValue() );
226         if  (   !sValue.getLength()
227             &&  m_pFormattedValue->getColumn().is()
228             &&  m_pFormattedValue->getColumn()->wasNull()
229             )
230         {
231             m_aLastKnownValue.clear();
232         }
233         else
234         {
235 	        m_aLastKnownValue <<= sValue;
236         }
237     }
238     else
239         m_aLastKnownValue.clear();
240 
241     return m_aLastKnownValue.hasValue() ? m_aLastKnownValue : makeAny( ::rtl::OUString() );
242         // (m_aLastKnownValue is alllowed to be VOID, the control value isn't)
243 }
244 
245 // XReset
246 //------------------------------------------------------------------------------
247 Any OPatternModel::getDefaultForReset() const
248 {
249     return makeAny( m_aDefaultText );
250 }
251 
252 //------------------------------------------------------------------------------
253 void OPatternModel::resetNoBroadcast()
254 {
255     OEditBaseModel::resetNoBroadcast();
256     m_aLastKnownValue.clear();
257 }
258 
259 //.........................................................................
260 }   // namespace frm
261 //.........................................................................
262 
263