xref: /trunk/main/dbaccess/source/ui/dlg/adtabdlg.cxx (revision 3ce6c2dd3860afd353ceca3849779821e9b9f6f5)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_dbui.hxx"
26 
27 #include "adtabdlg.hxx"
28 #include "adtabdlg.hrc"
29 #include "sqlmessage.hxx"
30 #include <tools/debug.hxx>
31 #include <tools/diagnose_ex.h>
32 #include <svtools/localresaccess.hxx>
33 #include "dbaccess_helpid.hrc"
34 #include "dbu_resource.hrc"
35 #include "dbu_dlg.hrc"
36 #include <sfx2/sfxsids.hrc>
37 #include "QueryTableView.hxx"
38 #include "QueryDesignView.hxx"
39 #include "querycontroller.hxx"
40 #include <connectivity/dbtools.hxx>
41 #include "browserids.hxx"
42 #include <com/sun/star/sdb/XQueriesSupplier.hpp>
43 #include <com/sun/star/sdbcx/XViewsSupplier.hpp>
44 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
45 #include <com/sun/star/container/XNameAccess.hpp>
46 #include "UITools.hxx"
47 #include "imageprovider.hxx"
48 
49 #include <comphelper/containermultiplexer.hxx>
50 #include "cppuhelper/basemutex.hxx"
51 #include <functional>
52 #include <algorithm>
53 
54 // slot ids
55 using namespace dbaui;
56 using namespace ::com::sun::star;
57 using namespace ::com::sun::star::uno;
58 using namespace ::com::sun::star::container;
59 using namespace ::com::sun::star::sdb;
60 using namespace ::com::sun::star::sdbc;
61 using namespace ::com::sun::star::sdbcx;
62 using namespace dbtools;
63 
64 //==============================================================================
65 //= TableObjectListFacade
66 //==============================================================================
~TableObjectListFacade()67 TableObjectListFacade::~TableObjectListFacade()
68 {
69 }
70 
71 //==============================================================================
72 //= TableListFacade
73 //==============================================================================
74 class TableListFacade : public ::cppu::BaseMutex
75                     ,   public TableObjectListFacade
76                     ,   public ::comphelper::OContainerListener
77 {
78     OTableTreeListBox&          m_rTableList;
79     Reference< XConnection >    m_xConnection;
80     ::rtl::Reference< comphelper::OContainerListenerAdapter>
81                                 m_pContainerListener;
82     bool                        m_bAllowViews;
83 
84 public:
TableListFacade(OTableTreeListBox & _rTableList,const Reference<XConnection> & _rxConnection)85     TableListFacade( OTableTreeListBox& _rTableList, const Reference< XConnection >& _rxConnection )
86         : ::comphelper::OContainerListener(m_aMutex)
87         ,m_rTableList( _rTableList )
88         ,m_xConnection( _rxConnection )
89         ,m_bAllowViews(true)
90     {
91     }
92     virtual ~TableListFacade();
93 
94 
95 private:
96     virtual void    updateTableObjectList( bool _bAllowViews );
97     virtual String  getSelectedName( String& _out_rAliasName ) const;
98     virtual bool    isLeafSelected() const;
99     // OContainerListener
100     virtual void _elementInserted( const ::com::sun::star::container::ContainerEvent& _rEvent );
101     virtual void _elementRemoved( const  ::com::sun::star::container::ContainerEvent& _rEvent );
102     virtual void _elementReplaced( const ::com::sun::star::container::ContainerEvent& _rEvent );
103 };
104 
~TableListFacade()105 TableListFacade::~TableListFacade()
106 {
107     if ( m_pContainerListener.is() )
108         m_pContainerListener->dispose();
109 }
110 //------------------------------------------------------------------------------
getSelectedName(String & _out_rAliasName) const111 String TableListFacade::getSelectedName( String& _out_rAliasName ) const
112 {
113     SvLBoxEntry* pEntry = m_rTableList.FirstSelected();
114     if ( !pEntry )
115         return String();
116 
117     ::rtl::OUString aCatalog, aSchema, aTableName;
118     SvLBoxEntry* pSchema = m_rTableList.GetParent(pEntry);
119     if(pSchema && pSchema != m_rTableList.getAllObjectsEntry())
120     {
121         SvLBoxEntry* pCatalog = m_rTableList.GetParent(pSchema);
122         if(pCatalog && pCatalog != m_rTableList.getAllObjectsEntry())
123             aCatalog = m_rTableList.GetEntryText(pCatalog);
124         aSchema = m_rTableList.GetEntryText(pSchema);
125     }
126     aTableName = m_rTableList.GetEntryText(pEntry);
127 
128     ::rtl::OUString aComposedName;
129     try
130     {
131         Reference< XDatabaseMetaData > xMeta( m_xConnection->getMetaData(), UNO_QUERY_THROW );
132         if (  !aCatalog.getLength()
133             && aSchema.getLength()
134             && xMeta->supportsCatalogsInDataManipulation()
135             && !xMeta->supportsSchemasInDataManipulation() )
136         {
137             aCatalog = aSchema;
138             aSchema = ::rtl::OUString();
139         }
140 
141         aComposedName = ::dbtools::composeTableName(
142             xMeta, aCatalog, aSchema, aTableName, sal_False, ::dbtools::eInDataManipulation );
143     }
144     catch ( const Exception& )
145     {
146         DBG_UNHANDLED_EXCEPTION();
147     }
148 
149     _out_rAliasName = aTableName;
150     return aComposedName;
151 }
152 // -----------------------------------------------------------------------------
_elementInserted(const container::ContainerEvent &)153 void TableListFacade::_elementInserted( const container::ContainerEvent& /*_rEvent*/ )
154 {
155     updateTableObjectList(m_bAllowViews);
156 }
157 // -----------------------------------------------------------------------------
_elementRemoved(const container::ContainerEvent &)158 void TableListFacade::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ )
159 {
160     updateTableObjectList(m_bAllowViews);
161 }
162 // -----------------------------------------------------------------------------
_elementReplaced(const container::ContainerEvent &)163 void TableListFacade::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
164 {
165 }
166 //------------------------------------------------------------------------------
updateTableObjectList(bool _bAllowViews)167 void TableListFacade::updateTableObjectList( bool _bAllowViews )
168 {
169     m_bAllowViews = _bAllowViews;
170     m_rTableList.Clear();
171     try
172     {
173         Reference< XTablesSupplier > xTableSupp( m_xConnection, UNO_QUERY_THROW );
174 
175         Reference< XViewsSupplier > xViewSupp;
176         Reference< XNameAccess > xTables, xViews;
177         Sequence< ::rtl::OUString > sTables, sViews;
178 
179         xTables = xTableSupp->getTables();
180         if ( xTables.is() )
181         {
182             if ( !m_pContainerListener.is() )
183             {
184                 Reference< XContainer> xContainer(xTables,uno::UNO_QUERY);
185                 if ( xContainer.is() )
186                     m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
187             }
188             sTables = xTables->getElementNames();
189         } // if ( xTables.is() )
190 
191         xViewSupp.set( xTableSupp, UNO_QUERY );
192         if ( xViewSupp.is() )
193         {
194             xViews = xViewSupp->getViews();
195             if ( xViews.is() )
196                 sViews = xViews->getElementNames();
197         }
198 
199         // if no views are allowed remove the views also out the table name filter
200         if ( !_bAllowViews )
201         {
202             const ::rtl::OUString* pTableBegin  = sTables.getConstArray();
203             const ::rtl::OUString* pTableEnd    = pTableBegin + sTables.getLength();
204             ::std::vector< ::rtl::OUString > aTables(pTableBegin,pTableEnd);
205 
206             const ::rtl::OUString* pViewBegin = sViews.getConstArray();
207             const ::rtl::OUString* pViewEnd   = pViewBegin + sViews.getLength();
208             ::comphelper::TStringMixEqualFunctor aEqualFunctor;
209             for(;pViewBegin != pViewEnd;++pViewBegin)
210                 aTables.erase(::std::remove_if(aTables.begin(),aTables.end(),::std::bind(aEqualFunctor, ::std::placeholders::_1,*pViewBegin)),aTables.end());
211             ::rtl::OUString* pTables = aTables.empty() ? 0 : &aTables[0];
212             sTables = Sequence< ::rtl::OUString>(pTables, aTables.size());
213             sViews = Sequence< ::rtl::OUString>();
214         }
215 
216         m_rTableList.UpdateTableList( m_xConnection, sTables, sViews );
217         SvLBoxEntry* pEntry = m_rTableList.First();
218         while( pEntry && m_rTableList.GetModel()->HasChilds( pEntry ) )
219         {
220             m_rTableList.Expand( pEntry );
221             pEntry = m_rTableList.Next( pEntry );
222         }
223         if ( pEntry )
224             m_rTableList.Select(pEntry);
225     }
226     catch( const Exception& )
227     {
228         DBG_UNHANDLED_EXCEPTION();
229     }
230 }
231 
232 //------------------------------------------------------------------------------
isLeafSelected() const233 bool TableListFacade::isLeafSelected() const
234 {
235     SvLBoxEntry* pEntry = m_rTableList.FirstSelected();
236     return pEntry && !m_rTableList.GetModel()->HasChilds( pEntry );
237 }
238 
239 //==============================================================================
240 //= QueryListFacade
241 //==============================================================================
242 class QueryListFacade : public ::cppu::BaseMutex
243                     ,   public TableObjectListFacade
244                     ,   public ::comphelper::OContainerListener
245 {
246     SvTreeListBox&              m_rQueryList;
247     Reference< XConnection >    m_xConnection;
248     ::rtl::Reference< comphelper::OContainerListenerAdapter>
249                                 m_pContainerListener;
250 
251 public:
QueryListFacade(SvTreeListBox & _rQueryList,const Reference<XConnection> & _rxConnection)252     QueryListFacade( SvTreeListBox& _rQueryList, const Reference< XConnection >& _rxConnection )
253         : ::comphelper::OContainerListener(m_aMutex)
254         ,m_rQueryList( _rQueryList )
255         ,m_xConnection( _rxConnection )
256     {
257     }
258     virtual ~QueryListFacade();
259 private:
260     virtual void    updateTableObjectList( bool _bAllowViews );
261     virtual String  getSelectedName( String& _out_rAliasName ) const;
262     virtual bool    isLeafSelected() const;
263     // OContainerListener
264     virtual void _elementInserted( const ::com::sun::star::container::ContainerEvent& _rEvent );
265     virtual void _elementRemoved( const  ::com::sun::star::container::ContainerEvent& _rEvent );
266     virtual void _elementReplaced( const ::com::sun::star::container::ContainerEvent& _rEvent );
267 };
~QueryListFacade()268 QueryListFacade::~QueryListFacade()
269 {
270     if ( m_pContainerListener.is() )
271         m_pContainerListener->dispose();
272 }
273 // -----------------------------------------------------------------------------
_elementInserted(const container::ContainerEvent & _rEvent)274 void QueryListFacade::_elementInserted( const container::ContainerEvent& _rEvent )
275 {
276     ::rtl::OUString sName;
277     if ( _rEvent.Accessor >>= sName )
278         m_rQueryList.InsertEntry( sName );
279 }
280 // -----------------------------------------------------------------------------
_elementRemoved(const container::ContainerEvent &)281 void QueryListFacade::_elementRemoved( const container::ContainerEvent& /*_rEvent*/ )
282 {
283     updateTableObjectList(true);
284 }
285 // -----------------------------------------------------------------------------
_elementReplaced(const container::ContainerEvent &)286 void QueryListFacade::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ )
287 {
288 }
289 
290 //------------------------------------------------------------------------------
updateTableObjectList(bool)291 void QueryListFacade::updateTableObjectList( bool /*_bAllowViews*/ )
292 {
293     m_rQueryList.Clear();
294     try
295     {
296         ImageProvider aImageProvider( m_xConnection );
297         Image aQueryImage( aImageProvider.getDefaultImage( DatabaseObject::QUERY, false ) );
298         Image aQueryImageHC( aImageProvider.getDefaultImage( DatabaseObject::QUERY, true ) );
299 
300         m_rQueryList.SetDefaultExpandedEntryBmp( aQueryImage, BMP_COLOR_NORMAL );
301         m_rQueryList.SetDefaultCollapsedEntryBmp( aQueryImage, BMP_COLOR_NORMAL );
302         m_rQueryList.SetDefaultExpandedEntryBmp( aQueryImageHC, BMP_COLOR_HIGHCONTRAST );
303         m_rQueryList.SetDefaultCollapsedEntryBmp( aQueryImageHC, BMP_COLOR_HIGHCONTRAST );
304 
305         Reference< XQueriesSupplier > xSuppQueries( m_xConnection, UNO_QUERY_THROW );
306         Reference< XNameAccess > xQueries( xSuppQueries->getQueries(), UNO_QUERY_THROW );
307         if ( !m_pContainerListener.is() )
308         {
309             Reference< XContainer> xContainer(xQueries,UNO_QUERY_THROW);
310             m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
311         }
312         Sequence< ::rtl::OUString > aQueryNames = xQueries->getElementNames();
313 
314         const ::rtl::OUString* pQuery = aQueryNames.getConstArray();
315         const ::rtl::OUString* pQueryEnd = aQueryNames.getConstArray() + aQueryNames.getLength();
316         while ( pQuery != pQueryEnd )
317             m_rQueryList.InsertEntry( *pQuery++ );
318     }
319     catch( const Exception& )
320     {
321         DBG_UNHANDLED_EXCEPTION();
322     }
323 }
324 
325 //------------------------------------------------------------------------------
getSelectedName(String & _out_rAliasName) const326 String QueryListFacade::getSelectedName( String& _out_rAliasName ) const
327 {
328     String sSelected;
329     SvLBoxEntry* pEntry = m_rQueryList.FirstSelected();
330     if ( pEntry )
331         sSelected = _out_rAliasName = m_rQueryList.GetEntryText( pEntry );
332     return sSelected;
333 }
334 
335 //------------------------------------------------------------------------------
isLeafSelected() const336 bool QueryListFacade::isLeafSelected() const
337 {
338     SvLBoxEntry* pEntry = m_rQueryList.FirstSelected();
339     return pEntry && !m_rQueryList.GetModel()->HasChilds( pEntry );
340 }
341 
342 //==============================================================================
343 //= OAddTableDlg
344 //==============================================================================
345 //------------------------------------------------------------------------------
OAddTableDlg(Window * pParent,IAddTableDialogContext & _rContext)346 OAddTableDlg::OAddTableDlg( Window* pParent, IAddTableDialogContext& _rContext )
347              :ModelessDialog( pParent, ModuleRes(DLG_JOIN_TABADD) )
348              ,m_aCaseTables( this, ModuleRes( RB_CASE_TABLES ) )
349              ,m_aCaseQueries( this, ModuleRes( RB_CASE_QUERIES ) )
350              ,m_aTableList( this, NULL, ModuleRes( LB_TABLE_OR_QUERY ), sal_False )
351              ,m_aQueryList( this, ModuleRes( LB_TABLE_OR_QUERY ) )
352              ,aAddButton( this, ModuleRes( PB_ADDTABLE ) )
353              ,aCloseButton( this, ModuleRes( PB_CLOSE ) )
354              ,aHelpButton( this, ModuleRes( PB_HELP ) )
355              ,m_rContext( _rContext )
356 {
357     // der Close-Button hat schon einen Standard-Help-Text, den ich aber hier nicht haben moechte, also den Text ruecksetzen
358     // und eine neue ID verteilen
359     aCloseButton.SetHelpText(String());
360     aCloseButton.SetHelpId(HID_JOINSH_ADDTAB_CLOSE);
361 
362     m_aTableList.SetHelpId( HID_JOINSH_ADDTAB_TABLELIST );
363     m_aQueryList.SetHelpId( HID_JOINSH_ADDTAB_QUERYLIST );
364 
365     //////////////////////////////////////////////////////////////////////
366     m_aCaseTables.SetClickHdl( LINK( this, OAddTableDlg, OnTypeSelected ) );
367     m_aCaseQueries.SetClickHdl( LINK( this, OAddTableDlg, OnTypeSelected ) );
368     aAddButton.SetClickHdl( LINK( this, OAddTableDlg, AddClickHdl ) );
369     aCloseButton.SetClickHdl( LINK( this, OAddTableDlg, CloseClickHdl ) );
370     m_aTableList.SetDoubleClickHdl( LINK( this, OAddTableDlg, TableListDoubleClickHdl ) );
371     m_aTableList.SetSelectHdl( LINK( this, OAddTableDlg, TableListSelectHdl ) );
372     m_aQueryList.SetDoubleClickHdl( LINK( this, OAddTableDlg, TableListDoubleClickHdl ) );
373     m_aQueryList.SetSelectHdl( LINK( this, OAddTableDlg, TableListSelectHdl ) );
374 
375     //////////////////////////////////////////////////////////////////////
376     m_aTableList.EnableInplaceEditing( sal_False );
377     m_aTableList.SetStyle(m_aTableList.GetStyle() | WB_BORDER | WB_HASLINES |WB_HASBUTTONS | WB_HASBUTTONSATROOT | WB_HASLINESATROOT | WB_SORT | WB_HSCROLL );
378     m_aTableList.EnableCheckButton( NULL ); // do not show any buttons
379     m_aTableList.SetSelectionMode( SINGLE_SELECTION );
380     m_aTableList.notifyHiContrastChanged();
381     m_aTableList.suppressEmptyFolders();
382 
383     //////////////////////////////////////////////////////////////////////
384     m_aQueryList.EnableInplaceEditing( sal_False );
385     m_aQueryList.SetSelectionMode( SINGLE_SELECTION );
386 
387     //////////////////////////////////////////////////////////////////////
388     if ( !m_rContext.allowQueries() )
389     {
390         m_aCaseTables.Hide();
391         m_aCaseQueries.Hide();
392 
393         long nPixelDiff = m_aTableList.GetPosPixel().Y() - m_aCaseTables.GetPosPixel().Y();
394 
395         Point aListPos( m_aTableList.GetPosPixel() );
396         aListPos.Y() -= nPixelDiff;
397 
398         Size aListSize( m_aTableList.GetSizePixel() );
399         aListSize.Height() += nPixelDiff;
400 
401         m_aTableList.SetPosSizePixel( aListPos, aListSize );
402     }
403 
404     FreeResource();
405 
406     SetText( getDialogTitleForContext( m_rContext ) );
407 }
408 
409 //------------------------------------------------------------------------------
~OAddTableDlg()410 OAddTableDlg::~OAddTableDlg()
411 {
412     m_rContext.onWindowClosing( this );
413 }
414 
415 //------------------------------------------------------------------------------
impl_switchTo(ObjectList _eList)416 void OAddTableDlg::impl_switchTo( ObjectList _eList )
417 {
418     switch ( _eList )
419     {
420     case Tables:
421         m_aTableList.Show( sal_True );  m_aCaseTables.Check( sal_True );
422         m_aQueryList.Show( sal_False ); m_aCaseQueries.Check( sal_False );
423         m_pCurrentList.reset( new TableListFacade( m_aTableList, m_rContext.getConnection() ) );
424         m_aTableList.GrabFocus();
425         break;
426 
427     case Queries:
428         m_aTableList.Show( sal_False ); m_aCaseTables.Check( sal_False );
429         m_aQueryList.Show( sal_True );  m_aCaseQueries.Check( sal_True );
430         m_pCurrentList.reset( new QueryListFacade( m_aQueryList, m_rContext.getConnection() ) );
431         m_aQueryList.GrabFocus();
432         break;
433     }
434     m_pCurrentList->updateTableObjectList( m_rContext.allowViews() );
435 }
436 
437 //------------------------------------------------------------------------------
Update()438 void OAddTableDlg::Update()
439 {
440     if ( !m_pCurrentList.get() )
441         impl_switchTo( Tables );
442     else
443         m_pCurrentList->updateTableObjectList( m_rContext.allowViews() );
444 }
445 
446 //------------------------------------------------------------------------------
impl_addTable()447 void OAddTableDlg::impl_addTable()
448 {
449     if ( m_pCurrentList->isLeafSelected() )
450     {
451         String sSelectedName, sAliasName;
452         sSelectedName = m_pCurrentList->getSelectedName( sAliasName );
453 
454         m_rContext.addTableWindow( sSelectedName, sAliasName );
455     }
456 }
457 
458 //------------------------------------------------------------------------------
459 IMPL_LINK( OAddTableDlg, AddClickHdl, Button*, /*pButton*/ )
460 {
461     TableListDoubleClickHdl(NULL);
462     return 0;
463 }
464 
465 //------------------------------------------------------------------------------
466 IMPL_LINK( OAddTableDlg, TableListDoubleClickHdl, void*, /*EMPTY_ARG*/ )
467 {
468     if ( impl_isAddAllowed() )
469     {
470         impl_addTable();
471         if ( !impl_isAddAllowed() )
472             Close();
473         return 1L;  // handled
474     }
475 
476     return 0L;  // not handled
477 }
478 
479 //------------------------------------------------------------------------------
480 IMPL_LINK( OAddTableDlg, TableListSelectHdl, void*, /*EMPTY_ARG*/ )
481 {
482     aAddButton.Enable( m_pCurrentList->isLeafSelected() );
483     return 0;
484 }
485 
486 //------------------------------------------------------------------------------
487 IMPL_LINK( OAddTableDlg, CloseClickHdl, Button*, /*pButton*/ )
488 {
489     return Close();
490 }
491 
492 //------------------------------------------------------------------------------
493 IMPL_LINK( OAddTableDlg, OnTypeSelected, void*, /*EMPTY_ARG*/ )
494 {
495     if ( m_aCaseTables.IsChecked() )
496         impl_switchTo( Tables );
497     else
498         impl_switchTo( Queries );
499     return 0;
500 }
501 
502 //------------------------------------------------------------------------------
Close()503 sal_Bool OAddTableDlg::Close()
504 {
505     m_rContext.onWindowClosing( this );
506     return ModelessDialog::Close();
507 }
508 
509 //------------------------------------------------------------------------------
impl_isAddAllowed()510 bool OAddTableDlg::impl_isAddAllowed()
511 {
512     return  m_rContext.allowAddition();
513 }
514 
515 //------------------------------------------------------------------------------
getDialogTitleForContext(IAddTableDialogContext & _rContext)516 String OAddTableDlg::getDialogTitleForContext( IAddTableDialogContext& _rContext )
517 {
518     String sTitle;
519 
520     ::svt::OLocalResourceAccess aLocalRes( ModuleRes( DLG_JOIN_TABADD ), RSC_MODELESSDIALOG );
521     if ( _rContext.allowQueries() )
522         sTitle = String( ModuleRes( STR_ADD_TABLE_OR_QUERY ) );
523     else
524         sTitle = String( ModuleRes( STR_ADD_TABLES ) );
525 
526     return sTitle;
527 }
528 
529 // -----------------------------------------------------------------------------
530