1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "exsrcbrw.hxx"
22 #include <com/sun/star/form/FormComponentType.hpp>
23 #include <com/sun/star/util/XURLTransformer.hpp>
24 #include <com/sun/star/form/XGridColumnFactory.hpp>
25 #include <com/sun/star/form/XLoadable.hpp>
26 #include <com/sun/star/frame/FrameSearchFlag.hpp>
27 #include "formadapter.hxx"
28 #include <comphelper/processfactory.hxx>
29 #include "dbustrings.hrc"
30 #include "dbu_reghelper.hxx"
31 #include <tools/diagnose_ex.h>
32 #include <rtl/strbuf.hxx>
34 using namespace ::com::sun::star::uno
;
35 using namespace ::com::sun::star::sdb
;
36 using namespace ::com::sun::star::sdbc
;
37 using namespace ::com::sun::star::sdbcx
;
38 using namespace ::com::sun::star::beans
;
39 using namespace ::com::sun::star::container
;
40 using namespace ::com::sun::star::lang
;
41 using namespace ::com::sun::star::form
;
42 using namespace ::com::sun::star::frame
;
43 using namespace dbaui
;
45 //==============================================================================
46 //= SbaExternalSourceBrowser
47 //==============================================================================
48 extern "C" void SAL_CALL
createRegistryInfo_OFormGridView()
50 static OMultiInstanceAutoRegistration
< SbaExternalSourceBrowser
> aAutoRegistration
;
52 //------------------------------------------------------------------------------
53 Any SAL_CALL
SbaExternalSourceBrowser::queryInterface(const Type
& _rType
) throw (RuntimeException
)
55 Any aRet
= SbaXDataBrowserController::queryInterface(_rType
);
57 aRet
= ::cppu::queryInterface(_rType
,
58 (::com::sun::star::util::XModifyBroadcaster
*)this,
59 (::com::sun::star::form::XLoadListener
*)this);
63 DBG_NAME(SbaExternalSourceBrowser
)
64 //------------------------------------------------------------------------------
65 SbaExternalSourceBrowser::SbaExternalSourceBrowser(const Reference
< ::com::sun::star::uno::XComponentContext
>& _rM
)
66 :SbaXDataBrowserController(_rM
)
67 ,m_aModifyListeners(getMutex())
68 ,m_pDataSourceImpl(NULL
)
69 ,m_bInQueryDispatch( sal_False
)
71 DBG_CTOR(SbaExternalSourceBrowser
,NULL
);
75 //------------------------------------------------------------------------------
76 SbaExternalSourceBrowser::~SbaExternalSourceBrowser()
79 DBG_DTOR(SbaExternalSourceBrowser
,NULL
);
82 //-------------------------------------------------------------------------
83 ::comphelper::StringSequence SAL_CALL
SbaExternalSourceBrowser::getSupportedServiceNames() throw(RuntimeException
)
85 return getSupportedServiceNames_Static();
87 // -------------------------------------------------------------------------
88 OUString
SbaExternalSourceBrowser::getImplementationName_Static() throw(RuntimeException
)
90 return OUString("org.openoffice.comp.dbu.OFormGridView");
92 //-------------------------------------------------------------------------
93 ::comphelper::StringSequence
SbaExternalSourceBrowser::getSupportedServiceNames_Static() throw(RuntimeException
)
95 ::comphelper::StringSequence
aSupported(1);
96 aSupported
.getArray()[0] = OUString("com.sun.star.sdb.FormGridView");
99 //-------------------------------------------------------------------------
100 Reference
< XInterface
> SAL_CALL
SbaExternalSourceBrowser::Create(const Reference
<XMultiServiceFactory
>& _rxFactory
)
102 return *(new SbaExternalSourceBrowser( comphelper::getComponentContext(_rxFactory
)));
104 //-------------------------------------------------------------------------
105 OUString SAL_CALL
SbaExternalSourceBrowser::getImplementationName() throw(RuntimeException
)
107 return getImplementationName_Static();
109 //------------------------------------------------------------------------------
110 Reference
< XRowSet
> SbaExternalSourceBrowser::CreateForm()
112 m_pDataSourceImpl
= new SbaXFormAdapter();
113 return m_pDataSourceImpl
;
116 //------------------------------------------------------------------------------
117 sal_Bool
SbaExternalSourceBrowser::InitializeForm(const Reference
< XPropertySet
> & /*i_formProperties*/)
122 //------------------------------------------------------------------
123 sal_Bool
SbaExternalSourceBrowser::LoadForm()
125 // as we don't have a main form (yet), we have nothing to do
126 // we don't call FormLoaded, because this expects a working data source
131 //------------------------------------------------------------------
132 void SbaExternalSourceBrowser::modified(const ::com::sun::star::lang::EventObject
& aEvent
) throw( RuntimeException
)
134 SbaXDataBrowserController::modified(aEvent
);
136 // multiplex this event to all my listeners
137 ::com::sun::star::lang::EventObject
aEvt(*this);
138 ::cppu::OInterfaceIteratorHelper
aIt(m_aModifyListeners
);
139 while (aIt
.hasMoreElements())
140 ((::com::sun::star::util::XModifyListener
*)aIt
.next())->modified(aEvt
);
143 //------------------------------------------------------------------
144 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
)
146 const ::com::sun::star::beans::PropertyValue
* pArguments
= aArgs
.getConstArray();
147 if ( aURL
.Complete
== ".uno:FormSlots/AddGridColumn" )
149 // search the argument describing the column to create
150 OUString sControlType
;
151 sal_Int32 nControlPos
= -1;
152 Sequence
< ::com::sun::star::beans::PropertyValue
> aControlProps
;
154 for ( i
= 0; i
< aArgs
.getLength(); ++i
, ++pArguments
)
156 if ( pArguments
->Name
== "ColumnType" )
158 sal_Bool bCorrectType
= pArguments
->Value
.getValueType().equals(::getCppuType((const OUString
*)0));
159 OSL_ENSURE(bCorrectType
, "invalid type for argument \"ColumnType\" !");
161 sControlType
= ::comphelper::getString(pArguments
->Value
);
163 else if ( pArguments
->Name
== "ColumnPosition" )
165 sal_Bool bCorrectType
= pArguments
->Value
.getValueType().equals(::getCppuType((const sal_Int16
*)0));
166 OSL_ENSURE(bCorrectType
, "invalid type for argument \"ColumnPosition\" !");
168 nControlPos
= ::comphelper::getINT16(pArguments
->Value
);
170 else if ( pArguments
->Name
== "ColumnProperties" )
172 sal_Bool bCorrectType
= pArguments
->Value
.getValueType().equals(::getCppuType((const Sequence
< ::com::sun::star::beans::PropertyValue
>*)0));
173 OSL_ENSURE(bCorrectType
, "invalid type for argument \"ColumnProperties\" !");
175 aControlProps
= *(Sequence
< ::com::sun::star::beans::PropertyValue
>*)pArguments
->Value
.getValue();
178 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(AddGridColumn) : unknown argument (" << pArguments
->Name
<< ") !");
180 if (sControlType
.isEmpty())
182 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnType) !");
183 sControlType
= OUString("TextField");
185 OSL_ENSURE(aControlProps
.getLength(), "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnProperties) !");
188 Reference
< ::com::sun::star::form::XGridColumnFactory
> xColFactory(getControlModel(), UNO_QUERY
);
189 Reference
< ::com::sun::star::beans::XPropertySet
> xNewCol
= xColFactory
->createColumn(sControlType
);
190 Reference
< XPropertySetInfo
> xNewColProperties
;
192 xNewColProperties
= xNewCol
->getPropertySetInfo();
193 // set its properties
194 if (xNewColProperties
.is())
196 const ::com::sun::star::beans::PropertyValue
* pControlProps
= aControlProps
.getConstArray();
197 for (i
=0; i
<aControlProps
.getLength(); ++i
, ++pControlProps
)
201 if (xNewColProperties
->hasPropertyByName(pControlProps
->Name
))
202 xNewCol
->setPropertyValue(pControlProps
->Name
, pControlProps
->Value
);
204 catch (const Exception
&)
206 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch : could not set a column property (" << pControlProps
->Name
<< ")!");
211 // correct the position
212 Reference
< ::com::sun::star::container::XIndexContainer
> xColContainer(getControlModel(), UNO_QUERY
);
214 if (nControlPos
> xColContainer
->getCount())
215 nControlPos
= xColContainer
->getCount();
220 xColContainer
->insertByIndex(nControlPos
, makeAny(xNewCol
));
222 else if ( aURL
.Complete
== ".uno:FormSlots/ClearView" )
226 else if ( aURL
.Complete
== ".uno:FormSlots/AttachToForm" )
228 if (!m_pDataSourceImpl
)
231 Reference
< XRowSet
> xMasterForm
;
232 // search the arguments for the master form
233 for (sal_uInt16 i
=0; i
<aArgs
.getLength(); ++i
, ++pArguments
)
235 if ( (pArguments
->Name
== "MasterForm") && (pArguments
->Value
.getValueTypeClass() == TypeClass_INTERFACE
) )
237 xMasterForm
= Reference
< XRowSet
> (*(Reference
< XInterface
> *)pArguments
->Value
.getValue(), UNO_QUERY
);
241 if (!xMasterForm
.is())
243 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(FormSlots/AttachToForm) : please specify a form to attach to as argument !");
250 SbaXDataBrowserController::dispatch(aURL
, aArgs
);
253 //------------------------------------------------------------------
254 Reference
< ::com::sun::star::frame::XDispatch
> SAL_CALL
SbaExternalSourceBrowser::queryDispatch(const ::com::sun::star::util::URL
& aURL
, const OUString
& aTargetFrameName
, sal_Int32 nSearchFlags
) throw( RuntimeException
)
256 Reference
< ::com::sun::star::frame::XDispatch
> xReturn
;
257 if (m_bInQueryDispatch
)
260 m_bInQueryDispatch
= sal_True
;
262 if ( ( aURL
.Complete
== ".uno:FormSlots/AttachToForm" )
263 // attach a new external form
264 || ( aURL
.Complete
== ".uno:FormSlots/AddGridColumn" )
265 // add a column to the grid
266 || ( aURL
.Complete
== ".uno:FormSlots/ClearView" )
269 xReturn
= (::com::sun::star::frame::XDispatch
*)this;
272 && ( (aURL
.Complete
== ".uno:FormSlots/moveToFirst" ) || (aURL
.Complete
== ".uno:FormSlots/moveToPrev" )
273 || (aURL
.Complete
== ".uno:FormSlots/moveToNext" ) || (aURL
.Complete
== ".uno:FormSlots/moveToLast" )
274 || (aURL
.Complete
== ".uno:FormSlots/moveToNew" ) || (aURL
.Complete
== ".uno:FormSlots/undoRecord" )
278 OSL_ENSURE(aURL
.Mark
.isEmpty(), "SbaExternalSourceBrowser::queryDispatch : the ::com::sun::star::util::URL shouldn't have a mark !");
279 ::com::sun::star::util::URL aNewUrl
= aURL
;
281 // split the ::com::sun::star::util::URL
282 OSL_ENSURE( m_xUrlTransformer
.is(), "SbaExternalSourceBrowser::queryDispatch : could not create an URLTransformer !" );
283 if ( m_xUrlTransformer
.is() )
284 m_xUrlTransformer
->parseStrict( aNewUrl
);
287 aNewUrl
.Mark
= OUString("DB/FormGridView");
288 // this controller is instantiated when somebody dispatches the ".component:DB/FormGridView" in any
289 // frame, so we use "FormGridView" as mark that a dispatch request came from this view
291 if (m_xUrlTransformer
.is())
292 m_xUrlTransformer
->assemble(aNewUrl
);
294 Reference
< XDispatchProvider
> xFrameDispatcher( getFrame(), UNO_QUERY
);
295 if (xFrameDispatcher
.is())
296 xReturn
= xFrameDispatcher
->queryDispatch(aNewUrl
, aTargetFrameName
, FrameSearchFlag::PARENT
);
301 xReturn
= SbaXDataBrowserController::queryDispatch(aURL
, aTargetFrameName
, nSearchFlags
);
303 m_bInQueryDispatch
= sal_False
;
307 //------------------------------------------------------------------
308 void SAL_CALL
SbaExternalSourceBrowser::disposing()
310 // say our modify listeners goodbye
311 ::com::sun::star::lang::EventObject aEvt
;
312 aEvt
.Source
= (XWeak
*) this;
313 m_aModifyListeners
.disposeAndClear(aEvt
);
317 SbaXDataBrowserController::disposing();
320 //------------------------------------------------------------------
321 void SAL_CALL
SbaExternalSourceBrowser::addModifyListener(const Reference
< ::com::sun::star::util::XModifyListener
> & aListener
) throw( RuntimeException
)
323 m_aModifyListeners
.addInterface(aListener
);
326 //------------------------------------------------------------------
327 void SAL_CALL
SbaExternalSourceBrowser::removeModifyListener(const Reference
< ::com::sun::star::util::XModifyListener
> & aListener
) throw( RuntimeException
)
329 m_aModifyListeners
.removeInterface(aListener
);
332 //------------------------------------------------------------------
333 void SAL_CALL
SbaExternalSourceBrowser::unloading(const ::com::sun::star::lang::EventObject
& aEvent
) throw( RuntimeException
)
335 if (m_pDataSourceImpl
&& (m_pDataSourceImpl
->getAttachedForm() == aEvent
.Source
))
340 SbaXDataBrowserController::unloading(aEvent
);
343 //------------------------------------------------------------------
344 void SbaExternalSourceBrowser::Attach(const Reference
< XRowSet
> & xMaster
)
347 sal_Bool bWasInsertRow
= sal_False
;
348 sal_Bool bBeforeFirst
= sal_True
;
349 sal_Bool bAfterLast
= sal_True
;
350 Reference
< XResultSet
> xResultSet(xMaster
, UNO_QUERY
);
351 Reference
< XRowLocate
> xCursor(xMaster
, UNO_QUERY
);
352 Reference
< XPropertySet
> xMasterProps(xMaster
, UNO_QUERY
);
356 // switch the control to design mode
357 if (getBrowserView() && getBrowserView()->getGridControl().is())
358 getBrowserView()->getGridControl()->setDesignMode(sal_True
);
360 // the grid will move the form's cursor to the first record, but we want the form to remain unchanged
361 // restore the old position
362 if (xCursor
.is() && xResultSet
.is())
364 bBeforeFirst
= xResultSet
->isBeforeFirst();
365 bAfterLast
= xResultSet
->isAfterLast();
366 if(!bBeforeFirst
&& !bAfterLast
)
367 aOldPos
= xCursor
->getBookmark();
370 if (xMasterProps
.is())
371 xMasterProps
->getPropertyValue(PROPERTY_ISNEW
) >>= bWasInsertRow
;
373 catch( const Exception
& )
375 DBG_UNHANDLED_EXCEPTION();
378 onStartLoading( Reference
< XLoadable
>( xMaster
, UNO_QUERY
) );
381 m_pDataSourceImpl
->AttachForm(xMaster
);
386 // at this point we have to reset the formatter for the new form
388 // assume that the master form is already loaded
389 #if OSL_DEBUG_LEVEL > 0
391 Reference
< XLoadable
> xLoadable( xMaster
, UNO_QUERY
);
392 OSL_ENSURE( xLoadable
.is() && xLoadable
->isLoaded(), "SbaExternalSourceBrowser::Attach: master is not loaded!" );
396 LoadFinished(sal_True
);
398 Reference
< XResultSetUpdate
> xUpdate(xMaster
, UNO_QUERY
);
401 if (bWasInsertRow
&& xUpdate
.is())
402 xUpdate
->moveToInsertRow();
403 else if (xCursor
.is() && aOldPos
.hasValue())
404 xCursor
->moveToBookmark(aOldPos
);
405 else if(bBeforeFirst
&& xResultSet
.is())
406 xResultSet
->beforeFirst();
407 else if(bAfterLast
&& xResultSet
.is())
408 xResultSet
->afterLast();
412 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::Attach : couldn't restore the cursor position !");
418 //------------------------------------------------------------------
419 void SbaExternalSourceBrowser::ClearView()
421 // set a new (empty) datasource
422 Attach(Reference
< XRowSet
> ());
425 // clear all cols in the grid
426 Reference
< ::com::sun::star::container::XIndexContainer
> xColContainer(getControlModel(), UNO_QUERY
);
427 while (xColContainer
->getCount() > 0)
428 xColContainer
->removeByIndex(0);
431 //------------------------------------------------------------------
432 void SAL_CALL
SbaExternalSourceBrowser::disposing(const ::com::sun::star::lang::EventObject
& Source
) throw( RuntimeException
)
434 if (m_pDataSourceImpl
&& (m_pDataSourceImpl
->getAttachedForm() == Source
.Source
))
439 SbaXDataBrowserController::disposing(Source
);
442 //------------------------------------------------------------------
443 void SbaExternalSourceBrowser::startListening()
445 if (m_pDataSourceImpl
&& m_pDataSourceImpl
->getAttachedForm().is())
447 Reference
< ::com::sun::star::form::XLoadable
> xLoadable(m_pDataSourceImpl
->getAttachedForm(), UNO_QUERY
);
448 xLoadable
->addLoadListener((::com::sun::star::form::XLoadListener
*)this);
452 //------------------------------------------------------------------
453 void SbaExternalSourceBrowser::stopListening()
455 if (m_pDataSourceImpl
&& m_pDataSourceImpl
->getAttachedForm().is())
457 Reference
< ::com::sun::star::form::XLoadable
> xLoadable(m_pDataSourceImpl
->getAttachedForm(), UNO_QUERY
);
458 xLoadable
->removeLoadListener((::com::sun::star::form::XLoadListener
*)this);
462 //==================================================================
463 //==================================================================
465 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */