1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 #ifndef DBACCESS_TABLEWINDOWACCESS_HXX
31 #include "TableWindowAccess.hxx"
32 #endif
33 #ifndef DBACCESS_JACCESS_HXX
34 #include "JAccess.hxx"
35 #endif
36 #ifndef DBAUI_TABLEWINDOW_HXX
37 #include "TableWindow.hxx"
38 #endif
39 #ifndef DBAUI_TABLEWINDOWLISTBOX_HXX
40 #include "TableWindowListBox.hxx"
41 #endif
42 #ifndef DBAUI_JOINDESIGNVIEW_HXX
43 #include "JoinDesignView.hxx"
44 #endif
45 #ifndef DBAUI_JOINCONTROLLER_HXX
46 #include "JoinController.hxx"
47 #endif
48 #ifndef DBAUI_JOINTABLEVIEW_HXX
49 #include "JoinTableView.hxx"
50 #endif
51 #ifndef _COM_SUN_STAR_ACCESSIBILITY_ACCESSIBLEROLE_HPP_
52 #include <com/sun/star/accessibility/AccessibleRole.hpp>
53 #endif
54 #ifndef _COM_SUN_STAR_ACCESSIBILITY_ACCESSIBLERELATIONTYPE_HPP_
55 #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
56 #endif
57 #ifndef _COM_SUN_STAR_ACCESSIBILITY_ACCESSIBLESTATETYPE_HPP_
58 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
59 #endif
60 #ifndef _COM_SUN_STAR_ACCESSIBILITY_ACCESSIBLEEVENTID_HPP_
61 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
62 #endif
63 #ifndef _COMPHELPER_SEQUENCE_HXX_
64 #include <comphelper/sequence.hxx>
65 #endif
66 #ifndef _DBU_QRY_HRC_
67 #include "dbu_qry.hrc"
68 #endif
69 
70 
71 namespace dbaui
72 {
73 	using namespace ::com::sun::star::accessibility;
74 	using namespace ::com::sun::star::uno;
75 	using namespace ::com::sun::star::beans;
76 	using namespace ::com::sun::star::lang;
77 	//	using namespace ::com::sun::star::awt;
78 	using namespace ::com::sun::star;
79 
80 	OTableWindowAccess::OTableWindowAccess(OTableWindow* _pTable)
81 		:VCLXAccessibleComponent(_pTable->GetComponentInterface().is() ? _pTable->GetWindowPeer() : NULL)
82 		,m_pTable(_pTable)
83 	{
84 	}
85 	// -----------------------------------------------------------------------------
86     void SAL_CALL OTableWindowAccess::disposing()
87 	{
88 		m_pTable = NULL;
89 		VCLXAccessibleComponent::disposing();
90 	}
91     // -----------------------------------------------------------------------------
92     void OTableWindowAccess::ProcessWindowEvent( const VclWindowEvent& rVclWindowEvent )
93 	{
94         if ( rVclWindowEvent.GetId() == VCLEVENT_OBJECT_DYING )
95 		{
96         	::osl::MutexGuard aGuard( m_aMutex );
97             m_pTable = NULL;
98 		}
99 
100         VCLXAccessibleComponent::ProcessWindowEvent( rVclWindowEvent );
101 	}
102 	// -----------------------------------------------------------------------------
103 	Any SAL_CALL OTableWindowAccess::queryInterface( const Type& aType ) throw (RuntimeException)
104 	{
105 		Any aRet(VCLXAccessibleComponent::queryInterface( aType ));
106 		return aRet.hasValue() ? aRet : OTableWindowAccess_BASE::queryInterface( aType );
107 	}
108 	// -----------------------------------------------------------------------------
109 	Sequence< Type > SAL_CALL OTableWindowAccess::getTypes(  ) throw (RuntimeException)
110 	{
111 		return ::comphelper::concatSequences(VCLXAccessibleComponent::getTypes(),OTableWindowAccess_BASE::getTypes());
112 	}
113 	// -----------------------------------------------------------------------------
114 	::rtl::OUString SAL_CALL OTableWindowAccess::getImplementationName() throw(RuntimeException)
115 	{
116 		return getImplementationName_Static();
117 	}
118 	// -----------------------------------------------------------------------------
119 	Sequence< ::rtl::OUString > SAL_CALL OTableWindowAccess::getSupportedServiceNames() throw(RuntimeException)
120 	{
121 		return getSupportedServiceNames_Static();
122 	}
123 	// -----------------------------------------------------------------------------
124 	// XServiceInfo - static methods
125 	Sequence< ::rtl::OUString > OTableWindowAccess::getSupportedServiceNames_Static(void) throw( RuntimeException )
126 	{
127 		Sequence< ::rtl::OUString > aSupported(2);
128 		aSupported[0] = ::rtl::OUString::createFromAscii("com.sun.star.accessibility.Accessible");
129 		aSupported[1] = ::rtl::OUString::createFromAscii("com.sun.star.accessibility.AccessibleContext");
130 		return aSupported;
131 	}
132 	// -----------------------------------------------------------------------------
133 	::rtl::OUString OTableWindowAccess::getImplementationName_Static(void) throw( RuntimeException )
134 	{
135 		return ::rtl::OUString::createFromAscii("org.openoffice.comp.dbu.TableWindowAccessibility");
136 	}
137 	// -----------------------------------------------------------------------------
138 	// XAccessibleContext
139     sal_Int32 SAL_CALL OTableWindowAccess::getAccessibleChildCount(  ) throw (RuntimeException)
140     {
141         ::osl::MutexGuard aGuard( m_aMutex );
142         sal_Int32 nCount = 0;
143         if(m_pTable)
144         {
145             if(m_pTable->GetTitleCtrl())
146                 ++nCount;
147             if(m_pTable->GetListBox())
148                 ++nCount;
149         }
150 		return nCount;
151 	}
152     // -----------------------------------------------------------------------------
153     Reference< XAccessible > SAL_CALL OTableWindowAccess::getAccessibleChild( sal_Int32 i ) throw (IndexOutOfBoundsException,RuntimeException)
154     {
155         ::osl::MutexGuard aGuard( m_aMutex );
156         Reference< XAccessible > aRet;
157         if(m_pTable)
158         {
159             switch(i)
160             {
161                 case 0:
162                     if(m_pTable->GetTitleCtrl())
163                     {
164                         aRet = m_pTable->GetTitleCtrl()->GetAccessible();
165                         break;
166                     } // fall through if title control does not exist
167                 case 1:
168                     if(m_pTable->GetListBox())
169                         aRet = m_pTable->GetListBox()->GetAccessible();
170                     break;
171                 default:
172                     throw IndexOutOfBoundsException();
173             }
174         }
175         return aRet;
176     }
177 	// -----------------------------------------------------------------------------
178 	sal_Int32 SAL_CALL OTableWindowAccess::getAccessibleIndexInParent(  ) throw (RuntimeException)
179 	{
180 		::osl::MutexGuard aGuard( m_aMutex  );
181 		sal_Int32 nIndex = -1;
182 		if( m_pTable )
183 		{
184 			// search the postion of our table window in the table window map
185 			OJoinTableView::OTableWindowMap* pMap = m_pTable->getTableView()->GetTabWinMap();
186 			OJoinTableView::OTableWindowMap::iterator aIter = pMap->begin();
187             OJoinTableView::OTableWindowMap::iterator aEnd = pMap->end();
188 			for (nIndex = 0; aIter != aEnd && aIter->second != m_pTable; ++nIndex,++aIter)
189 				;
190 			nIndex = aIter != aEnd ? nIndex : -1;
191 		}
192 		return nIndex;
193 	}
194 	// -----------------------------------------------------------------------------
195 	sal_Int16 SAL_CALL OTableWindowAccess::getAccessibleRole(  ) throw (RuntimeException)
196 	{
197 		return AccessibleRole::PANEL; // ? or may be an AccessibleRole::WINDOW
198 	}
199 	// -----------------------------------------------------------------------------
200 	Reference< XAccessibleRelationSet > SAL_CALL OTableWindowAccess::getAccessibleRelationSet(  ) throw (RuntimeException)
201 	{
202 		::osl::MutexGuard aGuard( m_aMutex  );
203 		return this;
204 	}
205 	// -----------------------------------------------------------------------------
206 	// XAccessibleComponent
207 	Reference< XAccessible > SAL_CALL OTableWindowAccess::getAccessibleAtPoint( const awt::Point& _aPoint ) throw (RuntimeException)
208 	{
209 		::osl::MutexGuard aGuard( m_aMutex  );
210 		Reference< XAccessible > aRet;
211 		if( m_pTable )
212 		{
213 			Point aPoint(_aPoint.X,_aPoint.Y);
214 			Rectangle aRect(m_pTable->GetDesktopRectPixel());
215 			if( aRect.IsInside(aPoint) )
216 				aRet = this;
217 			else if( m_pTable->GetListBox()->GetDesktopRectPixel().IsInside(aPoint))
218 				aRet = m_pTable->GetListBox()->GetAccessible();
219 		}
220 		return aRet;
221 	}
222 	// -----------------------------------------------------------------------------
223 	Reference< XAccessible > OTableWindowAccess::getParentChild(sal_Int32 _nIndex)
224 	{
225 		Reference< XAccessible > xReturn;
226 		Reference< XAccessible > xParent = getAccessibleParent();
227 		if ( xParent.is() )
228 		{
229 			Reference< XAccessibleContext > xParentContext = xParent->getAccessibleContext();
230 			if ( xParentContext.is() )
231 			{
232 				xReturn = xParentContext->getAccessibleChild(_nIndex);
233 			}
234 		}
235 		return xReturn;
236 	}
237 	// -----------------------------------------------------------------------------
238 
239 	sal_Int32 SAL_CALL OTableWindowAccess::getRelationCount(  ) throw (RuntimeException)
240 	{
241 		::osl::MutexGuard aGuard( m_aMutex  );
242 		return m_pTable ? m_pTable->getTableView()->getConnectionCount(m_pTable) : sal_Int32(0);
243 	}
244 	// -----------------------------------------------------------------------------
245 	AccessibleRelation SAL_CALL OTableWindowAccess::getRelation( sal_Int32 nIndex ) throw (IndexOutOfBoundsException, RuntimeException)
246 	{
247 		::osl::MutexGuard aGuard( m_aMutex  );
248 		if( nIndex < 0 || nIndex >= getRelationCount() )
249 			throw IndexOutOfBoundsException();
250 
251 		AccessibleRelation aRet;
252 		if( m_pTable )
253 		{
254 			OJoinTableView* pView = m_pTable->getTableView();
255 			::std::vector<OTableConnection*>::const_iterator aIter = pView->getTableConnections(m_pTable) + nIndex;
256 			aRet.TargetSet.realloc(1);
257 			aRet.TargetSet[0] = getParentChild(aIter - pView->getTableConnections()->begin());
258 			aRet.RelationType = AccessibleRelationType::CONTROLLER_FOR;
259 		}
260 		return aRet;
261 	}
262 	// -----------------------------------------------------------------------------
263 	sal_Bool SAL_CALL OTableWindowAccess::containsRelation( sal_Int16 aRelationType ) throw (RuntimeException)
264 	{
265 		::osl::MutexGuard aGuard( m_aMutex  );
266 		return		AccessibleRelationType::CONTROLLER_FOR == aRelationType
267 				&&	m_pTable && m_pTable->getTableView()->ExistsAConn(m_pTable);
268 	}
269 	// -----------------------------------------------------------------------------
270 	AccessibleRelation SAL_CALL OTableWindowAccess::getRelationByType( sal_Int16 aRelationType ) throw (RuntimeException)
271 	{
272 		::osl::MutexGuard aGuard( m_aMutex  );
273 		if( AccessibleRelationType::CONTROLLER_FOR == aRelationType && m_pTable)
274 		{
275 			OJoinTableView* pView = m_pTable->getTableView();
276 			const ::std::vector<OTableConnection*>* pConnectionList = pView->getTableConnections();
277 
278 			::std::vector<OTableConnection*>::const_iterator aIter = pView->getTableConnections(m_pTable);
279             ::std::vector<OTableConnection*>::const_iterator aEnd = pConnectionList->end();
280 			::std::vector< Reference<XInterface> > aRelations;
281 			aRelations.reserve(5); // just guessing
282 			for (; aIter != aEnd ; ++aIter )
283 				aRelations.push_back(getParentChild(aIter - pConnectionList->begin()));
284 
285 			Reference<XInterface> *pRelations = aRelations.empty() ? 0 : &aRelations[0];
286 			Sequence< Reference<XInterface> > aSeq(pRelations, aRelations.size());
287 			return AccessibleRelation(AccessibleRelationType::CONTROLLER_FOR,aSeq);
288 		}
289 		return AccessibleRelation();
290 	}
291 	// -----------------------------------------------------------------------------
292 	sal_Bool OTableWindowAccess::isEditable() const
293 	{
294 		return m_pTable && !m_pTable->getTableView()->getDesignView()->getController().isReadOnly();
295 	}
296 	// -----------------------------------------------------------------------------
297 	::rtl::OUString SAL_CALL OTableWindowAccess::getTitledBorderText(  ) throw (RuntimeException)
298 	{
299 		return getAccessibleName(  );
300 	}
301 	// -----------------------------------------------------------------------------
302 	::rtl::OUString SAL_CALL OTableWindowAccess::getAccessibleName(  ) throw (RuntimeException)
303 	{
304 		::osl::MutexGuard aGuard( m_aMutex  );
305 		::rtl::OUString sAccessibleName;
306 		if ( m_pTable )
307 			sAccessibleName = m_pTable->getTitle();
308 		return sAccessibleName;
309 	}
310 	// -----------------------------------------------------------------------------
311 	Reference< XAccessibleContext > SAL_CALL OTableWindowAccess::getAccessibleContext(  ) throw (::com::sun::star::uno::RuntimeException)
312 	{
313 		return this;
314 	}
315 	// -----------------------------------------------------------------------------
316 
317 }
318 // -----------------------------------------------------------------------------
319 
320