1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 #include "vbastyles.hxx" 24 #include "vbastyle.hxx" 25 #include <ooo/vba/excel/XRange.hpp> 26 27 using namespace ::ooo::vba; 28 using namespace ::com::sun::star; 29 30 static rtl::OUString SDEFAULTCELLSTYLENAME( RTL_CONSTASCII_USTRINGPARAM("Default") ); 31 css::uno::Any 32 lcl_createAPIStyleToVBAObject( const css::uno::Any& aObject, const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel ) 33 { 34 uno::Reference< beans::XPropertySet > xStyleProps( aObject, uno::UNO_QUERY_THROW ); 35 uno::Reference< excel::XStyle > xStyle( new ScVbaStyle( xParent, xContext, xStyleProps, xModel ) ); 36 return uno::makeAny( xStyle ); 37 } 38 39 40 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 ) 41 { 42 try 43 { 44 mxMSF.set( mxModel, uno::UNO_QUERY_THROW ); 45 mxNameContainerCellStyles.set( m_xNameAccess, uno::UNO_QUERY_THROW ); 46 } 47 catch (uno::Exception& ) 48 { 49 DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString()); 50 } 51 } 52 53 uno::Sequence< rtl::OUString > 54 ScVbaStyles::getStyleNames() throw ( uno::RuntimeException ) 55 { 56 return mxNameContainerCellStyles->getElementNames(); 57 } 58 59 60 uno::Any 61 ScVbaStyles::createCollectionObject(const uno::Any& aObject) 62 { 63 return lcl_createAPIStyleToVBAObject( aObject, mxParent, mxContext, mxModel ); 64 } 65 66 uno::Type SAL_CALL 67 ScVbaStyles::getElementType() throw (uno::RuntimeException) 68 { 69 return excel::XStyle::static_type(0); 70 } 71 72 class EnumWrapper : public EnumerationHelper_BASE 73 { 74 uno::Reference<container::XIndexAccess > m_xIndexAccess; 75 uno::Reference<XHelperInterface > m_xParent; 76 uno::Reference<uno::XComponentContext > m_xContext; 77 uno::Reference<frame::XModel > m_xModel; 78 79 sal_Int32 nIndex; 80 public: 81 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 ) {} 82 virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException) 83 { 84 return ( nIndex < m_xIndexAccess->getCount() ); 85 } 86 virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) 87 { 88 if ( nIndex < m_xIndexAccess->getCount() ) 89 return lcl_createAPIStyleToVBAObject( m_xIndexAccess->getByIndex( nIndex++ ), m_xParent, m_xContext, m_xModel ); 90 throw container::NoSuchElementException(); 91 } 92 }; 93 94 uno::Reference< container::XEnumeration > SAL_CALL 95 ScVbaStyles::createEnumeration() throw (uno::RuntimeException) 96 { 97 return new EnumWrapper( m_xIndexAccess, mxParent, mxContext, mxModel ); 98 } 99 100 uno::Reference< excel::XStyle > SAL_CALL 101 ScVbaStyles::Add( const ::rtl::OUString& _sName, const uno::Any& _aBasedOn ) throw (script::BasicErrorException, uno::RuntimeException) 102 { 103 uno::Reference< excel::XStyle > aRet; 104 try 105 { 106 rtl::OUString sParentCellStyleName( RTL_CONSTASCII_USTRINGPARAM("Default")); 107 if ( _aBasedOn.hasValue() ) 108 { 109 uno::Reference< excel::XRange > oRange; 110 if ( _aBasedOn >>= oRange) 111 { 112 uno::Reference< excel::XStyle > oStyle( oRange->getStyle(), uno::UNO_QUERY_THROW ); 113 if ( oStyle.is() ) 114 { 115 sParentCellStyleName = oStyle->getName(); 116 } 117 else 118 { 119 DebugHelper::exception(SbERR_BAD_ARGUMENT, rtl::OUString() ); 120 } 121 } 122 else 123 { 124 DebugHelper::exception(SbERR_BAD_ARGUMENT, rtl::OUString()); 125 } 126 } 127 128 uno::Reference< style::XStyle > xStyle( mxMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.style.CellStyle"))), uno::UNO_QUERY_THROW ); 129 130 if (!mxNameContainerCellStyles->hasByName(_sName)) 131 { 132 mxNameContainerCellStyles->insertByName(_sName, uno::makeAny( xStyle) ); 133 } 134 if (!sParentCellStyleName.equals(SDEFAULTCELLSTYLENAME)) 135 { 136 xStyle->setParentStyle( sParentCellStyleName ); 137 } 138 aRet.set( Item( uno::makeAny( _sName ), uno::Any() ), uno::UNO_QUERY_THROW ); 139 } 140 catch (uno::Exception& ) 141 { 142 DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString()); 143 } 144 return aRet; 145 } 146 147 void 148 ScVbaStyles::Delete(const rtl::OUString _sStyleName) throw ( script::BasicErrorException ) 149 { 150 try 151 { 152 if (mxNameContainerCellStyles->hasByName( _sStyleName ) ) 153 mxNameContainerCellStyles->removeByName( _sStyleName ); 154 } 155 catch (uno::Exception& ) 156 { 157 DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString()); 158 } 159 } 160 161 rtl::OUString& 162 ScVbaStyles::getServiceImplName() 163 { 164 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaStyles") ); 165 return sImplName; 166 } 167 168 uno::Sequence< rtl::OUString > 169 ScVbaStyles::getServiceNames() 170 { 171 static uno::Sequence< rtl::OUString > aServiceNames; 172 if ( aServiceNames.getLength() == 0 ) 173 { 174 aServiceNames.realloc( 1 ); 175 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.XStyles" ) ); 176 } 177 return aServiceNames; 178 } 179