summaryrefslogtreecommitdiff
path: root/sc/source/ui/vba/vbaformatconditions.cxx
blob: b5a6dc9e992b85ade3348ec8af3cc718509d777a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#include <ooo/vba/excel/XRange.hpp>
#include <com/sun/star/sheet/XCellRangeAddressable.hpp>
#include <com/sun/star/sheet/XSheetConditionalEntry.hpp>
#include <vector>
#include "vbaformatconditions.hxx"
#include "vbaformatcondition.hxx"
#include "vbaworkbook.hxx"
#include "vbastyles.hxx"
using namespace ::ooo::vba;
using namespace ::com::sun::star;

typedef std::vector< beans::PropertyValue > VecPropValues;

static rtl::OUString OPERATOR( RTL_CONSTASCII_USTRINGPARAM("Operator") );
static rtl::OUString FORMULA1( RTL_CONSTASCII_USTRINGPARAM("Formula1") );
static rtl::OUString FORMULA2( RTL_CONSTASCII_USTRINGPARAM("Formula2") );
static rtl::OUString STYLENAME( RTL_CONSTASCII_USTRINGPARAM("StyleName") );
static rtl::OUString sStyleNamePrefix( RTL_CONSTASCII_USTRINGPARAM("Excel_CondFormat") );

ScVbaFormatConditions::ScVbaFormatConditions( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< sheet::XSheetConditionalEntries >& _xSheetConditionalEntries, const uno::Reference< frame::XModel >& xModel ) : ScVbaFormatConditions_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( _xSheetConditionalEntries, uno::UNO_QUERY_THROW ) ), mxSheetConditionalEntries( _xSheetConditionalEntries )
{
    mxRangeParent.set( xParent, uno::UNO_QUERY_THROW );
    uno::Reference< excel::XWorkbook > xWorkbook = new ScVbaWorkbook(  uno::Reference< XHelperInterface >( ScVbaGlobals::getGlobalsImpl( xContext )->getApplication(), uno::UNO_QUERY_THROW ), xContext, xModel );
    mxStyles.set( xWorkbook->Styles( uno::Any() ), uno::UNO_QUERY_THROW );
    uno::Reference< sheet::XCellRangeAddressable > xCellRange( mxRangeParent->getCellRange(), uno::UNO_QUERY_THROW );
    mxParentRangePropertySet.set( xCellRange, uno::UNO_QUERY_THROW );
    
    table::CellRangeAddress rangeAddress = xCellRange->getRangeAddress();
    maCellAddress = table::CellAddress( rangeAddress.Sheet, rangeAddress.StartColumn,  rangeAddress.StartRow );
}

void SAL_CALL 
ScVbaFormatConditions::Delete(  ) throw (script::BasicErrorException, uno::RuntimeException)
{
    try
    {
        ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
        if ( !pStyles )
            DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
        sal_Int32 nCount = mxSheetConditionalEntries->getCount();
        for (sal_Int32 i = nCount - 1; i >= 0; i--)
        {
            uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
            pStyles->Delete(xSheetConditionalEntry->getStyleName());
            mxSheetConditionalEntries->removeByIndex(i);           
        }
        notifyRange();
    }
    catch (uno::Exception& )
    {
        DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
    }
}

uno::Type SAL_CALL 
ScVbaFormatConditions::getElementType() throw (css::uno::RuntimeException)
{
    return excel::XFormatCondition::static_type(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 ) 
{
    uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry;
    aObject >>= xSheetConditionalEntry;

    uno::Reference< excel::XStyle > xStyle( xStyles->Item( uno::makeAny( xSheetConditionalEntry->getStyleName() ), uno::Any() ), uno::UNO_QUERY_THROW );
    uno::Reference< excel::XFormatCondition > xCondition = new ScVbaFormatCondition( xRangeParent, xContext,  xSheetConditionalEntry, xStyle, xFormatConditions, xRangeProps ); 
    return uno::makeAny( xCondition );
}

uno::Any 
ScVbaFormatConditions::createCollectionObject(const uno::Any& aObject )
{
    return xSheetConditionToFormatCondition( uno::Reference< XHelperInterface >( mxRangeParent, uno::UNO_QUERY_THROW ), mxContext, mxStyles, this, mxParentRangePropertySet, aObject );
}

class EnumWrapper : public EnumerationHelper_BASE
{
        uno::Reference<container::XIndexAccess > m_xIndexAccess;
        uno::Reference<excel::XRange > m_xParentRange;
        uno::Reference<uno::XComponentContext > m_xContext;
        uno::Reference<excel::XStyles > m_xStyles;
        uno::Reference<excel::XFormatConditions > m_xParentCollection;
        uno::Reference<beans::XPropertySet > m_xProps;

        sal_Int32 nIndex;
public:
        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 ) {}
        virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
        {
                return ( nIndex < m_xIndexAccess->getCount() );
        }

        virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
        {
                if ( nIndex < m_xIndexAccess->getCount() )
                        return xSheetConditionToFormatCondition( uno::Reference< XHelperInterface >( m_xParentRange, uno::UNO_QUERY_THROW ), m_xContext, m_xStyles, m_xParentCollection, m_xProps, m_xIndexAccess->getByIndex( nIndex++ ) );
                throw container::NoSuchElementException();
        }
};

uno::Reference< excel::XFormatCondition > SAL_CALL 
ScVbaFormatConditions::Add( ::sal_Int32 _nType, const uno::Any& _aOperator, const uno::Any& _aFormula1, const uno::Any& _aFormula2 ) throw (script::BasicErrorException, uno::RuntimeException)
{
    return Add( _nType, _aOperator, _aFormula1, _aFormula2, uno::Reference< excel::XStyle >() );
}

uno::Reference< excel::XFormatCondition > 
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)
{
    // #TODO
    // #FIXME 
    // This method will NOT handle r1c1 formulas [*]and only assumes that
    // the formulas are _xlA1 based ( need to hook into calc work ths should
    // address this )
    // [*] reason: getA1Formula method below is just a hook and just 
    // returns whats it gets ( e.g. doesn't convert anything )
    uno::Reference< excel::XStyle > xStyle( _xStyle );
    uno::Reference< excel::XFormatCondition > xFormatCondition;
    try
    {
        rtl::OUString sStyleName;
        if ( !xStyle.is() )
        {	
            sStyleName = getStyleName();
            xStyle = mxStyles->Add(sStyleName, uno::Any() );
        }
        else
        {
            sStyleName = xStyle->getName();
        }

        VecPropValues aPropertyValueVector;
        sheet::ConditionOperator aType = ScVbaFormatCondition::retrieveAPIType(_nType, uno::Reference< sheet::XSheetCondition >() );
        uno::Any aValue;

        if ( aType == sheet::ConditionOperator_FORMULA)
            aValue = uno::makeAny( sheet::ConditionOperator_FORMULA );
        else
            aValue = uno::makeAny( ScVbaFormatCondition::retrieveAPIOperator(_aOperator) );

        beans::PropertyValue aProperty( OPERATOR, 0, aValue, beans::PropertyState_DIRECT_VALUE );
        aPropertyValueVector.push_back( aProperty );

        if ( _aFormula1.hasValue() )
        {        
            beans::PropertyValue aProp( FORMULA1, 0, uno::makeAny( getA1Formula( _aFormula1 ) ), beans::PropertyState_DIRECT_VALUE );
            aPropertyValueVector.push_back( aProp );
        }
        if ( _aFormula2.hasValue() )
        {        
            beans::PropertyValue aProp( FORMULA2, 0, uno::makeAny( getA1Formula( _aFormula2 ) ), beans::PropertyState_DIRECT_VALUE );
            aPropertyValueVector.push_back( aProp );
        }
        aProperty.Name = STYLENAME;
        aProperty.Value = uno::makeAny( sStyleName );

        // convert vector to sequence
        uno::Sequence< beans::PropertyValue > aPropertyValueList(aPropertyValueVector.size());
        VecPropValues::iterator it = aPropertyValueVector.begin();
        VecPropValues::iterator it_end = aPropertyValueVector.end();
        for ( sal_Int32 index=0; it != it_end; ++it )
            aPropertyValueList[ index++ ] = *it;

        mxSheetConditionalEntries->addNew(aPropertyValueList);
        for (sal_Int32 i = mxSheetConditionalEntries->getCount()-1; i >= 0; i--)
        {
            uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
            if (xSheetConditionalEntry->getStyleName().equals(sStyleName))
            {
                xFormatCondition =  new ScVbaFormatCondition(uno::Reference< XHelperInterface >( mxRangeParent, uno::UNO_QUERY_THROW ), mxContext, xSheetConditionalEntry, xStyle, this, mxParentRangePropertySet);
                notifyRange();
                return xFormatCondition;
            }
        }
    }
    catch (uno::Exception& )
    {
    }
    DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
    return xFormatCondition;
}


uno::Reference< container::XEnumeration > SAL_CALL 
ScVbaFormatConditions::createEnumeration() throw (uno::RuntimeException)
{
    return new EnumWrapper( m_xIndexAccess, mxRangeParent, mxContext, mxStyles, this, mxParentRangePropertySet  );
}


void 
ScVbaFormatConditions::notifyRange() throw ( script::BasicErrorException )
{
    try
    {
        mxParentRangePropertySet->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ConditionalFormat")), uno::makeAny( mxSheetConditionalEntries ));
    }
    catch (uno::Exception& )
    {
        DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
    }
}

rtl::OUString 
ScVbaFormatConditions::getA1Formula(const css::uno::Any& _aFormula) throw ( script::BasicErrorException )
{
    // #TODO, #FIXME hook-in proper formula conversion detection & logic
    rtl::OUString sFormula;
    if ( !( _aFormula >>= sFormula ) )
        DebugHelper::exception(SbERR_BAD_PARAMETER, rtl::OUString() );
    return sFormula;
}

rtl::OUString 
ScVbaFormatConditions::getStyleName()
{
    ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
    if ( !pStyles )
        DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
    uno::Sequence< rtl::OUString > sCellStyleNames = pStyles->getStyleNames();
    return ContainerUtilities::getUniqueName(sCellStyleNames, sStyleNamePrefix, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("_") ));
}

void 
ScVbaFormatConditions::removeFormatCondition( const rtl::OUString& _sStyleName, sal_Bool _bRemoveStyle) throw ( script::BasicErrorException )
{
    try
    {
        sal_Int32 nElems = mxSheetConditionalEntries->getCount();
        for (sal_Int32 i = 0; i < nElems; i++)
        {
            uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
            if (_sStyleName.equals(xSheetConditionalEntry->getStyleName()))
            {
                mxSheetConditionalEntries->removeByIndex(i);
                if (_bRemoveStyle)
                {
                    ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
                    if ( !pStyles )
                        DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
                    pStyles->Delete( _sStyleName );
                }
                return;
            }
        }
    }
    catch (uno::Exception& )
    {
        DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
    }
}

rtl::OUString& 
ScVbaFormatConditions::getServiceImplName()
{
    static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaFormatConditions") );
    return sImplName;
}

uno::Sequence< rtl::OUString > 
ScVbaFormatConditions::getServiceNames()
{
    static uno::Sequence< rtl::OUString > aServiceNames;
    if ( aServiceNames.getLength() == 0 )
    {
        aServiceNames.realloc( 1 );
        aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.FormatConditions" ) );
    }
    return aServiceNames;
}