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 <sal/config.h>
24 #include "gridwizard.hxx"
25 #include <com/sun/star/sdbc/DataType.hpp>
26 #include <com/sun/star/form/XGridColumnFactory.hpp>
27 #include <com/sun/star/awt/MouseWheelBehavior.hpp>
28 #include <com/sun/star/container/XNameContainer.hpp>
29 #include <sal/log.hxx>
30 #include <tools/debug.hxx>
31 #include "dbptools.hxx"
34 #define GW_STATE_DATASOURCE_SELECTION 0
35 #define GW_STATE_FIELDSELECTION 1
42 using namespace ::com::sun::star::uno
;
43 using namespace ::com::sun::star::lang
;
44 using namespace ::com::sun::star::beans
;
45 using namespace ::com::sun::star::sdbc
;
46 using namespace ::com::sun::star::container
;
47 using namespace ::com::sun::star::form
;
48 using namespace ::com::sun::star::awt
;
50 OGridWizard::OGridWizard(weld::Window
* _pParent
,
51 const Reference
< XPropertySet
>& _rxObjectModel
, const Reference
< XComponentContext
>& _rxContext
)
52 : OControlWizard(_pParent
, _rxObjectModel
, _rxContext
)
53 , m_bHadDataSelection(true)
55 initControlSettings(&m_aSettings
);
57 m_xPrevPage
->set_help_id(HID_GRIDWIZARD_PREVIOUS
);
58 m_xNextPage
->set_help_id(HID_GRIDWIZARD_NEXT
);
59 m_xCancel
->set_help_id(HID_GRIDWIZARD_CANCEL
);
60 m_xFinish
->set_help_id(HID_GRIDWIZARD_FINISH
);
61 setTitleBase(compmodule::ModuleRes(RID_STR_GRIDWIZARD_TITLE
));
63 // if we do not need the data source selection page ...
64 if (!needDatasourceSelection())
67 m_bHadDataSelection
= false;
71 bool OGridWizard::approveControl(sal_Int16 _nClassId
)
73 if (FormComponentType::GRIDCONTROL
!= _nClassId
)
76 Reference
< XGridColumnFactory
> xColumnFactory(getContext().xObjectModel
, UNO_QUERY
);
77 return xColumnFactory
.is();
80 void OGridWizard::implApplySettings()
82 const OControlWizardContext
& rContext
= getContext();
84 // the factory for the columns
85 Reference
< XGridColumnFactory
> xColumnFactory(rContext
.xObjectModel
, UNO_QUERY
);
86 DBG_ASSERT(xColumnFactory
.is(), "OGridWizard::implApplySettings: should never have made it 'til here!");
87 // (if we're here, what the hell happened in approveControl??)
89 // the container for the columns
90 Reference
< XNameContainer
> xColumnContainer(rContext
.xObjectModel
, UNO_QUERY
);
91 DBG_ASSERT(xColumnContainer
.is(), "OGridWizard::implApplySettings: no container!");
93 if (!xColumnFactory
.is() || !xColumnContainer
.is())
96 static constexpr OUString s_sMouseWheelBehavior
= u
"MouseWheelBehavior"_ustr
;
97 static constexpr OUString s_sEmptyString
= u
""_ustr
;
99 // collect "descriptors" for the to-be-created (grid)columns
100 std::vector
< OUString
> aColumnServiceNames
; // service names to be used with the XGridColumnFactory
101 std::vector
< OUString
> aColumnLabelPostfixes
; // postfixes to append to the column labels
102 std::vector
< OUString
> aFormFieldNames
; // data field names
104 aColumnServiceNames
.reserve(getSettings().aSelectedFields
.getLength());
105 aColumnLabelPostfixes
.reserve(getSettings().aSelectedFields
.getLength());
106 aFormFieldNames
.reserve(getSettings().aSelectedFields
.getLength());
108 // loop through the selected field names
109 const OUString
* pSelectedFields
= getSettings().aSelectedFields
.getConstArray();
110 const OUString
* pEnd
= pSelectedFields
+ getSettings().aSelectedFields
.getLength();
111 for (;pSelectedFields
< pEnd
; ++pSelectedFields
)
113 // get the information for the selected column
114 sal_Int32 nFieldType
= DataType::OTHER
;
115 OControlWizardContext::TNameTypeMap::const_iterator aFind
= rContext
.aTypes
.find(*pSelectedFields
);
116 if ( aFind
!= rContext
.aTypes
.end() )
117 nFieldType
= aFind
->second
;
119 aFormFieldNames
.push_back(*pSelectedFields
);
123 case DataType::BOOLEAN
:
124 aColumnServiceNames
.push_back(OUString("CheckBox"));
125 aColumnLabelPostfixes
.push_back(s_sEmptyString
);
128 case DataType::TINYINT
:
129 case DataType::SMALLINT
:
130 case DataType::INTEGER
:
131 aColumnServiceNames
.push_back(OUString("NumericField"));
132 aColumnLabelPostfixes
.push_back(s_sEmptyString
);
135 case DataType::FLOAT
:
137 case DataType::DOUBLE
:
138 case DataType::NUMERIC
:
139 case DataType::DECIMAL
:
140 aColumnServiceNames
.push_back(OUString("FormattedField"));
141 aColumnLabelPostfixes
.push_back(s_sEmptyString
);
145 aColumnServiceNames
.push_back(OUString("DateField"));
146 aColumnLabelPostfixes
.push_back(s_sEmptyString
);
150 aColumnServiceNames
.push_back(OUString("TimeField"));
151 aColumnLabelPostfixes
.push_back(s_sEmptyString
);
154 case DataType::TIMESTAMP
:
155 aColumnServiceNames
.push_back(OUString("DateField"));
156 aColumnLabelPostfixes
.push_back(compmodule::ModuleRes(RID_STR_DATEPOSTFIX
));
158 aFormFieldNames
.push_back(*pSelectedFields
);
159 aColumnServiceNames
.push_back(OUString("TimeField"));
160 aColumnLabelPostfixes
.push_back(compmodule::ModuleRes(RID_STR_TIMEPOSTFIX
));
164 aColumnServiceNames
.push_back(OUString("TextField"));
165 aColumnLabelPostfixes
.push_back(s_sEmptyString
);
169 DBG_ASSERT( aFormFieldNames
.size() == aColumnServiceNames
.size()
170 && aColumnServiceNames
.size() == aColumnLabelPostfixes
.size(),
171 "OGridWizard::implApplySettings: inconsistent descriptor sequences!");
173 // now loop through the descriptions and create the (grid)columns out of th descriptors
175 Reference
< XNameAccess
> xExistenceChecker(xColumnContainer
);
177 std::vector
< OUString
>::const_iterator pColumnLabelPostfix
= aColumnLabelPostfixes
.begin();
178 std::vector
< OUString
>::const_iterator pFormFieldName
= aFormFieldNames
.begin();
180 for (const auto& rColumnServiceName
: aColumnServiceNames
)
182 // create a (grid)column for the (resultset)column
185 Reference
< XPropertySet
> xColumn( xColumnFactory
->createColumn(rColumnServiceName
), UNO_SET_THROW
);
186 Reference
< XPropertySetInfo
> xColumnPSI( xColumn
->getPropertySetInfo(), UNO_SET_THROW
);
188 OUString
sColumnName(rColumnServiceName
);
189 disambiguateName(xExistenceChecker
, sColumnName
);
191 // the data field the column should be bound to
192 xColumn
->setPropertyValue("DataField", Any(*pFormFieldName
));
194 xColumn
->setPropertyValue("Label", Any(*pFormFieldName
+ *pColumnLabelPostfix
));
195 // the width (<void/> => column will be auto-sized)
196 xColumn
->setPropertyValue("Width", Any());
198 if ( xColumnPSI
->hasPropertyByName( s_sMouseWheelBehavior
) )
199 xColumn
->setPropertyValue( s_sMouseWheelBehavior
, Any( MouseWheelBehavior::SCROLL_DISABLED
) );
202 xColumnContainer
->insertByName(sColumnName
, Any(xColumn
));
204 catch(const Exception
&)
206 SAL_WARN( "extensions.dbpilots", "OGridWizard::implApplySettings: "
207 "unexpected exception while creating the grid column for field " <<
211 ++pColumnLabelPostfix
;
217 std::unique_ptr
<BuilderPage
> OGridWizard::createPage(WizardState _nState
)
219 OUString
sIdent(OUString::number(_nState
));
220 weld::Container
* pPageContainer
= m_xAssistant
->append_page(sIdent
);
224 case GW_STATE_DATASOURCE_SELECTION
:
225 return std::make_unique
<OTableSelectionPage
>(pPageContainer
, this);
226 case GW_STATE_FIELDSELECTION
:
227 return std::make_unique
<OGridFieldsSelection
>(pPageContainer
, this);
233 vcl::WizardTypes::WizardState
OGridWizard::determineNextState( WizardState _nCurrentState
) const
235 switch (_nCurrentState
)
237 case GW_STATE_DATASOURCE_SELECTION
:
238 return GW_STATE_FIELDSELECTION
;
239 case GW_STATE_FIELDSELECTION
:
240 return WZS_INVALID_STATE
;
243 return WZS_INVALID_STATE
;
246 void OGridWizard::enterState(WizardState _nState
)
248 OControlWizard::enterState(_nState
);
250 enableButtons(WizardButtonFlags::PREVIOUS
, m_bHadDataSelection
? (GW_STATE_DATASOURCE_SELECTION
< _nState
) : GW_STATE_FIELDSELECTION
< _nState
);
251 enableButtons(WizardButtonFlags::NEXT
, GW_STATE_FIELDSELECTION
!= _nState
);
252 if (_nState
< GW_STATE_FIELDSELECTION
)
253 enableButtons(WizardButtonFlags::FINISH
, false);
255 if (GW_STATE_FIELDSELECTION
== _nState
)
256 defaultButton(WizardButtonFlags::FINISH
);
260 bool OGridWizard::leaveState(WizardState _nState
)
262 if (!OControlWizard::leaveState(_nState
))
265 if (GW_STATE_FIELDSELECTION
== _nState
)
266 defaultButton(WizardButtonFlags::NEXT
);
272 bool OGridWizard::onFinish()
274 if ( !OControlWizard::onFinish() )
282 OGridFieldsSelection::OGridFieldsSelection(weld::Container
* pPage
, OGridWizard
* pWizard
)
283 : OGridPage(pPage
, pWizard
, "modules/sabpilot/ui/gridfieldsselectionpage.ui", "GridFieldsSelection")
284 , m_xExistFields(m_xBuilder
->weld_tree_view("existingfields"))
285 , m_xSelectOne(m_xBuilder
->weld_button("fieldright"))
286 , m_xSelectAll(m_xBuilder
->weld_button("allfieldsright"))
287 , m_xDeselectOne(m_xBuilder
->weld_button("fieldleft"))
288 , m_xDeselectAll(m_xBuilder
->weld_button("allfieldsleft"))
289 , m_xSelFields(m_xBuilder
->weld_tree_view("selectedfields"))
291 enableFormDatasourceDisplay();
293 m_xSelectOne
->connect_clicked(LINK(this, OGridFieldsSelection
, OnMoveOneEntry
));
294 m_xSelectAll
->connect_clicked(LINK(this, OGridFieldsSelection
, OnMoveAllEntries
));
295 m_xDeselectOne
->connect_clicked(LINK(this, OGridFieldsSelection
, OnMoveOneEntry
));
296 m_xDeselectAll
->connect_clicked(LINK(this, OGridFieldsSelection
, OnMoveAllEntries
));
298 m_xExistFields
->connect_changed(LINK(this, OGridFieldsSelection
, OnEntrySelected
));
299 m_xSelFields
->connect_changed(LINK(this, OGridFieldsSelection
, OnEntrySelected
));
300 m_xExistFields
->connect_row_activated(LINK(this, OGridFieldsSelection
, OnEntryDoubleClicked
));
301 m_xSelFields
->connect_row_activated(LINK(this, OGridFieldsSelection
, OnEntryDoubleClicked
));
304 OGridFieldsSelection::~OGridFieldsSelection()
308 void OGridFieldsSelection::Activate()
310 OGridPage::Activate();
311 m_xExistFields
->grab_focus();
314 bool OGridFieldsSelection::canAdvance() const
317 // we're the last page in our wizard
320 void OGridFieldsSelection::initializePage()
322 OGridPage::initializePage();
324 const OControlWizardContext
& rContext
= getContext();
325 fillListBox(*m_xExistFields
, rContext
.aFieldNames
);
327 m_xSelFields
->clear();
328 const OGridSettings
& rSettings
= getSettings();
329 const OUString
* pSelected
= rSettings
.aSelectedFields
.getConstArray();
330 const OUString
* pEnd
= pSelected
+ rSettings
.aSelectedFields
.getLength();
331 for (; pSelected
< pEnd
; ++pSelected
)
333 m_xSelFields
->append_text(*pSelected
);
334 m_xExistFields
->remove_text(*pSelected
);
340 bool OGridFieldsSelection::commitPage( ::vcl::WizardTypes::CommitPageReason _eReason
)
342 if (!OGridPage::commitPage(_eReason
))
345 OGridSettings
& rSettings
= getSettings();
346 const sal_Int32 nSelected
= m_xSelFields
->n_children();
348 rSettings
.aSelectedFields
.realloc(nSelected
);
349 OUString
* pSelected
= rSettings
.aSelectedFields
.getArray();
351 for (sal_Int32 i
=0; i
<nSelected
; ++i
, ++pSelected
)
352 *pSelected
= m_xSelFields
->get_text(i
);
357 void OGridFieldsSelection::implCheckButtons()
359 m_xSelectOne
->set_sensitive(m_xExistFields
->count_selected_rows() != 0);
360 m_xSelectAll
->set_sensitive(m_xExistFields
->n_children() != 0);
362 m_xDeselectOne
->set_sensitive(m_xSelFields
->count_selected_rows() != 0);
363 m_xDeselectAll
->set_sensitive(m_xSelFields
->n_children() != 0);
365 getDialog()->enableButtons(WizardButtonFlags::FINISH
, 0 != m_xSelFields
->n_children());
368 IMPL_LINK(OGridFieldsSelection
, OnEntryDoubleClicked
, weld::TreeView
&, rList
, bool)
370 weld::Button
* pSimulateButton
= m_xExistFields
.get() == &rList
? m_xSelectOne
.get() : m_xDeselectOne
.get();
371 if (pSimulateButton
->get_sensitive())
372 OnMoveOneEntry(*pSimulateButton
);
376 IMPL_LINK_NOARG(OGridFieldsSelection
, OnEntrySelected
, weld::TreeView
&, void)
381 IMPL_LINK(OGridFieldsSelection
, OnMoveOneEntry
, weld::Button
&, rButton
, void)
383 bool bMoveRight
= (m_xSelectOne
.get() == &rButton
);
384 weld::TreeView
& rMoveTo
= bMoveRight
? *m_xSelFields
: *m_xExistFields
;
386 // the index of the selected entry
387 const sal_Int32 nSelected
= bMoveRight
? m_xExistFields
->get_selected_index() : m_xSelFields
->get_selected_index();
388 // the (original) relative position of the entry
389 int nRelativeIndex
= bMoveRight
? m_xExistFields
->get_id(nSelected
).toInt32() : m_xSelFields
->get_id(nSelected
).toInt32();
391 sal_Int32 nInsertPos
= -1;
393 { // need to determine an insert pos which reflects the original
395 while (nInsertPos
< rMoveTo
.n_children())
397 if (rMoveTo
.get_id(nInsertPos
).toInt32() > nRelativeIndex
)
403 // the text of the entry to move
404 OUString sMovingEntry
= bMoveRight
? m_xExistFields
->get_text(nSelected
) : m_xSelFields
->get_text(nSelected
);
406 // insert the entry preserving it's "relative position" entry data
407 OUString
sId(OUString::number(nRelativeIndex
));
408 rMoveTo
.insert(nullptr, nInsertPos
, &sMovingEntry
, &sId
, nullptr, nullptr, false, nullptr);
410 // remove the entry from its old list
413 sal_Int32 nSelectPos
= m_xExistFields
->get_selected_index();
414 m_xExistFields
->remove(nSelected
);
415 if ((nSelectPos
!= -1) && (nSelectPos
< m_xExistFields
->n_children()))
416 m_xExistFields
->select(nSelectPos
);
418 m_xExistFields
->grab_focus();
422 sal_Int32 nSelectPos
= m_xSelFields
->get_selected_index();
423 m_xSelFields
->remove(nSelected
);
424 if ((nSelectPos
!= -1) && (nSelectPos
< m_xSelFields
->n_children()))
425 m_xSelFields
->select(nSelectPos
);
427 m_xSelFields
->grab_focus();
433 IMPL_LINK(OGridFieldsSelection
, OnMoveAllEntries
, weld::Button
&, rButton
, void)
435 bool bMoveRight
= (m_xSelectAll
.get() == &rButton
);
436 m_xExistFields
->clear();
437 m_xSelFields
->clear();
438 fillListBox(bMoveRight
? *m_xSelFields
: *m_xExistFields
, getContext().aFieldNames
);
446 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */