12d785d7eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32d785d7eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42d785d7eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52d785d7eSAndrew Rist  * distributed with this work for additional information
62d785d7eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72d785d7eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
82d785d7eSAndrew Rist  * "License"); you may not use this file except in compliance
92d785d7eSAndrew Rist  * with the License.  You may obtain a copy of the License at
102d785d7eSAndrew Rist  *
112d785d7eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122d785d7eSAndrew Rist  *
132d785d7eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
142d785d7eSAndrew Rist  * software distributed under the License is distributed on an
152d785d7eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162d785d7eSAndrew Rist  * KIND, either express or implied.  See the License for the
172d785d7eSAndrew Rist  * specific language governing permissions and limitations
182d785d7eSAndrew Rist  * under the License.
192d785d7eSAndrew Rist  *
202d785d7eSAndrew Rist  *************************************************************/
212d785d7eSAndrew Rist 
222d785d7eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _FRM_GROUPMANAGER_HXX_
25cdf0e10cSrcweir #define _FRM_GROUPMANAGER_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/sdbc/XRowSet.hpp>
28cdf0e10cSrcweir #include <com/sun/star/awt/XControlModel.hpp>
29cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
30cdf0e10cSrcweir #include <com/sun/star/beans/XPropertyChangeListener.hpp>
31cdf0e10cSrcweir #include <com/sun/star/container/XContainerListener.hpp>
32cdf0e10cSrcweir #include <com/sun/star/container/XContainer.hpp>
33cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
34cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
35cdf0e10cSrcweir #include <comphelper/stl_types.hxx>
36cdf0e10cSrcweir #include <comphelper/types.hxx>
37cdf0e10cSrcweir using namespace comphelper;
38cdf0e10cSrcweir 
39cdf0e10cSrcweir /*========================================================================
40cdf0e10cSrcweir Funktionsweise GroupManager:
41cdf0e10cSrcweir 
42cdf0e10cSrcweir Der GroupManager horcht an der starform, ob FormComponents eingefuegt oder entfernt
43cdf0e10cSrcweir werden. Zusaetzlich horcht er bei den FormComponents an den Properties
44cdf0e10cSrcweir "Name" und "TabIndex". Mit diesen Infos aktualisiert er seine Gruppen.
45cdf0e10cSrcweir 
46cdf0e10cSrcweir Der GroupManager verwaltet eine Gruppe, in der alle Controls nach TabIndex
47cdf0e10cSrcweir geordnet sind, und ein Array von Gruppen, in dem jede FormComponent noch
48cdf0e10cSrcweir einmal einer Gruppe dem Namen nach zugeordnet wird.
49cdf0e10cSrcweir Die einzelnen Gruppen werden ueber eine Map aktiviert, wenn sie mehr als
50cdf0e10cSrcweir ein Element besitzen.
51cdf0e10cSrcweir 
52cdf0e10cSrcweir Die Gruppen verwalten intern die FormComponents in zwei Arrays. In dem
53*300d4866SJohn Bampton GroupCompArray werden die Components nach TabIndex und Einfuegeposition
54cdf0e10cSrcweir sortiert. Da auf dieses Array ueber die FormComponent zugegriffen
55cdf0e10cSrcweir wird, gibt es noch das GroupCompAccessArray, in dem die FormComponents
56cdf0e10cSrcweir nach ihrer Speicheradresse sortiert sind. Jedes Element des
57cdf0e10cSrcweir GroupCompAccessArrays ist mit einem Element des GroupCompArrays verpointert.
58cdf0e10cSrcweir 
59cdf0e10cSrcweir ========================================================================*/
60cdf0e10cSrcweir 
61cdf0e10cSrcweir //.........................................................................
62cdf0e10cSrcweir namespace frm
63cdf0e10cSrcweir {
64cdf0e10cSrcweir //.........................................................................
65cdf0e10cSrcweir 
66cdf0e10cSrcweir //========================================================================
67cdf0e10cSrcweir 	template <class ELEMENT, class LESS_COMPARE>
insert_sorted(::std::vector<ELEMENT> & _rArray,const ELEMENT & _rNewElement,const LESS_COMPARE & _rCompareOp)68cdf0e10cSrcweir 	sal_Int32 insert_sorted(::std::vector<ELEMENT>& _rArray, const ELEMENT& _rNewElement, const LESS_COMPARE& _rCompareOp)
69cdf0e10cSrcweir 	{
70cdf0e10cSrcweir 		typename ::std::vector<ELEMENT>::iterator aInsertPos = lower_bound(
71cdf0e10cSrcweir 			_rArray.begin(),
72cdf0e10cSrcweir 			_rArray.end(),
73cdf0e10cSrcweir 			_rNewElement,
74cdf0e10cSrcweir 			_rCompareOp
75cdf0e10cSrcweir 		);
76cdf0e10cSrcweir 		aInsertPos = _rArray.insert(aInsertPos, _rNewElement);
77cdf0e10cSrcweir 		return aInsertPos - _rArray.begin();
78cdf0e10cSrcweir 	}
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	template <class ELEMENT, class LESS_COMPARE>
seek_entry(const::std::vector<ELEMENT> & _rArray,const ELEMENT & _rNewElement,sal_Int32 & nPos,const LESS_COMPARE & _rCompareOp)81cdf0e10cSrcweir 	sal_Bool seek_entry(const ::std::vector<ELEMENT>& _rArray, const ELEMENT& _rNewElement, sal_Int32& nPos, const LESS_COMPARE& _rCompareOp)
82cdf0e10cSrcweir 	{
83cdf0e10cSrcweir 		typename ::std::vector<ELEMENT>::const_iterator aExistentPos = lower_bound(
84cdf0e10cSrcweir 			_rArray.begin(),
85cdf0e10cSrcweir 			_rArray.end(),
86cdf0e10cSrcweir 			_rNewElement,
87cdf0e10cSrcweir 			_rCompareOp
88cdf0e10cSrcweir 		);
89cdf0e10cSrcweir 		if ((aExistentPos != _rArray.end()) && (*aExistentPos == _rNewElement))
90cdf0e10cSrcweir 		{	// we have a valid "lower or equal" element and it's really "equal"
91cdf0e10cSrcweir 			nPos = aExistentPos - _rArray.begin();
92cdf0e10cSrcweir 			return sal_True;
93cdf0e10cSrcweir 		}
94cdf0e10cSrcweir 		nPos = -1;
95cdf0e10cSrcweir 		return sal_False;
96cdf0e10cSrcweir 	}
97cdf0e10cSrcweir 
98cdf0e10cSrcweir //========================================================================
99cdf0e10cSrcweir class OGroupComp
100cdf0e10cSrcweir {
101cdf0e10cSrcweir 	::rtl::OUString	m_aName;
102cdf0e10cSrcweir 	::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> 	m_xComponent;
103cdf0e10cSrcweir 	::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel> 	m_xControlModel;
104cdf0e10cSrcweir 	sal_Int32	m_nPos;
105cdf0e10cSrcweir 	sal_Int16	m_nTabIndex;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 	friend class OGroupCompLess;
108cdf0e10cSrcweir 
109cdf0e10cSrcweir public:
110cdf0e10cSrcweir 	OGroupComp(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& rxElement, sal_Int32 nInsertPos );
111cdf0e10cSrcweir 	OGroupComp(const OGroupComp& _rSource);
112cdf0e10cSrcweir 	OGroupComp();
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 	sal_Bool operator==( const OGroupComp& rComp ) const;
115cdf0e10cSrcweir 
GetComponent() const116cdf0e10cSrcweir 	inline const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& GetComponent() const { return m_xComponent; }
GetControlModel() const117cdf0e10cSrcweir 	inline const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel>&	GetControlModel() const { return m_xControlModel; }
118cdf0e10cSrcweir 
GetPos() const119cdf0e10cSrcweir 	sal_Int32	GetPos() const { return m_nPos; }
GetTabIndex() const120cdf0e10cSrcweir 	sal_Int16	GetTabIndex() const { return m_nTabIndex; }
GetName() const121cdf0e10cSrcweir 	::rtl::OUString GetName() const { return m_aName; }
122cdf0e10cSrcweir };
123cdf0e10cSrcweir 
124cdf0e10cSrcweir DECLARE_STL_VECTOR(OGroupComp, OGroupCompArr);
125cdf0e10cSrcweir 
126cdf0e10cSrcweir //========================================================================
127cdf0e10cSrcweir class OGroupComp;
128cdf0e10cSrcweir class OGroupCompAcc
129cdf0e10cSrcweir {
130cdf0e10cSrcweir 	::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet> 	m_xComponent;
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 	OGroupComp										m_aGroupComp;
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 	friend class OGroupCompAccLess;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir public:
137cdf0e10cSrcweir 	OGroupCompAcc(const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& rxElement, const OGroupComp& _rGroupComp );
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 	sal_Bool operator==( const OGroupCompAcc& rCompAcc ) const;
140cdf0e10cSrcweir 
GetComponent() const141cdf0e10cSrcweir 	inline const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>&	GetComponent() const { return m_xComponent; }
GetGroupComponent() const142cdf0e10cSrcweir 	const OGroupComp&	GetGroupComponent() const { return m_aGroupComp; }
143cdf0e10cSrcweir };
144cdf0e10cSrcweir 
145cdf0e10cSrcweir DECLARE_STL_VECTOR(OGroupCompAcc, OGroupCompAccArr);
146cdf0e10cSrcweir 
147cdf0e10cSrcweir //========================================================================
148cdf0e10cSrcweir class OGroup
149cdf0e10cSrcweir {
150cdf0e10cSrcweir 	OGroupCompArr		m_aCompArray;
151cdf0e10cSrcweir 	OGroupCompAccArr	m_aCompAccArray;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir 	::rtl::OUString m_aGroupName;
154cdf0e10cSrcweir 	sal_uInt16	m_nInsertPos;				// Die Einfugeposition der GroupComps wird von der Gruppe bestimmt.
155cdf0e10cSrcweir 
156cdf0e10cSrcweir 	friend class OGroupLess;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir public:
159cdf0e10cSrcweir 	OGroup( const ::rtl::OUString& rGroupName );
160cdf0e10cSrcweir #ifdef DBG_UTIL
161cdf0e10cSrcweir 	OGroup( const OGroup& _rSource );	// just to ensure the DBG_CTOR call
162cdf0e10cSrcweir #endif
163cdf0e10cSrcweir 	virtual ~OGroup();
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 	sal_Bool operator==( const OGroup& rGroup ) const;
166cdf0e10cSrcweir 
GetGroupName() const167cdf0e10cSrcweir 	::rtl::OUString GetGroupName() const { return m_aGroupName; }
168cdf0e10cSrcweir 	::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel>  > GetControlModels() const;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 	void InsertComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& rxElement );
171cdf0e10cSrcweir 	void RemoveComponent( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& rxElement );
Count() const172cdf0e10cSrcweir 	sal_uInt16 Count() const { return sal::static_int_cast< sal_uInt16 >(m_aCompArray.size()); }
GetObject(sal_uInt16 nP) const173cdf0e10cSrcweir 	const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& GetObject( sal_uInt16 nP ) const
174cdf0e10cSrcweir 		{ return m_aCompArray[nP].GetComponent(); }
175cdf0e10cSrcweir };
176cdf0e10cSrcweir 
177cdf0e10cSrcweir DECLARE_STL_USTRINGACCESS_MAP(OGroup, OGroupArr);
178cdf0e10cSrcweir DECLARE_STL_VECTOR(OGroupArr::iterator, OActiveGroups);
179cdf0e10cSrcweir 
180cdf0e10cSrcweir //========================================================================
181cdf0e10cSrcweir class OGroupManager	: public ::cppu::WeakImplHelper2< ::com::sun::star::beans::XPropertyChangeListener, ::com::sun::star::container::XContainerListener>
182cdf0e10cSrcweir {
183cdf0e10cSrcweir 	OGroup*			m_pCompGroup;			// Alle Components nach TabIndizes sortiert
184cdf0e10cSrcweir 	OGroupArr		m_aGroupArr;			// Alle Components nach Gruppen sortiert
185cdf0e10cSrcweir 	OActiveGroups	m_aActiveGroupMap;		// In dieser Map werden die Indizes aller Gruppen gehalten,
186cdf0e10cSrcweir 										// die mehr als 1 Element haben
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	::com::sun::star::uno::Reference< ::com::sun::star::container::XContainer >
189cdf0e10cSrcweir 					m_xContainer;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 	// Helper functions
192cdf0e10cSrcweir 	void InsertElement( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& rxElement );
193cdf0e10cSrcweir 	void RemoveElement( const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& rxElement );
194cdf0e10cSrcweir 	void removeFromGroupMap(const ::rtl::OUString& _sGroupName,const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet>& _xSet);
195cdf0e10cSrcweir 
196cdf0e10cSrcweir public:
197cdf0e10cSrcweir 	OGroupManager(const ::com::sun::star::uno::Reference< ::com::sun::star::container::XContainer >& _rxContainer);
198cdf0e10cSrcweir 	virtual ~OGroupManager();
199cdf0e10cSrcweir 
200cdf0e10cSrcweir // ::com::sun::star::lang::XEventListener
201cdf0e10cSrcweir 	virtual void SAL_CALL disposing(const ::com::sun::star::lang::EventObject& _rSource) throw(::com::sun::star::uno::RuntimeException);
202cdf0e10cSrcweir 
203cdf0e10cSrcweir // ::com::sun::star::beans::XPropertyChangeListener
204cdf0e10cSrcweir 	virtual void SAL_CALL propertyChange(const ::com::sun::star::beans::PropertyChangeEvent& evt) throw ( ::com::sun::star::uno::RuntimeException);
205cdf0e10cSrcweir 
206cdf0e10cSrcweir // ::com::sun::star::container::XContainerListener
207cdf0e10cSrcweir 	virtual void SAL_CALL elementInserted(const ::com::sun::star::container::ContainerEvent& _rEvent) throw ( ::com::sun::star::uno::RuntimeException);
208cdf0e10cSrcweir 	virtual void SAL_CALL elementRemoved(const ::com::sun::star::container::ContainerEvent& _rEvent) throw ( ::com::sun::star::uno::RuntimeException);
209cdf0e10cSrcweir 	virtual void SAL_CALL elementReplaced(const ::com::sun::star::container::ContainerEvent& _rEvent) throw ( ::com::sun::star::uno::RuntimeException);
210cdf0e10cSrcweir 
211cdf0e10cSrcweir // Other functions
212cdf0e10cSrcweir 	sal_Int32 getGroupCount();
213cdf0e10cSrcweir 	void getGroup(sal_Int32 nGroup, ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel> >& _rGroup, ::rtl::OUString& Name);
214cdf0e10cSrcweir 	void getGroupByName(const ::rtl::OUString& Name, ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel> >& _rGroup);
215cdf0e10cSrcweir 	::com::sun::star::uno::Sequence< ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlModel> > getControlModels();
216cdf0e10cSrcweir };
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 
219cdf0e10cSrcweir //.........................................................................
220cdf0e10cSrcweir }	// namespace frm
221cdf0e10cSrcweir //.........................................................................
222cdf0e10cSrcweir 
223cdf0e10cSrcweir #endif // _FRM_GROUPMANAGER_HXX_
224cdf0e10cSrcweir 
225