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