1*ca5ec200SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ca5ec200SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ca5ec200SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ca5ec200SAndrew Rist  * distributed with this work for additional information
6*ca5ec200SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ca5ec200SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ca5ec200SAndrew Rist  * "License"); you may not use this file except in compliance
9*ca5ec200SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ca5ec200SAndrew Rist  *
11*ca5ec200SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ca5ec200SAndrew Rist  *
13*ca5ec200SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ca5ec200SAndrew Rist  * software distributed under the License is distributed on an
15*ca5ec200SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ca5ec200SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ca5ec200SAndrew Rist  * specific language governing permissions and limitations
18*ca5ec200SAndrew Rist  * under the License.
19*ca5ec200SAndrew Rist  *
20*ca5ec200SAndrew Rist  *************************************************************/
21*ca5ec200SAndrew Rist 
22*ca5ec200SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "oox/helper/modelobjecthelper.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <com/sun/star/awt/Gradient.hpp>
27cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
28cdf0e10cSrcweir #include <com/sun/star/drawing/LineDash.hpp>
29cdf0e10cSrcweir #include <com/sun/star/drawing/PolyPolygonBezierCoords.hpp>
30cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31cdf0e10cSrcweir #include "oox/helper/containerhelper.hxx"
32cdf0e10cSrcweir #include "oox/helper/helper.hxx"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir namespace oox {
35cdf0e10cSrcweir 
36cdf0e10cSrcweir // ============================================================================
37cdf0e10cSrcweir 
38cdf0e10cSrcweir using namespace ::com::sun::star::awt;
39cdf0e10cSrcweir using namespace ::com::sun::star::drawing;
40cdf0e10cSrcweir using namespace ::com::sun::star::lang;
41cdf0e10cSrcweir using namespace ::com::sun::star::uno;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir using ::rtl::OUString;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir // ============================================================================
46cdf0e10cSrcweir 
ObjectContainer(const Reference<XMultiServiceFactory> & rxModelFactory,const OUString & rServiceName)47cdf0e10cSrcweir ObjectContainer::ObjectContainer( const Reference< XMultiServiceFactory >& rxModelFactory, const OUString& rServiceName ) :
48cdf0e10cSrcweir     mxModelFactory( rxModelFactory ),
49cdf0e10cSrcweir     maServiceName( rServiceName ),
50cdf0e10cSrcweir     mnIndex( 0 )
51cdf0e10cSrcweir {
52cdf0e10cSrcweir     OSL_ENSURE( mxModelFactory.is(), "ObjectContainer::ObjectContainer - missing service factory" );
53cdf0e10cSrcweir }
54cdf0e10cSrcweir 
~ObjectContainer()55cdf0e10cSrcweir ObjectContainer::~ObjectContainer()
56cdf0e10cSrcweir {
57cdf0e10cSrcweir }
58cdf0e10cSrcweir 
hasObject(const OUString & rObjName) const59cdf0e10cSrcweir bool ObjectContainer::hasObject( const OUString& rObjName ) const
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     createContainer();
62cdf0e10cSrcweir     return mxContainer.is() && mxContainer->hasByName( rObjName );
63cdf0e10cSrcweir }
64cdf0e10cSrcweir 
getObject(const OUString & rObjName) const65cdf0e10cSrcweir Any ObjectContainer::getObject( const OUString& rObjName ) const
66cdf0e10cSrcweir {
67cdf0e10cSrcweir     createContainer();
68cdf0e10cSrcweir     if( mxContainer.is() ) try
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir         return mxContainer->getByName( rObjName );
71cdf0e10cSrcweir     }
72cdf0e10cSrcweir     catch( Exception& )
73cdf0e10cSrcweir     {
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir     return Any();
76cdf0e10cSrcweir }
77cdf0e10cSrcweir 
insertObject(const OUString & rObjName,const Any & rObj,bool bInsertByUnusedName)78cdf0e10cSrcweir OUString ObjectContainer::insertObject( const OUString& rObjName, const Any& rObj, bool bInsertByUnusedName )
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     createContainer();
81cdf0e10cSrcweir     if( mxContainer.is() )
82cdf0e10cSrcweir     {
83cdf0e10cSrcweir         if( bInsertByUnusedName )
84cdf0e10cSrcweir             return ContainerHelper::insertByUnusedName( mxContainer, rObjName + OUString::valueOf( ++mnIndex ), ' ', rObj );
85cdf0e10cSrcweir         if( ContainerHelper::insertByName( mxContainer, rObjName, rObj ) )
86cdf0e10cSrcweir             return rObjName;
87cdf0e10cSrcweir     }
88cdf0e10cSrcweir     return OUString();
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
createContainer() const91cdf0e10cSrcweir void ObjectContainer::createContainer() const
92cdf0e10cSrcweir {
93cdf0e10cSrcweir     if( !mxContainer.is() && mxModelFactory.is() ) try
94cdf0e10cSrcweir     {
95cdf0e10cSrcweir         mxContainer.set( mxModelFactory->createInstance( maServiceName ), UNO_QUERY_THROW );
96cdf0e10cSrcweir         mxModelFactory.clear();
97cdf0e10cSrcweir     }
98cdf0e10cSrcweir     catch( Exception& )
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir     }
101cdf0e10cSrcweir     OSL_ENSURE( mxContainer.is(), "ObjectContainer::createContainer - container not found" );
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir // ============================================================================
105cdf0e10cSrcweir 
ModelObjectHelper(const Reference<XMultiServiceFactory> & rxModelFactory)106cdf0e10cSrcweir ModelObjectHelper::ModelObjectHelper( const Reference< XMultiServiceFactory >& rxModelFactory ) :
107cdf0e10cSrcweir     maMarkerContainer(    rxModelFactory, CREATE_OUSTRING( "com.sun.star.drawing.MarkerTable" ) ),
108cdf0e10cSrcweir     maDashContainer(      rxModelFactory, CREATE_OUSTRING( "com.sun.star.drawing.DashTable" ) ),
109cdf0e10cSrcweir     maGradientContainer(  rxModelFactory, CREATE_OUSTRING( "com.sun.star.drawing.GradientTable" ) ),
110cdf0e10cSrcweir     maBitmapUrlContainer( rxModelFactory, CREATE_OUSTRING( "com.sun.star.drawing.BitmapTable" ) ),
111cdf0e10cSrcweir     maDashNameBase(      CREATE_OUSTRING( "msLineDash " ) ),
112cdf0e10cSrcweir     maGradientNameBase(  CREATE_OUSTRING( "msFillGradient " ) ),
113cdf0e10cSrcweir     maBitmapUrlNameBase( CREATE_OUSTRING( "msFillBitmap " ) )
114cdf0e10cSrcweir {
115cdf0e10cSrcweir }
116cdf0e10cSrcweir 
hasLineMarker(const OUString & rMarkerName) const117cdf0e10cSrcweir bool ModelObjectHelper::hasLineMarker( const OUString& rMarkerName ) const
118cdf0e10cSrcweir {
119cdf0e10cSrcweir     return maMarkerContainer.hasObject( rMarkerName );
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
insertLineMarker(const OUString & rMarkerName,const PolyPolygonBezierCoords & rMarker)122cdf0e10cSrcweir bool ModelObjectHelper::insertLineMarker( const OUString& rMarkerName, const PolyPolygonBezierCoords& rMarker )
123cdf0e10cSrcweir {
124cdf0e10cSrcweir     OSL_ENSURE( rMarker.Coordinates.hasElements(), "ModelObjectHelper::insertLineMarker - line marker without coordinates" );
125cdf0e10cSrcweir     if( rMarker.Coordinates.hasElements() )
126cdf0e10cSrcweir         return maMarkerContainer.insertObject( rMarkerName, Any( rMarker ), false ).getLength() > 0;
127cdf0e10cSrcweir     return false;
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
insertLineDash(const LineDash & rDash)130cdf0e10cSrcweir OUString ModelObjectHelper::insertLineDash( const LineDash& rDash )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     return maDashContainer.insertObject( maDashNameBase, Any( rDash ), true );
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
insertFillGradient(const Gradient & rGradient)135cdf0e10cSrcweir OUString ModelObjectHelper::insertFillGradient( const Gradient& rGradient )
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     return maGradientContainer.insertObject( maGradientNameBase, Any( rGradient ), true );
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
insertFillBitmapUrl(const OUString & rGraphicUrl)140cdf0e10cSrcweir OUString ModelObjectHelper::insertFillBitmapUrl( const OUString& rGraphicUrl )
141cdf0e10cSrcweir {
142cdf0e10cSrcweir     if( rGraphicUrl.getLength() > 0 )
143cdf0e10cSrcweir         return maBitmapUrlContainer.insertObject( maBitmapUrlNameBase, Any( rGraphicUrl ), true );
144cdf0e10cSrcweir     return OUString();
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir // ============================================================================
148cdf0e10cSrcweir 
149cdf0e10cSrcweir } // namespace oox
150