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 .
20 #include "exsrcbrw.hxx"
21 #include "uiservices.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 // SbaExternalSourceBrowser
46 extern "C" void SAL_CALL
createRegistryInfo_OFormGridView()
48 static OMultiInstanceAutoRegistration
< SbaExternalSourceBrowser
> aAutoRegistration
;
51 Any SAL_CALL
SbaExternalSourceBrowser::queryInterface(const Type
& _rType
) throw (RuntimeException
, std::exception
)
53 Any aRet
= SbaXDataBrowserController::queryInterface(_rType
);
55 aRet
= ::cppu::queryInterface(_rType
,
56 (::com::sun::star::util::XModifyBroadcaster
*)this,
57 (::com::sun::star::form::XLoadListener
*)this);
62 SbaExternalSourceBrowser::SbaExternalSourceBrowser(const Reference
< ::com::sun::star::uno::XComponentContext
>& _rM
)
63 :SbaXDataBrowserController(_rM
)
64 ,m_aModifyListeners(getMutex())
65 ,m_pDataSourceImpl(NULL
)
66 ,m_bInQueryDispatch( false )
71 SbaExternalSourceBrowser::~SbaExternalSourceBrowser()
76 ::comphelper::StringSequence SAL_CALL
SbaExternalSourceBrowser::getSupportedServiceNames() throw(RuntimeException
, std::exception
)
78 return getSupportedServiceNames_Static();
81 OUString
SbaExternalSourceBrowser::getImplementationName_Static() throw(RuntimeException
)
83 return OUString("org.openoffice.comp.dbu.OFormGridView");
86 ::comphelper::StringSequence
SbaExternalSourceBrowser::getSupportedServiceNames_Static() throw(RuntimeException
)
88 ::comphelper::StringSequence
aSupported(1);
89 aSupported
[0] = "com.sun.star.sdb.FormGridView";
93 Reference
< XInterface
> SAL_CALL
SbaExternalSourceBrowser::Create(const Reference
<XMultiServiceFactory
>& _rxFactory
)
95 return *(new SbaExternalSourceBrowser( comphelper::getComponentContext(_rxFactory
)));
98 OUString SAL_CALL
SbaExternalSourceBrowser::getImplementationName() throw(RuntimeException
, std::exception
)
100 return getImplementationName_Static();
103 Reference
< XRowSet
> SbaExternalSourceBrowser::CreateForm()
105 m_pDataSourceImpl
= new SbaXFormAdapter();
106 return m_pDataSourceImpl
;
109 bool SbaExternalSourceBrowser::InitializeForm(const Reference
< XPropertySet
> & /*i_formProperties*/)
114 bool SbaExternalSourceBrowser::LoadForm()
116 // as we don't have a main form (yet), we have nothing to do
117 // we don't call FormLoaded, because this expects a working data source
121 void SbaExternalSourceBrowser::modified(const ::com::sun::star::lang::EventObject
& aEvent
) throw( RuntimeException
, std::exception
)
123 SbaXDataBrowserController::modified(aEvent
);
125 // multiplex this event to all my listeners
126 ::com::sun::star::lang::EventObject
aEvt(*this);
127 ::cppu::OInterfaceIteratorHelper
aIt(m_aModifyListeners
);
128 while (aIt
.hasMoreElements())
129 static_cast< ::com::sun::star::util::XModifyListener
*>(aIt
.next())->modified(aEvt
);
132 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
, std::exception
)
134 const ::com::sun::star::beans::PropertyValue
* pArguments
= aArgs
.getConstArray();
135 if ( aURL
.Complete
== ".uno:FormSlots/AddGridColumn" )
137 // search the argument describing the column to create
138 OUString sControlType
;
139 sal_Int32 nControlPos
= -1;
140 Sequence
< ::com::sun::star::beans::PropertyValue
> aControlProps
;
142 for ( i
= 0; i
< aArgs
.getLength(); ++i
, ++pArguments
)
144 if ( pArguments
->Name
== "ColumnType" )
146 bool bCorrectType
= pArguments
->Value
.getValueType().equals(::cppu::UnoType
<OUString
>::get());
147 OSL_ENSURE(bCorrectType
, "invalid type for argument \"ColumnType\" !");
149 sControlType
= ::comphelper::getString(pArguments
->Value
);
151 else if ( pArguments
->Name
== "ColumnPosition" )
153 bool bCorrectType
= pArguments
->Value
.getValueType().equals(::cppu::UnoType
<sal_Int16
>::get());
154 OSL_ENSURE(bCorrectType
, "invalid type for argument \"ColumnPosition\" !");
156 nControlPos
= ::comphelper::getINT16(pArguments
->Value
);
158 else if ( pArguments
->Name
== "ColumnProperties" )
160 bool bCorrectType
= pArguments
->Value
.getValueType().equals(cppu::UnoType
<Sequence
< ::com::sun::star::beans::PropertyValue
>>::get());
161 OSL_ENSURE(bCorrectType
, "invalid type for argument \"ColumnProperties\" !");
163 aControlProps
= *static_cast<Sequence
< ::com::sun::star::beans::PropertyValue
> const *>(pArguments
->Value
.getValue());
166 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(AddGridColumn) : unknown argument (" << pArguments
->Name
<< ") !");
168 if (sControlType
.isEmpty())
170 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnType) !");
171 sControlType
= "TextField";
173 OSL_ENSURE(aControlProps
.getLength(), "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnProperties) !");
176 Reference
< ::com::sun::star::form::XGridColumnFactory
> xColFactory(getControlModel(), UNO_QUERY
);
177 Reference
< ::com::sun::star::beans::XPropertySet
> xNewCol
= xColFactory
->createColumn(sControlType
);
178 Reference
< XPropertySetInfo
> xNewColProperties
;
180 xNewColProperties
= xNewCol
->getPropertySetInfo();
181 // set its properties
182 if (xNewColProperties
.is())
184 const ::com::sun::star::beans::PropertyValue
* pControlProps
= aControlProps
.getConstArray();
185 for (i
=0; i
<aControlProps
.getLength(); ++i
, ++pControlProps
)
189 if (xNewColProperties
->hasPropertyByName(pControlProps
->Name
))
190 xNewCol
->setPropertyValue(pControlProps
->Name
, pControlProps
->Value
);
192 catch (const Exception
&)
194 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch : could not set a column property (" << pControlProps
->Name
<< ")!");
199 // correct the position
200 Reference
< ::com::sun::star::container::XIndexContainer
> xColContainer(getControlModel(), UNO_QUERY
);
202 if (nControlPos
> xColContainer
->getCount())
203 nControlPos
= xColContainer
->getCount();
208 xColContainer
->insertByIndex(nControlPos
, makeAny(xNewCol
));
210 else if ( aURL
.Complete
== ".uno:FormSlots/ClearView" )
214 else if ( aURL
.Complete
== ".uno:FormSlots/AttachToForm" )
216 if (!m_pDataSourceImpl
)
219 Reference
< XRowSet
> xMasterForm
;
220 // search the arguments for the master form
221 for (sal_uInt16 i
=0; i
<aArgs
.getLength(); ++i
, ++pArguments
)
223 if ( (pArguments
->Name
== "MasterForm") && (pArguments
->Value
.getValueTypeClass() == TypeClass_INTERFACE
) )
225 xMasterForm
= Reference
< XRowSet
> (*static_cast<Reference
< XInterface
> const *>(pArguments
->Value
.getValue()), UNO_QUERY
);
229 if (!xMasterForm
.is())
231 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(FormSlots/AttachToForm) : please specify a form to attach to as argument !");
238 SbaXDataBrowserController::dispatch(aURL
, aArgs
);
241 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
, std::exception
)
243 Reference
< ::com::sun::star::frame::XDispatch
> xReturn
;
244 if (m_bInQueryDispatch
)
247 m_bInQueryDispatch
= true;
249 if ( ( aURL
.Complete
== ".uno:FormSlots/AttachToForm" )
250 // attach a new external form
251 || ( aURL
.Complete
== ".uno:FormSlots/AddGridColumn" )
252 // add a column to the grid
253 || ( aURL
.Complete
== ".uno:FormSlots/ClearView" )
256 xReturn
= (::com::sun::star::frame::XDispatch
*)this;
259 && ( (aURL
.Complete
== ".uno:FormSlots/moveToFirst" ) || (aURL
.Complete
== ".uno:FormSlots/moveToPrev" )
260 || (aURL
.Complete
== ".uno:FormSlots/moveToNext" ) || (aURL
.Complete
== ".uno:FormSlots/moveToLast" )
261 || (aURL
.Complete
== ".uno:FormSlots/moveToNew" ) || (aURL
.Complete
== ".uno:FormSlots/undoRecord" )
265 OSL_ENSURE(aURL
.Mark
.isEmpty(), "SbaExternalSourceBrowser::queryDispatch : the ::com::sun::star::util::URL shouldn't have a mark !");
266 ::com::sun::star::util::URL aNewUrl
= aURL
;
268 // split the ::com::sun::star::util::URL
269 OSL_ENSURE( m_xUrlTransformer
.is(), "SbaExternalSourceBrowser::queryDispatch : could not create an URLTransformer !" );
270 if ( m_xUrlTransformer
.is() )
271 m_xUrlTransformer
->parseStrict( aNewUrl
);
274 aNewUrl
.Mark
= "DB/FormGridView";
275 // this controller is instantiated when somebody dispatches the ".component:DB/FormGridView" in any
276 // frame, so we use "FormGridView" as mark that a dispatch request came from this view
278 if (m_xUrlTransformer
.is())
279 m_xUrlTransformer
->assemble(aNewUrl
);
281 Reference
< XDispatchProvider
> xFrameDispatcher( getFrame(), UNO_QUERY
);
282 if (xFrameDispatcher
.is())
283 xReturn
= xFrameDispatcher
->queryDispatch(aNewUrl
, aTargetFrameName
, FrameSearchFlag::PARENT
);
288 xReturn
= SbaXDataBrowserController::queryDispatch(aURL
, aTargetFrameName
, nSearchFlags
);
290 m_bInQueryDispatch
= false;
294 void SAL_CALL
SbaExternalSourceBrowser::disposing()
296 // say our modify listeners goodbye
297 ::com::sun::star::lang::EventObject aEvt
;
298 aEvt
.Source
= (XWeak
*) this;
299 m_aModifyListeners
.disposeAndClear(aEvt
);
303 SbaXDataBrowserController::disposing();
306 void SAL_CALL
SbaExternalSourceBrowser::addModifyListener(const Reference
< ::com::sun::star::util::XModifyListener
> & aListener
) throw( RuntimeException
, std::exception
)
308 m_aModifyListeners
.addInterface(aListener
);
311 void SAL_CALL
SbaExternalSourceBrowser::removeModifyListener(const Reference
< ::com::sun::star::util::XModifyListener
> & aListener
) throw( RuntimeException
, std::exception
)
313 m_aModifyListeners
.removeInterface(aListener
);
316 void SAL_CALL
SbaExternalSourceBrowser::unloading(const ::com::sun::star::lang::EventObject
& aEvent
) throw( RuntimeException
, std::exception
)
318 if (m_pDataSourceImpl
&& (m_pDataSourceImpl
->getAttachedForm() == aEvent
.Source
))
323 SbaXDataBrowserController::unloading(aEvent
);
326 void SbaExternalSourceBrowser::Attach(const Reference
< XRowSet
> & xMaster
)
329 bool bWasInsertRow
= false;
330 bool bBeforeFirst
= true;
331 bool bAfterLast
= true;
332 Reference
< XRowLocate
> xCursor(xMaster
, UNO_QUERY
);
333 Reference
< XPropertySet
> xMasterProps(xMaster
, UNO_QUERY
);
337 // switch the control to design mode
338 if (getBrowserView() && getBrowserView()->getGridControl().is())
339 getBrowserView()->getGridControl()->setDesignMode(sal_True
);
341 // the grid will move the form's cursor to the first record, but we want the form to remain unchanged
342 // restore the old position
343 if (xCursor
.is() && xMaster
.is())
345 bBeforeFirst
= xMaster
->isBeforeFirst();
346 bAfterLast
= xMaster
->isAfterLast();
347 if(!bBeforeFirst
&& !bAfterLast
)
348 aOldPos
= xCursor
->getBookmark();
351 if (xMasterProps
.is())
352 xMasterProps
->getPropertyValue(PROPERTY_ISNEW
) >>= bWasInsertRow
;
354 catch( const Exception
& )
356 DBG_UNHANDLED_EXCEPTION();
359 onStartLoading( Reference
< XLoadable
>( xMaster
, UNO_QUERY
) );
362 m_pDataSourceImpl
->AttachForm(xMaster
);
367 // at this point we have to reset the formatter for the new form
369 // assume that the master form is already loaded
370 #if OSL_DEBUG_LEVEL > 0
372 Reference
< XLoadable
> xLoadable( xMaster
, UNO_QUERY
);
373 OSL_ENSURE( xLoadable
.is() && xLoadable
->isLoaded(), "SbaExternalSourceBrowser::Attach: master is not loaded!" );
379 Reference
< XResultSetUpdate
> xUpdate(xMaster
, UNO_QUERY
);
382 if (bWasInsertRow
&& xUpdate
.is())
383 xUpdate
->moveToInsertRow();
384 else if (xCursor
.is() && aOldPos
.hasValue())
385 xCursor
->moveToBookmark(aOldPos
);
386 else if(bBeforeFirst
&& xMaster
.is())
387 xMaster
->beforeFirst();
388 else if(bAfterLast
&& xMaster
.is())
389 xMaster
->afterLast();
393 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::Attach : couldn't restore the cursor position !");
399 void SbaExternalSourceBrowser::ClearView()
401 // set a new (empty) datasource
402 Attach(Reference
< XRowSet
> ());
404 // clear all cols in the grid
405 Reference
< ::com::sun::star::container::XIndexContainer
> xColContainer(getControlModel(), UNO_QUERY
);
406 while (xColContainer
->getCount() > 0)
407 xColContainer
->removeByIndex(0);
410 void SAL_CALL
SbaExternalSourceBrowser::disposing(const ::com::sun::star::lang::EventObject
& Source
) throw( RuntimeException
, std::exception
)
412 if (m_pDataSourceImpl
&& (m_pDataSourceImpl
->getAttachedForm() == Source
.Source
))
417 SbaXDataBrowserController::disposing(Source
);
420 void SbaExternalSourceBrowser::startListening()
422 if (m_pDataSourceImpl
&& m_pDataSourceImpl
->getAttachedForm().is())
424 Reference
< ::com::sun::star::form::XLoadable
> xLoadable(m_pDataSourceImpl
->getAttachedForm(), UNO_QUERY
);
425 xLoadable
->addLoadListener((::com::sun::star::form::XLoadListener
*)this);
429 void SbaExternalSourceBrowser::stopListening()
431 if (m_pDataSourceImpl
&& m_pDataSourceImpl
->getAttachedForm().is())
433 Reference
< ::com::sun::star::form::XLoadable
> xLoadable(m_pDataSourceImpl
->getAttachedForm(), UNO_QUERY
);
434 xLoadable
->removeLoadListener((::com::sun::star::form::XLoadListener
*)this);
438 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */