Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / misc / databaseobjectview.cxx
blob60597f36584f740cc5b83cf8fbce0fa3ee17696a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "databaseobjectview.hxx"
31 #include "dbustrings.hrc"
32 #include "asyncmodaldialog.hxx"
34 /** === begin UNO includes === **/
35 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
36 #include <com/sun/star/frame/XDispatchProvider.hpp>
37 #include <com/sun/star/frame/XFrame.hpp>
38 #include <com/sun/star/frame/XFrames.hpp>
39 #include <com/sun/star/frame/FrameSearchFlag.hpp>
40 #include <com/sun/star/sdb/CommandType.hpp>
41 #include <com/sun/star/sdb/application/XTableUIProvider.hpp>
42 #include <com/sun/star/beans/NamedValue.hpp>
43 #include <com/sun/star/awt/Rectangle.hpp>
44 /** === end UNO includes === **/
46 #include <comphelper/extract.hxx>
47 #include <comphelper/sequence.hxx>
48 #include <connectivity/dbtools.hxx>
49 #include <osl/diagnose.h>
50 #include <toolkit/helper/vclunohelper.hxx>
51 #include <tools/diagnose_ex.h>
52 #include <vcl/window.hxx>
54 // .........................................................................
55 namespace dbaui
57 // .........................................................................
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::sdbc;
61 using namespace ::com::sun::star::sdb;
62 using namespace ::com::sun::star::sdb::application;
63 using namespace ::com::sun::star::ui::dialogs;
64 using namespace ::com::sun::star::frame;
65 using namespace ::com::sun::star::lang;
66 using namespace ::com::sun::star::beans;
67 using namespace ::com::sun::star::awt;
69 //======================================================================
70 //= DatabaseObjectView
71 //======================================================================
72 DatabaseObjectView::DatabaseObjectView( const Reference< XMultiServiceFactory >& _rxORB,
73 const Reference< XDatabaseDocumentUI >& _rxApplication,
74 const Reference< XFrame >& _rxParentFrame,
75 const ::rtl::OUString& _rComponentURL )
76 :m_xORB ( _rxORB )
77 ,m_xParentFrame ( _rxParentFrame )
78 ,m_xFrameLoader ( )
79 ,m_xApplication ( _rxApplication )
80 ,m_sComponentURL ( _rComponentURL )
82 OSL_ENSURE( m_xORB.is(), "DatabaseObjectView::DatabaseObjectView: invalid service factory!" );
83 OSL_ENSURE( m_xApplication.is(), "DatabaseObjectView::DatabaseObjectView: invalid connection!" );
86 //----------------------------------------------------------------------
87 Reference< XConnection > DatabaseObjectView::getConnection() const
89 Reference< XConnection > xConnection;
90 if ( m_xApplication.is() )
91 xConnection = m_xApplication->getActiveConnection();
92 return xConnection;
95 //----------------------------------------------------------------------
96 Reference< XComponent > DatabaseObjectView::createNew( const Reference< XDataSource >& _xDataSource, const ::comphelper::NamedValueCollection& i_rDispatchArgs )
98 return doCreateView( makeAny( _xDataSource ), ::rtl::OUString(), i_rDispatchArgs );
101 //----------------------------------------------------------------------
102 Reference< XComponent > DatabaseObjectView::openExisting( const Any& _rDataSource, const ::rtl::OUString& _rName,
103 const ::comphelper::NamedValueCollection& i_rDispatchArgs )
105 return doCreateView( _rDataSource, _rName, i_rDispatchArgs );
108 //----------------------------------------------------------------------
109 Reference< XComponent > DatabaseObjectView::doCreateView( const Any& _rDataSource, const ::rtl::OUString& _rObjectName,
110 const ::comphelper::NamedValueCollection& i_rCreationArgs )
112 ::comphelper::NamedValueCollection aDispatchArgs;
114 aDispatchArgs.merge( i_rCreationArgs, false ); // false => do not overwrite
115 fillDispatchArgs( aDispatchArgs, _rDataSource, _rObjectName );
116 aDispatchArgs.merge( i_rCreationArgs, true ); // true => do overwrite
118 return doDispatch( aDispatchArgs );
121 //----------------------------------------------------------------------
122 Reference< XComponent > DatabaseObjectView::doDispatch( const ::comphelper::NamedValueCollection& i_rDispatchArgs )
124 Reference< XComponent > xReturn;
125 if ( m_xORB.is() )
129 // if we have no externally provided frame, create one
130 if ( !m_xFrameLoader.is() )
132 Reference< XSingleServiceFactory > xFact(m_xORB->createInstance(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.frame.TaskCreator"))), UNO_QUERY_THROW);
133 Sequence< Any > lArgs(2);
134 NamedValue aProp;
135 sal_Int32 nArg = 0;
137 aProp.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ParentFrame"));
138 aProp.Value <<= m_xParentFrame;
139 lArgs[nArg++] <<= aProp;
141 aProp.Name = ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("TopWindow"));
142 aProp.Value <<= sal_True;
143 lArgs[nArg++] <<= aProp;
145 m_xFrameLoader.set(xFact->createInstanceWithArguments(lArgs), UNO_QUERY_THROW);
147 // everything we load can be considered a "top level document", so set the respective bit at the window.
148 // This, amongst other things, triggers that the component in this task participates in the
149 // "ThisComponent"-game for the global application Basic.
150 const Reference< XFrame > xFrame( m_xFrameLoader, UNO_QUERY_THROW );
151 const Reference< XWindow > xFrameWindow( xFrame->getContainerWindow(), UNO_SET_THROW );
152 Window* pContainerWindow = VCLUnoHelper::GetWindow( xFrameWindow );
153 ENSURE_OR_THROW( pContainerWindow, "no implementation access to the frame's container window!" );
154 pContainerWindow->SetExtendedStyle( pContainerWindow->GetExtendedStyle() | WB_EXT_DOCUMENT );
157 Reference< XComponentLoader > xFrameLoader( m_xFrameLoader, UNO_QUERY_THROW );
158 xReturn = xFrameLoader->loadComponentFromURL(
159 m_sComponentURL,
160 ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("_self")),
162 i_rDispatchArgs.getPropertyValues()
165 catch( const Exception& )
167 DBG_UNHANDLED_EXCEPTION();
170 return xReturn;
173 //----------------------------------------------------------------------
174 void DatabaseObjectView::fillDispatchArgs(
175 ::comphelper::NamedValueCollection& i_rDispatchArgs,
176 const Any& _aDataSource,
177 const ::rtl::OUString& /* _rName */
180 ::rtl::OUString sDataSource;
181 Reference<XDataSource> xDataSource;
182 if ( _aDataSource >>= sDataSource )
184 i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_DATASOURCENAME, sDataSource );
186 else if ( _aDataSource >>= xDataSource )
188 i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_DATASOURCE, xDataSource );
191 i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_ACTIVE_CONNECTION, getConnection() );
194 //======================================================================
195 //= QueryDesigner
196 //======================================================================
197 //----------------------------------------------------------------------
198 QueryDesigner::QueryDesigner( const Reference< XMultiServiceFactory >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication,
199 const Reference< XFrame >& _rxParentFrame, bool _bCreateView )
200 :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, _bCreateView ? URL_COMPONENT_VIEWDESIGN : URL_COMPONENT_QUERYDESIGN )
201 ,m_nCommandType( _bCreateView ? CommandType::TABLE : CommandType::QUERY )
205 //----------------------------------------------------------------------
206 void QueryDesigner::fillDispatchArgs( ::comphelper::NamedValueCollection& i_rDispatchArgs, const Any& _aDataSource,
207 const ::rtl::OUString& _rObjectName )
209 DatabaseObjectView::fillDispatchArgs( i_rDispatchArgs, _aDataSource, _rObjectName );
211 const bool bIncludeQueryName = !_rObjectName.isEmpty();
212 const bool bGraphicalDesign = i_rDispatchArgs.getOrDefault( (::rtl::OUString)PROPERTY_GRAPHICAL_DESIGN, sal_True );
213 const bool bEditViewAsSQLCommand = ( m_nCommandType == CommandType::TABLE ) && !bGraphicalDesign;
215 i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_COMMAND_TYPE, m_nCommandType );
217 if ( bIncludeQueryName )
219 i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_COMMAND, _rObjectName );
222 if ( bEditViewAsSQLCommand )
224 i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_ESCAPE_PROCESSING, sal_False );
228 //======================================================================
229 //= TableDesigner
230 //======================================================================
231 //----------------------------------------------------------------------
232 TableDesigner::TableDesigner( const Reference< XMultiServiceFactory >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication, const Reference< XFrame >& _rxParentFrame )
233 :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, static_cast< ::rtl::OUString >( URL_COMPONENT_TABLEDESIGN ) )
237 //----------------------------------------------------------------------
238 void TableDesigner::fillDispatchArgs( ::comphelper::NamedValueCollection& i_rDispatchArgs, const Any& _aDataSource,
239 const ::rtl::OUString& _rObjectName )
241 DatabaseObjectView::fillDispatchArgs( i_rDispatchArgs, _aDataSource, _rObjectName );
243 if ( !_rObjectName.isEmpty() )
245 i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_CURRENTTABLE, _rObjectName );
249 //----------------------------------------------------------------------
250 Reference< XComponent > TableDesigner::doCreateView( const Any& _rDataSource, const ::rtl::OUString& _rObjectName,
251 const ::comphelper::NamedValueCollection& i_rCreationArgs )
253 bool bIsNewDesign = _rObjectName.isEmpty();
255 // let's see whether the connection can provide a dedicated table desginer
256 Reference< XInterface > xDesigner;
257 if ( !bIsNewDesign )
258 xDesigner = impl_getConnectionProvidedDesigner_nothrow( _rObjectName );
260 if ( !xDesigner.is() )
261 return DatabaseObjectView::doCreateView( _rDataSource, _rObjectName, i_rCreationArgs );
263 // try whether the designer is a dialog
264 Reference< XExecutableDialog > xDialog( xDesigner, UNO_QUERY_THROW );
265 if ( xDialog.is() )
267 try { AsyncDialogExecutor::executeModalDialogAsync( xDialog ); }
268 catch( const Exception& ) { DBG_UNHANDLED_EXCEPTION(); }
269 return NULL;
272 Reference< XComponent > xDesignerComponent( xDesigner, UNO_QUERY );
273 OSL_ENSURE( xDesignerComponent.is(), "TableDesigner::doCreateView: a designer which is no dialog and no component?" );
274 return xDesignerComponent;
277 //----------------------------------------------------------------------
278 Reference< XInterface > TableDesigner::impl_getConnectionProvidedDesigner_nothrow( const ::rtl::OUString& _rTableName )
280 Reference< XInterface > xDesigner;
283 Reference< XTableUIProvider > xTableUIProv( getConnection(), UNO_QUERY );
284 if ( xTableUIProv.is() )
285 xDesigner = xTableUIProv->getTableEditor( getApplicationUI(), _rTableName );
287 catch( const Exception& )
289 DBG_UNHANDLED_EXCEPTION();
291 return xDesigner;
294 //======================================================================
295 //= ResultSetBrowser
296 //======================================================================
297 //----------------------------------------------------------------------
298 ResultSetBrowser::ResultSetBrowser( const Reference< XMultiServiceFactory >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication, const Reference< XFrame >& _rxParentFrame,
299 sal_Bool _bTable )
300 :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, static_cast < ::rtl::OUString >( URL_COMPONENT_DATASOURCEBROWSER ) )
301 ,m_bTable(_bTable)
305 //----------------------------------------------------------------------
306 void ResultSetBrowser::fillDispatchArgs( ::comphelper::NamedValueCollection& i_rDispatchArgs, const Any& _aDataSource,
307 const ::rtl::OUString& _rQualifiedName)
309 DatabaseObjectView::fillDispatchArgs( i_rDispatchArgs, _aDataSource, _rQualifiedName );
310 OSL_ENSURE( !_rQualifiedName.isEmpty(),"A Table name must be set");
311 ::rtl::OUString sCatalog;
312 ::rtl::OUString sSchema;
313 ::rtl::OUString sTable;
314 if ( m_bTable )
315 ::dbtools::qualifiedNameComponents( getConnection()->getMetaData(), _rQualifiedName, sCatalog, sSchema, sTable, ::dbtools::eInDataManipulation );
317 i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_COMMAND_TYPE, (m_bTable ? CommandType::TABLE : CommandType::QUERY) );
318 i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_COMMAND, _rQualifiedName );
319 i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_ENABLE_BROWSER, sal_False );
321 if ( m_bTable )
323 i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_UPDATE_CATALOGNAME, sCatalog );
324 i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_UPDATE_SCHEMANAME, sSchema );
325 i_rDispatchArgs.put( (::rtl::OUString)PROPERTY_UPDATE_TABLENAME, sTable );
329 //======================================================================
330 //= RelationDesigner
331 //======================================================================
332 //----------------------------------------------------------------------
333 RelationDesigner::RelationDesigner( const Reference< XMultiServiceFactory >& _rxORB, const Reference< XDatabaseDocumentUI >& _rxApplication, const Reference< XFrame >& _rxParentFrame )
334 :DatabaseObjectView( _rxORB, _rxApplication, _rxParentFrame, static_cast< ::rtl::OUString >( URL_COMPONENT_RELATIONDESIGN ) )
337 // .........................................................................
338 } // namespace dbaui
339 // .........................................................................
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */