xref: /AOO41X/main/chart2/source/controller/inc/ItemConverter.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir #ifndef CHART_ITEMCONVERTER_HXX
28*cdf0e10cSrcweir #define CHART_ITEMCONVERTER_HXX
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include <unotools/eventlisteneradapter.hxx>
31*cdf0e10cSrcweir #include <svl/itempool.hxx>
32*cdf0e10cSrcweir #include <svl/itemset.hxx>
33*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir // for pair
36*cdf0e10cSrcweir #include <utility>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir namespace comphelper
39*cdf0e10cSrcweir {
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir /** This class serves for conversion between properties of an XPropertySet and
42*cdf0e10cSrcweir     SfxItems in SfxItemSets.
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir     With this helper classes, you can feed dialogs with XPropertySets and let
45*cdf0e10cSrcweir     those modify by the dialogs.
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir     You must implement GetWhichPairs() such that an SfxItemSet created with
48*cdf0e10cSrcweir     CreateEmptyItemSet() is able to hold all items that may be mapped.
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir     You also have to implement GetItemProperty(), in order to return the
51*cdf0e10cSrcweir     property name for a given which-id together with the corresponding member-id
52*cdf0e10cSrcweir     that has to be used for conversion in QueryValue/PutValue.
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir     FillSpecialItem and ApplySpecialItem may be used for special handling of
55*cdf0e10cSrcweir     individual item, e.g. if you need member-ids in QueryValue/PutValue
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     A typical use could be the following:
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     ::comphelper::ChartTypeItemConverter aItemConverter( xPropertySet, GetItemPool() );
60*cdf0e10cSrcweir     SfxItemSet aItemSet = aItemConverter.CreateEmptyItemSet();
61*cdf0e10cSrcweir     aItemConverter.FillItemSet( aItemSet );
62*cdf0e10cSrcweir     bool bChanged = false;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     MyDialog aDlg( aItemSet );
65*cdf0e10cSrcweir     if( aDlg.Execute() == RET_OK )
66*cdf0e10cSrcweir     {
67*cdf0e10cSrcweir         const SfxItemSet* pOutItemSet = aDlg.GetOutputItemSet();
68*cdf0e10cSrcweir         if( pOutItemSet )
69*cdf0e10cSrcweir             bChanged = aItemConverter.ApplyItemSet( *pOutItemSet );
70*cdf0e10cSrcweir     }
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir     if( bChanged )
73*cdf0e10cSrcweir     {
74*cdf0e10cSrcweir         [ apply model changes to view ]
75*cdf0e10cSrcweir     }
76*cdf0e10cSrcweir  */
77*cdf0e10cSrcweir class ItemConverter :
78*cdf0e10cSrcweir         public ::utl::OEventListenerAdapter
79*cdf0e10cSrcweir {
80*cdf0e10cSrcweir public:
81*cdf0e10cSrcweir     /** Construct an item converter that uses the given property set for
82*cdf0e10cSrcweir         reading/writing converted items
83*cdf0e10cSrcweir      */
84*cdf0e10cSrcweir 	ItemConverter(
85*cdf0e10cSrcweir         const ::com::sun::star::uno::Reference<
86*cdf0e10cSrcweir             ::com::sun::star::beans::XPropertySet > & rPropertySet ,
87*cdf0e10cSrcweir             SfxItemPool& rItemPool );
88*cdf0e10cSrcweir 	virtual ~ItemConverter();
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     // typedefs -------------------------------
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     typedef sal_uInt16          tWhichIdType;
93*cdf0e10cSrcweir     typedef ::rtl::OUString tPropertyNameType;
94*cdf0e10cSrcweir     typedef sal_uInt8            tMemberIdType;
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     typedef ::std::pair< tPropertyNameType, tMemberIdType > tPropertyNameWithMemberId;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir     // ----------------------------------------
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     /** applies all properties that can be mapped to items into the given item
101*cdf0e10cSrcweir         set.
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir         Call this method before opening a dialog.
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir         @param rOutItemSet
106*cdf0e10cSrcweir             the SfxItemSet is filled with all items that are a result of a
107*cdf0e10cSrcweir             conversion from a property of the internal XPropertySet.
108*cdf0e10cSrcweir      */
109*cdf0e10cSrcweir     virtual void FillItemSet( SfxItemSet & rOutItemSet ) const;
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir     /** applies all properties that are results of a conversion from all items
112*cdf0e10cSrcweir         in rItemSet to the internal XPropertySet.
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         Call this method after a dialog was closed with OK
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir         @return true, if any properties have been changed, false otherwise.
117*cdf0e10cSrcweir      */
118*cdf0e10cSrcweir     virtual bool ApplyItemSet( const SfxItemSet & rItemSet );
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir     /** creates an empty item set using the given pool or a common pool if empty
121*cdf0e10cSrcweir         (see GetItemPool) and allowing all items given in the ranges returned by
122*cdf0e10cSrcweir         GetWhichPairs.
123*cdf0e10cSrcweir      */
124*cdf0e10cSrcweir     SfxItemSet CreateEmptyItemSet() const;
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     /** Invalidates all items in rDestSet, that are set (state SFX_ITEM_SET) in
127*cdf0e10cSrcweir         both item sets (rDestSet and rSourceSet) and have differing content.
128*cdf0e10cSrcweir      */
129*cdf0e10cSrcweir     static void InvalidateUnequalItems( SfxItemSet &rDestSet, const SfxItemSet &rSourceSet );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir protected:
132*cdf0e10cSrcweir     // ________
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     /** implement this method to provide an array of which-ranges of the form:
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         const sal_uInt16 aMyPairs[] =
137*cdf0e10cSrcweir         {
138*cdf0e10cSrcweir             from_1, to_1,
139*cdf0e10cSrcweir             from_2, to_2,
140*cdf0e10cSrcweir             ...
141*cdf0e10cSrcweir             from_n, to_n,
142*cdf0e10cSrcweir             0
143*cdf0e10cSrcweir         };
144*cdf0e10cSrcweir     */
145*cdf0e10cSrcweir     virtual const sal_uInt16 * GetWhichPairs() const = 0;
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     /** implement this method to return a Property object for a given which id.
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir         @param rOutProperty
150*cdf0e10cSrcweir             If true is returned, this contains the property name and the
151*cdf0e10cSrcweir             corresponding Member-Id.
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir         @return true, if the item can be mapped to a property.
154*cdf0e10cSrcweir      */
155*cdf0e10cSrcweir     virtual bool GetItemProperty( tWhichIdType nWhichId, tPropertyNameWithMemberId & rOutProperty ) const = 0;
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     /** for items that can not be mapped directly to a property.
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir         This method is called from FillItemSet(), if GetItemProperty() returns
160*cdf0e10cSrcweir         false.
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir         The default implementation does nothing except showing an assertion
163*cdf0e10cSrcweir      */
164*cdf0e10cSrcweir     virtual void FillSpecialItem( sal_uInt16 nWhichId, SfxItemSet & rOutItemSet ) const
165*cdf0e10cSrcweir         throw( ::com::sun::star::uno::Exception );
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     /** for items that can not be mapped directly to a property.
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         This method is called from ApplyItemSet(), if GetItemProperty() returns
170*cdf0e10cSrcweir         false.
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir         The default implementation returns just false and shows an assertion
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         @return true if the item changed a property, false otherwise.
175*cdf0e10cSrcweir      */
176*cdf0e10cSrcweir     virtual bool ApplySpecialItem( sal_uInt16 nWhichId, const SfxItemSet & rItemSet )
177*cdf0e10cSrcweir         throw( ::com::sun::star::uno::Exception );
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir     // ________
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     /// Returns the pool
182*cdf0e10cSrcweir     SfxItemPool & GetItemPool() const;
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir     /** Returns the XPropertySet that was given in the CTOR and is used to apply
185*cdf0e10cSrcweir         items in ApplyItemSet().
186*cdf0e10cSrcweir      */
187*cdf0e10cSrcweir     ::com::sun::star::uno::Reference<
188*cdf0e10cSrcweir         ::com::sun::star::beans::XPropertySet >  GetPropertySet() const;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir     // ____ ::utl::OEventListenerAdapter ____
191*cdf0e10cSrcweir     virtual void _disposing( const ::com::sun::star::lang::EventObject& rSource );
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir protected:
194*cdf0e10cSrcweir     /** sets a new property set, that you get with GetPropertySet().  It should
195*cdf0e10cSrcweir         not be necessary to use this method.  It is introduced to allow changing
196*cdf0e10cSrcweir         the regression type of a regression curve which changes the object
197*cdf0e10cSrcweir         identity.
198*cdf0e10cSrcweir      */
199*cdf0e10cSrcweir     void resetPropertySet( const ::com::sun::star::uno::Reference<
200*cdf0e10cSrcweir                            ::com::sun::star::beans::XPropertySet > & xPropSet );
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir private:
203*cdf0e10cSrcweir     ::com::sun::star::uno::Reference<
204*cdf0e10cSrcweir         ::com::sun::star::beans::XPropertySet >     m_xPropertySet;
205*cdf0e10cSrcweir     ::com::sun::star::uno::Reference<
206*cdf0e10cSrcweir         ::com::sun::star::beans::XPropertySetInfo > m_xPropertySetInfo;
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir     SfxItemPool&                                    m_rItemPool;
209*cdf0e10cSrcweir     bool                                            m_bIsValid;
210*cdf0e10cSrcweir };
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir } //  namespace comphelper
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir // CHART_ITEMCONVERTER_HXX
215*cdf0e10cSrcweir #endif
216