xref: /trunk/main/sc/source/ui/vba/vbastyle.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 
28 #include "vbastyle.hxx"
29 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
30 
31 using namespace ::ooo::vba;
32 using namespace ::com::sun::star;
33 
34 static rtl::OUString DISPLAYNAME( RTL_CONSTASCII_USTRINGPARAM("DisplayName") );
35 
36 
37 
38 uno::Reference< container::XNameAccess >
39 ScVbaStyle::getStylesNameContainer( const uno::Reference< frame::XModel >& xModel ) throw ( uno::RuntimeException )
40 {
41     uno::Reference< style::XStyleFamiliesSupplier > xStyleSupplier( xModel, uno::UNO_QUERY_THROW);
42     uno::Reference< container::XNameAccess > xStylesAccess( xStyleSupplier->getStyleFamilies()->getByName( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CellStyles" ) ) ), uno::UNO_QUERY_THROW );
43     return xStylesAccess;
44 }
45 
46 uno::Reference< beans::XPropertySet >
47 lcl_getStyleProps( const rtl::OUString& sStyleName, const uno::Reference< frame::XModel >& xModel ) throw ( script::BasicErrorException, uno::RuntimeException )
48 {
49 
50     uno::Reference< beans::XPropertySet > xStyleProps( ScVbaStyle::getStylesNameContainer( xModel )->getByName( sStyleName ), uno::UNO_QUERY_THROW );
51     return xStyleProps;
52 }
53 
54 
55 void ScVbaStyle::initialise() throw ( uno::RuntimeException )
56 {
57     if (!mxModel.is() )
58         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "XModel Interface could not be retrieved")) );
59     uno::Reference< lang::XServiceInfo > xServiceInfo( mxPropertySet, uno::UNO_QUERY_THROW );
60     if ( !xServiceInfo->supportsService( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.style.CellStyle" ) ) ) )
61     {
62             DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
63     }
64     mxStyle.set( mxPropertySet, uno::UNO_QUERY_THROW );
65 
66     uno::Reference< style::XStyleFamiliesSupplier > xStyleSupplier( mxModel, uno::UNO_QUERY_THROW );
67     mxStyleFamilyNameContainer.set(  ScVbaStyle::getStylesNameContainer( mxModel ), uno::UNO_QUERY_THROW );
68 
69 }
70 
71 ScVbaStyle::ScVbaStyle( const uno::Reference< ov::XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const rtl::OUString& sStyleName, const uno::Reference< frame::XModel >& _xModel ) throw ( script::BasicErrorException, uno::RuntimeException ) :  ScVbaStyle_BASE( xParent, xContext, lcl_getStyleProps( sStyleName, _xModel ), _xModel, false ), mxModel( _xModel )
72 {
73     try
74     {
75         initialise();
76     }
77     catch (uno::Exception& )
78     {
79         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
80     }
81 }
82 
83 ScVbaStyle::ScVbaStyle( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< beans::XPropertySet >& _xPropertySet, const uno::Reference< frame::XModel >& _xModel ) throw ( script::BasicErrorException, uno::RuntimeException ) : ScVbaStyle_BASE( xParent, xContext, _xPropertySet, _xModel, false ),  mxModel( _xModel )
84 {
85     try
86     {
87         initialise();
88     }
89     catch (uno::Exception& )
90     {
91         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
92     }
93 }
94 
95 
96 ::sal_Bool SAL_CALL
97 ScVbaStyle::BuiltIn() throw (script::BasicErrorException, uno::RuntimeException)
98 {
99     return !mxStyle->isUserDefined();
100 
101 }
102 void SAL_CALL
103 ScVbaStyle::setName( const ::rtl::OUString& Name ) throw (script::BasicErrorException, uno::RuntimeException)
104 {
105     mxStyle->setName(Name);
106 }
107 
108 ::rtl::OUString SAL_CALL
109 ScVbaStyle::getName() throw (script::BasicErrorException, uno::RuntimeException)
110 {
111     return mxStyle->getName();
112 }
113 
114 void SAL_CALL
115 ScVbaStyle::setNameLocal( const ::rtl::OUString& NameLocal ) throw (script::BasicErrorException, uno::RuntimeException)
116 {
117     try
118     {
119         mxPropertySet->setPropertyValue(DISPLAYNAME, uno::makeAny( NameLocal ) );
120     }
121     catch (uno::Exception& e)
122     {
123         DebugHelper::exception(e);
124     }
125 }
126 
127 ::rtl::OUString SAL_CALL
128 ScVbaStyle::getNameLocal() throw (script::BasicErrorException, uno::RuntimeException)
129 {
130     rtl::OUString sName;
131     try
132     {
133         mxPropertySet->getPropertyValue(DISPLAYNAME) >>= sName;
134     }
135     catch (uno::Exception e)
136     {
137         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
138     }
139     return sName;
140 }
141 
142 void SAL_CALL
143 ScVbaStyle::Delete() throw (script::BasicErrorException, uno::RuntimeException)
144 {
145     try
146     {
147         mxStyleFamilyNameContainer->removeByName(mxStyle->getName());
148     }
149     catch (uno::Exception& )
150     {
151         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString());
152     }
153 }
154 
155 void SAL_CALL
156 ScVbaStyle::setMergeCells( const uno::Any& /*MergeCells*/ ) throw (script::BasicErrorException, uno::RuntimeException)
157 {
158     DebugHelper::exception(SbERR_NOT_IMPLEMENTED, rtl::OUString());
159 }
160 
161 uno::Any SAL_CALL
162 ScVbaStyle::getMergeCells(  ) throw (script::BasicErrorException, uno::RuntimeException)
163 {
164     DebugHelper::exception(SbERR_NOT_IMPLEMENTED, rtl::OUString());
165     return uno::Any();
166 }
167 
168 
169 rtl::OUString&
170 ScVbaStyle::getServiceImplName()
171 {
172         static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaStyle") );
173         return sImplName;
174 }
175 
176 uno::Sequence< rtl::OUString >
177 ScVbaStyle::getServiceNames()
178 {
179         static uno::Sequence< rtl::OUString > aServiceNames;
180         if ( aServiceNames.getLength() == 0 )
181         {
182                 aServiceNames.realloc( 1 );
183                 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.XStyle" ) );
184         }
185         return aServiceNames;
186 }
187