bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / dbpilots / listcombowizard.cxx
blob7feea7d7024d72309dcea965b0dc3f861cee4e31
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 <vcl/msgbox.hxx>
29 #include <connectivity/dbtools.hxx>
30 #include "dbpilots.hrc"
31 #include <comphelper/extract.hxx>
34 namespace dbp
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 ::svt;
46 using namespace ::dbtools;
48 OListComboWizard::OListComboWizard( vcl::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_pPrevPage->SetHelpId(HID_LISTWIZARD_PREVIOUS);
57 m_pNextPage->SetHelpId(HID_LISTWIZARD_NEXT);
58 m_pCancel->SetHelpId(HID_LISTWIZARD_CANCEL);
59 m_pFinish->SetHelpId(HID_LISTWIZARD_FINISH);
61 // if we do not need the data source selection page ...
62 if (!needDatasourceSelection())
63 { // ... skip it!
64 skip(1);
65 m_bHadDataSelection = false;
70 bool OListComboWizard::approveControl(sal_Int16 _nClassId)
72 switch (_nClassId)
74 case FormComponentType::LISTBOX:
75 m_bListBox = true;
76 setTitleBase(ModuleRes(RID_STR_LISTWIZARD_TITLE).toString());
77 return true;
78 case FormComponentType::COMBOBOX:
79 m_bListBox = false;
80 setTitleBase(ModuleRes(RID_STR_COMBOWIZARD_TITLE).toString());
81 return true;
83 return false;
87 VclPtr<TabPage> OListComboWizard::createPage(WizardState _nState)
89 switch (_nState)
91 case LCW_STATE_DATASOURCE_SELECTION:
92 return VclPtr<OTableSelectionPage>::Create(this);
93 case LCW_STATE_TABLESELECTION:
94 return VclPtr<OContentTableSelection>::Create(this);
95 case LCW_STATE_FIELDSELECTION:
96 return VclPtr<OContentFieldSelection>::Create(this);
97 case LCW_STATE_FIELDLINK:
98 return VclPtr<OLinkFieldsPage>::Create(this);
99 case LCW_STATE_COMBODBFIELD:
100 return VclPtr<OComboDBFieldPage>::Create(this);
103 return VclPtr<TabPage>();
107 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;
123 void OListComboWizard::enterState(WizardState _nState)
125 OControlWizard::enterState(_nState);
127 enableButtons(WizardButtonFlags::PREVIOUS, m_bHadDataSelection ? (LCW_STATE_DATASOURCE_SELECTION < _nState) : LCW_STATE_TABLESELECTION < _nState);
128 enableButtons(WizardButtonFlags::NEXT, getFinalState() != _nState);
129 if (_nState < getFinalState())
130 enableButtons(WizardButtonFlags::FINISH, false);
132 if (getFinalState() == _nState)
133 defaultButton(WizardButtonFlags::FINISH);
137 bool OListComboWizard::leaveState(WizardState _nState)
139 if (!OControlWizard::leaveState(_nState))
140 return false;
142 if (getFinalState() == _nState)
143 defaultButton(WizardButtonFlags::NEXT);
145 return true;
149 void OListComboWizard::implApplySettings()
153 // for quoting identifiers, we need the connection meta data
154 Reference< XConnection > xConn = getFormConnection();
155 DBG_ASSERT(xConn.is(), "OListComboWizard::implApplySettings: no connection, unable to quote!");
156 Reference< XDatabaseMetaData > xMetaData;
157 if (xConn.is())
158 xMetaData = xConn->getMetaData();
160 // do some quotings
161 if (xMetaData.is())
163 OUString sQuoteString = xMetaData->getIdentifierQuoteString();
164 if (isListBox()) // only when we have a listbox this should be not empty
165 getSettings().sLinkedListField = quoteName(sQuoteString, getSettings().sLinkedListField);
167 OUString sCatalog, sSchema, sName;
168 ::dbtools::qualifiedNameComponents( xMetaData, getSettings().sListContentTable, sCatalog, sSchema, sName, ::dbtools::eInDataManipulation );
169 getSettings().sListContentTable = ::dbtools::composeTableNameForSelect( xConn, sCatalog, sSchema, sName );
171 getSettings().sListContentField = quoteName(sQuoteString, getSettings().sListContentField);
174 // ListSourceType: SQL
175 getContext().xObjectModel->setPropertyValue("ListSourceType", makeAny((sal_Int32)ListSourceType_SQL));
177 if (isListBox())
179 // BoundColumn: 1
180 getContext().xObjectModel->setPropertyValue("BoundColumn", makeAny((sal_Int16)1));
182 // build the statement to set as list source
183 OUString sStatement = "SELECT " +
184 OUString( getSettings().sListContentField ) + ", " + OUString( getSettings().sLinkedListField ) +
185 " FROM " + OUString( getSettings().sListContentTable );
186 Sequence< OUString > aListSource(1);
187 aListSource[0] = sStatement;
188 getContext().xObjectModel->setPropertyValue("ListSource", makeAny(aListSource));
190 else
192 // build the statement to set as list source
193 OUString sStatement = "SELECT DISTINCT " +
194 OUString( getSettings().sListContentField ) +
195 " FROM " + OUString( getSettings().sListContentTable );
196 getContext().xObjectModel->setPropertyValue( "ListSource", makeAny(OUString(sStatement)));
199 // the bound field
200 getContext().xObjectModel->setPropertyValue("DataField", makeAny(OUString(getSettings().sLinkedFormField)));
202 catch(const Exception&)
204 OSL_FAIL("OListComboWizard::implApplySettings: could not set the property values for the listbox!");
209 bool OListComboWizard::onFinish()
211 if ( !OControlWizard::onFinish() )
212 return false;
214 implApplySettings();
215 return true;
218 Reference< XNameAccess > OLCPage::getTables(bool _bNeedIt)
220 Reference< XConnection > xConn = getFormConnection();
221 DBG_ASSERT(!_bNeedIt || xConn.is(), "OLCPage::getTables: should have an active connection when reaching this page!");
222 (void)_bNeedIt;
224 Reference< XTablesSupplier > xSuppTables(xConn, UNO_QUERY);
225 Reference< XNameAccess > xTables;
226 if (xSuppTables.is())
227 xTables = xSuppTables->getTables();
229 DBG_ASSERT(!_bNeedIt || xTables.is() || !xConn.is(), "OLCPage::getTables: got no tables from the connection!");
231 return xTables;
235 Sequence< OUString > OLCPage::getTableFields(bool _bNeedIt)
237 Reference< XNameAccess > xTables = getTables(_bNeedIt);
238 Sequence< OUString > aColumnNames;
239 if (xTables.is())
243 // the list table as XColumnsSupplier
244 Reference< XColumnsSupplier > xSuppCols;
245 xTables->getByName(getSettings().sListContentTable) >>= xSuppCols;
246 DBG_ASSERT(!_bNeedIt || xSuppCols.is(), "OLCPage::getTableFields: no columns supplier!");
248 // the columns
249 Reference< XNameAccess > xColumns;
250 if (xSuppCols.is())
251 xColumns = xSuppCols->getColumns();
253 // the column names
254 if (xColumns.is())
255 aColumnNames = xColumns->getElementNames();
257 catch(const Exception&)
259 DBG_ASSERT(!_bNeedIt, "OLinkFieldsPage::initializePage: caught an exception while retrieving the columns!");
262 return aColumnNames;
265 OContentTableSelection::OContentTableSelection( OListComboWizard* _pParent )
266 :OLCPage(_pParent, "TableSelectionPage", "modules/sabpilot/ui/contenttablepage.ui")
268 get(m_pSelectTable, "table");
270 enableFormDatasourceDisplay();
272 m_pSelectTable->SetDoubleClickHdl(LINK(this, OContentTableSelection, OnTableDoubleClicked));
273 m_pSelectTable->SetSelectHdl(LINK(this, OContentTableSelection, OnTableSelected));
276 OContentTableSelection::~OContentTableSelection()
278 disposeOnce();
281 void OContentTableSelection::dispose()
283 m_pSelectTable.clear();
284 OLCPage::dispose();
287 void OContentTableSelection::ActivatePage()
289 OLCPage::ActivatePage();
290 m_pSelectTable->GrabFocus();
294 bool OContentTableSelection::canAdvance() const
296 if (!OLCPage::canAdvance())
297 return false;
299 return 0 != m_pSelectTable->GetSelectEntryCount();
303 IMPL_LINK( OContentTableSelection, OnTableSelected, ListBox*, /*_pListBox*/ )
305 updateDialogTravelUI();
306 return 0L;
310 IMPL_LINK( OContentTableSelection, OnTableDoubleClicked, ListBox*, _pListBox )
312 if (_pListBox->GetSelectEntryCount())
313 getDialog()->travelNext();
314 return 0L;
318 void OContentTableSelection::initializePage()
320 OLCPage::initializePage();
322 // fill the list with the table name
323 m_pSelectTable->Clear();
326 Reference< XNameAccess > xTables = getTables(true);
327 Sequence< OUString > aTableNames;
328 if (xTables.is())
329 aTableNames = xTables->getElementNames();
330 fillListBox(*m_pSelectTable, aTableNames);
332 catch(const Exception&)
334 OSL_FAIL("OContentTableSelection::initializePage: could not retrieve the table names!");
337 m_pSelectTable->SelectEntry(getSettings().sListContentTable);
341 bool OContentTableSelection::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
343 if (!OLCPage::commitPage(_eReason))
344 return false;
346 OListComboSettings& rSettings = getSettings();
347 rSettings.sListContentTable = m_pSelectTable->GetSelectEntry();
348 if (rSettings.sListContentTable.isEmpty() && (::svt::WizardTypes::eTravelBackward != _eReason))
349 // need to select a table
350 return false;
352 return true;
355 OContentFieldSelection::OContentFieldSelection( OListComboWizard* _pParent )
356 :OLCPage(_pParent, "FieldSelectionPage", "modules/sabpilot/ui/contentfieldpage.ui")
358 get(m_pSelectTableField, "selectfield");
359 get(m_pDisplayedField, "displayfield");
360 get(m_pInfo, "info");
361 m_pInfo->SetText(ModuleRes( isListBox() ? RID_STR_FIELDINFO_LISTBOX : RID_STR_FIELDINFO_COMBOBOX).toString());
362 m_pSelectTableField->SetSelectHdl(LINK(this, OContentFieldSelection, OnFieldSelected));
363 m_pSelectTableField->SetDoubleClickHdl(LINK(this, OContentFieldSelection, OnTableDoubleClicked));
366 OContentFieldSelection::~OContentFieldSelection()
368 disposeOnce();
371 void OContentFieldSelection::dispose()
373 m_pSelectTableField.clear();
374 m_pDisplayedField.clear();
375 m_pInfo.clear();
376 OLCPage::dispose();
379 void OContentFieldSelection::ActivatePage()
381 OLCPage::ActivatePage();
385 void OContentFieldSelection::initializePage()
387 OLCPage::initializePage();
389 // fill the list of fields
390 fillListBox(*m_pSelectTableField, getTableFields(true));
392 m_pSelectTableField->SelectEntry(getSettings().sListContentField);
393 m_pDisplayedField->SetText(getSettings().sListContentField);
397 bool OContentFieldSelection::canAdvance() const
399 if (!OLCPage::canAdvance())
400 return false;
402 return 0 != m_pSelectTableField->GetSelectEntryCount();
406 IMPL_LINK( OContentFieldSelection, OnTableDoubleClicked, ListBox*, /*NOTINTERESTEDIN*/ )
408 if (m_pSelectTableField->GetSelectEntryCount())
409 getDialog()->travelNext();
410 return 0L;
414 IMPL_LINK( OContentFieldSelection, OnFieldSelected, ListBox*, /*NOTINTERESTEDIN*/ )
416 updateDialogTravelUI();
417 m_pDisplayedField->SetText(m_pSelectTableField->GetSelectEntry());
418 return 0L;
422 bool OContentFieldSelection::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
424 if (!OLCPage::commitPage(_eReason))
425 return false;
427 getSettings().sListContentField = m_pSelectTableField->GetSelectEntry();
429 return true;
432 OLinkFieldsPage::OLinkFieldsPage( OListComboWizard* _pParent )
433 :OLCPage(_pParent, "FieldLinkPage", "modules/sabpilot/ui/fieldlinkpage.ui")
435 get(m_pValueListField, "valuefield");
436 get(m_pTableField, "listtable");
438 m_pValueListField->SetModifyHdl(LINK(this, OLinkFieldsPage, OnSelectionModified));
439 m_pTableField->SetModifyHdl(LINK(this, OLinkFieldsPage, OnSelectionModified));
440 m_pValueListField->SetSelectHdl(LINK(this, OLinkFieldsPage, OnSelectionModified));
441 m_pTableField->SetSelectHdl(LINK(this, OLinkFieldsPage, OnSelectionModified));
444 OLinkFieldsPage::~OLinkFieldsPage()
446 disposeOnce();
449 void OLinkFieldsPage::dispose()
451 m_pValueListField.clear();
452 m_pTableField.clear();
453 OLCPage::dispose();
456 void OLinkFieldsPage::ActivatePage()
458 OLCPage::ActivatePage();
459 m_pValueListField->GrabFocus();
463 void OLinkFieldsPage::initializePage()
465 OLCPage::initializePage();
467 // fill the value list
468 fillListBox(*m_pValueListField, getContext().aFieldNames);
469 // fill the table field list
470 fillListBox(*m_pTableField, getTableFields(true));
472 // the initial selections
473 m_pValueListField->SetText(getSettings().sLinkedFormField);
474 m_pTableField->SetText(getSettings().sLinkedListField);
476 implCheckFinish();
480 bool OLinkFieldsPage::canAdvance() const
482 // we're on the last page here, no travelNext allowed ...
483 return false;
487 void OLinkFieldsPage::implCheckFinish()
489 bool bInvalidSelection = (COMBOBOX_ENTRY_NOTFOUND == m_pValueListField->GetEntryPos(m_pValueListField->GetText()));
490 bInvalidSelection |= (COMBOBOX_ENTRY_NOTFOUND == m_pTableField->GetEntryPos(m_pTableField->GetText()));
491 getDialog()->enableButtons(WizardButtonFlags::FINISH, !bInvalidSelection);
495 IMPL_LINK_NOARG(OLinkFieldsPage, OnSelectionModified)
497 implCheckFinish();
498 return 0L;
502 bool OLinkFieldsPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
504 if (!OLCPage::commitPage(_eReason))
505 return false;
507 getSettings().sLinkedFormField = m_pValueListField->GetText();
508 getSettings().sLinkedListField = m_pTableField->GetText();
510 return true;
513 OComboDBFieldPage::OComboDBFieldPage( OControlWizard* _pParent )
514 :ODBFieldPage(_pParent)
516 setDescriptionText(ModuleRes(RID_STR_COMBOWIZ_DBFIELD).toString());
520 OUString& OComboDBFieldPage::getDBFieldSetting()
522 return getSettings().sLinkedFormField;
526 void OComboDBFieldPage::ActivatePage()
528 ODBFieldPage::ActivatePage();
529 getDialog()->enableButtons(WizardButtonFlags::FINISH, true);
533 bool OComboDBFieldPage::canAdvance() const
535 // we're on the last page here, no travelNext allowed ...
536 return false;
540 } // namespace dbp
543 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */