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 "listcombowizard.hxx"
21 #include "commonpagesdbp.hxx"
22 #include <com/sun/star/sdbc/XConnection.hpp>
23 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
24 #include <com/sun/star/container/XNameAccess.hpp>
25 #include <com/sun/star/form/ListSourceType.hpp>
26 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
27 #include <tools/debug.hxx>
28 #include <comphelper/diagnose_ex.hxx>
29 #include <connectivity/dbtools.hxx>
31 #include <osl/diagnose.h>
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::lang
;
40 using namespace ::com::sun::star::beans
;
41 using namespace ::com::sun::star::sdbc
;
42 using namespace ::com::sun::star::sdbcx
;
43 using namespace ::com::sun::star::container
;
44 using namespace ::com::sun::star::form
;
45 using namespace ::dbtools
;
47 OListComboWizard::OListComboWizard(weld::Window
* _pParent
,
48 const Reference
< XPropertySet
>& _rxObjectModel
, const Reference
< XComponentContext
>& _rxContext
)
49 : OControlWizard(_pParent
, _rxObjectModel
, _rxContext
)
51 , m_bHadDataSelection(true)
53 initControlSettings(&m_aSettings
);
55 m_xPrevPage
->set_help_id(HID_LISTWIZARD_PREVIOUS
);
56 m_xNextPage
->set_help_id(HID_LISTWIZARD_NEXT
);
57 m_xCancel
->set_help_id(HID_LISTWIZARD_CANCEL
);
58 m_xFinish
->set_help_id(HID_LISTWIZARD_FINISH
);
60 // if we do not need the data source selection page ...
61 if (!needDatasourceSelection())
64 m_bHadDataSelection
= false;
68 bool OListComboWizard::approveControl(sal_Int16 _nClassId
)
72 case FormComponentType::LISTBOX
:
74 setTitleBase(compmodule::ModuleRes(RID_STR_LISTWIZARD_TITLE
));
76 case FormComponentType::COMBOBOX
:
78 setTitleBase(compmodule::ModuleRes(RID_STR_COMBOWIZARD_TITLE
));
84 std::unique_ptr
<BuilderPage
> OListComboWizard::createPage(WizardState _nState
)
86 OUString
sIdent(OUString::number(_nState
));
87 weld::Container
* pPageContainer
= m_xAssistant
->append_page(sIdent
);
91 case LCW_STATE_DATASOURCE_SELECTION
:
92 return std::make_unique
<OTableSelectionPage
>(pPageContainer
, this);
93 case LCW_STATE_TABLESELECTION
:
94 return std::make_unique
<OContentTableSelection
>(pPageContainer
, this);
95 case LCW_STATE_FIELDSELECTION
:
96 return std::make_unique
<OContentFieldSelection
>(pPageContainer
, this);
97 case LCW_STATE_FIELDLINK
:
98 return std::make_unique
<OLinkFieldsPage
>(pPageContainer
, this);
99 case LCW_STATE_COMBODBFIELD
:
100 return std::make_unique
<OComboDBFieldPage
>(pPageContainer
, this);
106 vcl::WizardTypes::WizardState
OListComboWizard::determineNextState( WizardState _nCurrentState
) const
108 switch (_nCurrentState
)
110 case LCW_STATE_DATASOURCE_SELECTION
:
111 return LCW_STATE_TABLESELECTION
;
112 case LCW_STATE_TABLESELECTION
:
113 return LCW_STATE_FIELDSELECTION
;
114 case LCW_STATE_FIELDSELECTION
:
115 return getFinalState();
118 return WZS_INVALID_STATE
;
121 void OListComboWizard::enterState(WizardState _nState
)
123 OControlWizard::enterState(_nState
);
125 enableButtons(WizardButtonFlags::PREVIOUS
, m_bHadDataSelection
? (LCW_STATE_DATASOURCE_SELECTION
< _nState
) : LCW_STATE_TABLESELECTION
< _nState
);
126 enableButtons(WizardButtonFlags::NEXT
, getFinalState() != _nState
);
127 if (_nState
< getFinalState())
128 enableButtons(WizardButtonFlags::FINISH
, false);
130 if (getFinalState() == _nState
)
131 defaultButton(WizardButtonFlags::FINISH
);
135 bool OListComboWizard::leaveState(WizardState _nState
)
137 if (!OControlWizard::leaveState(_nState
))
140 if (getFinalState() == _nState
)
141 defaultButton(WizardButtonFlags::NEXT
);
147 void OListComboWizard::implApplySettings()
151 // for quoting identifiers, we need the connection meta data
152 Reference
< XConnection
> xConn
= getFormConnection();
153 DBG_ASSERT(xConn
.is(), "OListComboWizard::implApplySettings: no connection, unable to quote!");
154 Reference
< XDatabaseMetaData
> xMetaData
;
156 xMetaData
= xConn
->getMetaData();
161 OUString sQuoteString
= xMetaData
->getIdentifierQuoteString();
162 if (isListBox()) // only when we have a listbox this should be not empty
163 getSettings().sLinkedListField
= quoteName(sQuoteString
, getSettings().sLinkedListField
);
165 OUString sCatalog
, sSchema
, sName
;
166 ::dbtools::qualifiedNameComponents( xMetaData
, getSettings().sListContentTable
, sCatalog
, sSchema
, sName
, ::dbtools::EComposeRule::InDataManipulation
);
167 getSettings().sListContentTable
= ::dbtools::composeTableNameForSelect( xConn
, sCatalog
, sSchema
, sName
);
169 getSettings().sListContentField
= quoteName(sQuoteString
, getSettings().sListContentField
);
172 // ListSourceType: SQL
173 getContext().xObjectModel
->setPropertyValue("ListSourceType", Any(sal_Int32(ListSourceType_SQL
)));
178 getContext().xObjectModel
->setPropertyValue("BoundColumn", Any(sal_Int16(1)));
180 // build the statement to set as list source
181 OUString sStatement
= "SELECT " +
182 getSettings().sListContentField
+ ", " + getSettings().sLinkedListField
+
183 " FROM " + getSettings().sListContentTable
;
184 Sequence
< OUString
> aListSource
{ sStatement
};
185 getContext().xObjectModel
->setPropertyValue("ListSource", Any(aListSource
));
189 // build the statement to set as list source
190 OUString sStatement
= "SELECT DISTINCT " +
191 getSettings().sListContentField
+
192 " FROM " + getSettings().sListContentTable
;
193 getContext().xObjectModel
->setPropertyValue( "ListSource", Any(sStatement
));
197 getContext().xObjectModel
->setPropertyValue("DataField", Any(getSettings().sLinkedFormField
));
199 catch(const Exception
&)
201 OSL_FAIL("OListComboWizard::implApplySettings: could not set the property values for the listbox!");
206 bool OListComboWizard::onFinish()
208 if ( !OControlWizard::onFinish() )
215 Reference
< XNameAccess
> OLCPage::getTables() const
217 Reference
< XConnection
> xConn
= getFormConnection();
218 DBG_ASSERT(xConn
.is(), "OLCPage::getTables: should have an active connection when reaching this page!");
220 Reference
< XTablesSupplier
> xSuppTables(xConn
, UNO_QUERY
);
221 Reference
< XNameAccess
> xTables
;
222 if (xSuppTables
.is())
223 xTables
= xSuppTables
->getTables();
225 DBG_ASSERT(xTables
.is() || !xConn
.is(), "OLCPage::getTables: got no tables from the connection!");
231 Sequence
< OUString
> OLCPage::getTableFields()
233 Reference
< XNameAccess
> xTables
= getTables();
234 Sequence
< OUString
> aColumnNames
;
239 // the list table as XColumnsSupplier
240 Reference
< XColumnsSupplier
> xSuppCols
;
241 xTables
->getByName(getSettings().sListContentTable
) >>= xSuppCols
;
242 DBG_ASSERT(xSuppCols
.is(), "OLCPage::getTableFields: no columns supplier!");
245 Reference
< XNameAccess
> xColumns
;
247 xColumns
= xSuppCols
->getColumns();
251 aColumnNames
= xColumns
->getElementNames();
253 catch(const Exception
&)
255 TOOLS_WARN_EXCEPTION( "extensions.dbpilots", "OLinkFieldsPage::initializePage: caught an exception while retrieving the columns");
261 OContentTableSelection::OContentTableSelection(weld::Container
* pPage
, OListComboWizard
* pWizard
)
262 : OLCPage(pPage
, pWizard
, "modules/sabpilot/ui/contenttablepage.ui", "TableSelectionPage")
263 , m_xSelectTable(m_xBuilder
->weld_tree_view("table"))
265 enableFormDatasourceDisplay();
267 m_xSelectTable
->connect_row_activated(LINK(this, OContentTableSelection
, OnTableDoubleClicked
));
268 m_xSelectTable
->connect_changed(LINK(this, OContentTableSelection
, OnTableSelected
));
271 OContentTableSelection::~OContentTableSelection()
275 void OContentTableSelection::Activate()
278 m_xSelectTable
->grab_focus();
281 bool OContentTableSelection::canAdvance() const
283 if (!OLCPage::canAdvance())
286 return 0 != m_xSelectTable
->count_selected_rows();
289 IMPL_LINK_NOARG( OContentTableSelection
, OnTableSelected
, weld::TreeView
&, void )
291 updateDialogTravelUI();
294 IMPL_LINK( OContentTableSelection
, OnTableDoubleClicked
, weld::TreeView
&, _rListBox
, bool )
296 if (_rListBox
.count_selected_rows())
297 getDialog()->travelNext();
301 void OContentTableSelection::initializePage()
303 OLCPage::initializePage();
305 // fill the list with the table name
306 m_xSelectTable
->clear();
309 Reference
< XNameAccess
> xTables
= getTables();
310 Sequence
< OUString
> aTableNames
;
312 aTableNames
= xTables
->getElementNames();
313 fillListBox(*m_xSelectTable
, aTableNames
);
315 catch(const Exception
&)
317 OSL_FAIL("OContentTableSelection::initializePage: could not retrieve the table names!");
320 m_xSelectTable
->select_text(getSettings().sListContentTable
);
324 bool OContentTableSelection::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
326 if (!OLCPage::commitPage(_eReason
))
329 OListComboSettings
& rSettings
= getSettings();
330 rSettings
.sListContentTable
= m_xSelectTable
->get_selected_text();
331 if (rSettings
.sListContentTable
.isEmpty() && (::vcl::WizardTypes::eTravelBackward
!= _eReason
))
332 // need to select a table
338 OContentFieldSelection::OContentFieldSelection(weld::Container
* pPage
, OListComboWizard
* pWizard
)
339 : OLCPage(pPage
, pWizard
, "modules/sabpilot/ui/contentfieldpage.ui", "FieldSelectionPage")
340 , m_xSelectTableField(m_xBuilder
->weld_tree_view("selectfield"))
341 , m_xDisplayedField(m_xBuilder
->weld_entry("displayfield"))
342 , m_xInfo(m_xBuilder
->weld_label("info"))
344 m_xInfo
->set_label(compmodule::ModuleRes( isListBox() ? RID_STR_FIELDINFO_LISTBOX
: RID_STR_FIELDINFO_COMBOBOX
));
345 m_xSelectTableField
->connect_changed(LINK(this, OContentFieldSelection
, OnFieldSelected
));
346 m_xSelectTableField
->connect_row_activated(LINK(this, OContentFieldSelection
, OnTableDoubleClicked
));
349 OContentFieldSelection::~OContentFieldSelection()
353 void OContentFieldSelection::initializePage()
355 OLCPage::initializePage();
357 // fill the list of fields
358 fillListBox(*m_xSelectTableField
, getTableFields());
360 m_xSelectTableField
->select_text(getSettings().sListContentField
);
361 m_xDisplayedField
->set_text(getSettings().sListContentField
);
364 bool OContentFieldSelection::canAdvance() const
366 if (!OLCPage::canAdvance())
369 return 0 != m_xSelectTableField
->count_selected_rows();
372 IMPL_LINK_NOARG( OContentFieldSelection
, OnTableDoubleClicked
, weld::TreeView
&, bool )
374 if (m_xSelectTableField
->count_selected_rows())
375 getDialog()->travelNext();
379 IMPL_LINK_NOARG( OContentFieldSelection
, OnFieldSelected
, weld::TreeView
&, void )
381 updateDialogTravelUI();
382 m_xDisplayedField
->set_text(m_xSelectTableField
->get_selected_text());
385 bool OContentFieldSelection::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
387 if (!OLCPage::commitPage(_eReason
))
390 getSettings().sListContentField
= m_xSelectTableField
->get_selected_text();
395 OLinkFieldsPage::OLinkFieldsPage(weld::Container
* pPage
, OListComboWizard
* pWizard
)
396 : OLCPage(pPage
, pWizard
, "modules/sabpilot/ui/fieldlinkpage.ui", "FieldLinkPage")
397 , m_xValueListField(m_xBuilder
->weld_combo_box("valuefield"))
398 , m_xTableField(m_xBuilder
->weld_combo_box("listtable"))
400 m_xValueListField
->connect_changed(LINK(this, OLinkFieldsPage
, OnSelectionModified
));
401 m_xTableField
->connect_changed(LINK(this, OLinkFieldsPage
, OnSelectionModified
));
404 OLinkFieldsPage::~OLinkFieldsPage()
408 void OLinkFieldsPage::Activate()
411 m_xValueListField
->grab_focus();
414 void OLinkFieldsPage::initializePage()
416 OLCPage::initializePage();
418 // fill the value list
419 fillListBox(*m_xValueListField
, getContext().aFieldNames
);
420 // fill the table field list
421 fillListBox(*m_xTableField
, getTableFields());
423 // the initial selections
424 m_xValueListField
->set_entry_text(getSettings().sLinkedFormField
);
425 m_xTableField
->set_entry_text(getSettings().sLinkedListField
);
430 bool OLinkFieldsPage::canAdvance() const
432 // we're on the last page here, no travelNext allowed ...
436 void OLinkFieldsPage::implCheckFinish()
438 bool bInvalidSelection
= (-1 == m_xValueListField
->find_text(m_xValueListField
->get_active_text()));
439 bInvalidSelection
|= (-1 == m_xTableField
->find_text(m_xTableField
->get_active_text()));
440 getDialog()->enableButtons(WizardButtonFlags::FINISH
, !bInvalidSelection
);
443 IMPL_LINK_NOARG(OLinkFieldsPage
, OnSelectionModified
, weld::ComboBox
&, void)
448 bool OLinkFieldsPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
450 if (!OLCPage::commitPage(_eReason
))
453 getSettings().sLinkedFormField
= m_xValueListField
->get_active_text();
454 getSettings().sLinkedListField
= m_xTableField
->get_active_text();
459 OComboDBFieldPage::OComboDBFieldPage(weld::Container
* pPage
, OControlWizard
* pWizard
)
460 : ODBFieldPage(pPage
, pWizard
)
462 setDescriptionText(compmodule::ModuleRes(RID_STR_COMBOWIZ_DBFIELD
));
465 OUString
& OComboDBFieldPage::getDBFieldSetting()
467 return static_cast<OListComboWizard
*>(getDialog())->getSettings().sLinkedFormField
;
470 void OComboDBFieldPage::Activate()
472 ODBFieldPage::Activate();
473 getDialog()->enableButtons(WizardButtonFlags::FINISH
, true);
476 bool OComboDBFieldPage::canAdvance() const
478 // we're on the last page here, no travelNext allowed ...
485 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */