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 ::svt
;
47 using namespace ::dbtools
;
49 OListComboWizard::OListComboWizard( vcl::Window
* _pParent
,
50 const Reference
< XPropertySet
>& _rxObjectModel
, const Reference
< XComponentContext
>& _rxContext
)
51 :OControlWizard(_pParent
, _rxObjectModel
, _rxContext
)
53 ,m_bHadDataSelection(true)
55 initControlSettings(&m_aSettings
);
57 m_pPrevPage
->SetHelpId(HID_LISTWIZARD_PREVIOUS
);
58 m_pNextPage
->SetHelpId(HID_LISTWIZARD_NEXT
);
59 m_pCancel
->SetHelpId(HID_LISTWIZARD_CANCEL
);
60 m_pFinish
->SetHelpId(HID_LISTWIZARD_FINISH
);
62 // if we do not need the data source selection page ...
63 if (!needDatasourceSelection())
66 m_bHadDataSelection
= false;
71 bool OListComboWizard::approveControl(sal_Int16 _nClassId
)
75 case FormComponentType::LISTBOX
:
77 setTitleBase(compmodule::ModuleRes(RID_STR_LISTWIZARD_TITLE
));
79 case FormComponentType::COMBOBOX
:
81 setTitleBase(compmodule::ModuleRes(RID_STR_COMBOWIZARD_TITLE
));
88 VclPtr
<TabPage
> OListComboWizard::createPage(WizardState _nState
)
92 case LCW_STATE_DATASOURCE_SELECTION
:
93 return VclPtr
<OTableSelectionPage
>::Create(this);
94 case LCW_STATE_TABLESELECTION
:
95 return VclPtr
<OContentTableSelection
>::Create(this);
96 case LCW_STATE_FIELDSELECTION
:
97 return VclPtr
<OContentFieldSelection
>::Create(this);
98 case LCW_STATE_FIELDLINK
:
99 return VclPtr
<OLinkFieldsPage
>::Create(this);
100 case LCW_STATE_COMBODBFIELD
:
101 return VclPtr
<OComboDBFieldPage
>::Create(this);
104 return VclPtr
<TabPage
>();
108 WizardTypes::WizardState
OListComboWizard::determineNextState( WizardState _nCurrentState
) const
110 switch (_nCurrentState
)
112 case LCW_STATE_DATASOURCE_SELECTION
:
113 return LCW_STATE_TABLESELECTION
;
114 case LCW_STATE_TABLESELECTION
:
115 return LCW_STATE_FIELDSELECTION
;
116 case LCW_STATE_FIELDSELECTION
:
117 return getFinalState();
120 return WZS_INVALID_STATE
;
124 void OListComboWizard::enterState(WizardState _nState
)
126 OControlWizard::enterState(_nState
);
128 enableButtons(WizardButtonFlags::PREVIOUS
, m_bHadDataSelection
? (LCW_STATE_DATASOURCE_SELECTION
< _nState
) : LCW_STATE_TABLESELECTION
< _nState
);
129 enableButtons(WizardButtonFlags::NEXT
, getFinalState() != _nState
);
130 if (_nState
< getFinalState())
131 enableButtons(WizardButtonFlags::FINISH
, false);
133 if (getFinalState() == _nState
)
134 defaultButton(WizardButtonFlags::FINISH
);
138 bool OListComboWizard::leaveState(WizardState _nState
)
140 if (!OControlWizard::leaveState(_nState
))
143 if (getFinalState() == _nState
)
144 defaultButton(WizardButtonFlags::NEXT
);
150 void OListComboWizard::implApplySettings()
154 // for quoting identifiers, we need the connection meta data
155 Reference
< XConnection
> xConn
= getFormConnection();
156 DBG_ASSERT(xConn
.is(), "OListComboWizard::implApplySettings: no connection, unable to quote!");
157 Reference
< XDatabaseMetaData
> xMetaData
;
159 xMetaData
= xConn
->getMetaData();
164 OUString sQuoteString
= xMetaData
->getIdentifierQuoteString();
165 if (isListBox()) // only when we have a listbox this should be not empty
166 getSettings().sLinkedListField
= quoteName(sQuoteString
, getSettings().sLinkedListField
);
168 OUString sCatalog
, sSchema
, sName
;
169 ::dbtools::qualifiedNameComponents( xMetaData
, getSettings().sListContentTable
, sCatalog
, sSchema
, sName
, ::dbtools::EComposeRule::InDataManipulation
);
170 getSettings().sListContentTable
= ::dbtools::composeTableNameForSelect( xConn
, sCatalog
, sSchema
, sName
);
172 getSettings().sListContentField
= quoteName(sQuoteString
, getSettings().sListContentField
);
175 // ListSourceType: SQL
176 getContext().xObjectModel
->setPropertyValue("ListSourceType", makeAny(sal_Int32(ListSourceType_SQL
)));
181 getContext().xObjectModel
->setPropertyValue("BoundColumn", makeAny(sal_Int16(1)));
183 // build the statement to set as list source
184 OUString sStatement
= "SELECT " +
185 getSettings().sListContentField
+ ", " + getSettings().sLinkedListField
+
186 " FROM " + getSettings().sListContentTable
;
187 Sequence
< OUString
> aListSource
{ sStatement
};
188 getContext().xObjectModel
->setPropertyValue("ListSource", makeAny(aListSource
));
192 // build the statement to set as list source
193 OUString sStatement
= "SELECT DISTINCT " +
194 getSettings().sListContentField
+
195 " FROM " + getSettings().sListContentTable
;
196 getContext().xObjectModel
->setPropertyValue( "ListSource", makeAny(sStatement
));
200 getContext().xObjectModel
->setPropertyValue("DataField", makeAny(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() )
218 Reference
< XNameAccess
> OLCPage::getTables()
220 Reference
< XConnection
> xConn
= getFormConnection();
221 DBG_ASSERT(xConn
.is(), "OLCPage::getTables: should have an active connection when reaching this page!");
223 Reference
< XTablesSupplier
> xSuppTables(xConn
, UNO_QUERY
);
224 Reference
< XNameAccess
> xTables
;
225 if (xSuppTables
.is())
226 xTables
= xSuppTables
->getTables();
228 DBG_ASSERT(xTables
.is() || !xConn
.is(), "OLCPage::getTables: got no tables from the connection!");
234 Sequence
< OUString
> OLCPage::getTableFields()
236 Reference
< XNameAccess
> xTables
= getTables();
237 Sequence
< OUString
> aColumnNames
;
242 // the list table as XColumnsSupplier
243 Reference
< XColumnsSupplier
> xSuppCols
;
244 xTables
->getByName(getSettings().sListContentTable
) >>= xSuppCols
;
245 DBG_ASSERT(xSuppCols
.is(), "OLCPage::getTableFields: no columns supplier!");
248 Reference
< XNameAccess
> xColumns
;
250 xColumns
= xSuppCols
->getColumns();
254 aColumnNames
= xColumns
->getElementNames();
256 catch(const Exception
&)
258 css::uno::Any
ex( cppu::getCaughtException() );
259 SAL_WARN( "extensions.dbpilots", "OLinkFieldsPage::initializePage: caught an exception while retrieving the columns! " << exceptionToString(ex
));
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()
281 void OContentTableSelection::dispose()
283 m_pSelectTable
.clear();
287 void OContentTableSelection::ActivatePage()
289 OLCPage::ActivatePage();
290 m_pSelectTable
->GrabFocus();
294 bool OContentTableSelection::canAdvance() const
296 if (!OLCPage::canAdvance())
299 return 0 != m_pSelectTable
->GetSelectedEntryCount();
303 IMPL_LINK_NOARG( OContentTableSelection
, OnTableSelected
, ListBox
&, void )
305 updateDialogTravelUI();
309 IMPL_LINK( OContentTableSelection
, OnTableDoubleClicked
, ListBox
&, _rListBox
, void )
311 if (_rListBox
.GetSelectedEntryCount())
312 getDialog()->travelNext();
316 void OContentTableSelection::initializePage()
318 OLCPage::initializePage();
320 // fill the list with the table name
321 m_pSelectTable
->Clear();
324 Reference
< XNameAccess
> xTables
= getTables();
325 Sequence
< OUString
> aTableNames
;
327 aTableNames
= xTables
->getElementNames();
328 fillListBox(*m_pSelectTable
, aTableNames
);
330 catch(const Exception
&)
332 OSL_FAIL("OContentTableSelection::initializePage: could not retrieve the table names!");
335 m_pSelectTable
->SelectEntry(getSettings().sListContentTable
);
339 bool OContentTableSelection::commitPage( ::svt::WizardTypes::CommitPageReason _eReason
)
341 if (!OLCPage::commitPage(_eReason
))
344 OListComboSettings
& rSettings
= getSettings();
345 rSettings
.sListContentTable
= m_pSelectTable
->GetSelectedEntry();
346 if (rSettings
.sListContentTable
.isEmpty() && (::svt::WizardTypes::eTravelBackward
!= _eReason
))
347 // need to select a table
353 OContentFieldSelection::OContentFieldSelection( OListComboWizard
* _pParent
)
354 :OLCPage(_pParent
, "FieldSelectionPage", "modules/sabpilot/ui/contentfieldpage.ui")
356 get(m_pSelectTableField
, "selectfield");
357 get(m_pDisplayedField
, "displayfield");
358 get(m_pInfo
, "info");
359 m_pInfo
->SetText(compmodule::ModuleRes( isListBox() ? RID_STR_FIELDINFO_LISTBOX
: RID_STR_FIELDINFO_COMBOBOX
));
360 m_pSelectTableField
->SetSelectHdl(LINK(this, OContentFieldSelection
, OnFieldSelected
));
361 m_pSelectTableField
->SetDoubleClickHdl(LINK(this, OContentFieldSelection
, OnTableDoubleClicked
));
364 OContentFieldSelection::~OContentFieldSelection()
369 void OContentFieldSelection::dispose()
371 m_pSelectTableField
.clear();
372 m_pDisplayedField
.clear();
378 void OContentFieldSelection::initializePage()
380 OLCPage::initializePage();
382 // fill the list of fields
383 fillListBox(*m_pSelectTableField
, getTableFields());
385 m_pSelectTableField
->SelectEntry(getSettings().sListContentField
);
386 m_pDisplayedField
->SetText(getSettings().sListContentField
);
390 bool OContentFieldSelection::canAdvance() const
392 if (!OLCPage::canAdvance())
395 return 0 != m_pSelectTableField
->GetSelectedEntryCount();
399 IMPL_LINK_NOARG( OContentFieldSelection
, OnTableDoubleClicked
, ListBox
&, void )
401 if (m_pSelectTableField
->GetSelectedEntryCount())
402 getDialog()->travelNext();
406 IMPL_LINK_NOARG( OContentFieldSelection
, OnFieldSelected
, ListBox
&, void )
408 updateDialogTravelUI();
409 m_pDisplayedField
->SetText(m_pSelectTableField
->GetSelectedEntry());
413 bool OContentFieldSelection::commitPage( ::svt::WizardTypes::CommitPageReason _eReason
)
415 if (!OLCPage::commitPage(_eReason
))
418 getSettings().sListContentField
= m_pSelectTableField
->GetSelectedEntry();
423 OLinkFieldsPage::OLinkFieldsPage( OListComboWizard
* _pParent
)
424 :OLCPage(_pParent
, "FieldLinkPage", "modules/sabpilot/ui/fieldlinkpage.ui")
426 get(m_pValueListField
, "valuefield");
427 get(m_pTableField
, "listtable");
429 m_pValueListField
->SetModifyHdl(LINK(this, OLinkFieldsPage
, OnSelectionModified
));
430 m_pTableField
->SetModifyHdl(LINK(this, OLinkFieldsPage
, OnSelectionModified
));
431 m_pValueListField
->SetSelectHdl(LINK(this, OLinkFieldsPage
, OnSelectionModifiedCombBox
));
432 m_pTableField
->SetSelectHdl(LINK(this, OLinkFieldsPage
, OnSelectionModifiedCombBox
));
435 OLinkFieldsPage::~OLinkFieldsPage()
440 void OLinkFieldsPage::dispose()
442 m_pValueListField
.clear();
443 m_pTableField
.clear();
447 void OLinkFieldsPage::ActivatePage()
449 OLCPage::ActivatePage();
450 m_pValueListField
->GrabFocus();
454 void OLinkFieldsPage::initializePage()
456 OLCPage::initializePage();
458 // fill the value list
459 fillListBox(*m_pValueListField
, getContext().aFieldNames
);
460 // fill the table field list
461 fillListBox(*m_pTableField
, getTableFields());
463 // the initial selections
464 m_pValueListField
->SetText(getSettings().sLinkedFormField
);
465 m_pTableField
->SetText(getSettings().sLinkedListField
);
471 bool OLinkFieldsPage::canAdvance() const
473 // we're on the last page here, no travelNext allowed ...
478 void OLinkFieldsPage::implCheckFinish()
480 bool bInvalidSelection
= (COMBOBOX_ENTRY_NOTFOUND
== m_pValueListField
->GetEntryPos(m_pValueListField
->GetText()));
481 bInvalidSelection
|= (COMBOBOX_ENTRY_NOTFOUND
== m_pTableField
->GetEntryPos(m_pTableField
->GetText()));
482 getDialog()->enableButtons(WizardButtonFlags::FINISH
, !bInvalidSelection
);
486 IMPL_LINK_NOARG(OLinkFieldsPage
, OnSelectionModified
, Edit
&, void)
491 IMPL_LINK_NOARG(OLinkFieldsPage
, OnSelectionModifiedCombBox
, ComboBox
&, void)
496 bool OLinkFieldsPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason
)
498 if (!OLCPage::commitPage(_eReason
))
501 getSettings().sLinkedFormField
= m_pValueListField
->GetText();
502 getSettings().sLinkedListField
= m_pTableField
->GetText();
507 OComboDBFieldPage::OComboDBFieldPage( OControlWizard
* _pParent
)
508 :ODBFieldPage(_pParent
)
510 setDescriptionText(compmodule::ModuleRes(RID_STR_COMBOWIZ_DBFIELD
));
513 OUString
& OComboDBFieldPage::getDBFieldSetting()
515 return static_cast<OListComboWizard
*>(getDialog())->getSettings().sLinkedFormField
;
518 void OComboDBFieldPage::ActivatePage()
520 ODBFieldPage::ActivatePage();
521 getDialog()->enableButtons(WizardButtonFlags::FINISH
, true);
524 bool OComboDBFieldPage::canAdvance() const
526 // we're on the last page here, no travelNext allowed ...
533 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */