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