Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : : #include "vbavalidation.hxx"
29 : : #include "vbaformatcondition.hxx"
30 : : #include <com/sun/star/sheet/XSheetCondition.hpp>
31 : : #include <com/sun/star/sheet/ValidationType.hpp>
32 : : #include <com/sun/star/sheet/ValidationAlertStyle.hpp>
33 : : #include <com/sun/star/beans/XPropertySet.hpp>
34 : : #include <ooo/vba/excel/XlDVType.hpp>
35 : : #include <ooo/vba/excel/XlFormatConditionOperator.hpp>
36 : : #include <ooo/vba/excel/XlDVAlertStyle.hpp>
37 : :
38 : : #include "unonames.hxx"
39 : :
40 : : using namespace ::ooo::vba;
41 : : using namespace ::com::sun::star;
42 : :
43 : 3 : const static rtl::OUString VALIDATION( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_VALIDAT ) );
44 : 3 : const static rtl::OUString IGNOREBLANK( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_IGNOREBL ) );
45 : 3 : const static rtl::OUString SHOWINPUT( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_SHOWINP ) );
46 : 3 : const static rtl::OUString SHOWERROR( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_SHOWERR ) );
47 : 3 : const static rtl::OUString ERRORTITLE( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_ERRTITLE ) );
48 : 3 : const static rtl::OUString INPUTTITLE( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_INPTITLE ) );
49 : 3 : const static rtl::OUString INPUTMESS( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_INPMESS ) );
50 : 3 : const static rtl::OUString ERRORMESS( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_ERRMESS ) );
51 : 3 : const static rtl::OUString STYPE( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_TYPE ) );
52 : 3 : const static rtl::OUString SHOWLIST( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_SHOWLIST ) );
53 : 3 : const static rtl::OUString ALERTSTYLE( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_ERRALSTY ) );
54 : :
55 : : void
56 : 0 : lcl_setValidationProps( const uno::Reference< table::XCellRange >& xRange, const uno::Reference< beans::XPropertySet >& xProps )
57 : : {
58 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xRangeProps( xRange, uno::UNO_QUERY_THROW );
59 [ # # ][ # # ]: 0 : xRangeProps->setPropertyValue( VALIDATION , uno::makeAny( xProps ) );
[ # # ]
60 : 0 : }
61 : :
62 : : uno::Reference< beans::XPropertySet >
63 : 0 : lcl_getValidationProps( const uno::Reference< table::XCellRange >& xRange )
64 : : {
65 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( xRange, uno::UNO_QUERY_THROW );
66 : 0 : uno::Reference< beans::XPropertySet > xValProps;
67 [ # # ][ # # ]: 0 : xValProps.set( xProps->getPropertyValue( VALIDATION ), uno::UNO_QUERY_THROW );
[ # # ]
68 : 0 : return xValProps;
69 : : }
70 : :
71 : : ::sal_Bool SAL_CALL
72 : 0 : ScVbaValidation::getIgnoreBlank() throw (uno::RuntimeException)
73 : : {
74 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
75 : 0 : sal_Bool bBlank = false;
76 [ # # ][ # # ]: 0 : xProps->getPropertyValue( IGNOREBLANK ) >>= bBlank;
77 : 0 : return bBlank;
78 : : }
79 : :
80 : : void SAL_CALL
81 : 0 : ScVbaValidation::setIgnoreBlank( ::sal_Bool _ignoreblank ) throw (uno::RuntimeException)
82 : : {
83 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
84 [ # # ][ # # ]: 0 : xProps->setPropertyValue( IGNOREBLANK, uno::makeAny( _ignoreblank ) );
[ # # ]
85 [ # # ]: 0 : lcl_setValidationProps( m_xRange, xProps );
86 : 0 : }
87 : :
88 : : ::sal_Bool SAL_CALL
89 : 0 : ScVbaValidation::getInCellDropdown() throw (uno::RuntimeException)
90 : : {
91 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
92 : 0 : sal_Int32 nShowList = 0;
93 [ # # ][ # # ]: 0 : xProps->getPropertyValue( SHOWLIST ) >>= nShowList;
94 [ # # ]: 0 : return ( nShowList ? sal_True : false );
95 : : }
96 : :
97 : : void SAL_CALL
98 : 0 : ScVbaValidation::setInCellDropdown( ::sal_Bool _incelldropdown ) throw (uno::RuntimeException)
99 : : {
100 : 0 : sal_Int32 nDropDown = false;
101 [ # # ]: 0 : if ( _incelldropdown )
102 : 0 : nDropDown = 1;
103 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps(m_xRange) );
104 [ # # ][ # # ]: 0 : xProps->setPropertyValue( SHOWLIST, uno::makeAny( nDropDown ) );
[ # # ]
105 [ # # ]: 0 : lcl_setValidationProps( m_xRange, xProps );
106 : 0 : }
107 : :
108 : : ::sal_Bool SAL_CALL
109 : 0 : ScVbaValidation::getShowInput() throw (uno::RuntimeException)
110 : : {
111 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
112 : 0 : sal_Bool bShowInput = false;
113 [ # # ][ # # ]: 0 : xProps->getPropertyValue( SHOWINPUT ) >>= bShowInput;
114 : 0 : return bShowInput;
115 : : }
116 : :
117 : : void SAL_CALL
118 : 0 : ScVbaValidation:: setShowInput( ::sal_Bool _showinput ) throw (uno::RuntimeException)
119 : : {
120 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps(m_xRange) );
121 [ # # ][ # # ]: 0 : xProps->setPropertyValue( IGNOREBLANK, uno::makeAny( _showinput ) );
[ # # ]
122 [ # # ]: 0 : lcl_setValidationProps( m_xRange, xProps );
123 : 0 : }
124 : :
125 : : ::sal_Bool SAL_CALL
126 : 0 : ScVbaValidation::getShowError() throw (uno::RuntimeException)
127 : : {
128 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
129 : 0 : sal_Bool bShowError = false;
130 [ # # ][ # # ]: 0 : xProps->getPropertyValue( SHOWERROR ) >>= bShowError;
131 : 0 : return bShowError;
132 : : }
133 : :
134 : : void SAL_CALL
135 : 0 : ScVbaValidation::setShowError( ::sal_Bool _showerror ) throw (uno::RuntimeException)
136 : : {
137 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
138 [ # # ][ # # ]: 0 : xProps->setPropertyValue( SHOWERROR, uno::makeAny( _showerror ) );
[ # # ]
139 [ # # ]: 0 : lcl_setValidationProps( m_xRange, xProps );
140 : 0 : }
141 : :
142 : : ::rtl::OUString SAL_CALL
143 : 0 : ScVbaValidation::getErrorTitle() throw (uno::RuntimeException)
144 : : {
145 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
146 : 0 : rtl::OUString sErrorTitle;
147 [ # # ][ # # ]: 0 : xProps->getPropertyValue( ERRORTITLE ) >>= sErrorTitle;
148 : 0 : return sErrorTitle;
149 : : }
150 : :
151 : : void
152 : 0 : ScVbaValidation::setErrorTitle( const rtl::OUString& _errormessage ) throw (uno::RuntimeException)
153 : : {
154 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
155 [ # # ][ # # ]: 0 : xProps->setPropertyValue( ERRORTITLE, uno::makeAny( _errormessage ) );
[ # # ]
156 [ # # ]: 0 : lcl_setValidationProps( m_xRange, xProps );
157 : 0 : }
158 : :
159 : : ::rtl::OUString SAL_CALL
160 : 0 : ScVbaValidation::getInputMessage() throw (uno::RuntimeException)
161 : : {
162 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
163 : 0 : rtl::OUString sMsg;
164 [ # # ][ # # ]: 0 : xProps->getPropertyValue( INPUTMESS ) >>= sMsg;
165 : 0 : return sMsg;
166 : : }
167 : :
168 : : void SAL_CALL
169 : 0 : ScVbaValidation::setInputMessage( const ::rtl::OUString& _inputmessage ) throw (uno::RuntimeException)
170 : : {
171 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
172 [ # # ][ # # ]: 0 : xProps->setPropertyValue( INPUTMESS, uno::makeAny( _inputmessage ) );
[ # # ]
173 [ # # ]: 0 : lcl_setValidationProps( m_xRange, xProps );
174 : 0 : }
175 : :
176 : : ::rtl::OUString SAL_CALL
177 : 0 : ScVbaValidation::getInputTitle() throw (uno::RuntimeException)
178 : : {
179 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
180 : 0 : rtl::OUString sString;
181 [ # # ][ # # ]: 0 : xProps->getPropertyValue( INPUTTITLE ) >>= sString;
182 : 0 : return sString;
183 : : }
184 : :
185 : : void SAL_CALL
186 : 0 : ScVbaValidation::setInputTitle( const ::rtl::OUString& _inputtitle ) throw (uno::RuntimeException)
187 : : {
188 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
189 [ # # ][ # # ]: 0 : xProps->setPropertyValue( INPUTTITLE, uno::makeAny( _inputtitle ) );
[ # # ]
190 [ # # ]: 0 : lcl_setValidationProps( m_xRange, xProps );
191 : 0 : }
192 : :
193 : : ::rtl::OUString SAL_CALL
194 : 0 : ScVbaValidation::getErrorMessage() throw (uno::RuntimeException)
195 : : {
196 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
197 : 0 : rtl::OUString sString;
198 [ # # ][ # # ]: 0 : xProps->getPropertyValue( ERRORMESS ) >>= sString;
199 : 0 : return sString;
200 : : }
201 : :
202 : : void SAL_CALL
203 : 0 : ScVbaValidation::setErrorMessage( const ::rtl::OUString& _errormessage ) throw (uno::RuntimeException)
204 : : {
205 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
206 [ # # ][ # # ]: 0 : xProps->setPropertyValue( ERRORMESS, uno::makeAny( _errormessage ) );
[ # # ]
207 [ # # ]: 0 : lcl_setValidationProps( m_xRange, xProps );
208 : 0 : }
209 : :
210 : :
211 : : void SAL_CALL
212 : 0 : ScVbaValidation::Delete( ) throw (uno::RuntimeException)
213 : : {
214 : 0 : rtl::OUString sBlank;
215 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
216 [ # # ]: 0 : uno::Reference< sheet::XSheetCondition > xCond( xProps, uno::UNO_QUERY_THROW );
217 [ # # ][ # # ]: 0 : xProps->setPropertyValue( IGNOREBLANK, uno::makeAny( sal_True ) );
[ # # ]
218 [ # # ][ # # ]: 0 : xProps->setPropertyValue( SHOWINPUT, uno::makeAny( sal_True ) );
[ # # ]
219 [ # # ][ # # ]: 0 : xProps->setPropertyValue( SHOWERROR, uno::makeAny( sal_True ) );
[ # # ]
220 [ # # ][ # # ]: 0 : xProps->setPropertyValue( ERRORTITLE, uno::makeAny( sBlank ) );
[ # # ]
221 [ # # ][ # # ]: 0 : xProps->setPropertyValue( INPUTMESS, uno::makeAny( sBlank) );
[ # # ]
222 [ # # ][ # # ]: 0 : xProps->setPropertyValue( ALERTSTYLE, uno::makeAny( sheet::ValidationAlertStyle_STOP) );
[ # # ]
223 [ # # ][ # # ]: 0 : xProps->setPropertyValue( STYPE, uno::makeAny( sheet::ValidationType_ANY ) );
[ # # ]
224 [ # # ][ # # ]: 0 : xCond->setFormula1( sBlank );
225 [ # # ][ # # ]: 0 : xCond->setFormula2( sBlank );
226 [ # # ][ # # ]: 0 : xCond->setOperator( sheet::ConditionOperator_NONE );
227 : :
228 [ # # ]: 0 : lcl_setValidationProps( m_xRange, xProps );
229 : 0 : }
230 : :
231 : : // Fix the defect that validatation cannot work when the input should be limited between a lower bound and an upper bound
232 : : void SAL_CALL
233 : 0 : ScVbaValidation::Add( const uno::Any& Type, const uno::Any& AlertStyle, const uno::Any& Operator, const uno::Any& Formula1, const uno::Any& Formula2 ) throw (uno::RuntimeException)
234 : : {
235 [ # # ]: 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
236 [ # # ]: 0 : uno::Reference< sheet::XSheetCondition > xCond( xProps, uno::UNO_QUERY_THROW );
237 : :
238 : 0 : sheet::ValidationType nValType = sheet::ValidationType_ANY;
239 [ # # ][ # # ]: 0 : xProps->getPropertyValue( STYPE ) >>= nValType;
[ # # ]
240 [ # # ]: 0 : if ( nValType != sheet::ValidationType_ANY )
241 [ # # ][ # # ]: 0 : throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "validation object already exists" ) ), uno::Reference< uno::XInterface >() );
242 : 0 : sal_Int32 nType = -1;
243 [ # # ][ # # ]: 0 : if ( !Type.hasValue() || !( Type >>= nType ) )
[ # # ]
244 [ # # ][ # # ]: 0 : throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "missing required param" ) ), uno::Reference< uno::XInterface >() );
245 : :
246 [ # # ]: 0 : Delete(); // set up defaults
247 : 0 : rtl::OUString sFormula1;
248 : 0 : Formula1 >>= sFormula1;
249 : 0 : rtl::OUString sFormula2;
250 : 0 : Formula2 >>= sFormula2;
251 [ # # # ]: 0 : switch ( nType )
252 : : {
253 : : case excel::XlDVType::xlValidateList:
254 : : {
255 : : // for validate list
256 : : // at least formula1 is required
257 [ # # ]: 0 : if ( !Formula1.hasValue() )
258 [ # # ][ # # ]: 0 : throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "missing param" ) ), uno::Reference< uno::XInterface >() );
259 : 0 : nValType = sheet::ValidationType_LIST;
260 [ # # ][ # # ]: 0 : xProps->setPropertyValue( STYPE, uno::makeAny(nValType ));
[ # # ]
261 : : // #TODO validate required params
262 : : // #TODO need to correct the ';' delimited formula on get/set
263 : 0 : break;
264 : : }
265 : : case excel::XlDVType::xlValidateWholeNumber:
266 : 0 : nValType = sheet::ValidationType_WHOLE;
267 [ # # ][ # # ]: 0 : xProps->setPropertyValue( STYPE, uno::makeAny(nValType ));
[ # # ]
268 : 0 : break;
269 : : default:
270 [ # # ][ # # ]: 0 : throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "unsupported operation..." ) ), uno::Reference< uno::XInterface >() );
271 : : }
272 : :
273 : 0 : sheet::ValidationAlertStyle eStyle = sheet::ValidationAlertStyle_STOP;
274 : 0 : sal_Int32 nVbaAlertStyle = excel::XlDVAlertStyle::xlValidAlertStop;
275 [ # # ][ # # ]: 0 : if ( AlertStyle.hasValue() && ( AlertStyle >>= nVbaAlertStyle ) )
[ # # ]
276 : : {
277 [ # # # # ]: 0 : switch( nVbaAlertStyle )
278 : : {
279 : : case excel::XlDVAlertStyle::xlValidAlertStop:
280 : : // yes I know it's already defaulted but safer to assume
281 : : // someone propbably could change the code above
282 : 0 : eStyle = sheet::ValidationAlertStyle_STOP;
283 : 0 : break;
284 : : case excel::XlDVAlertStyle::xlValidAlertWarning:
285 : 0 : eStyle = sheet::ValidationAlertStyle_WARNING;
286 : 0 : break;
287 : : case excel::XlDVAlertStyle::xlValidAlertInformation:
288 : 0 : eStyle = sheet::ValidationAlertStyle_INFO;
289 : 0 : break;
290 : : default:
291 [ # # ][ # # ]: 0 : throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "bad param..." ) ), uno::Reference< uno::XInterface >() );
292 : :
293 : : }
294 : : }
295 : :
296 [ # # ][ # # ]: 0 : xProps->setPropertyValue( ALERTSTYLE, uno::makeAny( eStyle ) );
[ # # ]
297 : :
298 : : // fix the defect that validatation cannot work when the input should be limited between a lower bound and an upper bound
299 [ # # ]: 0 : if ( Operator.hasValue() )
300 : : {
301 [ # # ]: 0 : css::sheet::ConditionOperator conOperator = ScVbaFormatCondition::retrieveAPIOperator( Operator );
302 [ # # ][ # # ]: 0 : xCond->setOperator( conOperator );
303 : : }
304 : :
305 [ # # ]: 0 : if ( !sFormula1.isEmpty() )
306 [ # # ][ # # ]: 0 : xCond->setFormula1( sFormula1 );
307 [ # # ]: 0 : if ( !sFormula2.isEmpty() )
308 [ # # ][ # # ]: 0 : xCond->setFormula2( sFormula2 );
309 : :
310 [ # # ]: 0 : lcl_setValidationProps( m_xRange, xProps );
311 : 0 : }
312 : :
313 : : ::rtl::OUString SAL_CALL
314 : 0 : ScVbaValidation::getFormula1() throw (uno::RuntimeException)
315 : : {
316 [ # # ][ # # ]: 0 : uno::Reference< sheet::XSheetCondition > xCond( lcl_getValidationProps( m_xRange ), uno::UNO_QUERY_THROW );
317 [ # # ][ # # ]: 0 : return xCond->getFormula1();
318 : : }
319 : :
320 : : ::rtl::OUString SAL_CALL
321 : 0 : ScVbaValidation::getFormula2() throw (uno::RuntimeException)
322 : : {
323 [ # # ][ # # ]: 0 : uno::Reference< sheet::XSheetCondition > xCond( lcl_getValidationProps( m_xRange ), uno::UNO_QUERY_THROW );
324 [ # # ][ # # ]: 0 : return xCond->getFormula2();
325 : : }
326 : :
327 : : rtl::OUString
328 : 0 : ScVbaValidation::getServiceImplName()
329 : : {
330 : 0 : return rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ScVbaValidation"));
331 : : }
332 : :
333 : : uno::Sequence< rtl::OUString >
334 : 0 : ScVbaValidation::getServiceNames()
335 : : {
336 [ # # ][ # # ]: 0 : static uno::Sequence< rtl::OUString > aServiceNames;
[ # # ][ # # ]
337 [ # # ]: 0 : if ( aServiceNames.getLength() == 0 )
338 : : {
339 : 0 : aServiceNames.realloc( 1 );
340 [ # # ]: 0 : aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Validation" ) );
341 : : }
342 : 0 : return aServiceNames;
343 [ + - ][ + - ]: 9 : }
344 : :
345 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|