1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #include "precompiled_reportdesign.hxx"
25 #include "PropertyForward.hxx"
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/beans/PropertyAttribute.hpp>
28 #include <comphelper/property.hxx>
29 #include <com/sun/star/sdbcx/XAppend.hpp>
30 #include <tools/debug.hxx>
31 #include <tools/diagnose_ex.h>
32 #include "corestrings.hrc"
33
34 //........................................................................
35 namespace rptui
36 {
37 //........................................................................
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::beans;
40 using namespace reportdesign;
41
DBG_NAME(rpt_OPropertyMediator)42 DBG_NAME( rpt_OPropertyMediator )
43 OPropertyMediator::OPropertyMediator(const Reference< XPropertySet>& _xSource
44 ,const Reference< XPropertySet>& _xDest
45 ,const TPropertyNamePair& _aNameMap
46 ,sal_Bool _bReverse)
47 : OPropertyForward_Base(m_aMutex)
48 ,m_aNameMap(_aNameMap)
49 ,m_xSource(_xSource)
50 ,m_xDest(_xDest)
51 ,m_bInChange(sal_False)
52 {
53 DBG_CTOR( rpt_OPropertyMediator,NULL);
54 osl_incrementInterlockedCount(&m_refCount);
55 OSL_ENSURE(m_xDest.is(),"Dest is NULL!");
56 OSL_ENSURE(m_xSource.is(),"Source is NULL!");
57 if ( m_xDest.is() && m_xSource.is() )
58 {
59 try
60 {
61 m_xDestInfo = m_xDest->getPropertySetInfo();
62 m_xSourceInfo = m_xSource->getPropertySetInfo();
63 if ( _bReverse )
64 {
65 ::comphelper::copyProperties(m_xDest,m_xSource);
66 TPropertyNamePair::iterator aIter = m_aNameMap.begin();
67 TPropertyNamePair::iterator aEnd = m_aNameMap.end();
68 for (; aIter != aEnd; ++aIter)
69 {
70 Property aProp = m_xSourceInfo->getPropertyByName(aIter->first);
71 if (0 == (aProp.Attributes & PropertyAttribute::READONLY))
72 {
73 Any aValue = _xDest->getPropertyValue(aIter->second.first);
74 if ( 0 != (aProp.Attributes & PropertyAttribute::MAYBEVOID) || aValue.hasValue() )
75 _xSource->setPropertyValue(aIter->first,aIter->second.second->operator()(aIter->second.first,aValue));
76 }
77 }
78 }
79 else
80 {
81 ::comphelper::copyProperties(m_xSource,m_xDest);
82 TPropertyNamePair::iterator aIter = m_aNameMap.begin();
83 TPropertyNamePair::iterator aEnd = m_aNameMap.end();
84 for (; aIter != aEnd; ++aIter)
85 _xDest->setPropertyValue(aIter->second.first,aIter->second.second->operator()(aIter->second.first,_xSource->getPropertyValue(aIter->first)));
86 }
87 startListening();
88 }
89 catch(Exception& e)
90 {
91 DBG_UNHANDLED_EXCEPTION();
92 (void)e;
93 }
94 } // if ( m_xDest.is() && m_xSource.is() )
95 osl_decrementInterlockedCount(&m_refCount);
96 }
97 // -----------------------------------------------------------------------------
~OPropertyMediator()98 OPropertyMediator::~OPropertyMediator()
99 {
100 DBG_DTOR( rpt_OPropertyMediator,NULL);
101 }
102 // -----------------------------------------------------------------------------
propertyChange(const PropertyChangeEvent & evt)103 void SAL_CALL OPropertyMediator::propertyChange( const PropertyChangeEvent& evt ) throw(RuntimeException)
104 {
105 ::osl::MutexGuard aGuard(m_aMutex);
106 if ( !m_bInChange )
107 {
108 m_bInChange = sal_True;
109 try
110 {
111 sal_Bool bDest = (evt.Source == m_xDest);
112 Reference<XPropertySet> xProp = bDest ? m_xSource : m_xDest;
113 Reference<XPropertySetInfo> xPropInfo = bDest ? m_xSourceInfo : m_xDestInfo;
114 if ( xProp.is() )
115 {
116 if ( xPropInfo.is() )
117 {
118 if ( xPropInfo->hasPropertyByName(evt.PropertyName) )
119 xProp->setPropertyValue(evt.PropertyName,evt.NewValue);
120 else
121 {
122 TPropertyNamePair::iterator aFind = m_aNameMap.find(evt.PropertyName);
123 ::rtl::OUString sPropName;
124 if ( aFind != m_aNameMap.end() )
125 sPropName = aFind->second.first;
126 else
127 {
128 aFind = ::std::find_if(
129 m_aNameMap.begin(),
130 m_aNameMap.end(),
131 ::std::compose1(
132 ::std::bind2nd(::std::equal_to< ::rtl::OUString >(), evt.PropertyName),
133 ::std::compose1(::std::select1st<TPropertyConverter>(),::std::select2nd<TPropertyNamePair::value_type>())
134 )
135 );
136 if ( aFind != m_aNameMap.end() )
137 sPropName = aFind->first;
138 }
139 if ( sPropName.getLength() && xPropInfo->hasPropertyByName(sPropName) )
140 xProp->setPropertyValue(sPropName,aFind->second.second->operator()(sPropName,evt.NewValue));
141 else if ( evt.PropertyName == PROPERTY_CHARFONTNAME
142 || evt.PropertyName == PROPERTY_CHARFONTSTYLENAME
143 || evt.PropertyName == PROPERTY_CHARSTRIKEOUT
144 || evt.PropertyName == PROPERTY_CHARWORDMODE
145 || evt.PropertyName == PROPERTY_CHARROTATION
146 || evt.PropertyName == PROPERTY_CHARSCALEWIDTH
147 || evt.PropertyName == PROPERTY_CHARFONTFAMILY
148 || evt.PropertyName == PROPERTY_CHARFONTCHARSET
149 || evt.PropertyName == PROPERTY_CHARFONTPITCH
150 || evt.PropertyName == PROPERTY_CHARHEIGHT
151 || evt.PropertyName == PROPERTY_CHARUNDERLINE
152 || evt.PropertyName == PROPERTY_CHARWEIGHT
153 || evt.PropertyName == PROPERTY_CHARPOSTURE)
154 {
155 xProp->setPropertyValue(PROPERTY_FONTDESCRIPTOR,m_xSource->getPropertyValue(PROPERTY_FONTDESCRIPTOR));
156 }
157 }
158 }
159 }
160 }
161 catch(Exception&)
162 {
163 OSL_ENSURE(0,"Exception caught!");
164 }
165 m_bInChange = sal_False;
166 }
167 }
168 // -----------------------------------------------------------------------------
disposing(const::com::sun::star::lang::EventObject &)169 void SAL_CALL OPropertyMediator::disposing( const ::com::sun::star::lang::EventObject& /*_rSource*/ ) throw (RuntimeException)
170 {
171 ::osl::MutexGuard aGuard(m_aMutex);
172 disposing();
173 }
174 // -----------------------------------------------------------------------------
disposing()175 void SAL_CALL OPropertyMediator::disposing()
176 {
177 stopListening();
178 m_xSource.clear();
179 m_xSourceInfo.clear();
180 m_xDest.clear();
181 m_xDestInfo.clear();
182 }
183 // -----------------------------------------------------------------------------
stopListening()184 void OPropertyMediator::stopListening()
185 {
186 if ( m_xSource.is() )
187 m_xSource->removePropertyChangeListener(::rtl::OUString(), this);
188 if ( m_xDest.is() )
189 m_xDest->removePropertyChangeListener(::rtl::OUString(), this);
190 }
191 // -----------------------------------------------------------------------------
startListening()192 void OPropertyMediator::startListening()
193 {
194 if ( m_xSource.is() )
195 m_xSource->addPropertyChangeListener(::rtl::OUString(), this);
196 if ( m_xDest.is() )
197 m_xDest->addPropertyChangeListener(::rtl::OUString(), this);
198 }
199 // -----------------------------------------------------------------------------
200 //........................................................................
201 } // namespace dbaccess
202 //........................................................................
203
204