Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / relationdesign / RelationTableView.cxx
blob7a46d965e1b7136789a6faabbf63543183b72117
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 ************************************************************************/
29 #include "RelationTableView.hxx"
30 #include "JoinExchange.hxx"
31 #include <comphelper/extract.hxx>
32 #include "browserids.hxx"
33 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
34 #include <com/sun/star/sdbc/XConnection.hpp>
35 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
36 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
37 #include <com/sun/star/sdbcx/KeyType.hpp>
38 #include <com/sun/star/container/XIndexAccess.hpp>
39 #include <com/sun/star/container/XNameAccess.hpp>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include "dbustrings.hrc"
42 #include <connectivity/dbtools.hxx>
43 #include <comphelper/sequence.hxx>
44 #include <tools/debug.hxx>
45 #include "dbaccess_helpid.hrc"
46 #include "RelationDesignView.hxx"
47 #include "JoinController.hxx"
48 #include "TableWindow.hxx"
49 #include "TableWindowData.hxx"
50 #include "RTableConnection.hxx"
51 #include "RTableConnectionData.hxx"
52 #include "RelationDlg.hxx"
53 #include "sqlmessage.hxx"
54 #include "dbu_rel.hrc"
55 #include "UITools.hxx"
56 #include <connectivity/dbexception.hxx>
57 #include "RTableWindow.hxx"
58 #include "JAccess.hxx"
59 #include <svl/undo.hxx>
60 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
62 using namespace dbaui;
63 using namespace ::dbtools;
64 using namespace ::com::sun::star;
65 using namespace ::com::sun::star::uno;
66 using namespace ::com::sun::star::sdbc;
67 using namespace ::com::sun::star::sdbcx;
68 using namespace ::com::sun::star::beans;
69 using namespace ::com::sun::star::container;
70 using namespace ::com::sun::star::accessibility;
72 //==================================================================
73 // class ORelationTableView
74 //==================================================================
75 DBG_NAME(ORelationTableView)
76 //------------------------------------------------------------------------
77 ORelationTableView::ORelationTableView( Window* pParent, ORelationDesignView* pView )
78 :OJoinTableView( pParent, pView )
79 , ::comphelper::OContainerListener(m_aMutex)
80 ,m_pExistingConnection(NULL)
81 ,m_bInRemove(false)
84 DBG_CTOR(ORelationTableView,NULL);
85 SetHelpId(HID_CTL_RELATIONTAB);
88 //------------------------------------------------------------------------
89 ORelationTableView::~ORelationTableView()
91 DBG_DTOR(ORelationTableView,NULL);
92 if ( m_pContainerListener.is() )
93 m_pContainerListener->dispose();
96 //------------------------------------------------------------------------
97 void ORelationTableView::ReSync()
99 DBG_CHKTHIS(ORelationTableView,NULL);
100 if ( !m_pContainerListener.is() )
102 Reference< XConnection> xConnection = m_pView->getController().getConnection();
103 Reference< XTablesSupplier > xTableSupp( xConnection, UNO_QUERY_THROW );
104 Reference< XNameAccess > xTables = xTableSupp->getTables();
105 Reference< XContainer> xContainer(xTables,uno::UNO_QUERY);
106 if ( xContainer.is() )
107 m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
109 // Es kann sein, dass in der DB Tabellen ausgeblendet wurden, die eigentlich Bestandteil einer Relation sind. Oder eine Tabelle
110 // befand sich im Layout (durchaus ohne Relation), existiert aber nicht mehr. In beiden Faellen wird das Anlegen des TabWins schief
111 // gehen, und alle solchen TabWinDatas oder darauf bezogenen ConnDatas muss ich dann loeschen.
112 ::std::vector< ::rtl::OUString> arrInvalidTables;
114 //////////////////////////////////////////////////////////////////////
115 // create and insert windows
116 TTableWindowData* pTabWinDataList = m_pView->getController().getTableWindowData();
117 TTableWindowData::reverse_iterator aIter = pTabWinDataList->rbegin();
118 for(;aIter != pTabWinDataList->rend();++aIter)
120 TTableWindowData::value_type pData = *aIter;
121 OTableWindow* pTabWin = createWindow(pData);
123 if (!pTabWin->Init())
125 // das Initialisieren ging schief, dass heisst, dieses TabWin steht nicht zur Verfuegung, also muss ich es inklusive
126 // seiner Daten am Dokument aufraeumen
127 pTabWin->clearListBox();
128 delete pTabWin;
129 arrInvalidTables.push_back(pData->GetTableName());
131 pTabWinDataList->erase( ::std::remove(pTabWinDataList->begin(),pTabWinDataList->end(),*aIter) ,pTabWinDataList->end());
132 continue;
135 (*GetTabWinMap())[pData->GetComposedName()] = pTabWin; // am Anfang einfuegen, da ich die DataList ja rueckwaerts durchlaufe
136 // wenn in den Daten keine Position oder Groesse steht -> Default
137 if (!pData->HasPosition() && !pData->HasSize())
138 SetDefaultTabWinPosSize(pTabWin);
140 pTabWin->Show();
143 // Verbindungen einfuegen
144 TTableConnectionData* pTabConnDataList = m_pView->getController().getTableConnectionData();
145 TTableConnectionData::reverse_iterator aConIter = pTabConnDataList->rbegin();
147 for(;aConIter != pTabConnDataList->rend();++aConIter)
149 ORelationTableConnectionData* pTabConnData = static_cast<ORelationTableConnectionData*>(aConIter->get());
150 if ( !arrInvalidTables.empty() )
152 // gibt es die beiden Tabellen zur Connection ?
153 ::rtl::OUString strTabExistenceTest = pTabConnData->getReferencingTable()->GetTableName();
154 sal_Bool bInvalid = ::std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
155 strTabExistenceTest = pTabConnData->getReferencedTable()->GetTableName();
156 bInvalid = bInvalid || ::std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
158 if (bInvalid)
159 { // nein -> Pech gehabt, die Connection faellt weg
160 pTabConnDataList->erase( ::std::remove(pTabConnDataList->begin(),pTabConnDataList->end(),*aConIter),pTabConnDataList->end() );
161 continue;
165 addConnection( new ORelationTableConnection(this, *aConIter), sal_False ); // don't add the data again
168 if ( !GetTabWinMap()->empty() )
169 GetTabWinMap()->begin()->second->GrabFocus();
171 //------------------------------------------------------------------------------
172 sal_Bool ORelationTableView::IsAddAllowed()
174 DBG_CHKTHIS(ORelationTableView,NULL);
176 return !m_pView->getController().isReadOnly();
178 //------------------------------------------------------------------------
179 void ORelationTableView::AddConnection(const OJoinExchangeData& jxdSource, const OJoinExchangeData& jxdDest)
181 DBG_CHKTHIS(ORelationTableView,NULL);
182 // Aus selektierten Feldnamen LineDataObject setzen
183 // check if relation already exists
184 OTableWindow* pSourceWin = jxdSource.pListBox->GetTabWin();
185 OTableWindow* pDestWin = jxdDest.pListBox->GetTabWin();
187 ::std::vector<OTableConnection*>::const_iterator aIter = getTableConnections()->begin();
188 ::std::vector<OTableConnection*>::const_iterator aEnd = getTableConnections()->end();
189 for(;aIter != aEnd;++aIter)
191 OTableConnection* pFirst = *aIter;
192 if((pFirst->GetSourceWin() == pSourceWin && pFirst->GetDestWin() == pDestWin) ||
193 (pFirst->GetSourceWin() == pDestWin && pFirst->GetDestWin() == pSourceWin))
195 m_pExistingConnection = pFirst;
196 break;
199 // insert table connection into view
201 TTableConnectionData::value_type pTabConnData(new ORelationTableConnectionData(pSourceWin->GetData(),
202 pDestWin->GetData()));
204 // die Namen der betroffenen Felder
205 ::rtl::OUString sSourceFieldName = jxdSource.pListBox->GetEntryText(jxdSource.pEntry);
206 ::rtl::OUString sDestFieldName = jxdDest.pListBox->GetEntryText(jxdDest.pEntry);
208 // die Anzahl der PKey-Felder in der Quelle
209 const Reference< XNameAccess> xPrimaryKeyColumns = getPrimaryKeyColumns_throw(pSourceWin->GetData()->getTable());
210 bool bAskUser = xPrimaryKeyColumns.is() && Reference< XIndexAccess>(xPrimaryKeyColumns,UNO_QUERY)->getCount() > 1;
212 pTabConnData->SetConnLine( 0, sSourceFieldName, sDestFieldName );
214 if ( bAskUser || m_pExistingConnection )
215 m_pCurrentlyTabConnData = pTabConnData; // this implies that we ask the user what to do
216 else
220 //////////////////////////////////////////////////////////////////////
221 // Daten der Datenbank uebergeben
222 if( pTabConnData->Update() )
224 //////////////////////////////////////////////////////////////////////
225 // UI-Object in ConnListe eintragen
226 addConnection( new ORelationTableConnection( this, pTabConnData ) );
229 catch(const SQLException&)
231 throw;
233 catch(const Exception&)
235 OSL_FAIL("ORelationTableView::AddConnection: Exception oocured!");
241 //------------------------------------------------------------------------
242 void ORelationTableView::ConnDoubleClicked( OTableConnection* pConnection )
244 DBG_CHKTHIS(ORelationTableView,NULL);
245 ORelationDialog aRelDlg( this, pConnection->GetData() );
246 switch (aRelDlg.Execute())
248 case RET_OK:
249 // successfully updated
250 pConnection->UpdateLineList();
251 // The connection references 1 ConnData and n ConnLines, each ConnData references n LineDatas, each Line exactly 1 LineData
252 // As the Dialog and the ConnData->Update may have changed the LineDatas we have to restore the consistent state
253 break;
255 case RET_NO:
256 // tried at least one update, but did not succeed -> the original connection is lost
257 RemoveConnection( pConnection ,sal_True);
258 break;
260 case RET_CANCEL:
261 // no break, as nothing happened and we don't need the code below
262 return;
266 Invalidate(INVALIDATE_NOCHILDREN);
269 //------------------------------------------------------------------------------
270 void ORelationTableView::AddNewRelation()
272 DBG_CHKTHIS(ORelationTableView,NULL);
274 TTableConnectionData::value_type pNewConnData( new ORelationTableConnectionData() );
275 ORelationDialog aRelDlg(this, pNewConnData, sal_True);
277 sal_Bool bSuccess = (aRelDlg.Execute() == RET_OK);
278 if (bSuccess)
280 // already updated by the dialog
281 // dem Dokument bekanntgeben
282 addConnection( new ORelationTableConnection(this, pNewConnData) );
286 //------------------------------------------------------------------------------
287 bool ORelationTableView::RemoveConnection( OTableConnection* pConn ,sal_Bool /*_bDelete*/)
289 DBG_CHKTHIS(ORelationTableView,NULL);
290 ORelationTableConnectionData* pTabConnData = (ORelationTableConnectionData*)pConn->GetData().get();
293 if ( m_bInRemove || pTabConnData->DropRelation())
294 return OJoinTableView::RemoveConnection( pConn ,sal_True);
296 catch(SQLException& e)
298 getDesignView()->getController().showError(SQLExceptionInfo(e));
300 catch(Exception&)
302 OSL_FAIL("ORelationTableView::RemoveConnection: Something other than SQLException occurred!");
304 return false;
307 //------------------------------------------------------------------------------
308 void ORelationTableView::AddTabWin(const ::rtl::OUString& _rComposedName, const ::rtl::OUString& rWinName, sal_Bool /*bNewTable*/)
310 DBG_CHKTHIS(ORelationTableView,NULL);
311 OSL_ENSURE(!_rComposedName.isEmpty(),"There must be a table name supplied!");
312 OJoinTableView::OTableWindowMap::iterator aIter = GetTabWinMap()->find(_rComposedName);
314 if(aIter != GetTabWinMap()->end())
316 aIter->second->SetZOrder(NULL, WINDOW_ZORDER_FIRST);
317 aIter->second->GrabFocus();
318 EnsureVisible(aIter->second);
319 // no new one
320 return;
323 //////////////////////////////////////////////////////////////////
324 // Neue Datenstruktur in DocShell eintragen
325 TTableWindowData::value_type pNewTabWinData(createTableWindowData( _rComposedName, rWinName,rWinName ));
326 pNewTabWinData->ShowAll(sal_False);
328 //////////////////////////////////////////////////////////////////
329 // Neues Fenster in Fensterliste eintragen
330 OTableWindow* pNewTabWin = createWindow( pNewTabWinData );
331 if(pNewTabWin->Init())
333 m_pView->getController().getTableWindowData()->push_back( pNewTabWinData);
334 // when we already have a table with this name insert the full qualified one instead
335 (*GetTabWinMap())[_rComposedName] = pNewTabWin;
337 SetDefaultTabWinPosSize( pNewTabWin );
338 pNewTabWin->Show();
340 modified();
342 if ( m_pAccessible )
343 m_pAccessible->notifyAccessibleEvent( AccessibleEventId::CHILD,
344 Any(),
345 makeAny(pNewTabWin->GetAccessible()));
347 else
349 pNewTabWin->clearListBox();
350 delete pNewTabWin;
353 // -----------------------------------------------------------------------------
354 void ORelationTableView::RemoveTabWin( OTableWindow* pTabWin )
356 OSQLWarningBox aDlg( this, ModuleRes( STR_QUERY_REL_DELETE_WINDOW ), WB_YES_NO | WB_DEF_YES );
357 if ( m_bInRemove || aDlg.Execute() == RET_YES )
359 m_pView->getController().ClearUndoManager();
360 OJoinTableView::RemoveTabWin( pTabWin );
362 m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
363 m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
364 m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
368 void ORelationTableView::lookForUiActivities()
370 if(m_pExistingConnection)
372 String sTitle(ModuleRes(STR_RELATIONDESIGN));
373 sTitle.Erase(0,3);
374 OSQLMessageBox aDlg(this,ModuleRes(STR_QUERY_REL_EDIT_RELATION),String(),0);
375 aDlg.SetText(sTitle);
376 aDlg.RemoveButton(aDlg.GetButtonId(0));
377 aDlg.AddButton( ModuleRes(STR_QUERY_REL_EDIT), BUTTONID_OK, BUTTONDIALOG_DEFBUTTON | BUTTONDIALOG_FOCUSBUTTON);
378 aDlg.AddButton( ModuleRes(STR_QUERY_REL_CREATE), BUTTONID_YES, 0);
379 aDlg.AddButton(BUTTON_CANCEL,BUTTONID_CANCEL,0);
380 sal_uInt16 nRet = aDlg.Execute();
381 if( nRet == RET_CANCEL)
383 m_pCurrentlyTabConnData.reset();
385 else if ( nRet == RET_OK ) // EDIT
387 ConnDoubleClicked(m_pExistingConnection);
388 m_pCurrentlyTabConnData.reset();
390 m_pExistingConnection = NULL;
392 if(m_pCurrentlyTabConnData)
394 ORelationDialog aRelDlg( this, m_pCurrentlyTabConnData );
395 if (aRelDlg.Execute() == RET_OK)
397 // already updated by the dialog
398 addConnection( new ORelationTableConnection( this, m_pCurrentlyTabConnData ) );
400 m_pCurrentlyTabConnData.reset();
404 // -----------------------------------------------------------------------------
405 OTableWindow* ORelationTableView::createWindow(const TTableWindowData::value_type& _pData)
407 return new ORelationTableWindow(this,_pData);
409 // -----------------------------------------------------------------------------
410 bool ORelationTableView::allowQueries() const
412 return false;
414 // -----------------------------------------------------------------------------
415 void ORelationTableView::_elementInserted( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException)
419 // -----------------------------------------------------------------------------
420 void ORelationTableView::_elementRemoved( const container::ContainerEvent& _rEvent ) throw(::com::sun::star::uno::RuntimeException)
422 m_bInRemove = true;
423 ::rtl::OUString sName;
424 if ( _rEvent.Accessor >>= sName )
426 OTableWindow* pTableWindow = GetTabWindow(sName);
427 if ( pTableWindow )
429 m_pView->getController().ClearUndoManager();
430 OJoinTableView::RemoveTabWin( pTableWindow );
432 m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
433 m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
434 m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
437 m_bInRemove = false;
439 // -----------------------------------------------------------------------------
440 void ORelationTableView::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException)
444 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */