xref: /aoo42x/main/sw/source/ui/vba/vbatables.cxx (revision cdf0e10c)
1 #include "vbatables.hxx"
2 #include "vbatable.hxx"
3 #include "vbarange.hxx"
4 #include <com/sun/star/text/XTextTable.hpp>
5 #include <com/sun/star/text/XTextTablesSupplier.hpp>
6 #include <com/sun/star/text/XTextDocument.hpp>
7 #include <comphelper/componentcontext.hxx>
8 
9 using namespace ::ooo::vba;
10 using namespace css;
11 
12 uno::Reference< container::XIndexAccess > lcl_getTables( const uno::Reference< frame::XModel >& xDoc )
13 {
14     uno::Reference< container::XIndexAccess > xTables;
15     uno::Reference< text::XTextTablesSupplier > xSupp( xDoc, uno::UNO_QUERY );
16     if ( xSupp.is() )
17         xTables.set( xSupp->getTextTables(), uno::UNO_QUERY_THROW );
18     return xTables;
19 }
20 
21 uno::Any lcl_createTable( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xDocument, const uno::Any& aSource )
22 {
23     uno::Reference< text::XTextTable > xTextTable( aSource, uno::UNO_QUERY_THROW );
24     uno::Reference< text::XTextDocument > xTextDocument( xDocument, uno::UNO_QUERY_THROW );
25     uno::Reference< word::XTable > xTable( new SwVbaTable( xParent, xContext, xTextDocument, xTextTable ) );
26     return uno::makeAny( xTable );
27 }
28 
29 typedef ::cppu::WeakImplHelper1< css::container::XEnumeration > EnumBase;
30 
31 class TableEnumerationImpl : public EnumBase
32 {
33     uno::Reference< XHelperInterface > mxParent;
34     uno::Reference< uno::XComponentContext > mxContext;
35     uno::Reference< frame::XModel > mxDocument;
36     uno::Reference< container::XIndexAccess > mxIndexAccess;
37     sal_Int32 mnCurIndex;
38 public:
39     TableEnumerationImpl(  const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xDocument, const uno::Reference< container::XIndexAccess >& xIndexAccess ) : mxParent( xParent ), mxContext( xContext ), mxDocument( xDocument ), mxIndexAccess( xIndexAccess ), mnCurIndex(0)
40     {
41     }
42     virtual ::sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException)
43     {
44         return ( mnCurIndex < mxIndexAccess->getCount() );
45     }
46     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
47     {
48         if ( !hasMoreElements() )
49             throw container::NoSuchElementException();
50         return lcl_createTable( mxParent, mxContext, mxDocument, mxIndexAccess->getByIndex( mnCurIndex++ ) );
51     }
52 
53 };
54 
55 SwVbaTables::SwVbaTables( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xDocument ) : SwVbaTables_BASE( xParent, xContext , lcl_getTables( xDocument ) ), mxDocument( xDocument )
56 {
57 }
58 
59 
60 uno::Reference< word::XTable > SAL_CALL
61 SwVbaTables::Add( const uno::Reference< word::XRange >& Range, const uno::Any& NumRows, const uno::Any& NumColumns, const uno::Any& /*DefaultTableBehavior*/, const uno::Any& /*AutoFitBehavior*/ ) throw (script::BasicErrorException, uno::RuntimeException)
62 {
63     sal_Int32 nCols = 0;
64     sal_Int32 nRows = 0;
65     SwVbaRange* pVbaRange = dynamic_cast< SwVbaRange* >( Range.get() );
66     // Preconditions
67     if ( !( pVbaRange && ( NumRows >>= nRows ) && ( NumColumns >>= nCols ) ) )
68         throw uno::RuntimeException(); // #FIXME better exception??
69     if ( nCols <= 0 || nRows <= 0 )
70         throw uno::RuntimeException(); // #FIXME better exception??
71 
72     uno::Reference< frame::XModel > xModel( pVbaRange->getDocument(), uno::UNO_QUERY_THROW );
73     uno::Reference< lang::XMultiServiceFactory > xMsf( xModel, uno::UNO_QUERY_THROW );
74     uno::Reference< text::XTextRange > xTextRange = pVbaRange->getXTextRange();
75 
76     uno::Reference< text::XTextTable > xTable;
77     xTable.set( xMsf->createInstance( rtl::OUString::createFromAscii("com.sun.star.text.TextTable") ), uno::UNO_QUERY_THROW );
78 /*
79     comphelper::ComponentContext aCtx( xMsf );
80     if ( !aCtx.createComponent( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.TextTable") ), xTable ) );
81         throw uno::RuntimeException(); // #FIXME better exception??
82 */
83     xTable->initialize( nRows, nCols );
84     uno::Reference< text::XText > xText = xTextRange->getText();
85     uno::Reference< text::XTextContent > xContext( xTable, uno::UNO_QUERY_THROW );
86 
87     xText->insertTextContent( xTextRange, xContext, true );
88     uno::Reference< word::XTable > xVBATable( new SwVbaTable( mxParent, mxContext,  pVbaRange->getDocument(), xTable ) );
89     return xVBATable;
90 }
91 
92 uno::Reference< container::XEnumeration > SAL_CALL
93 SwVbaTables::createEnumeration() throw (uno::RuntimeException)
94 {
95     return new TableEnumerationImpl( mxParent, mxContext, mxDocument, m_xIndexAccess );
96 }
97 
98 // ScVbaCollectionBaseImpl
99 uno::Any
100 SwVbaTables::createCollectionObject( const uno::Any& aSource )
101 {
102     return lcl_createTable( mxParent, mxContext, mxDocument, aSource );
103 }
104 
105 // XHelperInterface
106 rtl::OUString&
107 SwVbaTables::getServiceImplName()
108 {
109     static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("SwVbaTables") );
110     return sImplName;
111 }
112 
113 // XEnumerationAccess
114 uno::Type SAL_CALL
115 SwVbaTables::getElementType() throw (uno::RuntimeException)
116 {
117     return  word::XTable::static_type(0);
118 }
119 
120 uno::Sequence<rtl::OUString>
121 SwVbaTables::getServiceNames()
122 {
123     static uno::Sequence< rtl::OUString > aServiceNames;
124     if ( aServiceNames.getLength() == 0 )
125     {
126         aServiceNames.realloc( 1 );
127         aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.word.Tables" ) );
128     }
129     return aServiceNames;
130 }
131 
132