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 : #include "vbastyles.hxx"
20 : #include "vbastyle.hxx"
21 : #include <cppuhelper/exc_hlp.hxx>
22 : #include <ooo/vba/excel/XRange.hpp>
23 :
24 : using namespace ::ooo::vba;
25 : using namespace ::com::sun::star;
26 :
27 : static css::uno::Any
28 0 : lcl_createAPIStyleToVBAObject( const css::uno::Any& aObject, const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel )
29 : {
30 0 : uno::Reference< beans::XPropertySet > xStyleProps( aObject, uno::UNO_QUERY_THROW );
31 0 : uno::Reference< excel::XStyle > xStyle( new ScVbaStyle( xParent, xContext, xStyleProps, xModel ) );
32 0 : return uno::makeAny( xStyle );
33 : }
34 :
35 0 : 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 )
36 : {
37 : try
38 : {
39 0 : mxMSF.set( mxModel, uno::UNO_QUERY_THROW );
40 0 : mxNameContainerCellStyles.set( m_xNameAccess, uno::UNO_QUERY_THROW );
41 : }
42 0 : catch (uno::Exception& )
43 : {
44 0 : DebugHelper::basicexception(SbERR_METHOD_FAILED, OUString());
45 : }
46 0 : }
47 :
48 : uno::Sequence< OUString >
49 0 : ScVbaStyles::getStyleNames() throw ( uno::RuntimeException )
50 : {
51 0 : return mxNameContainerCellStyles->getElementNames();
52 : }
53 :
54 : uno::Any
55 0 : ScVbaStyles::createCollectionObject(const uno::Any& aObject)
56 : {
57 0 : return lcl_createAPIStyleToVBAObject( aObject, mxParent, mxContext, mxModel );
58 : }
59 :
60 : uno::Type SAL_CALL
61 0 : ScVbaStyles::getElementType() throw (uno::RuntimeException)
62 : {
63 0 : return cppu::UnoType<excel::XStyle>::get();
64 : }
65 :
66 : namespace {
67 :
68 0 : class EnumWrapper : public EnumerationHelper_BASE
69 : {
70 : uno::Reference<container::XIndexAccess > m_xIndexAccess;
71 : uno::Reference<XHelperInterface > m_xParent;
72 : uno::Reference<uno::XComponentContext > m_xContext;
73 : uno::Reference<frame::XModel > m_xModel;
74 :
75 : sal_Int32 nIndex;
76 : public:
77 0 : 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 ) {}
78 0 : virtual sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
79 : {
80 0 : return ( nIndex < m_xIndexAccess->getCount() );
81 : }
82 0 : virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
83 : {
84 : try
85 : {
86 0 : if ( nIndex < m_xIndexAccess->getCount() )
87 0 : return lcl_createAPIStyleToVBAObject( m_xIndexAccess->getByIndex( nIndex++ ), m_xParent, m_xContext, m_xModel );
88 : }
89 0 : catch (const container::NoSuchElementException&)
90 : {
91 0 : throw;
92 : }
93 0 : catch (const lang::WrappedTargetException&)
94 : {
95 0 : throw;
96 : }
97 0 : catch (const uno::RuntimeException&)
98 : {
99 0 : throw;
100 : }
101 0 : catch (const uno::Exception& e)
102 : {
103 0 : css::uno::Any a(cppu::getCaughtException());
104 : throw css::lang::WrappedTargetException(
105 0 : "wrapped Exception " + e.Message,
106 0 : css::uno::Reference<css::uno::XInterface>(), a);
107 : }
108 0 : throw container::NoSuchElementException();
109 : }
110 : };
111 :
112 : }
113 :
114 : uno::Reference< container::XEnumeration > SAL_CALL
115 0 : ScVbaStyles::createEnumeration() throw (uno::RuntimeException)
116 : {
117 0 : return new EnumWrapper( m_xIndexAccess, mxParent, mxContext, mxModel );
118 : }
119 :
120 : uno::Reference< excel::XStyle > SAL_CALL
121 0 : ScVbaStyles::Add( const OUString& _sName, const uno::Any& _aBasedOn ) throw (script::BasicErrorException, uno::RuntimeException, std::exception)
122 : {
123 0 : uno::Reference< excel::XStyle > aRet;
124 : try
125 : {
126 0 : OUString sParentCellStyleName("Default");
127 0 : if ( _aBasedOn.hasValue() )
128 : {
129 0 : uno::Reference< excel::XRange > oRange;
130 0 : if ( _aBasedOn >>= oRange)
131 : {
132 0 : uno::Reference< excel::XStyle > oStyle( oRange->getStyle(), uno::UNO_QUERY_THROW );
133 0 : if ( oStyle.is() )
134 : {
135 0 : sParentCellStyleName = oStyle->getName();
136 : }
137 : else
138 : {
139 0 : DebugHelper::basicexception(SbERR_BAD_ARGUMENT, OUString() );
140 0 : }
141 : }
142 : else
143 : {
144 0 : DebugHelper::basicexception(SbERR_BAD_ARGUMENT, OUString());
145 0 : }
146 : }
147 :
148 0 : uno::Reference< style::XStyle > xStyle( mxMSF->createInstance("com.sun.star.style.CellStyle"), uno::UNO_QUERY_THROW );
149 :
150 0 : if (!mxNameContainerCellStyles->hasByName(_sName))
151 : {
152 0 : mxNameContainerCellStyles->insertByName(_sName, uno::makeAny( xStyle) );
153 : }
154 0 : if (sParentCellStyleName != "Default")
155 : {
156 0 : xStyle->setParentStyle( sParentCellStyleName );
157 : }
158 0 : aRet.set( Item( uno::makeAny( _sName ), uno::Any() ), uno::UNO_QUERY_THROW );
159 : }
160 0 : catch (const uno::Exception&)
161 : {
162 0 : DebugHelper::basicexception(SbERR_METHOD_FAILED, OUString());
163 : }
164 0 : return aRet;
165 : }
166 :
167 : void
168 0 : ScVbaStyles::Delete(const OUString& _sStyleName) throw ( script::BasicErrorException )
169 : {
170 : try
171 : {
172 0 : if (mxNameContainerCellStyles->hasByName( _sStyleName ) )
173 0 : mxNameContainerCellStyles->removeByName( _sStyleName );
174 : }
175 0 : catch (const uno::Exception&)
176 : {
177 0 : DebugHelper::basicexception(SbERR_METHOD_FAILED, OUString());
178 : }
179 0 : }
180 :
181 : OUString
182 0 : ScVbaStyles::getServiceImplName()
183 : {
184 0 : return OUString("ScVbaStyles");
185 : }
186 :
187 : uno::Sequence< OUString >
188 0 : ScVbaStyles::getServiceNames()
189 : {
190 0 : static uno::Sequence< OUString > aServiceNames;
191 0 : if ( aServiceNames.getLength() == 0 )
192 : {
193 0 : aServiceNames.realloc( 1 );
194 0 : aServiceNames[ 0 ] = "ooo.vba.excel.XStyles";
195 : }
196 0 : return aServiceNames;
197 : }
198 :
199 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|