Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "sal/config.h"
21 :
22 : #include <vector>
23 :
24 : #include "gridwizard.hxx"
25 : #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
26 : #include <com/sun/star/sdbc/DataType.hpp>
27 : #include <com/sun/star/form/XGridColumnFactory.hpp>
28 : #include <com/sun/star/awt/MouseWheelBehavior.hpp>
29 : #include <com/sun/star/container/XNameContainer.hpp>
30 : #include <sal/log.hxx>
31 : #include <tools/debug.hxx>
32 : #include "dbptools.hxx"
33 : #include "dbpilots.hrc"
34 :
35 : #define GW_STATE_DATASOURCE_SELECTION 0
36 : #define GW_STATE_FIELDSELECTION 1
37 :
38 :
39 : namespace dbp
40 : {
41 :
42 :
43 : using namespace ::com::sun::star::uno;
44 : using namespace ::com::sun::star::lang;
45 : using namespace ::com::sun::star::beans;
46 : using namespace ::com::sun::star::sdbc;
47 : using namespace ::com::sun::star::container;
48 : using namespace ::com::sun::star::form;
49 : using namespace ::com::sun::star::awt;
50 : using namespace ::svt;
51 :
52 0 : OGridWizard::OGridWizard( vcl::Window* _pParent,
53 : const Reference< XPropertySet >& _rxObjectModel, const Reference< XComponentContext >& _rxContext )
54 : :OControlWizard(_pParent, _rxObjectModel, _rxContext)
55 0 : ,m_bHadDataSelection(true)
56 : {
57 0 : initControlSettings(&m_aSettings);
58 :
59 0 : m_pPrevPage->SetHelpId(HID_GRIDWIZARD_PREVIOUS);
60 0 : m_pNextPage->SetHelpId(HID_GRIDWIZARD_NEXT);
61 0 : m_pCancel->SetHelpId(HID_GRIDWIZARD_CANCEL);
62 0 : m_pFinish->SetHelpId(HID_GRIDWIZARD_FINISH);
63 0 : setTitleBase(ModuleRes(RID_STR_GRIDWIZARD_TITLE).toString());
64 :
65 : // if we do not need the data source selection page ...
66 0 : if (!needDatasourceSelection())
67 : { // ... skip it!
68 0 : skip(1);
69 0 : m_bHadDataSelection = false;
70 : }
71 0 : }
72 :
73 :
74 0 : bool OGridWizard::approveControl(sal_Int16 _nClassId)
75 : {
76 0 : if (FormComponentType::GRIDCONTROL != _nClassId)
77 0 : return false;
78 :
79 0 : Reference< XGridColumnFactory > xColumnFactory(getContext().xObjectModel, UNO_QUERY);
80 0 : if (!xColumnFactory.is())
81 0 : return false;
82 :
83 0 : return true;
84 : }
85 :
86 :
87 0 : void OGridWizard::implApplySettings()
88 : {
89 0 : const OControlWizardContext& rContext = getContext();
90 :
91 : // the factory for the columns
92 0 : Reference< XGridColumnFactory > xColumnFactory(rContext.xObjectModel, UNO_QUERY);
93 : DBG_ASSERT(xColumnFactory.is(), "OGridWizard::implApplySettings: should never have made it 'til here!");
94 : // (if we're here, what the hell happened in approveControl??)
95 :
96 : // the container for the columns
97 0 : Reference< XNameContainer > xColumnContainer(rContext.xObjectModel, UNO_QUERY);
98 : DBG_ASSERT(xColumnContainer.is(), "OGridWizard::implApplySettings: no container!");
99 :
100 0 : if (!xColumnFactory.is() || !xColumnContainer.is())
101 0 : return;
102 :
103 : static const char s_sDataFieldProperty [] = "DataField";
104 : static const char s_sLabelProperty [] = "Label";
105 : static const char s_sWidthProperty [] = "Width";
106 : static const char s_sMouseWheelBehavior[] = "MouseWheelBehavior";
107 : static const char s_sEmptyString[] = "";
108 :
109 : // collect "descriptors" for the to-be-created (grid)columns
110 0 : std::vector< OUString > aColumnServiceNames; // service names to be used with the XGridColumnFactory
111 0 : std::vector< OUString > aColumnLabelPostfixes; // postfixes to append to the column labels
112 0 : std::vector< OUString > aFormFieldNames; // data field names
113 :
114 0 : aColumnServiceNames.reserve(getSettings().aSelectedFields.getLength());
115 0 : aColumnLabelPostfixes.reserve(getSettings().aSelectedFields.getLength());
116 0 : aFormFieldNames.reserve(getSettings().aSelectedFields.getLength());
117 :
118 : // loop through the selected field names
119 0 : const OUString* pSelectedFields = getSettings().aSelectedFields.getConstArray();
120 0 : const OUString* pEnd = pSelectedFields + getSettings().aSelectedFields.getLength();
121 0 : for (;pSelectedFields < pEnd; ++pSelectedFields)
122 : {
123 : // get the information for the selected column
124 0 : sal_Int32 nFieldType = DataType::OTHER;
125 0 : OControlWizardContext::TNameTypeMap::const_iterator aFind = rContext.aTypes.find(*pSelectedFields);
126 0 : if ( aFind != rContext.aTypes.end() )
127 0 : nFieldType = aFind->second;
128 :
129 0 : aFormFieldNames.push_back(*pSelectedFields);
130 0 : switch (nFieldType)
131 : {
132 : case DataType::BIT:
133 : case DataType::BOOLEAN:
134 0 : aColumnServiceNames.push_back(OUString("CheckBox"));
135 0 : aColumnLabelPostfixes.push_back(s_sEmptyString);
136 0 : break;
137 :
138 : case DataType::TINYINT:
139 : case DataType::SMALLINT:
140 : case DataType::INTEGER:
141 0 : aColumnServiceNames.push_back(OUString("NumericField"));
142 0 : aColumnLabelPostfixes.push_back(s_sEmptyString);
143 0 : break;
144 :
145 : case DataType::FLOAT:
146 : case DataType::REAL:
147 : case DataType::DOUBLE:
148 : case DataType::NUMERIC:
149 : case DataType::DECIMAL:
150 0 : aColumnServiceNames.push_back(OUString("FormattedField"));
151 0 : aColumnLabelPostfixes.push_back(s_sEmptyString);
152 0 : break;
153 :
154 : case DataType::DATE:
155 0 : aColumnServiceNames.push_back(OUString("DateField"));
156 0 : aColumnLabelPostfixes.push_back(s_sEmptyString);
157 0 : break;
158 :
159 : case DataType::TIME:
160 0 : aColumnServiceNames.push_back(OUString("TimeField"));
161 0 : aColumnLabelPostfixes.push_back(s_sEmptyString);
162 0 : break;
163 :
164 : case DataType::TIMESTAMP:
165 0 : aColumnServiceNames.push_back(OUString("DateField"));
166 0 : aColumnLabelPostfixes.push_back(ModuleRes(RID_STR_DATEPOSTFIX).toString());
167 :
168 0 : aFormFieldNames.push_back(*pSelectedFields);
169 0 : aColumnServiceNames.push_back(OUString("TimeField"));
170 0 : aColumnLabelPostfixes.push_back(ModuleRes(RID_STR_TIMEPOSTFIX).toString());
171 0 : break;
172 :
173 : default:
174 0 : aColumnServiceNames.push_back(OUString("TextField"));
175 0 : aColumnLabelPostfixes.push_back(s_sEmptyString);
176 : }
177 : }
178 :
179 : DBG_ASSERT( aFormFieldNames.size() == aColumnServiceNames.size()
180 : && aColumnServiceNames.size() == aColumnLabelPostfixes.size(),
181 : "OGridWizard::implApplySettings: inconsistent descriptor sequences!");
182 :
183 : // now loop through the descriptions and create the (grid)columns out of th descriptors
184 : {
185 0 : Reference< XNameAccess > xExistenceChecker(xColumnContainer.get());
186 :
187 0 : std::vector< OUString >::const_iterator pColumnServiceName = aColumnServiceNames.begin();
188 0 : std::vector< OUString >::const_iterator pColumnLabelPostfix = aColumnLabelPostfixes.begin();
189 0 : std::vector< OUString >::const_iterator pFormFieldName = aFormFieldNames.begin();
190 0 : std::vector< OUString >::const_iterator pColumnServiceNameEnd = aColumnServiceNames.end();
191 :
192 0 : for (;pColumnServiceName < pColumnServiceNameEnd; ++pColumnServiceName, ++pColumnLabelPostfix, ++pFormFieldName)
193 : {
194 : // create a (grid)column for the (resultset)column
195 : try
196 : {
197 0 : Reference< XPropertySet > xColumn( xColumnFactory->createColumn(*pColumnServiceName), UNO_SET_THROW );
198 0 : Reference< XPropertySetInfo > xColumnPSI( xColumn->getPropertySetInfo(), UNO_SET_THROW );
199 :
200 0 : OUString sColumnName(*pColumnServiceName);
201 0 : disambiguateName(xExistenceChecker, sColumnName);
202 :
203 : // the data field the column should be bound to
204 0 : xColumn->setPropertyValue(s_sDataFieldProperty, makeAny(*pFormFieldName));
205 : // the label
206 0 : xColumn->setPropertyValue(s_sLabelProperty, makeAny(OUString(*pFormFieldName) += *pColumnLabelPostfix));
207 : // the width (<void/> => column will be auto-sized)
208 0 : xColumn->setPropertyValue(s_sWidthProperty, Any());
209 :
210 0 : if ( xColumnPSI->hasPropertyByName( s_sMouseWheelBehavior ) )
211 0 : xColumn->setPropertyValue( s_sMouseWheelBehavior, makeAny( MouseWheelBehavior::SCROLL_DISABLED ) );
212 :
213 : // insert the column
214 0 : xColumnContainer->insertByName(sColumnName, makeAny(xColumn));
215 : }
216 0 : catch(const Exception&)
217 : {
218 : SAL_WARN( "extensions.dbpilots", "OGridWizard::implApplySettings: "
219 : "unexpected exception while creating the grid column for field " <<
220 : pFormFieldName->getStr() << "!" );
221 : }
222 0 : }
223 0 : }
224 : }
225 :
226 :
227 0 : VclPtr<TabPage> OGridWizard::createPage(WizardState _nState)
228 : {
229 0 : switch (_nState)
230 : {
231 : case GW_STATE_DATASOURCE_SELECTION:
232 0 : return VclPtr<OTableSelectionPage>::Create(this);
233 : case GW_STATE_FIELDSELECTION:
234 0 : return VclPtr<OGridFieldsSelection>::Create(this);
235 : }
236 0 : return VclPtr<TabPage>();
237 : }
238 :
239 :
240 0 : WizardTypes::WizardState OGridWizard::determineNextState( WizardState _nCurrentState ) const
241 : {
242 0 : switch (_nCurrentState)
243 : {
244 : case GW_STATE_DATASOURCE_SELECTION:
245 0 : return GW_STATE_FIELDSELECTION;
246 : case GW_STATE_FIELDSELECTION:
247 0 : return WZS_INVALID_STATE;
248 : }
249 :
250 0 : return WZS_INVALID_STATE;
251 : }
252 :
253 :
254 0 : void OGridWizard::enterState(WizardState _nState)
255 : {
256 0 : OControlWizard::enterState(_nState);
257 :
258 0 : enableButtons(WizardButtonFlags::PREVIOUS, m_bHadDataSelection ? (GW_STATE_DATASOURCE_SELECTION < _nState) : GW_STATE_FIELDSELECTION < _nState);
259 0 : enableButtons(WizardButtonFlags::NEXT, GW_STATE_FIELDSELECTION != _nState);
260 0 : if (_nState < GW_STATE_FIELDSELECTION)
261 0 : enableButtons(WizardButtonFlags::FINISH, false);
262 :
263 0 : if (GW_STATE_FIELDSELECTION == _nState)
264 0 : defaultButton(WizardButtonFlags::FINISH);
265 0 : }
266 :
267 :
268 0 : bool OGridWizard::leaveState(WizardState _nState)
269 : {
270 0 : if (!OControlWizard::leaveState(_nState))
271 0 : return false;
272 :
273 0 : if (GW_STATE_FIELDSELECTION == _nState)
274 0 : defaultButton(WizardButtonFlags::NEXT);
275 :
276 0 : return true;
277 : }
278 :
279 :
280 0 : bool OGridWizard::onFinish()
281 : {
282 0 : if ( !OControlWizard::onFinish() )
283 0 : return false;
284 :
285 0 : implApplySettings();
286 :
287 0 : return true;
288 : }
289 :
290 0 : OGridFieldsSelection::OGridFieldsSelection( OGridWizard* _pParent )
291 0 : :OGridPage(_pParent, "GridFieldsSelection", "modules/sabpilot/ui/gridfieldsselectionpage.ui")
292 : {
293 0 : get(m_pExistFields,"existingfields");
294 0 : get(m_pSelectOne,"fieldright");
295 0 : get(m_pSelectAll,"allfieldsright");
296 0 : get(m_pDeselectOne,"fieldleft");
297 0 : get(m_pDeselectAll,"allfieldsleft");
298 0 : get(m_pSelFields,"selectedfields");
299 :
300 0 : enableFormDatasourceDisplay();
301 :
302 0 : m_pSelectOne->SetClickHdl(LINK(this, OGridFieldsSelection, OnMoveOneEntry));
303 0 : m_pSelectAll->SetClickHdl(LINK(this, OGridFieldsSelection, OnMoveAllEntries));
304 0 : m_pDeselectOne->SetClickHdl(LINK(this, OGridFieldsSelection, OnMoveOneEntry));
305 0 : m_pDeselectAll->SetClickHdl(LINK(this, OGridFieldsSelection, OnMoveAllEntries));
306 :
307 0 : m_pExistFields->SetSelectHdl(LINK(this, OGridFieldsSelection, OnEntrySelected));
308 0 : m_pSelFields->SetSelectHdl(LINK(this, OGridFieldsSelection, OnEntrySelected));
309 0 : m_pExistFields->SetDoubleClickHdl(LINK(this, OGridFieldsSelection, OnEntryDoubleClicked));
310 0 : m_pSelFields->SetDoubleClickHdl(LINK(this, OGridFieldsSelection, OnEntryDoubleClicked));
311 0 : }
312 :
313 0 : OGridFieldsSelection::~OGridFieldsSelection()
314 : {
315 0 : disposeOnce();
316 0 : }
317 :
318 0 : void OGridFieldsSelection::dispose()
319 : {
320 0 : m_pExistFields.clear();
321 0 : m_pSelectOne.clear();
322 0 : m_pSelectAll.clear();
323 0 : m_pDeselectOne.clear();
324 0 : m_pDeselectAll.clear();
325 0 : m_pSelFields.clear();
326 0 : OGridPage::dispose();
327 0 : }
328 :
329 0 : void OGridFieldsSelection::ActivatePage()
330 : {
331 0 : OGridPage::ActivatePage();
332 0 : m_pExistFields->GrabFocus();
333 0 : }
334 :
335 :
336 0 : bool OGridFieldsSelection::canAdvance() const
337 : {
338 0 : return false;
339 : // we're the last page in our wizard
340 : }
341 :
342 :
343 0 : void OGridFieldsSelection::initializePage()
344 : {
345 0 : OGridPage::initializePage();
346 :
347 0 : const OControlWizardContext& rContext = getContext();
348 0 : fillListBox(*m_pExistFields, rContext.aFieldNames);
349 :
350 0 : m_pSelFields->Clear();
351 0 : const OGridSettings& rSettings = getSettings();
352 0 : const OUString* pSelected = rSettings.aSelectedFields.getConstArray();
353 0 : const OUString* pEnd = pSelected + rSettings.aSelectedFields.getLength();
354 0 : for (; pSelected < pEnd; ++pSelected)
355 : {
356 0 : m_pSelFields->InsertEntry(*pSelected);
357 0 : m_pExistFields->RemoveEntry(*pSelected);
358 : }
359 :
360 0 : implCheckButtons();
361 0 : }
362 :
363 :
364 0 : bool OGridFieldsSelection::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
365 : {
366 0 : if (!OGridPage::commitPage(_eReason))
367 0 : return false;
368 :
369 0 : OGridSettings& rSettings = getSettings();
370 0 : sal_uInt16 nSelected = m_pSelFields->GetEntryCount();
371 :
372 0 : rSettings.aSelectedFields.realloc(nSelected);
373 0 : OUString* pSelected = rSettings.aSelectedFields.getArray();
374 :
375 0 : for (sal_uInt16 i=0; i<nSelected; ++i, ++pSelected)
376 0 : *pSelected = m_pSelFields->GetEntry(i);
377 :
378 0 : return true;
379 : }
380 :
381 :
382 0 : void OGridFieldsSelection::implCheckButtons()
383 : {
384 0 : m_pSelectOne->Enable(m_pExistFields->GetSelectEntryCount() != 0);
385 0 : m_pSelectAll->Enable(m_pExistFields->GetEntryCount() != 0);
386 :
387 0 : m_pDeselectOne->Enable(m_pSelFields->GetSelectEntryCount() != 0);
388 0 : m_pDeselectAll->Enable(m_pSelFields->GetEntryCount() != 0);
389 :
390 0 : getDialog()->enableButtons(WizardButtonFlags::FINISH, 0 != m_pSelFields->GetEntryCount());
391 0 : }
392 :
393 :
394 0 : IMPL_LINK(OGridFieldsSelection, OnEntryDoubleClicked, ListBox*, _pList)
395 : {
396 0 : PushButton* pSimulateButton = m_pExistFields == _pList ? m_pSelectOne : m_pDeselectOne;
397 0 : if (pSimulateButton->IsEnabled())
398 0 : return OnMoveOneEntry( pSimulateButton );
399 : else
400 0 : return 1L;
401 : }
402 :
403 :
404 0 : IMPL_LINK(OGridFieldsSelection, OnEntrySelected, ListBox*, /*NOTINTERESTEDIN*/)
405 : {
406 0 : implCheckButtons();
407 0 : return 0L;
408 : }
409 :
410 :
411 0 : IMPL_LINK(OGridFieldsSelection, OnMoveOneEntry, PushButton*, _pButton)
412 : {
413 0 : bool bMoveRight = (m_pSelectOne == _pButton);
414 0 : ListBox& rMoveTo = bMoveRight ? *m_pSelFields : *m_pExistFields;
415 :
416 : // the index of the selected entry
417 0 : sal_uInt16 nSelected = bMoveRight ? m_pExistFields->GetSelectEntryPos() : m_pSelFields->GetSelectEntryPos();
418 : // the (original) relative position of the entry
419 0 : sal_IntPtr nRelativeIndex = reinterpret_cast<sal_IntPtr>(bMoveRight ? m_pExistFields->GetEntryData(nSelected) : m_pSelFields->GetEntryData(nSelected));
420 :
421 0 : sal_uInt16 nInsertPos = SAL_MAX_UINT16;
422 0 : if (!bMoveRight)
423 : { // need to determine an insert pos which reflects the original
424 0 : nInsertPos = 0;
425 0 : while (nInsertPos < rMoveTo.GetEntryCount())
426 : {
427 0 : if (reinterpret_cast<sal_IntPtr>(rMoveTo.GetEntryData(nInsertPos)) > nRelativeIndex)
428 0 : break;
429 0 : ++nInsertPos;
430 : }
431 : }
432 :
433 : // the text of the entry to move
434 0 : OUString sMovingEntry = bMoveRight ? m_pExistFields->GetEntry(nSelected) : m_pSelFields->GetEntry(nSelected);
435 :
436 : // insert the entry
437 0 : nInsertPos = rMoveTo.InsertEntry(sMovingEntry, nInsertPos);
438 : // preserve it's "relative position" entry data
439 0 : rMoveTo.SetEntryData(nInsertPos, reinterpret_cast<void*>(nRelativeIndex));
440 :
441 : // remove the entry from it's old list
442 0 : if (bMoveRight)
443 : {
444 0 : sal_Int32 nSelectPos = m_pExistFields->GetSelectEntryPos();
445 0 : m_pExistFields->RemoveEntry(nSelected);
446 0 : if ((LISTBOX_ENTRY_NOTFOUND != nSelectPos) && (nSelectPos < m_pExistFields->GetEntryCount()))
447 0 : m_pExistFields->SelectEntryPos(nSelectPos);
448 :
449 0 : m_pExistFields->GrabFocus();
450 : }
451 : else
452 : {
453 0 : sal_Int32 nSelectPos = m_pSelFields->GetSelectEntryPos();
454 0 : m_pSelFields->RemoveEntry(nSelected);
455 0 : if ((LISTBOX_ENTRY_NOTFOUND != nSelectPos) && (nSelectPos < m_pSelFields->GetEntryCount()))
456 0 : m_pSelFields->SelectEntryPos(nSelectPos);
457 :
458 0 : m_pSelFields->GrabFocus();
459 : }
460 :
461 0 : implCheckButtons();
462 0 : return 0;
463 : }
464 :
465 :
466 0 : IMPL_LINK(OGridFieldsSelection, OnMoveAllEntries, PushButton*, _pButton)
467 : {
468 0 : bool bMoveRight = (m_pSelectAll == _pButton);
469 0 : m_pExistFields->Clear();
470 0 : m_pSelFields->Clear();
471 0 : fillListBox(bMoveRight ? *m_pSelFields : *m_pExistFields, getContext().aFieldNames);
472 :
473 0 : implCheckButtons();
474 0 : return 0;
475 : }
476 :
477 :
478 0 : } // namespace dbp
479 :
480 :
481 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|