xref: /aoo41x/main/sw/source/ui/vba/vbaparagraph.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #include "vbaparagraph.hxx"
28 #include <vbahelper/vbahelper.hxx>
29 #include <tools/diagnose_ex.h>
30 #include "vbarange.hxx"
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 
33 using namespace ::ooo::vba;
34 using namespace ::com::sun::star;
35 
36 SwVbaParagraph::SwVbaParagraph( const uno::Reference< ooo::vba::XHelperInterface >& rParent, const uno::Reference< uno::XComponentContext >& rContext, const uno::Reference< text::XTextDocument >& xDocument, const uno::Reference< text::XTextRange >& xTextRange ) throw ( uno::RuntimeException ) :
37     SwVbaParagraph_BASE( rParent, rContext ), mxTextDocument( xDocument ), mxTextRange( xTextRange )
38 {
39 }
40 
41 SwVbaParagraph::~SwVbaParagraph()
42 {
43 }
44 
45 uno::Reference< word::XRange > SAL_CALL
46 SwVbaParagraph::getRange( ) throw ( uno::RuntimeException )
47 {
48     return uno::Reference< word::XRange >( new SwVbaRange( this, mxContext, mxTextDocument, mxTextRange->getStart(), mxTextRange->getEnd(), mxTextRange->getText(), sal_True ) );
49 }
50 
51 rtl::OUString&
52 SwVbaParagraph::getServiceImplName()
53 {
54 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaParagraph") );
55 	return sImplName;
56 }
57 
58 uno::Sequence< rtl::OUString >
59 SwVbaParagraph::getServiceNames()
60 {
61 	static uno::Sequence< rtl::OUString > aServiceNames;
62 	if ( aServiceNames.getLength() == 0 )
63 	{
64 		aServiceNames.realloc( 1 );
65 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Paragraph" ) );
66 	}
67 	return aServiceNames;
68 }
69 
70 
71 //typedef ::cppu::WeakImplHelper1< container::XEnumeration > ParagraphEnumeration_BASE;
72 typedef ::cppu::WeakImplHelper2< container::XIndexAccess, container::XEnumerationAccess > ParagraphCollectionHelper_BASE;
73 
74 class ParagraphCollectionHelper : public ParagraphCollectionHelper_BASE
75 {
76 private:
77     uno::Reference< text::XTextDocument > mxTextDocument;
78 
79     uno::Reference< container::XEnumeration > getEnumeration() throw (uno::RuntimeException)
80     {
81         uno::Reference< container::XEnumerationAccess > xParEnumAccess( mxTextDocument->getText(), uno::UNO_QUERY_THROW );
82         return xParEnumAccess->createEnumeration();
83     }
84 
85 public:
86     ParagraphCollectionHelper( const uno::Reference< text::XTextDocument >& xDocument ) throw (uno::RuntimeException): mxTextDocument( xDocument )
87     {
88     }
89 	// XElementAccess
90 	virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException) { return  text::XTextRange::static_type(0); }
91 	virtual ::sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException) { return sal_True; }
92 	// XIndexAccess
93 	virtual ::sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException)
94     {
95         sal_Int32 nCount = 0;
96         uno::Reference< container::XEnumeration > xParEnum = getEnumeration();
97         while( xParEnum->hasMoreElements() )
98         {
99             uno::Reference< lang::XServiceInfo > xServiceInfo( xParEnum->nextElement(), uno::UNO_QUERY_THROW );
100             if( xServiceInfo->supportsService( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.Paragraph") ) ) )
101             {
102                 nCount++;
103             }
104         }
105         return nCount;
106     }
107 	virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException )
108 	{
109         if( Index < getCount() )
110         {
111             sal_Int32 nCount = 0;
112             uno::Reference< container::XEnumeration > xParEnum = getEnumeration();
113             while( xParEnum->hasMoreElements() )
114             {
115                 uno::Reference< lang::XServiceInfo > xServiceInfo( xParEnum->nextElement(), uno::UNO_QUERY_THROW );
116                 if( xServiceInfo->supportsService( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.Paragraph") ) ) )
117                 {
118                     if( Index == nCount )
119                         return uno::makeAny( xServiceInfo );
120                     nCount++;
121                 }
122             }
123         }
124         throw lang::IndexOutOfBoundsException();
125     }
126 	// XEnumerationAccess
127 	virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration(  ) throw (uno::RuntimeException)
128 	{
129 		return getEnumeration();
130     }
131 };
132 
133 SwVbaParagraphs::SwVbaParagraphs( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< ::com::sun::star::uno::XComponentContext > & xContext, const uno::Reference< text::XTextDocument >& xDocument ) throw (uno::RuntimeException) : SwVbaParagraphs_BASE( xParent, xContext, new ParagraphCollectionHelper( xDocument ) ), mxTextDocument( xDocument )
134 {
135 }
136 
137 // XEnumerationAccess
138 uno::Type
139 SwVbaParagraphs::getElementType() throw (uno::RuntimeException)
140 {
141 	return word::XParagraph::static_type(0);
142 }
143 uno::Reference< container::XEnumeration >
144 SwVbaParagraphs::createEnumeration() throw (uno::RuntimeException)
145 {
146     uno::Reference< container::XEnumerationAccess > xEnumerationAccess( m_xIndexAccess, uno::UNO_QUERY_THROW );
147     return xEnumerationAccess->createEnumeration();
148 }
149 
150 uno::Any
151 SwVbaParagraphs::createCollectionObject( const css::uno::Any& aSource )
152 {
153     uno::Reference< text::XTextRange > xTextRange( aSource, uno::UNO_QUERY_THROW );
154     return uno::makeAny( uno::Reference< word::XParagraph >( new SwVbaParagraph( this, mxContext, mxTextDocument, xTextRange ) ) );
155 }
156 
157 rtl::OUString&
158 SwVbaParagraphs::getServiceImplName()
159 {
160 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaParagraphs") );
161 	return sImplName;
162 }
163 
164 css::uno::Sequence<rtl::OUString>
165 SwVbaParagraphs::getServiceNames()
166 {
167 	static uno::Sequence< rtl::OUString > sNames;
168 	if ( sNames.getLength() == 0 )
169 	{
170 		sNames.realloc( 1 );
171 		sNames[0] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Paragraphs") );
172 	}
173 	return sNames;
174 }
175