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 <ooo/vba/excel/XRange.hpp>
21 : #include <com/sun/star/sheet/XCellRangeAddressable.hpp>
22 : #include <com/sun/star/sheet/XSheetConditionalEntry.hpp>
23 : #include <cppuhelper/exc_hlp.hxx>
24 : #include <vector>
25 : #include "unonames.hxx"
26 : #include "vbaformatconditions.hxx"
27 : #include "vbaformatcondition.hxx"
28 : #include "vbaworkbook.hxx"
29 : #include "vbastyles.hxx"
30 : #include "vbaglobals.hxx"
31 : using namespace ::ooo::vba;
32 : using namespace ::com::sun::star;
33 :
34 : typedef std::vector< beans::PropertyValue > VecPropValues;
35 :
36 : static const char OPERATOR[] = "Operator";
37 : static const char FORMULA1[] = "Formula1";
38 : static const char FORMULA2[] = "Formula2";
39 : static const char STYLENAME[] = "StyleName";
40 : static const char sStyleNamePrefix[] = "Excel_CondFormat";
41 :
42 : void SAL_CALL
43 0 : ScVbaFormatConditions::Delete( ) throw (script::BasicErrorException, uno::RuntimeException, std::exception)
44 : {
45 : try
46 : {
47 0 : ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
48 0 : if ( !pStyles )
49 0 : DebugHelper::basicexception(SbERR_METHOD_FAILED, OUString() );
50 0 : sal_Int32 nCount = mxSheetConditionalEntries->getCount();
51 0 : for (sal_Int32 i = nCount - 1; i >= 0; i--)
52 : {
53 0 : uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
54 0 : pStyles->Delete(xSheetConditionalEntry->getStyleName());
55 0 : mxSheetConditionalEntries->removeByIndex(i);
56 0 : }
57 0 : notifyRange();
58 : }
59 0 : catch (uno::Exception& )
60 : {
61 0 : DebugHelper::basicexception(SbERR_METHOD_FAILED, OUString());
62 : }
63 0 : }
64 :
65 : uno::Type SAL_CALL
66 0 : ScVbaFormatConditions::getElementType() throw (css::uno::RuntimeException)
67 : {
68 0 : return cppu::UnoType<excel::XFormatCondition>::get();
69 : }
70 :
71 0 : static uno::Any xSheetConditionToFormatCondition( const uno::Reference< XHelperInterface >& xRangeParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< excel::XStyles >& xStyles, const uno::Reference< excel::XFormatConditions >& xFormatConditions, const uno::Reference< beans::XPropertySet >& xRangeProps, const uno::Any& aObject )
72 : {
73 0 : uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry;
74 0 : aObject >>= xSheetConditionalEntry;
75 :
76 0 : uno::Reference< excel::XStyle > xStyle( xStyles->Item( uno::makeAny( xSheetConditionalEntry->getStyleName() ), uno::Any() ), uno::UNO_QUERY_THROW );
77 0 : uno::Reference< excel::XFormatCondition > xCondition = new ScVbaFormatCondition( xRangeParent, xContext, xSheetConditionalEntry, xStyle, xFormatConditions, xRangeProps );
78 0 : return uno::makeAny( xCondition );
79 : }
80 :
81 : uno::Any
82 0 : ScVbaFormatConditions::createCollectionObject(const uno::Any& aObject )
83 : {
84 0 : return xSheetConditionToFormatCondition( uno::Reference< XHelperInterface >( mxRangeParent, uno::UNO_QUERY_THROW ), mxContext, mxStyles, this, mxParentRangePropertySet, aObject );
85 : }
86 :
87 : namespace {
88 :
89 0 : class EnumWrapper : public EnumerationHelper_BASE
90 : {
91 : uno::Reference<container::XIndexAccess > m_xIndexAccess;
92 : uno::Reference<excel::XRange > m_xParentRange;
93 : uno::Reference<uno::XComponentContext > m_xContext;
94 : uno::Reference<excel::XStyles > m_xStyles;
95 : uno::Reference<excel::XFormatConditions > m_xParentCollection;
96 : uno::Reference<beans::XPropertySet > m_xProps;
97 :
98 : sal_Int32 nIndex;
99 : public:
100 0 : EnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess, const uno::Reference<excel::XRange >& xRange, const uno::Reference<uno::XComponentContext >& xContext, const uno::Reference<excel::XStyles >& xStyles, const uno::Reference< excel::XFormatConditions >& xCollection, const uno::Reference<beans::XPropertySet >& xProps ) : m_xIndexAccess( xIndexAccess ), m_xParentRange( xRange ), m_xContext( xContext ), m_xStyles( xStyles ), m_xParentCollection( xCollection ), m_xProps( xProps ), nIndex( 0 ) {}
101 0 : virtual sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
102 : {
103 0 : return ( nIndex < m_xIndexAccess->getCount() );
104 : }
105 :
106 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
107 : {
108 : try
109 : {
110 0 : if ( nIndex < m_xIndexAccess->getCount() )
111 0 : return xSheetConditionToFormatCondition( uno::Reference< XHelperInterface >( m_xParentRange, uno::UNO_QUERY_THROW ), m_xContext, m_xStyles, m_xParentCollection, m_xProps, m_xIndexAccess->getByIndex( nIndex++ ) );
112 : }
113 0 : catch (const container::NoSuchElementException&)
114 : {
115 0 : throw;
116 : }
117 0 : catch (const lang::WrappedTargetException&)
118 : {
119 0 : throw;
120 : }
121 0 : catch (const uno::RuntimeException&)
122 : {
123 0 : throw;
124 : }
125 0 : catch (const uno::Exception& e)
126 : {
127 0 : css::uno::Any a(cppu::getCaughtException());
128 : throw css::lang::WrappedTargetException(
129 0 : "wrapped Exception " + e.Message,
130 0 : css::uno::Reference<css::uno::XInterface>(), a);
131 : }
132 0 : throw container::NoSuchElementException();
133 : }
134 : };
135 :
136 : }
137 :
138 : uno::Reference< excel::XFormatCondition > SAL_CALL
139 0 : ScVbaFormatConditions::Add( ::sal_Int32 _nType, const uno::Any& _aOperator, const uno::Any& _aFormula1, const uno::Any& _aFormula2 ) throw (script::BasicErrorException, uno::RuntimeException, std::exception)
140 : {
141 0 : return Add( _nType, _aOperator, _aFormula1, _aFormula2, uno::Reference< excel::XStyle >() );
142 : }
143 :
144 : uno::Reference< excel::XFormatCondition >
145 0 : ScVbaFormatConditions::Add( ::sal_Int32 _nType, const uno::Any& _aOperator, const uno::Any& _aFormula1, const uno::Any& _aFormula2, const css::uno::Reference< excel::XStyle >& _xStyle ) throw (script::BasicErrorException, uno::RuntimeException)
146 : {
147 : // #TODO
148 : // #FIXME
149 : // This method will NOT handle r1c1 formulas [*]and only assumes that
150 : // the formulas are _xlA1 based ( need to hook into calc work this should
151 : // address this )
152 : // [*] reason: getA1Formula method below is just a hook and just
153 : // returns whats it gets ( e.g. doesn't convert anything )
154 0 : uno::Reference< excel::XStyle > xStyle( _xStyle );
155 0 : uno::Reference< excel::XFormatCondition > xFormatCondition;
156 : try
157 : {
158 0 : OUString sStyleName;
159 0 : if ( !xStyle.is() )
160 : {
161 0 : sStyleName = getStyleName();
162 0 : xStyle = mxStyles->Add(sStyleName, uno::Any() );
163 : }
164 : else
165 : {
166 0 : sStyleName = xStyle->getName();
167 : }
168 :
169 0 : VecPropValues aPropertyValueVector;
170 0 : sheet::ConditionOperator aType = ScVbaFormatCondition::retrieveAPIType(_nType, uno::Reference< sheet::XSheetCondition >() );
171 0 : uno::Any aValue;
172 :
173 0 : if ( aType == sheet::ConditionOperator_FORMULA)
174 0 : aValue = uno::makeAny( sheet::ConditionOperator_FORMULA );
175 : else
176 0 : aValue = uno::makeAny( ScVbaFormatCondition::retrieveAPIOperator(_aOperator) );
177 :
178 0 : beans::PropertyValue aProperty( OPERATOR, 0, aValue, beans::PropertyState_DIRECT_VALUE );
179 0 : aPropertyValueVector.push_back( aProperty );
180 :
181 0 : if ( _aFormula1.hasValue() )
182 : {
183 0 : beans::PropertyValue aProp( FORMULA1, 0, uno::makeAny( getA1Formula( _aFormula1 ) ), beans::PropertyState_DIRECT_VALUE );
184 0 : aPropertyValueVector.push_back( aProp );
185 : }
186 0 : if ( _aFormula2.hasValue() )
187 : {
188 0 : beans::PropertyValue aProp( FORMULA2, 0, uno::makeAny( getA1Formula( _aFormula2 ) ), beans::PropertyState_DIRECT_VALUE );
189 0 : aPropertyValueVector.push_back( aProp );
190 : }
191 0 : aProperty.Name = STYLENAME;
192 0 : aProperty.Value = uno::makeAny( sStyleName );
193 :
194 : // convert vector to sequence
195 0 : uno::Sequence< beans::PropertyValue > aPropertyValueList(aPropertyValueVector.size());
196 0 : VecPropValues::iterator it = aPropertyValueVector.begin();
197 0 : VecPropValues::iterator it_end = aPropertyValueVector.end();
198 0 : for ( sal_Int32 index=0; it != it_end; ++it )
199 0 : aPropertyValueList[ index++ ] = *it;
200 :
201 0 : mxSheetConditionalEntries->addNew(aPropertyValueList);
202 0 : for (sal_Int32 i = mxSheetConditionalEntries->getCount()-1; i >= 0; i--)
203 : {
204 0 : uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
205 0 : if (xSheetConditionalEntry->getStyleName().equals(sStyleName))
206 : {
207 0 : xFormatCondition = new ScVbaFormatCondition(uno::Reference< XHelperInterface >( mxRangeParent, uno::UNO_QUERY_THROW ), mxContext, xSheetConditionalEntry, xStyle, this, mxParentRangePropertySet);
208 0 : notifyRange();
209 0 : return xFormatCondition;
210 : }
211 0 : }
212 : }
213 0 : catch (uno::Exception& )
214 : {
215 : }
216 0 : DebugHelper::basicexception(SbERR_METHOD_FAILED, OUString() );
217 0 : return xFormatCondition;
218 : }
219 :
220 : uno::Reference< container::XEnumeration > SAL_CALL
221 0 : ScVbaFormatConditions::createEnumeration() throw (uno::RuntimeException)
222 : {
223 0 : return new EnumWrapper( m_xIndexAccess, mxRangeParent, mxContext, mxStyles, this, mxParentRangePropertySet );
224 : }
225 :
226 : void
227 0 : ScVbaFormatConditions::notifyRange() throw ( script::BasicErrorException )
228 : {
229 : try
230 : {
231 0 : mxParentRangePropertySet->setPropertyValue(SC_UNONAME_CONDFMT, uno::makeAny( mxSheetConditionalEntries ));
232 : }
233 0 : catch (uno::Exception& )
234 : {
235 0 : DebugHelper::basicexception(SbERR_METHOD_FAILED, OUString());
236 : }
237 0 : }
238 :
239 : OUString
240 0 : ScVbaFormatConditions::getA1Formula(const css::uno::Any& _aFormula) throw ( script::BasicErrorException )
241 : {
242 : // #TODO, #FIXME hook-in proper formula conversion detection & logic
243 0 : OUString sFormula;
244 0 : if ( !( _aFormula >>= sFormula ) )
245 0 : DebugHelper::basicexception(SbERR_BAD_PARAMETER, OUString() );
246 0 : return sFormula;
247 : }
248 :
249 : OUString
250 0 : ScVbaFormatConditions::getStyleName()
251 : {
252 0 : ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
253 0 : if ( !pStyles )
254 0 : DebugHelper::basicexception(SbERR_METHOD_FAILED, OUString() );
255 0 : uno::Sequence< OUString > sCellStyleNames = pStyles->getStyleNames();
256 0 : return ContainerUtilities::getUniqueName(sCellStyleNames, sStyleNamePrefix, OUString("_"));
257 : }
258 :
259 : void
260 0 : ScVbaFormatConditions::removeFormatCondition( const OUString& _sStyleName, bool _bRemoveStyle) throw ( script::BasicErrorException )
261 : {
262 : try
263 : {
264 0 : sal_Int32 nElems = mxSheetConditionalEntries->getCount();
265 0 : for (sal_Int32 i = 0; i < nElems; i++)
266 : {
267 0 : uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
268 0 : if (_sStyleName.equals(xSheetConditionalEntry->getStyleName()))
269 : {
270 0 : mxSheetConditionalEntries->removeByIndex(i);
271 0 : if (_bRemoveStyle)
272 : {
273 0 : ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
274 0 : if ( !pStyles )
275 0 : DebugHelper::basicexception(SbERR_METHOD_FAILED, OUString());
276 0 : pStyles->Delete( _sStyleName );
277 : }
278 0 : return;
279 : }
280 0 : }
281 : }
282 0 : catch (const uno::Exception&)
283 : {
284 0 : DebugHelper::basicexception(SbERR_METHOD_FAILED, OUString());
285 : }
286 : }
287 :
288 : OUString
289 0 : ScVbaFormatConditions::getServiceImplName()
290 : {
291 0 : return OUString("ScVbaFormatConditions");
292 : }
293 :
294 : uno::Sequence< OUString >
295 0 : ScVbaFormatConditions::getServiceNames()
296 : {
297 0 : static uno::Sequence< OUString > aServiceNames;
298 0 : if ( aServiceNames.getLength() == 0 )
299 : {
300 0 : aServiceNames.realloc( 1 );
301 0 : aServiceNames[ 0 ] = "ooo.vba.excel.FormatConditions";
302 : }
303 0 : return aServiceNames;
304 9 : }
305 :
306 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|