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 <com/sun/star/util/XURLTransformer.hpp>
22 #include <com/sun/star/form/XGridColumnFactory.hpp>
23 #include <com/sun/star/form/XLoadable.hpp>
24 #include <com/sun/star/frame/FrameSearchFlag.hpp>
25 #include <formadapter.hxx>
26 #include <strings.hxx>
27 #include <o3tl/any.hxx>
28 #include <comphelper/diagnose_ex.hxx>
29 #include <sal/log.hxx>
31 using namespace ::com::sun::star::uno
;
32 using namespace ::com::sun::star::sdb
;
33 using namespace ::com::sun::star::sdbc
;
34 using namespace ::com::sun::star::sdbcx
;
35 using namespace ::com::sun::star::beans
;
36 using namespace ::com::sun::star::container
;
37 using namespace ::com::sun::star::lang
;
38 using namespace ::com::sun::star::form
;
39 using namespace ::com::sun::star::frame
;
40 using namespace dbaui
;
42 // SbaExternalSourceBrowser
43 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
44 org_openoffice_comp_dbu_OFormGridView_get_implementation(
45 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const& )
47 return cppu::acquire(new SbaExternalSourceBrowser(context
));
50 Any SAL_CALL
SbaExternalSourceBrowser::queryInterface(const Type
& _rType
)
52 Any aRet
= SbaXDataBrowserController::queryInterface(_rType
);
54 aRet
= ::cppu::queryInterface(_rType
,
55 static_cast<css::util::XModifyBroadcaster
*>(this),
56 static_cast<css::form::XLoadListener
*>(this));
61 SbaExternalSourceBrowser::SbaExternalSourceBrowser(const Reference
< css::uno::XComponentContext
>& _rM
)
62 :SbaXDataBrowserController(_rM
)
63 ,m_aModifyListeners(getMutex())
64 ,m_bInQueryDispatch( false )
69 SbaExternalSourceBrowser::~SbaExternalSourceBrowser()
73 css::uno::Sequence
<OUString
> SAL_CALL
SbaExternalSourceBrowser::getSupportedServiceNames()
75 return { "com.sun.star.sdb.FormGridView" };
78 OUString SAL_CALL
SbaExternalSourceBrowser::getImplementationName()
80 return "org.openoffice.comp.dbu.OFormGridView";
83 Reference
< XRowSet
> SbaExternalSourceBrowser::CreateForm()
85 m_pDataSourceImpl
= new SbaXFormAdapter();
86 return m_pDataSourceImpl
;
89 bool SbaExternalSourceBrowser::InitializeForm(const Reference
< XPropertySet
> & /*i_formProperties*/)
94 bool SbaExternalSourceBrowser::LoadForm()
96 // as we don't have a main form (yet), we have nothing to do
97 // we don't call FormLoaded, because this expects a working data source
101 void SbaExternalSourceBrowser::modified(const css::lang::EventObject
& aEvent
)
103 SbaXDataBrowserController::modified(aEvent
);
105 // multiplex this event to all my listeners
106 css::lang::EventObject
aEvt(*this);
107 m_aModifyListeners
.notifyEach( &css::util::XModifyListener::modified
, aEvt
);
110 void SAL_CALL
SbaExternalSourceBrowser::dispatch(const css::util::URL
& aURL
, const Sequence
< css::beans::PropertyValue
>& aArgs
)
112 if ( aURL
.Complete
== ".uno:FormSlots/AddGridColumn" )
114 // search the argument describing the column to create
115 OUString sControlType
;
116 sal_Int32 nControlPos
= -1;
117 Sequence
< css::beans::PropertyValue
> aControlProps
;
118 for ( const css::beans::PropertyValue
& rArgument
: aArgs
)
120 if ( rArgument
.Name
== "ColumnType" )
122 auto s
= o3tl::tryAccess
<OUString
>(rArgument
.Value
);
123 OSL_ENSURE(s
, "invalid type for argument \"ColumnType\" !");
127 else if ( rArgument
.Name
== "ColumnPosition" )
129 auto n
= o3tl::tryAccess
<sal_Int16
>(rArgument
.Value
);
130 OSL_ENSURE(n
, "invalid type for argument \"ColumnPosition\" !");
134 else if ( rArgument
.Name
== "ColumnProperties" )
136 auto s
= o3tl::tryAccess
<Sequence
<css::beans::PropertyValue
>>(
138 OSL_ENSURE(s
, "invalid type for argument \"ColumnProperties\" !");
143 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(AddGridColumn) : unknown argument (" << rArgument
.Name
<< ") !");
145 if (sControlType
.isEmpty())
147 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnType) !");
148 sControlType
= "TextField";
150 OSL_ENSURE(aControlProps
.hasElements(), "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnProperties) !");
153 Reference
< css::form::XGridColumnFactory
> xColFactory(getControlModel(), UNO_QUERY
);
154 Reference
< css::beans::XPropertySet
> xNewCol
= xColFactory
->createColumn(sControlType
);
155 Reference
< XPropertySetInfo
> xNewColProperties
;
157 xNewColProperties
= xNewCol
->getPropertySetInfo();
158 // set its properties
159 if (xNewColProperties
.is())
161 for (const css::beans::PropertyValue
& rControlProp
: std::as_const(aControlProps
))
165 if (xNewColProperties
->hasPropertyByName(rControlProp
.Name
))
166 xNewCol
->setPropertyValue(rControlProp
.Name
, rControlProp
.Value
);
168 catch (const Exception
&)
170 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch : could not set a column property (" << rControlProp
.Name
<< ")!");
175 // correct the position
176 Reference
< css::container::XIndexContainer
> xColContainer(getControlModel(), UNO_QUERY
);
178 if (nControlPos
> xColContainer
->getCount())
179 nControlPos
= xColContainer
->getCount();
184 xColContainer
->insertByIndex(nControlPos
, Any(xNewCol
));
186 else if ( aURL
.Complete
== ".uno:FormSlots/ClearView" )
190 else if ( aURL
.Complete
== ".uno:FormSlots/AttachToForm" )
192 if (!m_pDataSourceImpl
)
195 Reference
< XRowSet
> xMasterForm
;
196 // search the arguments for the master form
197 for (const css::beans::PropertyValue
& rArgument
: aArgs
)
199 if ( (rArgument
.Name
== "MasterForm") && (rArgument
.Value
.getValueTypeClass() == TypeClass_INTERFACE
) )
201 xMasterForm
.set(rArgument
.Value
, UNO_QUERY
);
205 if (!xMasterForm
.is())
207 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::dispatch(FormSlots/AttachToForm) : please specify a form to attach to as argument !");
214 SbaXDataBrowserController::dispatch(aURL
, aArgs
);
217 Reference
< css::frame::XDispatch
> SAL_CALL
SbaExternalSourceBrowser::queryDispatch(const css::util::URL
& aURL
, const OUString
& aTargetFrameName
, sal_Int32 nSearchFlags
)
219 Reference
< css::frame::XDispatch
> xReturn
;
220 if (m_bInQueryDispatch
)
223 m_bInQueryDispatch
= true;
225 if ( ( aURL
.Complete
== ".uno:FormSlots/AttachToForm" )
226 // attach a new external form
227 || ( aURL
.Complete
== ".uno:FormSlots/AddGridColumn" )
228 // add a column to the grid
229 || ( aURL
.Complete
== ".uno:FormSlots/ClearView" )
232 xReturn
= static_cast<css::frame::XDispatch
*>(this);
235 && ( (aURL
.Complete
== ".uno:FormSlots/moveToFirst" ) || (aURL
.Complete
== ".uno:FormSlots/moveToPrev" )
236 || (aURL
.Complete
== ".uno:FormSlots/moveToNext" ) || (aURL
.Complete
== ".uno:FormSlots/moveToLast" )
237 || (aURL
.Complete
== ".uno:FormSlots/moveToNew" ) || (aURL
.Complete
== ".uno:FormSlots/undoRecord" )
241 OSL_ENSURE(aURL
.Mark
.isEmpty(), "SbaExternalSourceBrowser::queryDispatch : the css::util::URL shouldn't have a mark !");
242 css::util::URL aNewUrl
= aURL
;
244 // split the css::util::URL
245 OSL_ENSURE( m_xUrlTransformer
.is(), "SbaExternalSourceBrowser::queryDispatch : could not create a URLTransformer !" );
246 if ( m_xUrlTransformer
.is() )
247 m_xUrlTransformer
->parseStrict( aNewUrl
);
250 aNewUrl
.Mark
= "DB/FormGridView";
251 // this controller is instantiated when somebody dispatches the ".component:DB/FormGridView" in any
252 // frame, so we use "FormGridView" as mark that a dispatch request came from this view
254 if (m_xUrlTransformer
.is())
255 m_xUrlTransformer
->assemble(aNewUrl
);
257 Reference
< XDispatchProvider
> xFrameDispatcher( getFrame(), UNO_QUERY
);
258 if (xFrameDispatcher
.is())
259 xReturn
= xFrameDispatcher
->queryDispatch(aNewUrl
, aTargetFrameName
, FrameSearchFlag::PARENT
);
264 xReturn
= SbaXDataBrowserController::queryDispatch(aURL
, aTargetFrameName
, nSearchFlags
);
266 m_bInQueryDispatch
= false;
270 void SAL_CALL
SbaExternalSourceBrowser::disposing()
272 // say our modify listeners goodbye
273 css::lang::EventObject aEvt
;
274 aEvt
.Source
= static_cast<XWeak
*>(this);
275 m_aModifyListeners
.disposeAndClear(aEvt
);
279 SbaXDataBrowserController::disposing();
282 void SAL_CALL
SbaExternalSourceBrowser::addModifyListener(const Reference
< css::util::XModifyListener
> & aListener
)
284 m_aModifyListeners
.addInterface(aListener
);
287 void SAL_CALL
SbaExternalSourceBrowser::removeModifyListener(const Reference
< css::util::XModifyListener
> & aListener
)
289 m_aModifyListeners
.removeInterface(aListener
);
292 void SAL_CALL
SbaExternalSourceBrowser::unloading(const css::lang::EventObject
& aEvent
)
294 if (m_pDataSourceImpl
&& (m_pDataSourceImpl
->getAttachedForm() == aEvent
.Source
))
299 SbaXDataBrowserController::unloading(aEvent
);
302 void SbaExternalSourceBrowser::Attach(const Reference
< XRowSet
> & xMaster
)
305 bool bWasInsertRow
= false;
306 bool bBeforeFirst
= true;
307 bool bAfterLast
= true;
308 Reference
< XRowLocate
> xCursor(xMaster
, UNO_QUERY
);
309 Reference
< XPropertySet
> xMasterProps(xMaster
, UNO_QUERY
);
313 // switch the control to design mode
314 if (getBrowserView() && getBrowserView()->getGridControl().is())
315 getBrowserView()->getGridControl()->setDesignMode(true);
317 // the grid will move the form's cursor to the first record, but we want the form to remain unchanged
318 // restore the old position
319 if (xCursor
.is() && xMaster
.is())
321 bBeforeFirst
= xMaster
->isBeforeFirst();
322 bAfterLast
= xMaster
->isAfterLast();
323 if(!bBeforeFirst
&& !bAfterLast
)
324 aOldPos
= xCursor
->getBookmark();
327 if (xMasterProps
.is())
328 xMasterProps
->getPropertyValue(PROPERTY_ISNEW
) >>= bWasInsertRow
;
330 catch( const Exception
& )
332 DBG_UNHANDLED_EXCEPTION("dbaccess");
335 onStartLoading( Reference
< XLoadable
>( xMaster
, UNO_QUERY
) );
338 m_pDataSourceImpl
->AttachForm(xMaster
);
344 // at this point we have to reset the formatter for the new form
346 // assume that the master form is already loaded
347 #if OSL_DEBUG_LEVEL > 0
349 Reference
< XLoadable
> xLoadable( xMaster
, UNO_QUERY
);
350 OSL_ENSURE( xLoadable
.is() && xLoadable
->isLoaded(), "SbaExternalSourceBrowser::Attach: master is not loaded!" );
356 Reference
< XResultSetUpdate
> xUpdate(xMaster
, UNO_QUERY
);
359 if (bWasInsertRow
&& xUpdate
.is())
360 xUpdate
->moveToInsertRow();
361 else if (xCursor
.is() && aOldPos
.hasValue())
362 xCursor
->moveToBookmark(aOldPos
);
363 else if(bBeforeFirst
&& xMaster
.is())
364 xMaster
->beforeFirst();
365 else if(bAfterLast
&& xMaster
.is())
366 xMaster
->afterLast();
370 SAL_WARN("dbaccess.ui", "SbaExternalSourceBrowser::Attach : couldn't restore the cursor position !");
374 void SbaExternalSourceBrowser::ClearView()
376 // set a new (empty) datasource
377 Attach(Reference
< XRowSet
> ());
379 // clear all cols in the grid
380 Reference
< css::container::XIndexContainer
> xColContainer(getControlModel(), UNO_QUERY
);
381 while (xColContainer
->getCount() > 0)
382 xColContainer
->removeByIndex(0);
385 void SAL_CALL
SbaExternalSourceBrowser::disposing(const css::lang::EventObject
& Source
)
387 if (m_pDataSourceImpl
&& (m_pDataSourceImpl
->getAttachedForm() == Source
.Source
))
392 SbaXDataBrowserController::disposing(Source
);
395 void SbaExternalSourceBrowser::startListening()
397 if (m_pDataSourceImpl
&& m_pDataSourceImpl
->getAttachedForm().is())
399 Reference
< css::form::XLoadable
> xLoadable(m_pDataSourceImpl
->getAttachedForm(), UNO_QUERY
);
400 xLoadable
->addLoadListener(static_cast<css::form::XLoadListener
*>(this));
404 void SbaExternalSourceBrowser::stopListening()
406 if (m_pDataSourceImpl
&& m_pDataSourceImpl
->getAttachedForm().is())
408 Reference
< css::form::XLoadable
> xLoadable(m_pDataSourceImpl
->getAttachedForm(), UNO_QUERY
);
409 xLoadable
->removeLoadListener(static_cast<css::form::XLoadListener
*>(this));
413 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */