bump product version to 5.0.4.1
[LibreOffice.git] / forms / source / component / GroupManager.hxx
blob01d8c60a38d707485fa9375d61bb043b4c34f0eb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_FORMS_SOURCE_COMPONENT_GROUPMANAGER_HXX
21 #define INCLUDED_FORMS_SOURCE_COMPONENT_GROUPMANAGER_HXX
23 #include <com/sun/star/sdbc/XRowSet.hpp>
24 #include <com/sun/star/awt/XControlModel.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
27 #include <com/sun/star/container/XContainerListener.hpp>
28 #include <com/sun/star/container/XContainer.hpp>
29 #include <cppuhelper/implbase2.hxx>
30 #include <comphelper/types.hxx>
32 #include <algorithm>
33 #include <map>
34 #include <vector>
36 using namespace comphelper;
39 * The GroupManager listens at the StarForm for FormComponent insertion and removal as well as
40 * its properties "Name" and "TabIndex" and updates its Group using this information.
42 * The GroupManager manages a Group in which all Controls are sorted by TabIndex.
43 * It also manages an array of Groups, in which each FormComponent is assigned a
44 * Group according to its name.
45 * Each Group is activated using a Map, if they contain more than one element.
47 * The Groups manage the FormComponents internally using two arrays.
48 * In the GroupCompArray the Components are sorted by TabIndex and insertion position.
49 * Because this array is accessed via the FormComponent, we also have the GroupCompAccessArray
50 * in which the FormComponents are sorted by their storage address.
51 * Every element of the GroupCompArray has a pointer to the GroupCompArray.
53 namespace frm
57 template <class ELEMENT, class LESS_COMPARE>
58 sal_Int32 insert_sorted(::std::vector<ELEMENT>& _rArray, const ELEMENT& _rNewElement, const LESS_COMPARE& _rCompareOp)
60 typename ::std::vector<ELEMENT>::iterator aInsertPos = ::std::lower_bound(
61 _rArray.begin(),
62 _rArray.end(),
63 _rNewElement,
64 _rCompareOp
66 aInsertPos = _rArray.insert(aInsertPos, _rNewElement);
67 return aInsertPos - _rArray.begin();
70 template <class ELEMENT, class LESS_COMPARE>
71 bool seek_entry(const ::std::vector<ELEMENT>& _rArray, const ELEMENT& _rNewElement, sal_Int32& nPos, const LESS_COMPARE& _rCompareOp)
73 typename ::std::vector<ELEMENT>::const_iterator aExistentPos = ::std::lower_bound(
74 _rArray.begin(),
75 _rArray.end(),
76 _rNewElement,
77 _rCompareOp
79 if ((aExistentPos != _rArray.end()) && (*aExistentPos == _rNewElement))
80 { // we have a valid "lower or equal" element and it's really "equal"
81 nPos = aExistentPos - _rArray.begin();
82 return true;
84 nPos = -1;
85 return false;
89 class OGroupComp
91 OUString m_aName;
92 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> m_xComponent;
93 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel> m_xControlModel;
94 sal_Int32 m_nPos;
95 sal_Int16 m_nTabIndex;
97 friend class OGroupCompLess;
99 public:
100 OGroupComp(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& rxElement, sal_Int32 nInsertPos );
101 OGroupComp(const OGroupComp& _rSource);
102 OGroupComp();
104 bool operator==( const OGroupComp& rComp ) const;
106 inline const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& GetComponent() const { return m_xComponent; }
107 inline const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel>& GetControlModel() const { return m_xControlModel; }
109 sal_Int32 GetPos() const { return m_nPos; }
110 sal_Int16 GetTabIndex() const { return m_nTabIndex; }
111 OUString GetName() const { return m_aName; }
114 typedef std::vector<OGroupComp> OGroupCompArr;
117 class OGroupComp;
118 class OGroupCompAcc
120 ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> m_xComponent;
122 OGroupComp m_aGroupComp;
124 friend class OGroupCompAccLess;
126 public:
127 OGroupCompAcc(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& rxElement, const OGroupComp& _rGroupComp );
129 bool operator==( const OGroupCompAcc& rCompAcc ) const;
131 inline const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& GetComponent() const { return m_xComponent; }
132 const OGroupComp& GetGroupComponent() const { return m_aGroupComp; }
135 typedef std::vector<OGroupCompAcc> OGroupCompAccArr;
138 class OGroup
140 OGroupCompArr m_aCompArray;
141 OGroupCompAccArr m_aCompAccArray;
143 OUString m_aGroupName;
144 sal_uInt16 m_nInsertPos; // The insertion position of the GroupComps is determind by the Group
146 friend class OGroupLess;
148 public:
149 OGroup( const OUString& rGroupName );
150 virtual ~OGroup();
152 bool operator==( const OGroup& rGroup ) const;
154 OUString GetGroupName() const { return m_aGroupName; }
155 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel> > GetControlModels() const;
157 void InsertComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& rxElement );
158 void RemoveComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& rxElement );
159 sal_uInt16 Count() const { return sal::static_int_cast< sal_uInt16 >(m_aCompArray.size()); }
160 const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& GetObject( sal_uInt16 nP ) const
161 { return m_aCompArray[nP].GetComponent(); }
164 typedef std::map<OUString, OGroup> OGroupArr;
165 typedef std::vector<OGroupArr::iterator> OActiveGroups;
168 class OGroupManager : public ::cppu::WeakImplHelper2< ::com::sun::star::beans::XPropertyChangeListener, ::com::sun::star::container::XContainerListener>
170 OGroup* m_pCompGroup; // Sort all Components by TabIndices
171 OGroupArr m_aGroupArr; // Sort all Components by group
172 OActiveGroups m_aActiveGroupMap; // This map contains all indices of all groups with more than 1 element
174 ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainer >
175 m_xContainer;
177 // Helper functions
178 void InsertElement( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& rxElement );
179 void RemoveElement( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& rxElement );
180 void removeFromGroupMap(const OUString& _sGroupName,const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& _xSet);
182 public:
183 OGroupManager(const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainer >& _rxContainer);
184 virtual ~OGroupManager();
186 // ::com::sun::star::lang::XEventListener
187 virtual void SAL_CALL disposing(const ::com::sun::star::lang::EventObject& _rSource) throw(::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
189 // ::com::sun::star::beans::XPropertyChangeListener
190 virtual void SAL_CALL propertyChange(const ::com::sun::star::beans::PropertyChangeEvent& evt) throw ( ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
192 // ::com::sun::star::container::XContainerListener
193 virtual void SAL_CALL elementInserted(const ::com::sun::star::container::ContainerEvent& _rEvent) throw ( ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
194 virtual void SAL_CALL elementRemoved(const ::com::sun::star::container::ContainerEvent& _rEvent) throw ( ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
195 virtual void SAL_CALL elementReplaced(const ::com::sun::star::container::ContainerEvent& _rEvent) throw ( ::com::sun::star::uno::RuntimeException, std::exception) SAL_OVERRIDE;
197 // Other functions
198 sal_Int32 getGroupCount();
199 void getGroup(sal_Int32 nGroup, ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel> >& _rGroup, OUString& Name);
200 void getGroupByName(const OUString& Name, ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel> >& _rGroup);
201 ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel> > getControlModels();
203 static OUString GetGroupName( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> xComponent );
208 } // namespace frm
211 #endif // INCLUDED_FORMS_SOURCE_COMPONENT_GROUPMANAGER_HXX
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */