update dev300-m58
[ooovba.git] / dbaccess / source / ui / relationdesign / RelationTableView.cxx
blobfd47db6d26da9174af51efd8b6d938e547470eb8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: RelationTableView.cxx,v $
10 * $Revision: 1.30.26.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
34 #include "RelationTableView.hxx"
37 #include "JoinExchange.hxx"
40 #include <comphelper/extract.hxx>
43 #include "browserids.hxx"
46 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
49 #include <com/sun/star/sdbc/XConnection.hpp>
52 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
55 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
58 #include <com/sun/star/sdbcx/KeyType.hpp>
60 #include <com/sun/star/container/XIndexAccess.hpp>
61 #include <com/sun/star/container/XNameAccess.hpp>
62 #include <com/sun/star/beans/XPropertySet.hpp>
63 #include "dbustrings.hrc"
64 #include <connectivity/dbtools.hxx>
65 #include <comphelper/sequence.hxx>
66 #include <tools/debug.hxx>
67 #include "dbaccess_helpid.hrc"
68 #include "RelationDesignView.hxx"
69 #include "JoinController.hxx"
70 #include "TableWindow.hxx"
71 #include "TableWindowData.hxx"
72 #include "RTableConnection.hxx"
73 #include "RTableConnectionData.hxx"
74 #include "RelationDlg.hxx"
75 #include "sqlmessage.hxx"
76 #include "dbu_rel.hrc"
77 #include "UITools.hxx"
78 #include <connectivity/dbexception.hxx>
79 #include "RTableWindow.hxx"
80 #include "JAccess.hxx"
81 #include <svtools/undo.hxx>
82 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
84 using namespace dbaui;
85 using namespace ::dbtools;
86 using namespace ::com::sun::star;
87 using namespace ::com::sun::star::uno;
88 using namespace ::com::sun::star::sdbc;
89 using namespace ::com::sun::star::sdbcx;
90 using namespace ::com::sun::star::beans;
91 using namespace ::com::sun::star::container;
92 using namespace ::com::sun::star::accessibility;
94 //==================================================================
95 // class ORelationTableView
96 //==================================================================
97 DBG_NAME(ORelationTableView)
98 //------------------------------------------------------------------------
99 ORelationTableView::ORelationTableView( Window* pParent, ORelationDesignView* pView )
100 :OJoinTableView( pParent, pView )
101 , ::comphelper::OContainerListener(m_aMutex)
102 ,m_pExistingConnection(NULL)
103 ,m_bInRemove(false)
106 DBG_CTOR(ORelationTableView,NULL);
107 SetHelpId(HID_CTL_RELATIONTAB);
110 //------------------------------------------------------------------------
111 ORelationTableView::~ORelationTableView()
113 DBG_DTOR(ORelationTableView,NULL);
114 if ( m_pContainerListener.is() )
115 m_pContainerListener->dispose();
118 //------------------------------------------------------------------------
119 void ORelationTableView::ReSync()
121 DBG_CHKTHIS(ORelationTableView,NULL);
122 if ( !m_pContainerListener.is() )
124 Reference< XConnection> xConnection = m_pView->getController().getConnection();
125 Reference< XTablesSupplier > xTableSupp( xConnection, UNO_QUERY_THROW );
126 Reference< XNameAccess > xTables = xTableSupp->getTables();
127 Reference< XContainer> xContainer(xTables,uno::UNO_QUERY);
128 if ( xContainer.is() )
129 m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
131 // Es kann sein, dass in der DB Tabellen ausgeblendet wurden, die eigentlich Bestandteil einer Relation sind. Oder eine Tabelle
132 // befand sich im Layout (durchaus ohne Relation), existiert aber nicht mehr. In beiden Faellen wird das Anlegen des TabWins schief
133 // gehen, und alle solchen TabWinDatas oder darauf bezogenen ConnDatas muss ich dann loeschen.
134 ::std::vector< ::rtl::OUString> arrInvalidTables;
136 //////////////////////////////////////////////////////////////////////
137 // create and insert windows
138 TTableWindowData* pTabWinDataList = m_pView->getController().getTableWindowData();
139 TTableWindowData::reverse_iterator aIter = pTabWinDataList->rbegin();
140 for(;aIter != pTabWinDataList->rend();++aIter)
142 TTableWindowData::value_type pData = *aIter;
143 OTableWindow* pTabWin = createWindow(pData);
145 if (!pTabWin->Init())
147 // das Initialisieren ging schief, dass heisst, dieses TabWin steht nicht zur Verfuegung, also muss ich es inklusive
148 // seiner Daten am Dokument aufraeumen
149 pTabWin->clearListBox();
150 delete pTabWin;
151 arrInvalidTables.push_back(pData->GetTableName());
153 pTabWinDataList->erase( ::std::remove(pTabWinDataList->begin(),pTabWinDataList->end(),*aIter) ,pTabWinDataList->end());
154 continue;
157 (*GetTabWinMap())[pData->GetComposedName()] = pTabWin; // am Anfang einfuegen, da ich die DataList ja rueckwaerts durchlaufe
158 // wenn in den Daten keine Position oder Groesse steht -> Default
159 if (!pData->HasPosition() && !pData->HasSize())
160 SetDefaultTabWinPosSize(pTabWin);
162 pTabWin->Show();
165 // Verbindungen einfuegen
166 TTableConnectionData* pTabConnDataList = m_pView->getController().getTableConnectionData();
167 TTableConnectionData::reverse_iterator aConIter = pTabConnDataList->rbegin();
169 for(;aConIter != pTabConnDataList->rend();++aConIter)
171 ORelationTableConnectionData* pTabConnData = static_cast<ORelationTableConnectionData*>(aConIter->get());
172 if ( !arrInvalidTables.empty() )
174 // gibt es die beiden Tabellen zur Connection ?
175 ::rtl::OUString strTabExistenceTest = pTabConnData->getReferencingTable()->GetTableName();
176 sal_Bool bInvalid = ::std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
177 strTabExistenceTest = pTabConnData->getReferencedTable()->GetTableName();
178 bInvalid = bInvalid || ::std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
180 if (bInvalid)
181 { // nein -> Pech gehabt, die Connection faellt weg
182 pTabConnDataList->erase( ::std::remove(pTabConnDataList->begin(),pTabConnDataList->end(),*aConIter),pTabConnDataList->end() );
183 continue;
185 } // if ( !arrInvalidTables.empty() )
187 addConnection( new ORelationTableConnection(this, *aConIter), sal_False ); // don't add the data again
190 if ( !GetTabWinMap()->empty() )
191 GetTabWinMap()->begin()->second->GrabFocus();
193 //------------------------------------------------------------------------------
194 BOOL ORelationTableView::IsAddAllowed()
196 DBG_CHKTHIS(ORelationTableView,NULL);
198 return !m_pView->getController().isReadOnly();
200 //------------------------------------------------------------------------
201 void ORelationTableView::AddConnection(const OJoinExchangeData& jxdSource, const OJoinExchangeData& jxdDest)
203 DBG_CHKTHIS(ORelationTableView,NULL);
204 // Aus selektierten Feldnamen LineDataObject setzen
205 // check if relation already exists
206 OTableWindow* pSourceWin = jxdSource.pListBox->GetTabWin();
207 OTableWindow* pDestWin = jxdDest.pListBox->GetTabWin();
209 ::std::vector<OTableConnection*>::const_iterator aIter = getTableConnections()->begin();
210 ::std::vector<OTableConnection*>::const_iterator aEnd = getTableConnections()->end();
211 for(;aIter != aEnd;++aIter)
213 OTableConnection* pFirst = *aIter;
214 if((pFirst->GetSourceWin() == pSourceWin && pFirst->GetDestWin() == pDestWin) ||
215 (pFirst->GetSourceWin() == pDestWin && pFirst->GetDestWin() == pSourceWin))
217 m_pExistingConnection = pFirst;
218 break;
221 // insert table connection into view
223 TTableConnectionData::value_type pTabConnData(new ORelationTableConnectionData(pSourceWin->GetData(),
224 pDestWin->GetData()));
226 // die Namen der betroffenen Felder
227 ::rtl::OUString sSourceFieldName = jxdSource.pListBox->GetEntryText(jxdSource.pEntry);
228 ::rtl::OUString sDestFieldName = jxdDest.pListBox->GetEntryText(jxdDest.pEntry);
230 // die Anzahl der PKey-Felder in der Quelle
232 ::std::vector< Reference< XNameAccess> > aPkeys = ::dbaui::getKeyColumns(pSourceWin->GetData()->getKeys(),KeyType::PRIMARY);
233 bool bAskUser = aPkeys.size() == 1 && Reference< XIndexAccess>(aPkeys[0],UNO_QUERY)->getCount() > 1;
235 pTabConnData->SetConnLine( 0, sSourceFieldName, sDestFieldName );
237 if ( bAskUser || m_pExistingConnection )
238 m_pCurrentlyTabConnData = pTabConnData; // this implies that we ask the user what to do
239 else
243 //////////////////////////////////////////////////////////////////////
244 // Daten der Datenbank uebergeben
245 if( pTabConnData->Update() )
247 //////////////////////////////////////////////////////////////////////
248 // UI-Object in ConnListe eintragen
249 addConnection( new ORelationTableConnection( this, pTabConnData ) );
252 catch(const SQLException&)
254 throw;
256 catch(const Exception&)
258 OSL_ENSURE(0,"ORelationTableView::AddConnection: Exception oocured!");
264 //------------------------------------------------------------------------
265 void ORelationTableView::ConnDoubleClicked( OTableConnection* pConnection )
267 DBG_CHKTHIS(ORelationTableView,NULL);
268 ORelationDialog aRelDlg( this, pConnection->GetData() );
269 switch (aRelDlg.Execute())
271 case RET_OK:
272 // successfully updated
273 pConnection->UpdateLineList();
274 // The connection references 1 ConnData and n ConnLines, each ConnData references n LineDatas, each Line exactly 1 LineData
275 // As the Dialog and the ConnData->Update may have changed the LineDatas we have to restore the consistent state
276 break;
278 case RET_NO:
279 // tried at least one update, but did not succeed -> the original connection is lost
280 RemoveConnection( pConnection ,sal_True);
281 break;
283 case RET_CANCEL:
284 // no break, as nothing happened and we don't need the code below
285 return;
289 Invalidate(INVALIDATE_NOCHILDREN);
292 //------------------------------------------------------------------------------
293 void ORelationTableView::AddNewRelation()
295 DBG_CHKTHIS(ORelationTableView,NULL);
297 TTableConnectionData::value_type pNewConnData( new ORelationTableConnectionData() );
298 ORelationDialog aRelDlg(this, pNewConnData, TRUE);
300 BOOL bSuccess = (aRelDlg.Execute() == RET_OK);
301 if (bSuccess)
303 // already updated by the dialog
304 // dem Dokument bekanntgeben
305 addConnection( new ORelationTableConnection(this, pNewConnData) );
309 //------------------------------------------------------------------------------
310 bool ORelationTableView::RemoveConnection( OTableConnection* pConn ,sal_Bool /*_bDelete*/)
312 DBG_CHKTHIS(ORelationTableView,NULL);
313 ORelationTableConnectionData* pTabConnData = (ORelationTableConnectionData*)pConn->GetData().get();
316 if ( m_bInRemove || pTabConnData->DropRelation())
317 return OJoinTableView::RemoveConnection( pConn ,sal_True);
319 catch(SQLException& e)
321 getDesignView()->getController().showError(SQLExceptionInfo(e));
323 catch(Exception&)
325 OSL_ENSURE(0,"ORelationTableView::RemoveConnection: Something other than SQLException occured!");
327 return false;
330 //------------------------------------------------------------------------------
331 void ORelationTableView::AddTabWin(const ::rtl::OUString& _rComposedName, const ::rtl::OUString& rWinName, BOOL /*bNewTable*/)
333 DBG_CHKTHIS(ORelationTableView,NULL);
334 OSL_ENSURE(_rComposedName.getLength(),"There must be a table name supplied!");
335 OJoinTableView::OTableWindowMap::iterator aIter = GetTabWinMap()->find(_rComposedName);
337 if(aIter != GetTabWinMap()->end())
339 aIter->second->SetZOrder(NULL, WINDOW_ZORDER_FIRST);
340 aIter->second->GrabFocus();
341 EnsureVisible(aIter->second);
342 // no new one
343 return;
346 //////////////////////////////////////////////////////////////////
347 // Neue Datenstruktur in DocShell eintragen
348 TTableWindowData::value_type pNewTabWinData(createTableWindowData( _rComposedName, rWinName,rWinName ));
349 pNewTabWinData->ShowAll(FALSE);
351 //////////////////////////////////////////////////////////////////
352 // Neues Fenster in Fensterliste eintragen
353 OTableWindow* pNewTabWin = createWindow( pNewTabWinData );
354 if(pNewTabWin->Init())
356 m_pView->getController().getTableWindowData()->push_back( pNewTabWinData);
357 // when we already have a table with this name insert the full qualified one instead
358 (*GetTabWinMap())[_rComposedName] = pNewTabWin;
360 SetDefaultTabWinPosSize( pNewTabWin );
361 pNewTabWin->Show();
363 modified();
365 if ( m_pAccessible )
366 m_pAccessible->notifyAccessibleEvent( AccessibleEventId::CHILD,
367 Any(),
368 makeAny(pNewTabWin->GetAccessible()));
370 else
372 pNewTabWin->clearListBox();
373 delete pNewTabWin;
376 // -----------------------------------------------------------------------------
377 void ORelationTableView::RemoveTabWin( OTableWindow* pTabWin )
379 OSQLWarningBox aDlg( this, ModuleRes( STR_QUERY_REL_DELETE_WINDOW ), WB_YES_NO | WB_DEF_YES );
380 if ( m_bInRemove || aDlg.Execute() == RET_YES )
382 m_pView->getController().getUndoMgr()->Clear();
383 OJoinTableView::RemoveTabWin( pTabWin );
385 m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
386 m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
387 m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
390 // -----------------------------------------------------------------------------
391 //namespace
393 // class OReleationAskDialog : public ButtonDialog
394 // {
395 // FixedImage m_aInfoImage;
396 // FixedText m_aTitle;
397 // FixedText m_aMessage;
398 // public:
399 // OReleationDialog(Window* _pParent) : ButtonDialog(_pParent,WB_HORZ | WB_STDDIALOG)
400 // ,m_aInfoImage(this)
401 // ,m_aTitle(this,WB_WORDBREAK | WB_LEFT)
402 // ,m_aMessage(this,WB_WORDBREAK | WB_LEFT)
403 // {
404 // m_aMessage.SetText(ModuleRes(STR_QUERY_REL_EDIT_RELATION));
405 // m_aMessage.Show();
407 // // Changed as per BugID 79541 Branding/Configuration
408 // String sDialogTitle( lcl_getProductName() );
409 // SetText( sDialogTitle.AppendAscii( " Base" ) );
410 // m_aTitle.Show();
411 // }
412 // };
414 // -----------------------------------------------------------------------------
415 void ORelationTableView::lookForUiActivities()
417 if(m_pExistingConnection)
419 String sTitle(ModuleRes(STR_RELATIONDESIGN));
420 sTitle.Erase(0,3);
421 OSQLMessageBox aDlg(this,ModuleRes(STR_QUERY_REL_EDIT_RELATION),String(),0);
422 aDlg.SetText(sTitle);
423 aDlg.RemoveButton(aDlg.GetButtonId(0));
424 aDlg.AddButton( ModuleRes(STR_QUERY_REL_EDIT), BUTTONID_OK, BUTTONDIALOG_DEFBUTTON | BUTTONDIALOG_FOCUSBUTTON);
425 aDlg.AddButton( ModuleRes(STR_QUERY_REL_CREATE), BUTTONID_YES, 0);
426 aDlg.AddButton(BUTTON_CANCEL,BUTTONID_CANCEL,0);
427 UINT16 nRet = aDlg.Execute();
428 if( nRet == RET_CANCEL)
430 m_pCurrentlyTabConnData.reset();
432 else if ( nRet == RET_OK ) // EDIT
434 ConnDoubleClicked(m_pExistingConnection);
435 m_pCurrentlyTabConnData.reset();
437 m_pExistingConnection = NULL;
439 if(m_pCurrentlyTabConnData)
441 ORelationDialog aRelDlg( this, m_pCurrentlyTabConnData );
442 if (aRelDlg.Execute() == RET_OK)
444 // already updated by the dialog
445 addConnection( new ORelationTableConnection( this, m_pCurrentlyTabConnData ) );
447 m_pCurrentlyTabConnData.reset();
451 // -----------------------------------------------------------------------------
452 OTableWindow* ORelationTableView::createWindow(const TTableWindowData::value_type& _pData)
454 return new ORelationTableWindow(this,_pData);
456 // -----------------------------------------------------------------------------
457 bool ORelationTableView::allowQueries() const
459 return false;
461 // -----------------------------------------------------------------------------
462 void ORelationTableView::_elementInserted( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException)
466 // -----------------------------------------------------------------------------
467 void ORelationTableView::_elementRemoved( const container::ContainerEvent& _rEvent ) throw(::com::sun::star::uno::RuntimeException)
469 m_bInRemove = true;
470 ::rtl::OUString sName;
471 if ( _rEvent.Accessor >>= sName )
473 OTableWindow* pTableWindow = GetTabWindow(sName);
474 if ( pTableWindow )
476 m_pView->getController().getUndoMgr()->Clear();
477 OJoinTableView::RemoveTabWin( pTableWindow );
479 m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
480 m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
481 m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
483 } // if ( _rEvent.Accessor >>= sName )
484 m_bInRemove = false;
486 // -----------------------------------------------------------------------------
487 void ORelationTableView::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException)