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