Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_FORMS_SOURCE_COMPONENT_GROUPMANAGER_HXX
21 : #define INCLUDED_FORMS_SOURCE_COMPONENT_GROUPMANAGER_HXX
22 :
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>
31 :
32 : #include <algorithm>
33 : #include <map>
34 : #include <vector>
35 :
36 : using namespace comphelper;
37 :
38 : /*
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.
41 : *
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.
46 : *
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.
52 : */
53 : namespace frm
54 : {
55 :
56 :
57 : template <class ELEMENT, class LESS_COMPARE>
58 5596 : sal_Int32 insert_sorted(::std::vector<ELEMENT>& _rArray, const ELEMENT& _rNewElement, const LESS_COMPARE& _rCompareOp)
59 : {
60 : typename ::std::vector<ELEMENT>::iterator aInsertPos = ::std::lower_bound(
61 : _rArray.begin(),
62 : _rArray.end(),
63 : _rNewElement,
64 : _rCompareOp
65 5596 : );
66 5596 : aInsertPos = _rArray.insert(aInsertPos, _rNewElement);
67 5596 : return aInsertPos - _rArray.begin();
68 : }
69 :
70 : template <class ELEMENT, class LESS_COMPARE>
71 4964 : bool seek_entry(const ::std::vector<ELEMENT>& _rArray, const ELEMENT& _rNewElement, sal_Int32& nPos, const LESS_COMPARE& _rCompareOp)
72 : {
73 : typename ::std::vector<ELEMENT>::const_iterator aExistentPos = ::std::lower_bound(
74 : _rArray.begin(),
75 : _rArray.end(),
76 : _rNewElement,
77 : _rCompareOp
78 4964 : );
79 4964 : if ((aExistentPos != _rArray.end()) && (*aExistentPos == _rNewElement))
80 : { // we have a valid "lower or equal" element and it's really "equal"
81 4964 : nPos = aExistentPos - _rArray.begin();
82 4964 : return true;
83 : }
84 0 : nPos = -1;
85 0 : return false;
86 : }
87 :
88 :
89 21110 : class OGroupComp
90 : {
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;
96 :
97 : friend class OGroupCompLess;
98 :
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();
103 :
104 : bool operator==( const OGroupComp& rComp ) const;
105 :
106 2 : inline const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& GetComponent() const { return m_xComponent; }
107 652 : inline const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel>& GetControlModel() const { return m_xControlModel; }
108 :
109 5886 : sal_Int32 GetPos() const { return m_nPos; }
110 7056 : sal_Int16 GetTabIndex() const { return m_nTabIndex; }
111 : OUString GetName() const { return m_aName; }
112 : };
113 :
114 : typedef std::vector<OGroupComp> OGroupCompArr;
115 :
116 :
117 : class OGroupComp;
118 14748 : class OGroupCompAcc
119 : {
120 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> m_xComponent;
121 :
122 : OGroupComp m_aGroupComp;
123 :
124 : friend class OGroupCompAccLess;
125 :
126 : public:
127 : OGroupCompAcc(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& rxElement, const OGroupComp& _rGroupComp );
128 :
129 : bool operator==( const OGroupCompAcc& rCompAcc ) const;
130 :
131 2482 : inline const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& GetComponent() const { return m_xComponent; }
132 2482 : const OGroupComp& GetGroupComponent() const { return m_aGroupComp; }
133 : };
134 :
135 : typedef std::vector<OGroupCompAcc> OGroupCompAccArr;
136 :
137 :
138 1990 : class OGroup
139 : {
140 : OGroupCompArr m_aCompArray;
141 : OGroupCompAccArr m_aCompAccArray;
142 :
143 : OUString m_aGroupName;
144 : sal_uInt16 m_nInsertPos; // The insertion position of the GroupComps is determind by the Group
145 :
146 : friend class OGroupLess;
147 :
148 : public:
149 : OGroup( const OUString& rGroupName );
150 : virtual ~OGroup();
151 :
152 : bool operator==( const OGroup& rGroup ) const;
153 :
154 22 : OUString GetGroupName() const { return m_aGroupName; }
155 : ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel> > GetControlModels() const;
156 :
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 4039 : sal_uInt16 Count() const { return sal::static_int_cast< sal_uInt16 >(m_aCompArray.size()); }
160 2 : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& GetObject( sal_uInt16 nP ) const
161 2 : { return m_aCompArray[nP].GetComponent(); }
162 : };
163 :
164 : typedef std::map<OUString, OGroup> OGroupArr;
165 : typedef std::vector<OGroupArr::iterator> OActiveGroups;
166 :
167 :
168 : class OGroupManager : public ::cppu::WeakImplHelper2< ::com::sun::star::beans::XPropertyChangeListener, ::com::sun::star::container::XContainerListener>
169 : {
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
173 :
174 : ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainer >
175 : m_xContainer;
176 :
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);
181 :
182 : public:
183 : OGroupManager(const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainer >& _rxContainer);
184 : virtual ~OGroupManager();
185 :
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;
188 :
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;
191 :
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;
196 :
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();
202 :
203 : static OUString GetGroupName( ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> xComponent );
204 : };
205 :
206 :
207 :
208 : } // namespace frm
209 :
210 :
211 : #endif // INCLUDED_FORMS_SOURCE_COMPONENT_GROUPMANAGER_HXX
212 :
213 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|