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 <tools/diagnose_ex.h>
29 #include <connectivity/dbtools.hxx>
31 #include <sal/log.hxx>
32 #include <osl/diagnose.h>
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::lang
;
41 using namespace ::com::sun::star::beans
;
42 using namespace ::com::sun::star::sdbc
;
43 using namespace ::com::sun::star::sdbcx
;
44 using namespace ::com::sun::star::container
;
45 using namespace ::com::sun::star::form
;
46 using namespace ::dbtools
;
48 OListComboWizard::OListComboWizard(weld::Window
* _pParent
,
49 const Reference
< XPropertySet
>& _rxObjectModel
, const Reference
< XComponentContext
>& _rxContext
)
50 : OControlWizard(_pParent
, _rxObjectModel
, _rxContext
)
52 , m_bHadDataSelection(true)
54 initControlSettings(&m_aSettings
);
56 m_xPrevPage
->set_help_id(HID_LISTWIZARD_PREVIOUS
);
57 m_xNextPage
->set_help_id(HID_LISTWIZARD_NEXT
);
58 m_xCancel
->set_help_id(HID_LISTWIZARD_CANCEL
);
59 m_xFinish
->set_help_id(HID_LISTWIZARD_FINISH
);
61 // if we do not need the data source selection page ...
62 if (!needDatasourceSelection())
65 m_bHadDataSelection
= false;
69 bool OListComboWizard::approveControl(sal_Int16 _nClassId
)
73 case FormComponentType::LISTBOX
:
75 setTitleBase(compmodule::ModuleRes(RID_STR_LISTWIZARD_TITLE
));
77 case FormComponentType::COMBOBOX
:
79 setTitleBase(compmodule::ModuleRes(RID_STR_COMBOWIZARD_TITLE
));
85 std::unique_ptr
<BuilderPage
> OListComboWizard::createPage(WizardState _nState
)
87 OString
sIdent(OString::number(_nState
));
88 weld::Container
* pPageContainer
= m_xAssistant
->append_page(sIdent
);
92 case LCW_STATE_DATASOURCE_SELECTION
:
93 return std::make_unique
<OTableSelectionPage
>(pPageContainer
, this);
94 case LCW_STATE_TABLESELECTION
:
95 return std::make_unique
<OContentTableSelection
>(pPageContainer
, this);
96 case LCW_STATE_FIELDSELECTION
:
97 return std::make_unique
<OContentFieldSelection
>(pPageContainer
, this);
98 case LCW_STATE_FIELDLINK
:
99 return std::make_unique
<OLinkFieldsPage
>(pPageContainer
, this);
100 case LCW_STATE_COMBODBFIELD
:
101 return std::make_unique
<OComboDBFieldPage
>(pPageContainer
, this);
107 vcl::WizardTypes::WizardState
OListComboWizard::determineNextState( WizardState _nCurrentState
) const
109 switch (_nCurrentState
)
111 case LCW_STATE_DATASOURCE_SELECTION
:
112 return LCW_STATE_TABLESELECTION
;
113 case LCW_STATE_TABLESELECTION
:
114 return LCW_STATE_FIELDSELECTION
;
115 case LCW_STATE_FIELDSELECTION
:
116 return getFinalState();
119 return WZS_INVALID_STATE
;
122 void OListComboWizard::enterState(WizardState _nState
)
124 OControlWizard::enterState(_nState
);
126 enableButtons(WizardButtonFlags::PREVIOUS
, m_bHadDataSelection
? (LCW_STATE_DATASOURCE_SELECTION
< _nState
) : LCW_STATE_TABLESELECTION
< _nState
);
127 enableButtons(WizardButtonFlags::NEXT
, getFinalState() != _nState
);
128 if (_nState
< getFinalState())
129 enableButtons(WizardButtonFlags::FINISH
, false);
131 if (getFinalState() == _nState
)
132 defaultButton(WizardButtonFlags::FINISH
);
136 bool OListComboWizard::leaveState(WizardState _nState
)
138 if (!OControlWizard::leaveState(_nState
))
141 if (getFinalState() == _nState
)
142 defaultButton(WizardButtonFlags::NEXT
);
148 void OListComboWizard::implApplySettings()
152 // for quoting identifiers, we need the connection meta data
153 Reference
< XConnection
> xConn
= getFormConnection();
154 DBG_ASSERT(xConn
.is(), "OListComboWizard::implApplySettings: no connection, unable to quote!");
155 Reference
< XDatabaseMetaData
> xMetaData
;
157 xMetaData
= xConn
->getMetaData();
162 OUString sQuoteString
= xMetaData
->getIdentifierQuoteString();
163 if (isListBox()) // only when we have a listbox this should be not empty
164 getSettings().sLinkedListField
= quoteName(sQuoteString
, getSettings().sLinkedListField
);
166 OUString sCatalog
, sSchema
, sName
;
167 ::dbtools::qualifiedNameComponents( xMetaData
, getSettings().sListContentTable
, sCatalog
, sSchema
, sName
, ::dbtools::EComposeRule::InDataManipulation
);
168 getSettings().sListContentTable
= ::dbtools::composeTableNameForSelect( xConn
, sCatalog
, sSchema
, sName
);
170 getSettings().sListContentField
= quoteName(sQuoteString
, getSettings().sListContentField
);
173 // ListSourceType: SQL
174 getContext().xObjectModel
->setPropertyValue("ListSourceType", makeAny(sal_Int32(ListSourceType_SQL
)));
179 getContext().xObjectModel
->setPropertyValue("BoundColumn", makeAny(sal_Int16(1)));
181 // build the statement to set as list source
182 OUString sStatement
= "SELECT " +
183 getSettings().sListContentField
+ ", " + getSettings().sLinkedListField
+
184 " FROM " + getSettings().sListContentTable
;
185 Sequence
< OUString
> aListSource
{ sStatement
};
186 getContext().xObjectModel
->setPropertyValue("ListSource", makeAny(aListSource
));
190 // build the statement to set as list source
191 OUString sStatement
= "SELECT DISTINCT " +
192 getSettings().sListContentField
+
193 " FROM " + getSettings().sListContentTable
;
194 getContext().xObjectModel
->setPropertyValue( "ListSource", makeAny(sStatement
));
198 getContext().xObjectModel
->setPropertyValue("DataField", makeAny(getSettings().sLinkedFormField
));
200 catch(const Exception
&)
202 OSL_FAIL("OListComboWizard::implApplySettings: could not set the property values for the listbox!");
207 bool OListComboWizard::onFinish()
209 if ( !OControlWizard::onFinish() )
216 Reference
< XNameAccess
> OLCPage::getTables() const
218 Reference
< XConnection
> xConn
= getFormConnection();
219 DBG_ASSERT(xConn
.is(), "OLCPage::getTables: should have an active connection when reaching this page!");
221 Reference
< XTablesSupplier
> xSuppTables(xConn
, UNO_QUERY
);
222 Reference
< XNameAccess
> xTables
;
223 if (xSuppTables
.is())
224 xTables
= xSuppTables
->getTables();
226 DBG_ASSERT(xTables
.is() || !xConn
.is(), "OLCPage::getTables: got no tables from the connection!");
232 Sequence
< OUString
> OLCPage::getTableFields()
234 Reference
< XNameAccess
> xTables
= getTables();
235 Sequence
< OUString
> aColumnNames
;
240 // the list table as XColumnsSupplier
241 Reference
< XColumnsSupplier
> xSuppCols
;
242 xTables
->getByName(getSettings().sListContentTable
) >>= xSuppCols
;
243 DBG_ASSERT(xSuppCols
.is(), "OLCPage::getTableFields: no columns supplier!");
246 Reference
< XNameAccess
> xColumns
;
248 xColumns
= xSuppCols
->getColumns();
252 aColumnNames
= xColumns
->getElementNames();
254 catch(const Exception
&)
256 TOOLS_WARN_EXCEPTION( "extensions.dbpilots", "OLinkFieldsPage::initializePage: caught an exception while retrieving the columns");
262 OContentTableSelection::OContentTableSelection(weld::Container
* pPage
, OListComboWizard
* pWizard
)
263 : OLCPage(pPage
, pWizard
, "modules/sabpilot/ui/contenttablepage.ui", "TableSelectionPage")
264 , m_xSelectTable(m_xBuilder
->weld_tree_view("table"))
266 enableFormDatasourceDisplay();
268 m_xSelectTable
->connect_row_activated(LINK(this, OContentTableSelection
, OnTableDoubleClicked
));
269 m_xSelectTable
->connect_changed(LINK(this, OContentTableSelection
, OnTableSelected
));
272 OContentTableSelection::~OContentTableSelection()
276 void OContentTableSelection::Activate()
279 m_xSelectTable
->grab_focus();
282 bool OContentTableSelection::canAdvance() const
284 if (!OLCPage::canAdvance())
287 return 0 != m_xSelectTable
->count_selected_rows();
290 IMPL_LINK_NOARG( OContentTableSelection
, OnTableSelected
, weld::TreeView
&, void )
292 updateDialogTravelUI();
295 IMPL_LINK( OContentTableSelection
, OnTableDoubleClicked
, weld::TreeView
&, _rListBox
, bool )
297 if (_rListBox
.count_selected_rows())
298 getDialog()->travelNext();
302 void OContentTableSelection::initializePage()
304 OLCPage::initializePage();
306 // fill the list with the table name
307 m_xSelectTable
->clear();
310 Reference
< XNameAccess
> xTables
= getTables();
311 Sequence
< OUString
> aTableNames
;
313 aTableNames
= xTables
->getElementNames();
314 fillListBox(*m_xSelectTable
, aTableNames
);
316 catch(const Exception
&)
318 OSL_FAIL("OContentTableSelection::initializePage: could not retrieve the table names!");
321 m_xSelectTable
->select_text(getSettings().sListContentTable
);
325 bool OContentTableSelection::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
327 if (!OLCPage::commitPage(_eReason
))
330 OListComboSettings
& rSettings
= getSettings();
331 rSettings
.sListContentTable
= m_xSelectTable
->get_selected_text();
332 if (rSettings
.sListContentTable
.isEmpty() && (::vcl::WizardTypes::eTravelBackward
!= _eReason
))
333 // need to select a table
339 OContentFieldSelection::OContentFieldSelection(weld::Container
* pPage
, OListComboWizard
* pWizard
)
340 : OLCPage(pPage
, pWizard
, "modules/sabpilot/ui/contentfieldpage.ui", "FieldSelectionPage")
341 , m_xSelectTableField(m_xBuilder
->weld_tree_view("selectfield"))
342 , m_xDisplayedField(m_xBuilder
->weld_entry("displayfield"))
343 , m_xInfo(m_xBuilder
->weld_label("info"))
345 m_xInfo
->set_label(compmodule::ModuleRes( isListBox() ? RID_STR_FIELDINFO_LISTBOX
: RID_STR_FIELDINFO_COMBOBOX
));
346 m_xSelectTableField
->connect_changed(LINK(this, OContentFieldSelection
, OnFieldSelected
));
347 m_xSelectTableField
->connect_row_activated(LINK(this, OContentFieldSelection
, OnTableDoubleClicked
));
350 OContentFieldSelection::~OContentFieldSelection()
354 void OContentFieldSelection::initializePage()
356 OLCPage::initializePage();
358 // fill the list of fields
359 fillListBox(*m_xSelectTableField
, getTableFields());
361 m_xSelectTableField
->select_text(getSettings().sListContentField
);
362 m_xDisplayedField
->set_text(getSettings().sListContentField
);
365 bool OContentFieldSelection::canAdvance() const
367 if (!OLCPage::canAdvance())
370 return 0 != m_xSelectTableField
->count_selected_rows();
373 IMPL_LINK_NOARG( OContentFieldSelection
, OnTableDoubleClicked
, weld::TreeView
&, bool )
375 if (m_xSelectTableField
->count_selected_rows())
376 getDialog()->travelNext();
380 IMPL_LINK_NOARG( OContentFieldSelection
, OnFieldSelected
, weld::TreeView
&, void )
382 updateDialogTravelUI();
383 m_xDisplayedField
->set_text(m_xSelectTableField
->get_selected_text());
386 bool OContentFieldSelection::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
388 if (!OLCPage::commitPage(_eReason
))
391 getSettings().sListContentField
= m_xSelectTableField
->get_selected_text();
396 OLinkFieldsPage::OLinkFieldsPage(weld::Container
* pPage
, OListComboWizard
* pWizard
)
397 : OLCPage(pPage
, pWizard
, "modules/sabpilot/ui/fieldlinkpage.ui", "FieldLinkPage")
398 , m_xValueListField(m_xBuilder
->weld_combo_box("valuefield"))
399 , m_xTableField(m_xBuilder
->weld_combo_box("listtable"))
401 m_xValueListField
->connect_changed(LINK(this, OLinkFieldsPage
, OnSelectionModified
));
402 m_xTableField
->connect_changed(LINK(this, OLinkFieldsPage
, OnSelectionModified
));
405 OLinkFieldsPage::~OLinkFieldsPage()
409 void OLinkFieldsPage::Activate()
412 m_xValueListField
->grab_focus();
415 void OLinkFieldsPage::initializePage()
417 OLCPage::initializePage();
419 // fill the value list
420 fillListBox(*m_xValueListField
, getContext().aFieldNames
);
421 // fill the table field list
422 fillListBox(*m_xTableField
, getTableFields());
424 // the initial selections
425 m_xValueListField
->set_entry_text(getSettings().sLinkedFormField
);
426 m_xTableField
->set_entry_text(getSettings().sLinkedListField
);
431 bool OLinkFieldsPage::canAdvance() const
433 // we're on the last page here, no travelNext allowed ...
437 void OLinkFieldsPage::implCheckFinish()
439 bool bInvalidSelection
= (-1 == m_xValueListField
->find_text(m_xValueListField
->get_active_text()));
440 bInvalidSelection
|= (-1 == m_xTableField
->find_text(m_xTableField
->get_active_text()));
441 getDialog()->enableButtons(WizardButtonFlags::FINISH
, !bInvalidSelection
);
444 IMPL_LINK_NOARG(OLinkFieldsPage
, OnSelectionModified
, weld::ComboBox
&, void)
449 bool OLinkFieldsPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
451 if (!OLCPage::commitPage(_eReason
))
454 getSettings().sLinkedFormField
= m_xValueListField
->get_active_text();
455 getSettings().sLinkedListField
= m_xTableField
->get_active_text();
460 OComboDBFieldPage::OComboDBFieldPage(weld::Container
* pPage
, OControlWizard
* pWizard
)
461 : ODBFieldPage(pPage
, pWizard
)
463 setDescriptionText(compmodule::ModuleRes(RID_STR_COMBOWIZ_DBFIELD
));
466 OUString
& OComboDBFieldPage::getDBFieldSetting()
468 return static_cast<OListComboWizard
*>(getDialog())->getSettings().sLinkedFormField
;
471 void OComboDBFieldPage::Activate()
473 ODBFieldPage::Activate();
474 getDialog()->enableButtons(WizardButtonFlags::FINISH
, true);
477 bool OComboDBFieldPage::canAdvance() const
479 // we're on the last page here, no travelNext allowed ...
486 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */