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