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