Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / extensions / source / dbpilots / listcombowizard.cxx
bloba1440f2468a3d9ea903d6193af4e1d8a27734e7f
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 "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>
30 #include <helpids.h>
31 #include <sal/log.hxx>
32 #include <osl/diagnose.h>
35 namespace dbp
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)
51 , m_bListBox(false)
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())
63 { // ... skip it!
64 skip();
65 m_bHadDataSelection = false;
69 bool OListComboWizard::approveControl(sal_Int16 _nClassId)
71 switch (_nClassId)
73 case FormComponentType::LISTBOX:
74 m_bListBox = true;
75 setTitleBase(compmodule::ModuleRes(RID_STR_LISTWIZARD_TITLE));
76 return true;
77 case FormComponentType::COMBOBOX:
78 m_bListBox = false;
79 setTitleBase(compmodule::ModuleRes(RID_STR_COMBOWIZARD_TITLE));
80 return true;
82 return false;
85 std::unique_ptr<BuilderPage> OListComboWizard::createPage(WizardState _nState)
87 OString sIdent(OString::number(_nState));
88 weld::Container* pPageContainer = m_xAssistant->append_page(sIdent);
90 switch (_nState)
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);
104 return nullptr;
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))
139 return false;
141 if (getFinalState() == _nState)
142 defaultButton(WizardButtonFlags::NEXT);
144 return true;
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;
156 if (xConn.is())
157 xMetaData = xConn->getMetaData();
159 // do some quotings
160 if (xMetaData.is())
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)));
176 if (isListBox())
178 // BoundColumn: 1
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));
188 else
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));
197 // the bound field
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() )
210 return false;
212 implApplySettings();
213 return true;
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!");
228 return xTables;
232 Sequence< OUString > OLCPage::getTableFields()
234 Reference< XNameAccess > xTables = getTables();
235 Sequence< OUString > aColumnNames;
236 if (xTables.is())
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!");
245 // the columns
246 Reference< XNameAccess > xColumns;
247 if (xSuppCols.is())
248 xColumns = xSuppCols->getColumns();
250 // the column names
251 if (xColumns.is())
252 aColumnNames = xColumns->getElementNames();
254 catch(const Exception&)
256 TOOLS_WARN_EXCEPTION( "extensions.dbpilots", "OLinkFieldsPage::initializePage: caught an exception while retrieving the columns");
259 return aColumnNames;
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()
278 OLCPage::Activate();
279 m_xSelectTable->grab_focus();
282 bool OContentTableSelection::canAdvance() const
284 if (!OLCPage::canAdvance())
285 return false;
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();
299 return true;
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;
312 if (xTables.is())
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))
328 return false;
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
334 return false;
336 return true;
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())
368 return false;
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();
377 return true;
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))
389 return false;
391 getSettings().sListContentField = m_xSelectTableField->get_selected_text();
393 return true;
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()
411 OLCPage::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);
428 implCheckFinish();
431 bool OLinkFieldsPage::canAdvance() const
433 // we're on the last page here, no travelNext allowed ...
434 return false;
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)
446 implCheckFinish();
449 bool OLinkFieldsPage::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason )
451 if (!OLCPage::commitPage(_eReason))
452 return false;
454 getSettings().sLinkedFormField = m_xValueListField->get_active_text();
455 getSettings().sLinkedListField = m_xTableField->get_active_text();
457 return true;
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 ...
480 return false;
483 } // namespace dbp
486 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */