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