1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vbastyles.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "vbastyles.hxx"
31 #include "vbastyle.hxx"
32 #include <ooo/vba/excel/XRange.hpp>
34 using namespace ::ooo::vba
;
35 using namespace ::com::sun::star
;
37 static rtl::OUString
SDEFAULTCELLSTYLENAME( RTL_CONSTASCII_USTRINGPARAM("Default") );
39 lcl_createAPIStyleToVBAObject( const css::uno::Any
& aObject
, const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Reference
< frame::XModel
>& xModel
)
41 uno::Reference
< beans::XPropertySet
> xStyleProps( aObject
, uno::UNO_QUERY_THROW
);
42 uno::Reference
< excel::XStyle
> xStyle( new ScVbaStyle( xParent
, xContext
, xStyleProps
, xModel
) );
43 return uno::makeAny( xStyle
);
47 ScVbaStyles::ScVbaStyles( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< css::uno::XComponentContext
> & xContext
, const uno::Reference
< frame::XModel
>& xModel
) throw ( script::BasicErrorException
) : ScVbaStyles_BASE( xParent
, xContext
, uno::Reference
< container::XIndexAccess
>( ScVbaStyle::getStylesNameContainer( xModel
), uno::UNO_QUERY_THROW
) ), mxModel( xModel
), mxParent( xParent
)
51 mxMSF
.set( mxModel
, uno::UNO_QUERY_THROW
);
52 mxNameContainerCellStyles
.set( m_xNameAccess
, uno::UNO_QUERY_THROW
);
54 catch (uno::Exception
& )
56 DebugHelper::exception(SbERR_METHOD_FAILED
, rtl::OUString());
60 uno::Sequence
< rtl::OUString
>
61 ScVbaStyles::getStyleNames() throw ( uno::RuntimeException
)
63 return mxNameContainerCellStyles
->getElementNames();
68 ScVbaStyles::createCollectionObject(const uno::Any
& aObject
)
70 return lcl_createAPIStyleToVBAObject( aObject
, mxParent
, mxContext
, mxModel
);
74 ScVbaStyles::getElementType() throw (uno::RuntimeException
)
76 return excel::XStyle::static_type(0);
79 class EnumWrapper
: public EnumerationHelper_BASE
81 uno::Reference
<container::XIndexAccess
> m_xIndexAccess
;
82 uno::Reference
<XHelperInterface
> m_xParent
;
83 uno::Reference
<uno::XComponentContext
> m_xContext
;
84 uno::Reference
<frame::XModel
> m_xModel
;
88 EnumWrapper( const uno::Reference
< container::XIndexAccess
>& xIndexAccess
, const uno::Reference
<XHelperInterface
>& xParent
, const uno::Reference
<uno::XComponentContext
>& xContext
, const uno::Reference
<frame::XModel
>& xModel
) : m_xIndexAccess( xIndexAccess
), m_xParent( xParent
), m_xContext( xContext
), m_xModel( xModel
), nIndex( 0 ) {}
89 virtual ::sal_Bool SAL_CALL
hasMoreElements( ) throw (uno::RuntimeException
)
91 return ( nIndex
< m_xIndexAccess
->getCount() );
93 virtual uno::Any SAL_CALL
nextElement( ) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
95 if ( nIndex
< m_xIndexAccess
->getCount() )
96 return lcl_createAPIStyleToVBAObject( m_xIndexAccess
->getByIndex( nIndex
++ ), m_xParent
, m_xContext
, m_xModel
);
97 throw container::NoSuchElementException();
101 uno::Reference
< container::XEnumeration
> SAL_CALL
102 ScVbaStyles::createEnumeration() throw (uno::RuntimeException
)
104 return new EnumWrapper( m_xIndexAccess
, mxParent
, mxContext
, mxModel
);
107 uno::Reference
< excel::XStyle
> SAL_CALL
108 ScVbaStyles::Add( const ::rtl::OUString
& _sName
, const uno::Any
& _aBasedOn
) throw (script::BasicErrorException
, uno::RuntimeException
)
110 uno::Reference
< excel::XStyle
> aRet
;
113 rtl::OUString
sParentCellStyleName( RTL_CONSTASCII_USTRINGPARAM("Default"));
114 if ( _aBasedOn
.hasValue() )
116 uno::Reference
< excel::XRange
> oRange
;
117 if ( _aBasedOn
>>= oRange
)
119 uno::Reference
< excel::XStyle
> oStyle( oRange
->getStyle(), uno::UNO_QUERY_THROW
);
122 sParentCellStyleName
= oStyle
->getName();
126 DebugHelper::exception(SbERR_BAD_ARGUMENT
, rtl::OUString() );
131 DebugHelper::exception(SbERR_BAD_ARGUMENT
, rtl::OUString());
135 uno::Reference
< style::XStyle
> xStyle( mxMSF
->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.style.CellStyle"))), uno::UNO_QUERY_THROW
);
137 if (!mxNameContainerCellStyles
->hasByName(_sName
))
139 mxNameContainerCellStyles
->insertByName(_sName
, uno::makeAny( xStyle
) );
141 if (!sParentCellStyleName
.equals(SDEFAULTCELLSTYLENAME
))
143 xStyle
->setParentStyle( sParentCellStyleName
);
145 aRet
.set( Item( uno::makeAny( _sName
), uno::Any() ), uno::UNO_QUERY_THROW
);
147 catch (uno::Exception
& )
149 DebugHelper::exception(SbERR_METHOD_FAILED
, rtl::OUString());
155 ScVbaStyles::Delete(const rtl::OUString _sStyleName
) throw ( script::BasicErrorException
)
159 if (mxNameContainerCellStyles
->hasByName( _sStyleName
) )
160 mxNameContainerCellStyles
->removeByName( _sStyleName
);
162 catch (uno::Exception
& )
164 DebugHelper::exception(SbERR_METHOD_FAILED
, rtl::OUString());
169 ScVbaStyles::getServiceImplName()
171 static rtl::OUString
sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaStyles") );
175 uno::Sequence
< rtl::OUString
>
176 ScVbaStyles::getServiceNames()
178 static uno::Sequence
< rtl::OUString
> aServiceNames
;
179 if ( aServiceNames
.getLength() == 0 )
181 aServiceNames
.realloc( 1 );
182 aServiceNames
[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.XStyles" ) );
184 return aServiceNames
;