1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_forms.hxx"
26 
27 #include "Pattern.hxx"
28 
29 /** === begin UNO includes === **/
30 /** === end UNO includes === **/
31 
32 //.........................................................................
33 namespace frm
34 {
35 //.........................................................................
36 
37     /** === begin UNO using === **/
38     using ::com::sun::star::uno::Reference;
39     using ::com::sun::star::lang::XMultiServiceFactory;
40     using ::com::sun::star::uno::Sequence;
41     using ::com::sun::star::uno::Type;
42     using ::com::sun::star::beans::Property;
43     using ::com::sun::star::uno::Exception;
44     using ::com::sun::star::uno::XInterface;
45     using ::com::sun::star::uno::Any;
46     using ::com::sun::star::uno::makeAny;
47     using ::com::sun::star::sdbc::XRowSet;
48     using ::com::sun::star::uno::UNO_QUERY;
49 	/** === end UNO using === **/
50     namespace FormComponentType = ::com::sun::star::form::FormComponentType;
51 
52 //==================================================================
53 // OPatternControl
54 //==================================================================
55 //------------------------------------------------------------------
OPatternControl(const Reference<XMultiServiceFactory> & _rxFactory)56 OPatternControl::OPatternControl(const Reference<XMultiServiceFactory>& _rxFactory)
57     :OBoundControl(_rxFactory, VCL_CONTROL_PATTERNFIELD)
58 {
59 }
60 
61 //------------------------------------------------------------------
OPatternControl_CreateInstance(const Reference<XMultiServiceFactory> & _rxFactory)62 InterfaceRef SAL_CALL OPatternControl_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
63 {
64     return *(new OPatternControl(_rxFactory));
65 }
66 
67 //------------------------------------------------------------------------------
_getTypes()68 Sequence<Type> OPatternControl::_getTypes()
69 {
70     return OBoundControl::_getTypes();
71 }
72 
73 //------------------------------------------------------------------------------
getSupportedServiceNames()74 StringSequence OPatternControl::getSupportedServiceNames() throw()
75 {
76     StringSequence aSupported = OBoundControl::getSupportedServiceNames();
77     aSupported.realloc(aSupported.getLength() + 1);
78 
79     ::rtl::OUString*pArray = aSupported.getArray();
80     pArray[aSupported.getLength()-1] = FRM_SUN_CONTROL_PATTERNFIELD;
81     return aSupported;
82 }
83 
84 //==================================================================
85 // OPatternModel
86 //==================================================================
87 //------------------------------------------------------------------
OPatternModel_CreateInstance(const Reference<XMultiServiceFactory> & _rxFactory)88 InterfaceRef SAL_CALL OPatternModel_CreateInstance(const Reference<XMultiServiceFactory>& _rxFactory)
89 {
90     return *(new OPatternModel(_rxFactory));
91 }
92 
93 //------------------------------------------------------------------------------
_getTypes()94 Sequence<Type> OPatternModel::_getTypes()
95 {
96     return OEditBaseModel::_getTypes();
97 }
98 
99 //------------------------------------------------------------------
DBG_NAME(OPatternModel)100 DBG_NAME( OPatternModel )
101 //------------------------------------------------------------------
102 OPatternModel::OPatternModel(const Reference<XMultiServiceFactory>& _rxFactory)
103     :OEditBaseModel( _rxFactory, VCL_CONTROLMODEL_PATTERNFIELD, FRM_SUN_CONTROL_PATTERNFIELD, sal_False, sal_False )
104                                     // use the old control name for compytibility reasons
105 {
106 	DBG_CTOR( OPatternModel, NULL );
107 
108     m_nClassId = FormComponentType::PATTERNFIELD;
109 	initValueProperty( PROPERTY_TEXT, PROPERTY_ID_TEXT );
110 }
111 
112 //------------------------------------------------------------------
OPatternModel(const OPatternModel * _pOriginal,const Reference<XMultiServiceFactory> & _rxFactory)113 OPatternModel::OPatternModel( const OPatternModel* _pOriginal, const Reference<XMultiServiceFactory>& _rxFactory )
114     :OEditBaseModel( _pOriginal, _rxFactory )
115 {
116 	DBG_CTOR( OPatternModel, NULL );
117 }
118 
119 //------------------------------------------------------------------
~OPatternModel()120 OPatternModel::~OPatternModel()
121 {
122 	DBG_DTOR( OPatternModel, NULL );
123 }
124 
125 // XCloneable
126 //------------------------------------------------------------------------------
IMPLEMENT_DEFAULT_CLONING(OPatternModel)127 IMPLEMENT_DEFAULT_CLONING( OPatternModel )
128 
129 // XServiceInfo
130 //------------------------------------------------------------------------------
131 StringSequence SAL_CALL OPatternModel::getSupportedServiceNames() throw()
132 {
133     StringSequence aSupported = OBoundControlModel::getSupportedServiceNames();
134     aSupported.realloc(aSupported.getLength() + 2);
135 
136     ::rtl::OUString*pArray = aSupported.getArray();
137     pArray[aSupported.getLength()-2] = FRM_SUN_COMPONENT_DATABASE_PATTERNFIELD;
138     pArray[aSupported.getLength()-1] = FRM_SUN_COMPONENT_PATTERNFIELD;
139     return aSupported;
140 }
141 
142 
143 //------------------------------------------------------------------------------
describeFixedProperties(Sequence<Property> & _rProps) const144 void OPatternModel::describeFixedProperties( Sequence< Property >& _rProps ) const
145 {
146     BEGIN_DESCRIBE_PROPERTIES( 4, OEditBaseModel )
147         DECL_PROP2(DEFAULT_TEXT,    ::rtl::OUString,    BOUND, MAYBEDEFAULT);
148         DECL_BOOL_PROP1(EMPTY_IS_NULL,                  BOUND);
149         DECL_PROP1(TABINDEX,        sal_Int16,          BOUND);
150         DECL_PROP2(FILTERPROPOSAL,  sal_Bool,           BOUND, MAYBEDEFAULT);
151     END_DESCRIBE_PROPERTIES();
152 }
153 
154 //------------------------------------------------------------------------------
getServiceName()155 ::rtl::OUString SAL_CALL OPatternModel::getServiceName() throw ( ::com::sun::star::uno::RuntimeException)
156 {
157     return FRM_COMPONENT_PATTERNFIELD;  // old (non-sun) name for compatibility !
158 }
159 
160 //------------------------------------------------------------------------------
commitControlValueToDbColumn(bool)161 sal_Bool OPatternModel::commitControlValueToDbColumn( bool /*_bPostReset*/ )
162 {
163     Any aNewValue( m_xAggregateFastSet->getFastPropertyValue( getValuePropertyAggHandle() ) );
164 
165     if ( aNewValue != m_aLastKnownValue )
166     {
167         ::rtl::OUString sNewValue;
168         aNewValue >>= sNewValue;
169 
170 		if  (   !aNewValue.hasValue()
171             ||  (   !sNewValue.getLength()      // an empty string
172                 &&  m_bEmptyIsNull              // which should be interpreted as NULL
173                 )
174             )
175         {
176             m_xColumnUpdate->updateNull();
177         }
178         else
179         {
180             OSL_ENSURE( m_pFormattedValue.get(), "OPatternModel::commitControlValueToDbColumn: no value helper!" );
181             if ( !m_pFormattedValue.get() )
182                 return sal_False;
183 
184             if ( !m_pFormattedValue->setFormattedValue( sNewValue ) )
185                 return sal_False;
186         }
187 
188         m_aLastKnownValue = aNewValue;
189     }
190 
191     return sal_True;
192 }
193 
194 //------------------------------------------------------------------------------
onConnectedDbColumn(const Reference<XInterface> & _rxForm)195 void OPatternModel::onConnectedDbColumn( const Reference< XInterface >& _rxForm )
196 {
197     OEditBaseModel::onConnectedDbColumn( _rxForm );
198 
199     Reference< XPropertySet > xField( getField() );
200     if ( !xField.is() )
201         return;
202 
203     m_pFormattedValue.reset( new ::dbtools::FormattedColumnValue( getContext(), Reference< XRowSet >( _rxForm, UNO_QUERY ), xField ) );
204 }
205 
206 //------------------------------------------------------------------------------
onDisconnectedDbColumn()207 void OPatternModel::onDisconnectedDbColumn()
208 {
209     OEditBaseModel::onDisconnectedDbColumn();
210     m_pFormattedValue.reset();
211 }
212 
213 // XPropertyChangeListener
214 //------------------------------------------------------------------------------
translateDbColumnToControlValue()215 Any OPatternModel::translateDbColumnToControlValue()
216 {
217     OSL_PRECOND( m_pFormattedValue.get(), "OPatternModel::translateDbColumnToControlValue: no value helper!" );
218 
219     if ( m_pFormattedValue.get() )
220     {
221         ::rtl::OUString sValue( m_pFormattedValue->getFormattedValue() );
222         if  (   !sValue.getLength()
223             &&  m_pFormattedValue->getColumn().is()
224             &&  m_pFormattedValue->getColumn()->wasNull()
225             )
226         {
227             m_aLastKnownValue.clear();
228         }
229         else
230         {
231 	        m_aLastKnownValue <<= sValue;
232         }
233     }
234     else
235         m_aLastKnownValue.clear();
236 
237     return m_aLastKnownValue.hasValue() ? m_aLastKnownValue : makeAny( ::rtl::OUString() );
238         // (m_aLastKnownValue is alllowed to be VOID, the control value isn't)
239 }
240 
241 // XReset
242 //------------------------------------------------------------------------------
getDefaultForReset() const243 Any OPatternModel::getDefaultForReset() const
244 {
245     return makeAny( m_aDefaultText );
246 }
247 
248 //------------------------------------------------------------------------------
resetNoBroadcast()249 void OPatternModel::resetNoBroadcast()
250 {
251     OEditBaseModel::resetNoBroadcast();
252     m_aLastKnownValue.clear();
253 }
254 
255 //.........................................................................
256 }   // namespace frm
257 //.........................................................................
258 
259