1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <browserids.hxx>
21 #include <com/sun/star/sdbc/XRow.hpp>
22 #include <connectivity/dbexception.hxx>
23 #include <com/sun/star/ui/dialogs/XExecutableDialog.hpp>
24 #include <JoinController.hxx>
25 #include <TableWindowData.hxx>
26 #include <TableWindow.hxx>
27 #include <TableConnectionData.hxx>
28 #include <adtabdlg.hxx>
29 #include <vcl/svapp.hxx>
30 #include <osl/mutex.hxx>
31 #include <osl/diagnose.h>
33 using namespace ::com::sun::star::uno
;
34 using namespace ::com::sun::star::io
;
35 using namespace ::com::sun::star::beans
;
36 using namespace ::com::sun::star::frame
;
37 using namespace ::com::sun::star::container
;
38 using namespace ::com::sun::star::sdbc
;
39 using namespace ::dbtools
;
40 using namespace ::comphelper
;
45 // AddTableDialogContext
46 class AddTableDialogContext
: public IAddTableDialogContext
48 OJoinController
& m_rController
;
51 explicit AddTableDialogContext( OJoinController
& _rController
)
52 :m_rController( _rController
)
56 virtual ~AddTableDialogContext() {}
58 // IAddTableDialogContext
59 virtual css::uno::Reference
< css::sdbc::XConnection
>
60 getConnection() const override
;
61 virtual bool allowViews() const override
;
62 virtual bool allowQueries() const override
;
63 virtual bool allowAddition() const override
;
64 virtual void addTableWindow( const OUString
& _rQualifiedTableName
, const OUString
& _rAliasName
) override
;
65 virtual void onWindowClosing() override
;
68 OJoinTableView
* getTableView() const;
71 Reference
< XConnection
> AddTableDialogContext::getConnection() const
73 return m_rController
.getConnection();
76 bool AddTableDialogContext::allowViews() const
78 return m_rController
.allowViews();
81 bool AddTableDialogContext::allowQueries() const
83 return m_rController
.allowQueries();
86 bool AddTableDialogContext::allowAddition() const
88 return m_rController
.getJoinView()->getTableView()->IsAddAllowed();
91 void AddTableDialogContext::addTableWindow( const OUString
& _rQualifiedTableName
, const OUString
& _rAliasName
)
93 getTableView()->AddTabWin( _rQualifiedTableName
, _rAliasName
, true );
96 void AddTableDialogContext::onWindowClosing()
98 if (!m_rController
.getView())
100 m_rController
.InvalidateFeature( ID_BROWSER_ADDTABLE
);
101 m_rController
.getView()->GrabFocus();
104 OJoinTableView
* AddTableDialogContext::getTableView() const
106 if ( m_rController
.getJoinView() )
107 return m_rController
.getJoinView()->getTableView();
113 OJoinController::OJoinController(const Reference
< XComponentContext
>& _rM
)
114 : OJoinController_BASE(_rM
)
118 OJoinController::~OJoinController()
122 OJoinDesignView
* OJoinController::getJoinView()
124 return static_cast< OJoinDesignView
* >( getView() );
127 void OJoinController::disposing()
129 if (m_xAddTableDialog
)
131 m_xAddTableDialog
->response(RET_CLOSE
);
132 m_xAddTableDialog
.reset();
135 OJoinController_BASE::disposing();
139 m_vTableConnectionData
.clear();
140 m_vTableData
.clear();
143 void OJoinController::reconnect( bool _bUI
)
145 OJoinController_BASE::reconnect( _bUI
);
146 if ( isConnected() && m_xAddTableDialog
)
147 m_xAddTableDialog
->Update();
150 void OJoinController::impl_onModifyChanged()
152 OJoinController_BASE::impl_onModifyChanged();
153 InvalidateFeature( SID_RELATION_ADD_RELATION
);
156 void OJoinController::SaveTabWinPosSize(OTableWindow
const * pTabWin
, tools::Long nOffsetX
, tools::Long nOffsetY
)
158 // the data for the window
159 const TTableWindowData::value_type
& pData
= pTabWin
->GetData();
160 OSL_ENSURE(pData
!= nullptr, "SaveTabWinPosSize : TabWin has no data !");
162 // set Position & Size of data anew (with current window parameters)
163 Point aPos
= pTabWin
->GetPosPixel();
164 aPos
.AdjustX(nOffsetX
);
165 aPos
.AdjustY(nOffsetY
);
166 pData
->SetPosition(aPos
);
167 pData
->SetSize(pTabWin
->GetSizePixel());
171 FeatureState
OJoinController::GetState(sal_uInt16 _nId
) const
173 FeatureState aReturn
;
174 // (disabled automatically)
175 aReturn
.bEnabled
= true;
179 case ID_BROWSER_EDITDOC
:
180 aReturn
.bChecked
= isEditable();
182 case ID_BROWSER_ADDTABLE
:
183 aReturn
.bEnabled
= ( getView() != nullptr )
184 && const_cast< OJoinController
* >( this )->getJoinView()->getTableView()->IsAddAllowed();
185 aReturn
.bChecked
= aReturn
.bEnabled
&& m_xAddTableDialog
;
186 if ( aReturn
.bEnabled
)
187 aReturn
.sTitle
= OAddTableDlg::getDialogTitleForContext( impl_getDialogContext() );
191 aReturn
= OJoinController_BASE::GetState(_nId
);
196 AddTableDialogContext
& OJoinController::impl_getDialogContext() const
198 if (!m_pDialogContext
)
200 OJoinController
* pNonConstThis
= const_cast< OJoinController
* >( this );
201 pNonConstThis
->m_pDialogContext
.reset( new AddTableDialogContext( *pNonConstThis
) );
203 assert(m_pDialogContext
&& "always exists at this point");
204 return *m_pDialogContext
;
207 void OJoinController::Execute(sal_uInt16 _nId
, const Sequence
< PropertyValue
>& aArgs
)
211 case ID_BROWSER_EDITDOC
:
213 { // the state should be changed to not editable
214 switch (saveModified())
217 // don't change anything here so return
221 setModified(false); // and we are not modified yet
227 setEditable(!isEditable());
228 getJoinView()->setReadOnly(!isEditable());
231 case ID_BROWSER_ADDTABLE
:
232 if (m_xAddTableDialog
)
234 m_xAddTableDialog
->response(RET_CLOSE
);
235 getView()->GrabFocus();
243 OJoinController_BASE::Execute(_nId
,aArgs
);
245 InvalidateFeature(_nId
);
248 void OJoinController::runDialogAsync()
250 assert(!m_xAddTableDialog
);
251 m_xAddTableDialog
= std::make_shared
<OAddTableDlg
>(getFrameWeld(), impl_getDialogContext());
253 weld::WaitObject
aWaitCursor(getFrameWeld());
254 m_xAddTableDialog
->Update();
256 weld::DialogController::runAsync(m_xAddTableDialog
, [this](sal_Int32
/*nResult*/){
257 m_xAddTableDialog
->OnClose();
258 m_xAddTableDialog
.reset();
262 void OJoinController::SaveTabWinsPosSize( OJoinTableView::OTableWindowMap
* pTabWinList
, tools::Long nOffsetX
, tools::Long nOffsetY
)
264 // Deletion and recreation of the old implementation with the current model is not correct anymore:
265 // The TabWins have a pointer to their data, but they are managed by me. When I delete the old ones, the TabWins suddenly have a pointer to objects, which no longer exist.
266 // If the TabWins had a SetData, I could save that effort... but they don't, further I also would still have to set information anew, which actually didn't change.
267 // So I don't delete the TabWinDatas, but only update them.
268 OSL_ENSURE(m_vTableData
.size() == pTabWinList
->size(),
269 "OJoinController::SaveTabWinsPosSize : inconsistent state : should have as many TabWinDatas as TabWins !");
271 for (auto const& tabWin
: *pTabWinList
)
272 SaveTabWinPosSize(tabWin
.second
, nOffsetX
, nOffsetY
);
275 void OJoinController::removeConnectionData(const TTableConnectionData::value_type
& _pData
)
277 std::erase(m_vTableConnectionData
, _pData
);
280 void OJoinController::describeSupportedFeatures()
282 OJoinController_BASE::describeSupportedFeatures();
283 implDescribeSupportedFeature( u
".uno:Redo"_ustr
, ID_BROWSER_REDO
, CommandGroup::EDIT
);
284 implDescribeSupportedFeature( u
".uno:Save"_ustr
, ID_BROWSER_SAVEDOC
, CommandGroup::DOCUMENT
);
285 implDescribeSupportedFeature( u
".uno:Undo"_ustr
, ID_BROWSER_UNDO
, CommandGroup::EDIT
);
286 implDescribeSupportedFeature( u
".uno:AddTable"_ustr
, ID_BROWSER_ADDTABLE
,CommandGroup::EDIT
);
287 implDescribeSupportedFeature( u
".uno:EditDoc"_ustr
, ID_BROWSER_EDITDOC
, CommandGroup::EDIT
);
288 implDescribeSupportedFeature( u
".uno:GetUndoStrings"_ustr
, SID_GETUNDOSTRINGS
);
289 implDescribeSupportedFeature( u
".uno:GetRedoStrings"_ustr
, SID_GETREDOSTRINGS
);
292 sal_Bool SAL_CALL
OJoinController::suspend(sal_Bool _bSuspend
)
294 if ( getBroadcastHelper().bInDispose
|| getBroadcastHelper().bDisposed
)
297 SolarMutexGuard aSolarGuard
;
298 ::osl::MutexGuard
aGuard( getMutex() );
299 if ( getView() && getView()->IsInModalMode() )
304 bCheck
= saveModified() != RET_CANCEL
;
306 OSingleDocumentController::suspend(_bSuspend
);
311 void OJoinController::loadTableWindows( const ::comphelper::NamedValueCollection
& i_rViewSettings
)
313 m_vTableData
.clear();
315 m_aMinimumTableViewSize
= Point();
317 for (auto& table
: i_rViewSettings
.getOrDefault(u
"Tables"_ustr
, Sequence
<PropertyValue
>()))
319 ::comphelper::NamedValueCollection
aSingleTableData(table
.Value
);
320 loadTableWindow( aSingleTableData
);
322 if ( m_aMinimumTableViewSize
!= Point() )
324 getJoinView()->getScrollHelper()->resetRange( m_aMinimumTableViewSize
);
328 void OJoinController::loadTableWindow( const ::comphelper::NamedValueCollection
& i_rTableWindowSettings
)
330 sal_Int32 nX
= -1, nY
= -1, nHeight
= -1, nWidth
= -1;
332 OUString sComposedName
,sTableName
,sWindowName
;
333 bool bShowAll
= false;
335 sComposedName
= i_rTableWindowSettings
.getOrDefault( u
"ComposedName"_ustr
, sComposedName
);
336 sTableName
= i_rTableWindowSettings
.getOrDefault( u
"TableName"_ustr
, sTableName
);
337 sWindowName
= i_rTableWindowSettings
.getOrDefault( u
"WindowName"_ustr
, sWindowName
);
338 nY
= i_rTableWindowSettings
.getOrDefault( u
"WindowTop"_ustr
, nY
);
339 nX
= i_rTableWindowSettings
.getOrDefault( u
"WindowLeft"_ustr
, nX
);
340 nWidth
= i_rTableWindowSettings
.getOrDefault( u
"WindowWidth"_ustr
, nWidth
);
341 nHeight
= i_rTableWindowSettings
.getOrDefault( u
"WindowHeight"_ustr
, nHeight
);
342 bShowAll
= i_rTableWindowSettings
.getOrDefault( u
"ShowAll"_ustr
, bShowAll
);
344 TTableWindowData::value_type pData
= createTableWindowData(sComposedName
,sTableName
,sWindowName
);
347 pData
->SetPosition(Point(nX
,nY
));
348 pData
->SetSize( Size( nWidth
, nHeight
) );
349 pData
->ShowAll(bShowAll
);
350 m_vTableData
.push_back(pData
);
351 if ( m_aMinimumTableViewSize
.X() < (nX
+nWidth
) )
352 m_aMinimumTableViewSize
.setX( nX
+nWidth
);
353 if ( m_aMinimumTableViewSize
.Y() < (nY
+nHeight
) )
354 m_aMinimumTableViewSize
.setY( nY
+nHeight
);
358 void OJoinController::saveTableWindows( ::comphelper::NamedValueCollection
& o_rViewSettings
) const
360 if ( m_vTableData
.empty() )
363 ::comphelper::NamedValueCollection aAllTablesData
;
366 for (auto const& elem
: m_vTableData
)
368 ::comphelper::NamedValueCollection aWindowData
;
369 aWindowData
.put( u
"ComposedName"_ustr
, elem
->GetComposedName() );
370 aWindowData
.put( u
"TableName"_ustr
, elem
->GetTableName() );
371 aWindowData
.put( u
"WindowName"_ustr
, elem
->GetWinName() );
372 aWindowData
.put( u
"WindowTop"_ustr
, static_cast<sal_Int32
>(elem
->GetPosition().Y()) );
373 aWindowData
.put( u
"WindowLeft"_ustr
, static_cast<sal_Int32
>(elem
->GetPosition().X()) );
374 aWindowData
.put( u
"WindowWidth"_ustr
, static_cast<sal_Int32
>(elem
->GetSize().Width()) );
375 aWindowData
.put( u
"WindowHeight"_ustr
, static_cast<sal_Int32
>(elem
->GetSize().Height()) );
376 aWindowData
.put( u
"ShowAll"_ustr
, elem
->IsShowAll() );
378 const OUString
sTableName( "Table" + OUString::number( i
++ ) );
379 aAllTablesData
.put( sTableName
, aWindowData
.getPropertyValues() );
382 o_rViewSettings
.put( u
"Tables"_ustr
, aAllTablesData
.getPropertyValues() );
385 TTableWindowData::value_type
OJoinController::createTableWindowData(const OUString
& _sComposedName
,const OUString
& _sTableName
,const OUString
& _sWindowName
)
387 OJoinDesignView
* pView
= getJoinView();
389 return pView
->getTableView()->createTableWindowData(_sComposedName
,_sTableName
,_sWindowName
);
390 OSL_FAIL("We should never ever reach this point!");
392 return TTableWindowData::value_type();
397 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */