fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / dbaccess / source / ui / relationdesign / RelationTableView.cxx
blob71efb0999d33e176e8943f596518b4d475fcb8b6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "RelationTableView.hxx"
21 #include "JoinExchange.hxx"
22 #include <comphelper/extract.hxx>
23 #include "browserids.hxx"
24 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
25 #include <com/sun/star/sdbc/XConnection.hpp>
26 #include <com/sun/star/sdbcx/XKeysSupplier.hpp>
27 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
28 #include <com/sun/star/sdbcx/KeyType.hpp>
29 #include <com/sun/star/container/XIndexAccess.hpp>
30 #include <com/sun/star/container/XNameAccess.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include "dbustrings.hrc"
33 #include <connectivity/dbtools.hxx>
34 #include <comphelper/sequence.hxx>
35 #include <tools/debug.hxx>
36 #include "dbaccess_helpid.hrc"
37 #include "RelationDesignView.hxx"
38 #include "JoinController.hxx"
39 #include "TableWindow.hxx"
40 #include "TableWindowData.hxx"
41 #include "RTableConnection.hxx"
42 #include "RTableConnectionData.hxx"
43 #include "RelationDlg.hxx"
44 #include "sqlmessage.hxx"
45 #include "dbu_rel.hrc"
46 #include "UITools.hxx"
47 #include <connectivity/dbexception.hxx>
48 #include "RTableWindow.hxx"
49 #include "JAccess.hxx"
50 #include <svl/undo.hxx>
51 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
53 using namespace dbaui;
54 using namespace ::dbtools;
55 using namespace ::com::sun::star;
56 using namespace ::com::sun::star::uno;
57 using namespace ::com::sun::star::sdbc;
58 using namespace ::com::sun::star::sdbcx;
59 using namespace ::com::sun::star::beans;
60 using namespace ::com::sun::star::container;
61 using namespace ::com::sun::star::accessibility;
63 // class ORelationTableView
64 ORelationTableView::ORelationTableView( vcl::Window* pParent, ORelationDesignView* pView )
65 :OJoinTableView( pParent, pView )
66 , ::comphelper::OContainerListener(m_aMutex)
67 ,m_pExistingConnection(NULL)
68 ,m_bInRemove(false)
71 SetHelpId(HID_CTL_RELATIONTAB);
74 ORelationTableView::~ORelationTableView()
76 disposeOnce();
79 void ORelationTableView::dispose()
81 if ( m_pContainerListener.is() )
82 m_pContainerListener->dispose();
83 m_pExistingConnection.clear();
84 OJoinTableView::dispose();
87 void ORelationTableView::ReSync()
89 if ( !m_pContainerListener.is() )
91 Reference< XConnection> xConnection = m_pView->getController().getConnection();
92 Reference< XTablesSupplier > xTableSupp( xConnection, UNO_QUERY_THROW );
93 Reference< XNameAccess > xTables = xTableSupp->getTables();
94 Reference< XContainer> xContainer(xTables,uno::UNO_QUERY);
95 if ( xContainer.is() )
96 m_pContainerListener = new ::comphelper::OContainerListenerAdapter(this,xContainer);
98 // Tables could have been hidden in the database, which are part of a relation. Or a table was in layout
99 // (quite often without a relation) and does not exist anymore. In both cases creation of TabWins will fail
100 // and all TabWinDatas and related ConnDates should be deleted.
101 ::std::vector< OUString> arrInvalidTables;
103 // create and insert windows
104 TTableWindowData& rTabWinDataList = m_pView->getController().getTableWindowData();
105 TTableWindowData::reverse_iterator aIter = rTabWinDataList.rbegin();
106 for(;aIter != rTabWinDataList.rend();++aIter)
108 TTableWindowData::value_type pData = *aIter;
109 VclPtr<OTableWindow> pTabWin = createWindow(pData);
111 if (!pTabWin->Init())
113 // initialisation failed, which means this TabWin is not available, therefore,
114 // it should be cleaned up, including its data in the document
115 pTabWin->clearListBox();
116 pTabWin.disposeAndClear();
117 arrInvalidTables.push_back(pData->GetTableName());
119 rTabWinDataList.erase( ::std::remove(rTabWinDataList.begin(), rTabWinDataList.end(), *aIter), rTabWinDataList.end());
120 continue;
123 GetTabWinMap()[pData->GetComposedName()] = pTabWin; // insert at the beginning, as the Datalist is walked through backward
124 // if there's no position or size contained in the data -> Default
125 if (!pData->HasPosition() && !pData->HasSize())
126 SetDefaultTabWinPosSize(pTabWin);
128 pTabWin->Show();
131 // insert connection
132 TTableConnectionData& rTabConnDataList = m_pView->getController().getTableConnectionData();
133 TTableConnectionData::reverse_iterator aConIter = rTabConnDataList.rbegin();
135 for(;aConIter != rTabConnDataList.rend();++aConIter)
137 ORelationTableConnectionData* pTabConnData = static_cast<ORelationTableConnectionData*>(aConIter->get());
138 if ( !arrInvalidTables.empty() )
140 // do the tables to the connection exist?
141 OUString strTabExistenceTest = pTabConnData->getReferencingTable()->GetTableName();
142 bool bInvalid = ::std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
143 strTabExistenceTest = pTabConnData->getReferencedTable()->GetTableName();
144 bInvalid = bInvalid || ::std::find(arrInvalidTables.begin(),arrInvalidTables.end(),strTabExistenceTest) != arrInvalidTables.end();
146 if (bInvalid)
148 // no -> bad luck, the connection is gone
149 rTabConnDataList.erase( ::std::remove(rTabConnDataList.begin(), rTabConnDataList.end(), *aConIter), rTabConnDataList.end() );
150 continue;
154 addConnection( VclPtr<ORelationTableConnection>::Create(this, *aConIter), false );
157 if ( !GetTabWinMap().empty() )
158 GetTabWinMap().begin()->second->GrabFocus();
161 bool ORelationTableView::IsAddAllowed()
164 return !m_pView->getController().isReadOnly();
167 void ORelationTableView::AddConnection(const OJoinExchangeData& jxdSource, const OJoinExchangeData& jxdDest)
169 // Set LineDataObject based on selected fieldname
170 // check if relation already exists
171 OTableWindow* pSourceWin = jxdSource.pListBox->GetTabWin();
172 OTableWindow* pDestWin = jxdDest.pListBox->GetTabWin();
174 auto aIter = getTableConnections().begin();
175 auto aEnd = getTableConnections().end();
176 for(;aIter != aEnd;++aIter)
178 OTableConnection* pFirst = *aIter;
179 if((pFirst->GetSourceWin() == pSourceWin && pFirst->GetDestWin() == pDestWin) ||
180 (pFirst->GetSourceWin() == pDestWin && pFirst->GetDestWin() == pSourceWin))
182 m_pExistingConnection = pFirst;
183 break;
186 // insert table connection into view
187 TTableConnectionData::value_type pTabConnData(new ORelationTableConnectionData(pSourceWin->GetData(),
188 pDestWin->GetData()));
190 // the names of the affected fields
191 OUString sSourceFieldName = jxdSource.pListBox->GetEntryText(jxdSource.pEntry);
192 OUString sDestFieldName = jxdDest.pListBox->GetEntryText(jxdDest.pEntry);
194 // the number of PKey-Fields in the source
195 const Reference< XNameAccess> xPrimaryKeyColumns = getPrimaryKeyColumns_throw(pSourceWin->GetData()->getTable());
196 bool bAskUser = xPrimaryKeyColumns.is() && Reference< XIndexAccess>(xPrimaryKeyColumns,UNO_QUERY)->getCount() > 1;
198 pTabConnData->SetConnLine( 0, sSourceFieldName, sDestFieldName );
200 if ( bAskUser || m_pExistingConnection )
201 m_pCurrentlyTabConnData = pTabConnData; // this implies that we ask the user what to do
202 else
206 // hand over data to the database
207 if( pTabConnData->Update() )
209 // enter UI-object into ConnList
210 addConnection( VclPtr<ORelationTableConnection>::Create( this, pTabConnData ) );
213 catch(const SQLException&)
215 throw;
217 catch(const Exception&)
219 OSL_FAIL("ORelationTableView::AddConnection: Exception oocured!");
224 void ORelationTableView::ConnDoubleClicked( OTableConnection* pConnection )
226 ScopedVclPtrInstance< ORelationDialog > aRelDlg( this, pConnection->GetData() );
227 switch (aRelDlg->Execute())
229 case RET_OK:
230 // successfully updated
231 pConnection->UpdateLineList();
232 // The connection references 1 ConnData and n ConnLines, each ConnData references n LineDatas, each Line exactly 1 LineData
233 // As the Dialog and the ConnData->Update may have changed the LineDatas we have to restore the consistent state
234 break;
236 case RET_NO:
237 // tried at least one update, but did not succeed -> the original connection is lost
238 RemoveConnection( pConnection ,true);
239 break;
241 case RET_CANCEL:
242 // no break, as nothing happened and we don't need the code below
243 return;
247 Invalidate(INVALIDATE_NOCHILDREN);
250 void ORelationTableView::AddNewRelation()
253 TTableConnectionData::value_type pNewConnData( new ORelationTableConnectionData() );
254 ScopedVclPtrInstance< ORelationDialog > aRelDlg(this, pNewConnData, true);
256 bool bSuccess = (aRelDlg->Execute() == RET_OK);
257 if (bSuccess)
259 // already updated by the dialog
260 // announce it to the document
261 addConnection( VclPtr<ORelationTableConnection>::Create(this, pNewConnData) );
265 bool ORelationTableView::RemoveConnection( OTableConnection* pConn ,bool /*_bDelete*/)
267 ORelationTableConnectionData* pTabConnData = static_cast<ORelationTableConnectionData*>(pConn->GetData().get());
270 if ( m_bInRemove || pTabConnData->DropRelation())
271 return OJoinTableView::RemoveConnection( pConn ,true);
273 catch(SQLException& e)
275 getDesignView()->getController().showError(SQLExceptionInfo(e));
277 catch(Exception&)
279 OSL_FAIL("ORelationTableView::RemoveConnection: Something other than SQLException occurred!");
281 return false;
284 void ORelationTableView::AddTabWin(const OUString& _rComposedName, const OUString& rWinName, bool /*bNewTable*/)
286 OSL_ENSURE(!_rComposedName.isEmpty(),"There must be a table name supplied!");
287 OJoinTableView::OTableWindowMap::iterator aIter = GetTabWinMap().find(_rComposedName);
289 if(aIter != GetTabWinMap().end())
291 aIter->second->SetZOrder(NULL, WINDOW_ZORDER_FIRST);
292 aIter->second->GrabFocus();
293 EnsureVisible(aIter->second);
294 // no new one
295 return;
298 // enter the new data structure into DocShell
299 TTableWindowData::value_type pNewTabWinData(createTableWindowData( _rComposedName, rWinName,rWinName ));
300 pNewTabWinData->ShowAll(false);
302 // link new window into the window list
303 VclPtr<OTableWindow> pNewTabWin = createWindow( pNewTabWinData );
304 if(pNewTabWin->Init())
306 m_pView->getController().getTableWindowData().push_back( pNewTabWinData);
307 // when we already have a table with this name insert the full qualified one instead
308 GetTabWinMap()[_rComposedName] = pNewTabWin;
310 SetDefaultTabWinPosSize( pNewTabWin );
311 pNewTabWin->Show();
313 modified();
315 if ( m_pAccessible )
316 m_pAccessible->notifyAccessibleEvent( AccessibleEventId::CHILD,
317 Any(),
318 makeAny(pNewTabWin->GetAccessible()));
320 else
322 pNewTabWin->clearListBox();
323 pNewTabWin.disposeAndClear();
327 void ORelationTableView::RemoveTabWin( OTableWindow* pTabWin )
329 ScopedVclPtrInstance< OSQLWarningBox > aDlg( this, ModuleRes( STR_QUERY_REL_DELETE_WINDOW ), WB_YES_NO | WB_DEF_YES );
330 if ( m_bInRemove || aDlg->Execute() == RET_YES )
332 m_pView->getController().ClearUndoManager();
333 OJoinTableView::RemoveTabWin( pTabWin );
335 m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
336 m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
337 m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
341 void ORelationTableView::lookForUiActivities()
343 if(m_pExistingConnection)
345 OUString sTitle(ModuleRes(STR_RELATIONDESIGN));
346 sTitle = sTitle.copy(3);
347 ScopedVclPtrInstance< OSQLMessageBox > aDlg(this,ModuleRes(STR_QUERY_REL_EDIT_RELATION),OUString(),0);
348 aDlg->SetText(sTitle);
349 aDlg->RemoveButton(aDlg->GetButtonId(0));
350 aDlg->AddButton( ModuleRes(STR_QUERY_REL_EDIT), RET_OK, ButtonDialogFlags::Default | ButtonDialogFlags::Focus);
351 aDlg->AddButton( ModuleRes(STR_QUERY_REL_CREATE), RET_YES);
352 aDlg->AddButton( StandardButtonType::Cancel,RET_CANCEL);
353 sal_uInt16 nRet = aDlg->Execute();
354 if( nRet == RET_CANCEL)
356 m_pCurrentlyTabConnData.reset();
358 else if ( nRet == RET_OK ) // EDIT
360 ConnDoubleClicked(m_pExistingConnection);
361 m_pCurrentlyTabConnData.reset();
363 m_pExistingConnection = NULL;
365 if(m_pCurrentlyTabConnData)
367 ScopedVclPtrInstance< ORelationDialog > aRelDlg( this, m_pCurrentlyTabConnData );
368 if (aRelDlg->Execute() == RET_OK)
370 // already updated by the dialog
371 addConnection( VclPtr<ORelationTableConnection>::Create( this, m_pCurrentlyTabConnData ) );
373 m_pCurrentlyTabConnData.reset();
377 VclPtr<OTableWindow> ORelationTableView::createWindow(const TTableWindowData::value_type& _pData)
379 return VclPtr<ORelationTableWindow>::Create(this,_pData);
382 bool ORelationTableView::allowQueries() const
384 return false;
387 void ORelationTableView::_elementInserted( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException, std::exception)
392 void ORelationTableView::_elementRemoved( const container::ContainerEvent& _rEvent ) throw(::com::sun::star::uno::RuntimeException, std::exception)
394 m_bInRemove = true;
395 OUString sName;
396 if ( _rEvent.Accessor >>= sName )
398 OTableWindow* pTableWindow = GetTabWindow(sName);
399 if ( pTableWindow )
401 m_pView->getController().ClearUndoManager();
402 OJoinTableView::RemoveTabWin( pTableWindow );
404 m_pView->getController().InvalidateFeature(SID_RELATION_ADD_RELATION);
405 m_pView->getController().InvalidateFeature(ID_BROWSER_UNDO);
406 m_pView->getController().InvalidateFeature(ID_BROWSER_REDO);
409 m_bInRemove = false;
412 void ORelationTableView::_elementReplaced( const container::ContainerEvent& /*_rEvent*/ ) throw(::com::sun::star::uno::RuntimeException, std::exception)
416 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */