1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: exsrcbrw.cxx,v $
10 * $Revision: 1.32.6.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #ifndef _SBA_EXTCTRLR_HXX
35 #include "exsrcbrw.hxx"
37 #ifndef _COM_SUN_STAR_FORM_FORMCOMPONENTTYPE_HPP_
38 #include <com/sun/star/form/FormComponentType.hpp>
40 #ifndef _COM_SUN_STAR_UTIL_XURLTRANSFORMER_HPP_
41 #include <com/sun/star/util/XURLTransformer.hpp>
43 #ifndef _COM_SUN_STAR_FORM_XGRIDCOLUMNFACTORY_HPP_
44 #include <com/sun/star/form/XGridColumnFactory.hpp>
46 #ifndef _COM_SUN_STAR_FORM_XFORMCONTROLLER_HPP_
47 #include <com/sun/star/form/XFormController.hpp>
49 #ifndef _COM_SUN_STAR_FORM_XLOADABLE_HPP_
50 #include <com/sun/star/form/XLoadable.hpp>
52 #ifndef _COM_SUN_STAR_FRAME_FRAMESEARCHFLAG_HPP_
53 #include <com/sun/star/frame/FrameSearchFlag.hpp>
55 #ifndef _SBA_FORMADAPTER_HXX
56 #include "formadapter.hxx"
58 #ifndef _COMPHELPER_PROCESSFACTORY_HXX_
59 #include <comphelper/processfactory.hxx>
61 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
62 #include "dbustrings.hrc"
64 #ifndef _DBU_REGHELPER_HXX_
65 #include "dbu_reghelper.hxx"
67 #ifndef TOOLS_DIAGNOSE_EX_H
68 #include <tools/diagnose_ex.h>
71 using namespace ::com::sun::star::uno
;
72 using namespace ::com::sun::star::sdb
;
73 using namespace ::com::sun::star::sdbc
;
74 using namespace ::com::sun::star::sdbcx
;
75 using namespace ::com::sun::star::beans
;
76 using namespace ::com::sun::star::container
;
77 using namespace ::com::sun::star::lang
;
78 using namespace ::com::sun::star::form
;
79 using namespace ::com::sun::star::frame
;
80 using namespace dbaui
;
82 //==============================================================================
83 //= SbaExternalSourceBrowser
84 //==============================================================================
85 extern "C" void SAL_CALL
createRegistryInfo_OFormGridView()
87 static OMultiInstanceAutoRegistration
< SbaExternalSourceBrowser
> aAutoRegistration
;
89 //------------------------------------------------------------------------------
90 Any SAL_CALL
SbaExternalSourceBrowser::queryInterface(const Type
& _rType
) throw (RuntimeException
)
92 Any aRet
= SbaXDataBrowserController::queryInterface(_rType
);
94 aRet
= ::cppu::queryInterface(_rType
,
95 (::com::sun::star::util::XModifyBroadcaster
*)this,
96 (::com::sun::star::form::XLoadListener
*)this);
100 DBG_NAME(SbaExternalSourceBrowser
)
101 //------------------------------------------------------------------------------
102 SbaExternalSourceBrowser::SbaExternalSourceBrowser(const Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rM
)
103 :SbaXDataBrowserController(_rM
)
104 ,m_aModifyListeners(getMutex())
105 ,m_pDataSourceImpl(NULL
)
106 ,m_bInQueryDispatch( sal_False
)
108 DBG_CTOR(SbaExternalSourceBrowser
,NULL
);
112 //------------------------------------------------------------------------------
113 SbaExternalSourceBrowser::~SbaExternalSourceBrowser()
116 DBG_DTOR(SbaExternalSourceBrowser
,NULL
);
119 //-------------------------------------------------------------------------
120 ::comphelper::StringSequence SAL_CALL
SbaExternalSourceBrowser::getSupportedServiceNames() throw(RuntimeException
)
122 return getSupportedServiceNames_Static();
124 // -------------------------------------------------------------------------
125 ::rtl::OUString
SbaExternalSourceBrowser::getImplementationName_Static() throw(RuntimeException
)
127 return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.comp.dbu.OFormGridView"));
129 //-------------------------------------------------------------------------
130 ::comphelper::StringSequence
SbaExternalSourceBrowser::getSupportedServiceNames_Static() throw(RuntimeException
)
132 ::comphelper::StringSequence
aSupported(1);
133 aSupported
.getArray()[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.FormGridView"));
136 //-------------------------------------------------------------------------
137 Reference
< XInterface
> SAL_CALL
SbaExternalSourceBrowser::Create(const Reference
<XMultiServiceFactory
>& _rxFactory
)
139 return *(new SbaExternalSourceBrowser(_rxFactory
));
141 //-------------------------------------------------------------------------
142 ::rtl::OUString SAL_CALL
SbaExternalSourceBrowser::getImplementationName() throw(RuntimeException
)
144 return getImplementationName_Static();
146 //------------------------------------------------------------------------------
147 Reference
< XRowSet
> SbaExternalSourceBrowser::CreateForm()
149 m_pDataSourceImpl
= new SbaXFormAdapter();
150 return m_pDataSourceImpl
;
153 //------------------------------------------------------------------------------
154 sal_Bool
SbaExternalSourceBrowser::InitializeForm(const Reference
< XRowSet
> & /*xForm*/)
159 //------------------------------------------------------------------
160 sal_Bool
SbaExternalSourceBrowser::LoadForm()
162 // as we don't have a main form (yet), we have nothing to do
163 // we don't call FormLoaded, because this expects a working data source
168 //------------------------------------------------------------------
169 void SbaExternalSourceBrowser::modified(const ::com::sun::star::lang::EventObject
& aEvent
) throw( RuntimeException
)
171 SbaXDataBrowserController::modified(aEvent
);
173 // multiplex this event to all my listeners
174 ::com::sun::star::lang::EventObject
aEvt(*this);
175 ::cppu::OInterfaceIteratorHelper
aIt(m_aModifyListeners
);
176 while (aIt
.hasMoreElements())
177 ((::com::sun::star::util::XModifyListener
*)aIt
.next())->modified(aEvt
);
180 //------------------------------------------------------------------
181 void SAL_CALL
SbaExternalSourceBrowser::dispatch(const ::com::sun::star::util::URL
& aURL
, const Sequence
< ::com::sun::star::beans::PropertyValue
>& aArgs
) throw(::com::sun::star::uno::RuntimeException
)
183 const ::com::sun::star::beans::PropertyValue
* pArguments
= aArgs
.getConstArray();
184 if (aURL
.Complete
.equals(::rtl::OUString::createFromAscii(".uno:FormSlots/AddGridColumn")))
186 // search the argument describing the column to create
187 ::rtl::OUString sControlType
;
188 sal_Int32 nControlPos
= -1;
189 Sequence
< ::com::sun::star::beans::PropertyValue
> aControlProps
;
191 for ( i
= 0; i
< aArgs
.getLength(); ++i
, ++pArguments
)
193 if (pArguments
->Name
.equals(::rtl::OUString::createFromAscii("ColumnType")))
195 sal_Bool bCorrectType
= pArguments
->Value
.getValueType().equals(::getCppuType((const ::rtl::OUString
*)0));
196 OSL_ENSURE(bCorrectType
, "invalid type for argument \"ColumnType\" !");
198 sControlType
= ::comphelper::getString(pArguments
->Value
);
200 else if (pArguments
->Name
.equals(::rtl::OUString::createFromAscii("ColumnPosition")))
202 sal_Bool bCorrectType
= pArguments
->Value
.getValueType().equals(::getCppuType((const sal_Int16
*)0));
203 OSL_ENSURE(bCorrectType
, "invalid type for argument \"ColumnPosition\" !");
205 nControlPos
= ::comphelper::getINT16(pArguments
->Value
);
207 else if (pArguments
->Name
.equals(::rtl::OUString::createFromAscii("ColumnProperties")))
209 sal_Bool bCorrectType
= pArguments
->Value
.getValueType().equals(::getCppuType((const Sequence
< ::com::sun::star::beans::PropertyValue
>*)0));
210 OSL_ENSURE(bCorrectType
, "invalid type for argument \"ColumnProperties\" !");
212 aControlProps
= *(Sequence
< ::com::sun::star::beans::PropertyValue
>*)pArguments
->Value
.getValue();
215 OSL_ENSURE(sal_False
, ((ByteString("SbaExternalSourceBrowser::dispatch(AddGridColumn) : unknown argument (") += ByteString((const sal_Unicode
*)pArguments
->Name
, gsl_getSystemTextEncoding()).GetBuffer()) += ") !").GetBuffer());
217 if (!sControlType
.getLength())
219 OSL_ENSURE(sal_False
, "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnType) !");
220 sControlType
= ::rtl::OUString::createFromAscii("TextField");
222 OSL_ENSURE(aControlProps
.getLength(), "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnProperties) !");
225 Reference
< ::com::sun::star::form::XGridColumnFactory
> xColFactory(getControlModel(), UNO_QUERY
);
226 Reference
< ::com::sun::star::beans::XPropertySet
> xNewCol
= xColFactory
->createColumn(sControlType
);
227 Reference
< XPropertySetInfo
> xNewColProperties
;
229 xNewColProperties
= xNewCol
->getPropertySetInfo();
230 // set it's properties
231 if (xNewColProperties
.is())
233 const ::com::sun::star::beans::PropertyValue
* pControlProps
= aControlProps
.getConstArray();
234 for (i
=0; i
<aControlProps
.getLength(); ++i
, ++pControlProps
)
238 if (xNewColProperties
->hasPropertyByName(pControlProps
->Name
))
239 xNewCol
->setPropertyValue(pControlProps
->Name
, pControlProps
->Value
);
243 OSL_ENSURE(sal_False
,
244 ( ByteString("SbaExternalSourceBrowser::dispatch : could not set a column property (")
245 += ByteString(pControlProps
->Name
.getStr(), (sal_uInt16
)pControlProps
->Name
.getLength(), RTL_TEXTENCODING_ASCII_US
)
246 += ByteString(")!")).GetBuffer());
251 // correct the position
252 Reference
< ::com::sun::star::container::XIndexContainer
> xColContainer(getControlModel(), UNO_QUERY
);
254 if (nControlPos
> xColContainer
->getCount())
255 nControlPos
= xColContainer
->getCount();
260 xColContainer
->insertByIndex(nControlPos
, makeAny(xNewCol
));
262 else if (aURL
.Complete
.equals(::rtl::OUString::createFromAscii(".uno:FormSlots/ClearView")))
266 else if (aURL
.Complete
.equals(::rtl::OUString::createFromAscii(".uno:FormSlots/AttachToForm")))
268 if (!m_pDataSourceImpl
)
271 Reference
< XRowSet
> xMasterForm
;
272 // search the arguments for he master form
273 for (sal_uInt16 i
=0; i
<aArgs
.getLength(); ++i
, ++pArguments
)
275 if ((pArguments
->Name
.equals(::rtl::OUString::createFromAscii("MasterForm"))) && (pArguments
->Value
.getValueTypeClass() == TypeClass_INTERFACE
))
277 xMasterForm
= Reference
< XRowSet
> (*(Reference
< XInterface
> *)pArguments
->Value
.getValue(), UNO_QUERY
);
281 if (!xMasterForm
.is())
283 OSL_ENSURE(sal_False
, "SbaExternalSourceBrowser::dispatch(FormSlots/AttachToForm) : please specify a form to attach to as argument !");
290 SbaXDataBrowserController::dispatch(aURL
, aArgs
);
293 //------------------------------------------------------------------
294 Reference
< ::com::sun::star::frame::XDispatch
> SAL_CALL
SbaExternalSourceBrowser::queryDispatch(const ::com::sun::star::util::URL
& aURL
, const ::rtl::OUString
& aTargetFrameName
, sal_Int32 nSearchFlags
) throw( RuntimeException
)
296 Reference
< ::com::sun::star::frame::XDispatch
> xReturn
;
297 if (m_bInQueryDispatch
)
300 m_bInQueryDispatch
= sal_True
;
302 if ( (aURL
.Complete
.equals(::rtl::OUString::createFromAscii(".uno:FormSlots/AttachToForm")))
303 // attach a new external form
304 || (aURL
.Complete
.equals(::rtl::OUString::createFromAscii(".uno:FormSlots/AddGridColumn")))
305 // add a column to the grid
306 || (aURL
.Complete
.equals(::rtl::OUString::createFromAscii(".uno:FormSlots/ClearView")))
309 xReturn
= (::com::sun::star::frame::XDispatch
*)this;
312 && ( (aURL
.Complete
.equals(::rtl::OUString::createFromAscii(".uno:FormSlots/moveToFirst")))
313 || (aURL
.Complete
.equals(::rtl::OUString::createFromAscii(".uno:FormSlots/moveToPrev")))
314 || (aURL
.Complete
.equals(::rtl::OUString::createFromAscii(".uno:FormSlots/moveToNext")))
315 || (aURL
.Complete
.equals(::rtl::OUString::createFromAscii(".uno:FormSlots/moveToLast")))
316 || (aURL
.Complete
.equals(::rtl::OUString::createFromAscii(".uno:FormSlots/moveToNew")))
317 || (aURL
.Complete
.equals(::rtl::OUString::createFromAscii(".uno:FormSlots/undoRecord")))
321 OSL_ENSURE(aURL
.Mark
.getLength() == 0, "SbaExternalSourceBrowser::queryDispatch : the ::com::sun::star::util::URL shouldn't have a mark !");
322 ::com::sun::star::util::URL aNewUrl
= aURL
;
324 // split the ::com::sun::star::util::URL
325 OSL_ENSURE( m_xUrlTransformer
.is(), "SbaExternalSourceBrowser::queryDispatch : could not create an URLTransformer !" );
326 if ( m_xUrlTransformer
.is() )
327 m_xUrlTransformer
->parseStrict( aNewUrl
);
330 aNewUrl
.Mark
= ::rtl::OUString::createFromAscii("DB/FormGridView");
331 // this controller is instantiated when somebody dispatches the ".component:DB/FormGridView" in any
332 // frame, so we use "FormGridView" as mark that a dispatch request came from this view
334 if (m_xUrlTransformer
.is())
335 m_xUrlTransformer
->assemble(aNewUrl
);
337 Reference
< XDispatchProvider
> xFrameDispatcher( getFrame(), UNO_QUERY
);
338 if (xFrameDispatcher
.is())
339 xReturn
= xFrameDispatcher
->queryDispatch(aNewUrl
, aTargetFrameName
, FrameSearchFlag::PARENT
);
344 xReturn
= SbaXDataBrowserController::queryDispatch(aURL
, aTargetFrameName
, nSearchFlags
);
346 m_bInQueryDispatch
= sal_False
;
350 //------------------------------------------------------------------
351 void SAL_CALL
SbaExternalSourceBrowser::disposing()
353 // say our modify listeners goodbye
354 ::com::sun::star::lang::EventObject aEvt
;
355 aEvt
.Source
= (XWeak
*) this;
356 m_aModifyListeners
.disposeAndClear(aEvt
);
360 SbaXDataBrowserController::disposing();
363 //------------------------------------------------------------------
364 void SAL_CALL
SbaExternalSourceBrowser::addModifyListener(const Reference
< ::com::sun::star::util::XModifyListener
> & aListener
) throw( RuntimeException
)
366 m_aModifyListeners
.addInterface(aListener
);
369 //------------------------------------------------------------------
370 void SAL_CALL
SbaExternalSourceBrowser::removeModifyListener(const Reference
< ::com::sun::star::util::XModifyListener
> & aListener
) throw( RuntimeException
)
372 m_aModifyListeners
.removeInterface(aListener
);
375 //------------------------------------------------------------------
376 void SAL_CALL
SbaExternalSourceBrowser::unloading(const ::com::sun::star::lang::EventObject
& aEvent
) throw( RuntimeException
)
378 if (m_pDataSourceImpl
&& (m_pDataSourceImpl
->getAttachedForm() == aEvent
.Source
))
383 SbaXDataBrowserController::unloading(aEvent
);
386 //------------------------------------------------------------------
387 void SbaExternalSourceBrowser::Attach(const Reference
< XRowSet
> & xMaster
)
390 sal_Bool bWasInsertRow
= sal_False
;
391 sal_Bool bBeforeFirst
= sal_True
;
392 sal_Bool bAfterLast
= sal_True
;
393 Reference
< XResultSet
> xResultSet(xMaster
, UNO_QUERY
);
394 Reference
< XRowLocate
> xCursor(xMaster
, UNO_QUERY
);
395 Reference
< XPropertySet
> xMasterProps(xMaster
, UNO_QUERY
);
399 // switch the control to design mode
400 if (getBrowserView() && getBrowserView()->getGridControl().is())
401 getBrowserView()->getGridControl()->setDesignMode(sal_True
);
403 // the grid will move the form's cursor to the first record, but we want the form to remain unchanged
404 // restore the old position
405 if (xCursor
.is() && xResultSet
.is())
407 bBeforeFirst
= xResultSet
->isBeforeFirst();
408 bAfterLast
= xResultSet
->isAfterLast();
409 if(!bBeforeFirst
&& !bAfterLast
)
410 aOldPos
= xCursor
->getBookmark();
413 if (xMasterProps
.is())
414 xMasterProps
->getPropertyValue(PROPERTY_ISNEW
) >>= bWasInsertRow
;
416 catch( const Exception
& )
418 DBG_UNHANDLED_EXCEPTION();
421 onStartLoading( Reference
< XLoadable
>( xMaster
, UNO_QUERY
) );
424 m_pDataSourceImpl
->AttachForm(xMaster
);
429 // at this point we have to reset the formatter for the new form
431 // assume that the master form is already loaded
432 #if OSL_DEBUG_LEVEL > 0
434 Reference
< XLoadable
> xLoadable( xMaster
, UNO_QUERY
);
435 OSL_ENSURE( xLoadable
.is() && xLoadable
->isLoaded(), "SbaExternalSourceBrowser::Attach: master is not loaded!" );
439 LoadFinished(sal_True
);
441 Reference
< XResultSetUpdate
> xUpdate(xMaster
, UNO_QUERY
);
444 if (bWasInsertRow
&& xUpdate
.is())
445 xUpdate
->moveToInsertRow();
446 else if (xCursor
.is() && aOldPos
.hasValue())
447 xCursor
->moveToBookmark(aOldPos
);
448 else if(bBeforeFirst
&& xResultSet
.is())
449 xResultSet
->beforeFirst();
450 else if(bAfterLast
&& xResultSet
.is())
451 xResultSet
->afterLast();
455 OSL_ENSURE(sal_False
, "SbaExternalSourceBrowser::Attach : couldn't restore the cursor position !");
461 //------------------------------------------------------------------
462 void SbaExternalSourceBrowser::ClearView()
464 // set a new (empty) datasource
465 Attach(Reference
< XRowSet
> ());
468 // clear all cols in the grid
469 Reference
< ::com::sun::star::container::XIndexContainer
> xColContainer(getControlModel(), UNO_QUERY
);
470 while (xColContainer
->getCount() > 0)
471 xColContainer
->removeByIndex(0);
474 //------------------------------------------------------------------
475 void SAL_CALL
SbaExternalSourceBrowser::disposing(const ::com::sun::star::lang::EventObject
& Source
) throw( RuntimeException
)
477 if (m_pDataSourceImpl
&& (m_pDataSourceImpl
->getAttachedForm() == Source
.Source
))
482 SbaXDataBrowserController::disposing(Source
);
485 //------------------------------------------------------------------
486 void SbaExternalSourceBrowser::startListening()
488 if (m_pDataSourceImpl
&& m_pDataSourceImpl
->getAttachedForm().is())
490 Reference
< ::com::sun::star::form::XLoadable
> xLoadable(m_pDataSourceImpl
->getAttachedForm(), UNO_QUERY
);
491 xLoadable
->addLoadListener((::com::sun::star::form::XLoadListener
*)this);
495 //------------------------------------------------------------------
496 void SbaExternalSourceBrowser::stopListening()
498 if (m_pDataSourceImpl
&& m_pDataSourceImpl
->getAttachedForm().is())
500 Reference
< ::com::sun::star::form::XLoadable
> xLoadable(m_pDataSourceImpl
->getAttachedForm(), UNO_QUERY
);
501 xLoadable
->removeLoadListener((::com::sun::star::form::XLoadListener
*)this);
505 //==================================================================
506 //==================================================================