Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
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>
33 :
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;
44 :
45 : //==============================================================================
46 : //= SbaExternalSourceBrowser
47 : //==============================================================================
48 0 : extern "C" void SAL_CALL createRegistryInfo_OFormGridView()
49 : {
50 0 : static OMultiInstanceAutoRegistration< SbaExternalSourceBrowser > aAutoRegistration;
51 0 : }
52 : //------------------------------------------------------------------------------
53 0 : Any SAL_CALL SbaExternalSourceBrowser::queryInterface(const Type& _rType) throw (RuntimeException)
54 : {
55 0 : Any aRet = SbaXDataBrowserController::queryInterface(_rType);
56 0 : if(!aRet.hasValue())
57 : aRet = ::cppu::queryInterface(_rType,
58 : (::com::sun::star::util::XModifyBroadcaster*)this,
59 0 : (::com::sun::star::form::XLoadListener*)this);
60 :
61 0 : return aRet;
62 : }
63 : DBG_NAME(SbaExternalSourceBrowser)
64 : //------------------------------------------------------------------------------
65 0 : SbaExternalSourceBrowser::SbaExternalSourceBrowser(const Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rM)
66 : :SbaXDataBrowserController(_rM)
67 0 : ,m_aModifyListeners(getMutex())
68 : ,m_pDataSourceImpl(NULL)
69 0 : ,m_bInQueryDispatch( sal_False )
70 : {
71 : DBG_CTOR(SbaExternalSourceBrowser,NULL);
72 :
73 0 : }
74 :
75 : //------------------------------------------------------------------------------
76 0 : SbaExternalSourceBrowser::~SbaExternalSourceBrowser()
77 : {
78 :
79 : DBG_DTOR(SbaExternalSourceBrowser,NULL);
80 0 : }
81 :
82 : //-------------------------------------------------------------------------
83 0 : ::comphelper::StringSequence SAL_CALL SbaExternalSourceBrowser::getSupportedServiceNames() throw(RuntimeException)
84 : {
85 0 : return getSupportedServiceNames_Static();
86 : }
87 : // -------------------------------------------------------------------------
88 0 : ::rtl::OUString SbaExternalSourceBrowser::getImplementationName_Static() throw(RuntimeException)
89 : {
90 0 : return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("org.openoffice.comp.dbu.OFormGridView"));
91 : }
92 : //-------------------------------------------------------------------------
93 0 : ::comphelper::StringSequence SbaExternalSourceBrowser::getSupportedServiceNames_Static() throw(RuntimeException)
94 : {
95 0 : ::comphelper::StringSequence aSupported(1);
96 0 : aSupported.getArray()[0] = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.sdb.FormGridView"));
97 0 : return aSupported;
98 : }
99 : //-------------------------------------------------------------------------
100 0 : Reference< XInterface > SAL_CALL SbaExternalSourceBrowser::Create(const Reference<XMultiServiceFactory >& _rxFactory)
101 : {
102 0 : return *(new SbaExternalSourceBrowser(_rxFactory));
103 : }
104 : //-------------------------------------------------------------------------
105 0 : ::rtl::OUString SAL_CALL SbaExternalSourceBrowser::getImplementationName() throw(RuntimeException)
106 : {
107 0 : return getImplementationName_Static();
108 : }
109 : //------------------------------------------------------------------------------
110 0 : Reference< XRowSet > SbaExternalSourceBrowser::CreateForm()
111 : {
112 0 : m_pDataSourceImpl = new SbaXFormAdapter();
113 0 : return m_pDataSourceImpl;
114 : }
115 :
116 : //------------------------------------------------------------------------------
117 0 : sal_Bool SbaExternalSourceBrowser::InitializeForm(const Reference< XPropertySet > & /*i_formProperties*/)
118 : {
119 0 : return sal_True;
120 : }
121 :
122 : //------------------------------------------------------------------
123 0 : sal_Bool SbaExternalSourceBrowser::LoadForm()
124 : {
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
127 0 : return sal_True;
128 : }
129 :
130 :
131 : //------------------------------------------------------------------
132 0 : void SbaExternalSourceBrowser::modified(const ::com::sun::star::lang::EventObject& aEvent) throw( RuntimeException )
133 : {
134 0 : SbaXDataBrowserController::modified(aEvent);
135 :
136 : // multiplex this event to all my listeners
137 0 : ::com::sun::star::lang::EventObject aEvt(*this);
138 0 : ::cppu::OInterfaceIteratorHelper aIt(m_aModifyListeners);
139 0 : while (aIt.hasMoreElements())
140 0 : ((::com::sun::star::util::XModifyListener*)aIt.next())->modified(aEvt);
141 0 : }
142 :
143 : //------------------------------------------------------------------
144 0 : 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)
145 : {
146 0 : const ::com::sun::star::beans::PropertyValue* pArguments = aArgs.getConstArray();
147 0 : if ( aURL.Complete == ".uno:FormSlots/AddGridColumn" )
148 : {
149 : // search the argument describing the column to create
150 0 : ::rtl::OUString sControlType;
151 0 : sal_Int32 nControlPos = -1;
152 0 : Sequence< ::com::sun::star::beans::PropertyValue> aControlProps;
153 : sal_uInt16 i;
154 0 : for ( i = 0; i < aArgs.getLength(); ++i, ++pArguments )
155 : {
156 0 : if ( pArguments->Name == "ColumnType" )
157 : {
158 0 : sal_Bool bCorrectType = pArguments->Value.getValueType().equals(::getCppuType((const ::rtl::OUString*)0));
159 : OSL_ENSURE(bCorrectType, "invalid type for argument \"ColumnType\" !");
160 0 : if (bCorrectType)
161 0 : sControlType = ::comphelper::getString(pArguments->Value);
162 : }
163 0 : else if ( pArguments->Name == "ColumnPosition" )
164 : {
165 0 : sal_Bool bCorrectType = pArguments->Value.getValueType().equals(::getCppuType((const sal_Int16*)0));
166 : OSL_ENSURE(bCorrectType, "invalid type for argument \"ColumnPosition\" !");
167 0 : if (bCorrectType)
168 0 : nControlPos = ::comphelper::getINT16(pArguments->Value);
169 : }
170 0 : else if ( pArguments->Name == "ColumnProperties" )
171 : {
172 0 : 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\" !");
174 0 : if (bCorrectType)
175 0 : aControlProps = *(Sequence< ::com::sun::star::beans::PropertyValue>*)pArguments->Value.getValue();
176 : }
177 : else
178 : OSL_FAIL(rtl::OStringBuffer("SbaExternalSourceBrowser::dispatch(AddGridColumn) : unknown argument (").append(rtl::OUStringToOString(pArguments->Name, osl_getThreadTextEncoding())).append(") !").getStr());
179 : }
180 0 : if (sControlType.isEmpty())
181 : {
182 : OSL_FAIL("SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnType) !");
183 0 : sControlType = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TextField"));
184 : }
185 : OSL_ENSURE(aControlProps.getLength(), "SbaExternalSourceBrowser::dispatch(AddGridColumn) : missing argument (ColumnProperties) !");
186 :
187 : // create the col
188 0 : Reference< ::com::sun::star::form::XGridColumnFactory > xColFactory(getControlModel(), UNO_QUERY);
189 0 : Reference< ::com::sun::star::beans::XPropertySet > xNewCol = xColFactory->createColumn(sControlType);
190 0 : Reference< XPropertySetInfo > xNewColProperties;
191 0 : if (xNewCol.is())
192 0 : xNewColProperties = xNewCol->getPropertySetInfo();
193 : // set its properties
194 0 : if (xNewColProperties.is())
195 : {
196 0 : const ::com::sun::star::beans::PropertyValue* pControlProps = aControlProps.getConstArray();
197 0 : for (i=0; i<aControlProps.getLength(); ++i, ++pControlProps)
198 : {
199 : try
200 : {
201 0 : if (xNewColProperties->hasPropertyByName(pControlProps->Name))
202 0 : xNewCol->setPropertyValue(pControlProps->Name, pControlProps->Value);
203 : }
204 0 : catch (const Exception&)
205 : {
206 : OSL_FAIL(rtl::OStringBuffer(RTL_CONSTASCII_STRINGPARAM("SbaExternalSourceBrowser::dispatch : could not set a column property (")).append(rtl::OUStringToOString(pControlProps->Name, RTL_TEXTENCODING_ASCII_US)).append(RTL_CONSTASCII_STRINGPARAM(")!")).getStr());
207 : }
208 : }
209 : }
210 :
211 : // correct the position
212 0 : Reference< ::com::sun::star::container::XIndexContainer > xColContainer(getControlModel(), UNO_QUERY);
213 :
214 0 : if (nControlPos > xColContainer->getCount())
215 0 : nControlPos = xColContainer->getCount();
216 0 : if (nControlPos < 0)
217 0 : nControlPos = 0;
218 :
219 : // append the column
220 0 : xColContainer->insertByIndex(nControlPos, makeAny(xNewCol));
221 : }
222 0 : else if ( aURL.Complete == ".uno:FormSlots/ClearView" )
223 : {
224 0 : ClearView();
225 : }
226 0 : else if ( aURL.Complete == ".uno:FormSlots/AttachToForm" )
227 : {
228 0 : if (!m_pDataSourceImpl)
229 : return;
230 :
231 0 : Reference< XRowSet > xMasterForm;
232 : // search the arguments for the master form
233 0 : for (sal_uInt16 i=0; i<aArgs.getLength(); ++i, ++pArguments)
234 : {
235 0 : if ( (pArguments->Name == "MasterForm") && (pArguments->Value.getValueTypeClass() == TypeClass_INTERFACE) )
236 : {
237 0 : xMasterForm = Reference< XRowSet > (*(Reference< XInterface > *)pArguments->Value.getValue(), UNO_QUERY);
238 0 : break;
239 : }
240 : }
241 0 : if (!xMasterForm.is())
242 : {
243 : OSL_FAIL("SbaExternalSourceBrowser::dispatch(FormSlots/AttachToForm) : please specify a form to attach to as argument !");
244 : return;
245 : }
246 :
247 0 : Attach(xMasterForm);
248 : }
249 : else
250 0 : SbaXDataBrowserController::dispatch(aURL, aArgs);
251 : }
252 :
253 : //------------------------------------------------------------------
254 0 : 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 )
255 : {
256 0 : Reference< ::com::sun::star::frame::XDispatch > xReturn;
257 0 : if (m_bInQueryDispatch)
258 0 : return xReturn;
259 :
260 0 : m_bInQueryDispatch = sal_True;
261 :
262 0 : if ( ( aURL.Complete == ".uno:FormSlots/AttachToForm" )
263 : // attach a new external form
264 0 : || ( aURL.Complete == ".uno:FormSlots/AddGridColumn" )
265 : // add a column to the grid
266 0 : || ( aURL.Complete == ".uno:FormSlots/ClearView" )
267 : // clear the grid
268 : )
269 0 : xReturn = (::com::sun::star::frame::XDispatch*)this;
270 :
271 0 : if ( !xReturn.is()
272 0 : && ( (aURL.Complete == ".uno:FormSlots/moveToFirst" ) || (aURL.Complete == ".uno:FormSlots/moveToPrev" )
273 0 : || (aURL.Complete == ".uno:FormSlots/moveToNext" ) || (aURL.Complete == ".uno:FormSlots/moveToLast" )
274 0 : || (aURL.Complete == ".uno:FormSlots/moveToNew" ) || (aURL.Complete == ".uno:FormSlots/undoRecord" )
275 : )
276 : )
277 : {
278 : OSL_ENSURE(aURL.Mark.isEmpty(), "SbaExternalSourceBrowser::queryDispatch : the ::com::sun::star::util::URL shouldn't have a mark !");
279 0 : ::com::sun::star::util::URL aNewUrl = aURL;
280 :
281 : // split the ::com::sun::star::util::URL
282 : OSL_ENSURE( m_xUrlTransformer.is(), "SbaExternalSourceBrowser::queryDispatch : could not create an URLTransformer !" );
283 0 : if ( m_xUrlTransformer.is() )
284 0 : m_xUrlTransformer->parseStrict( aNewUrl );
285 :
286 : // set a new mark
287 0 : aNewUrl.Mark = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("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
290 :
291 0 : if (m_xUrlTransformer.is())
292 0 : m_xUrlTransformer->assemble(aNewUrl);
293 :
294 0 : Reference< XDispatchProvider > xFrameDispatcher( getFrame(), UNO_QUERY );
295 0 : if (xFrameDispatcher.is())
296 0 : xReturn = xFrameDispatcher->queryDispatch(aNewUrl, aTargetFrameName, FrameSearchFlag::PARENT);
297 :
298 : }
299 :
300 0 : if (!xReturn.is())
301 0 : xReturn = SbaXDataBrowserController::queryDispatch(aURL, aTargetFrameName, nSearchFlags);
302 :
303 0 : m_bInQueryDispatch = sal_False;
304 0 : return xReturn;
305 : }
306 :
307 : //------------------------------------------------------------------
308 0 : void SAL_CALL SbaExternalSourceBrowser::disposing()
309 : {
310 : // say our modify listeners goodbye
311 0 : ::com::sun::star::lang::EventObject aEvt;
312 0 : aEvt.Source = (XWeak*) this;
313 0 : m_aModifyListeners.disposeAndClear(aEvt);
314 :
315 0 : stopListening();
316 :
317 0 : SbaXDataBrowserController::disposing();
318 0 : }
319 :
320 : //------------------------------------------------------------------
321 0 : void SAL_CALL SbaExternalSourceBrowser::addModifyListener(const Reference< ::com::sun::star::util::XModifyListener > & aListener) throw( RuntimeException )
322 : {
323 0 : m_aModifyListeners.addInterface(aListener);
324 0 : }
325 :
326 : //------------------------------------------------------------------
327 0 : void SAL_CALL SbaExternalSourceBrowser::removeModifyListener(const Reference< ::com::sun::star::util::XModifyListener > & aListener) throw( RuntimeException )
328 : {
329 0 : m_aModifyListeners.removeInterface(aListener);
330 0 : }
331 :
332 : //------------------------------------------------------------------
333 0 : void SAL_CALL SbaExternalSourceBrowser::unloading(const ::com::sun::star::lang::EventObject& aEvent) throw( RuntimeException )
334 : {
335 0 : if (m_pDataSourceImpl && (m_pDataSourceImpl->getAttachedForm() == aEvent.Source))
336 : {
337 0 : ClearView();
338 : }
339 :
340 0 : SbaXDataBrowserController::unloading(aEvent);
341 0 : }
342 :
343 : //------------------------------------------------------------------
344 0 : void SbaExternalSourceBrowser::Attach(const Reference< XRowSet > & xMaster)
345 : {
346 0 : Any aOldPos;
347 0 : sal_Bool bWasInsertRow = sal_False;
348 0 : sal_Bool bBeforeFirst = sal_True;
349 0 : sal_Bool bAfterLast = sal_True;
350 0 : Reference< XResultSet > xResultSet(xMaster, UNO_QUERY);
351 0 : Reference< XRowLocate > xCursor(xMaster, UNO_QUERY);
352 0 : Reference< XPropertySet > xMasterProps(xMaster, UNO_QUERY);
353 :
354 : try
355 : {
356 : // switch the control to design mode
357 0 : if (getBrowserView() && getBrowserView()->getGridControl().is())
358 0 : getBrowserView()->getGridControl()->setDesignMode(sal_True);
359 :
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 0 : if (xCursor.is() && xResultSet.is())
363 : {
364 0 : bBeforeFirst = xResultSet->isBeforeFirst();
365 0 : bAfterLast = xResultSet->isAfterLast();
366 0 : if(!bBeforeFirst && !bAfterLast)
367 0 : aOldPos = xCursor->getBookmark();
368 : }
369 :
370 0 : if (xMasterProps.is())
371 0 : xMasterProps->getPropertyValue(PROPERTY_ISNEW) >>= bWasInsertRow;
372 : }
373 0 : catch( const Exception& )
374 : {
375 : DBG_UNHANDLED_EXCEPTION();
376 : }
377 :
378 0 : onStartLoading( Reference< XLoadable >( xMaster, UNO_QUERY ) );
379 :
380 0 : stopListening();
381 0 : m_pDataSourceImpl->AttachForm(xMaster);
382 0 : startListening();
383 :
384 0 : if (xMaster.is())
385 : {
386 : // at this point we have to reset the formatter for the new form
387 0 : initFormatter();
388 : // assume that the master form is already loaded
389 : #if OSL_DEBUG_LEVEL > 0
390 : {
391 : Reference< XLoadable > xLoadable( xMaster, UNO_QUERY );
392 : OSL_ENSURE( xLoadable.is() && xLoadable->isLoaded(), "SbaExternalSourceBrowser::Attach: master is not loaded!" );
393 : }
394 : #endif
395 :
396 0 : LoadFinished(sal_True);
397 :
398 0 : Reference< XResultSetUpdate > xUpdate(xMaster, UNO_QUERY);
399 : try
400 : {
401 0 : if (bWasInsertRow && xUpdate.is())
402 0 : xUpdate->moveToInsertRow();
403 0 : else if (xCursor.is() && aOldPos.hasValue())
404 0 : xCursor->moveToBookmark(aOldPos);
405 0 : else if(bBeforeFirst && xResultSet.is())
406 0 : xResultSet->beforeFirst();
407 0 : else if(bAfterLast && xResultSet.is())
408 0 : xResultSet->afterLast();
409 : }
410 0 : catch(Exception&)
411 : {
412 : OSL_FAIL("SbaExternalSourceBrowser::Attach : couldn't restore the cursor position !");
413 0 : }
414 :
415 0 : }
416 0 : }
417 :
418 : //------------------------------------------------------------------
419 0 : void SbaExternalSourceBrowser::ClearView()
420 : {
421 : // set a new (empty) datasource
422 0 : Attach(Reference< XRowSet > ());
423 :
424 :
425 : // clear all cols in the grid
426 0 : Reference< ::com::sun::star::container::XIndexContainer > xColContainer(getControlModel(), UNO_QUERY);
427 0 : while (xColContainer->getCount() > 0)
428 0 : xColContainer->removeByIndex(0);
429 0 : }
430 :
431 : //------------------------------------------------------------------
432 0 : void SAL_CALL SbaExternalSourceBrowser::disposing(const ::com::sun::star::lang::EventObject& Source) throw( RuntimeException )
433 : {
434 0 : if (m_pDataSourceImpl && (m_pDataSourceImpl->getAttachedForm() == Source.Source))
435 : {
436 0 : ClearView();
437 : }
438 :
439 0 : SbaXDataBrowserController::disposing(Source);
440 0 : }
441 :
442 : //------------------------------------------------------------------
443 0 : void SbaExternalSourceBrowser::startListening()
444 : {
445 0 : if (m_pDataSourceImpl && m_pDataSourceImpl->getAttachedForm().is())
446 : {
447 0 : Reference< ::com::sun::star::form::XLoadable > xLoadable(m_pDataSourceImpl->getAttachedForm(), UNO_QUERY);
448 0 : xLoadable->addLoadListener((::com::sun::star::form::XLoadListener*)this);
449 : }
450 0 : }
451 :
452 : //------------------------------------------------------------------
453 0 : void SbaExternalSourceBrowser::stopListening()
454 : {
455 0 : if (m_pDataSourceImpl && m_pDataSourceImpl->getAttachedForm().is())
456 : {
457 0 : Reference< ::com::sun::star::form::XLoadable > xLoadable(m_pDataSourceImpl->getAttachedForm(), UNO_QUERY);
458 0 : xLoadable->removeLoadListener((::com::sun::star::form::XLoadListener*)this);
459 : }
460 0 : }
461 :
462 : //==================================================================
463 : //==================================================================
464 :
465 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|