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 <tools/string.hxx>
28 : #include <com/sun/star/form/XGridColumnFactory.hpp>
29 : #include <com/sun/star/awt/MouseWheelBehavior.hpp>
30 : #include <com/sun/star/container/XNameContainer.hpp>
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 : //=====================================================================
53 : //= OGridWizard
54 : //=====================================================================
55 : //---------------------------------------------------------------------
56 0 : OGridWizard::OGridWizard( Window* _pParent,
57 : const Reference< XPropertySet >& _rxObjectModel, const Reference< XComponentContext >& _rxContext )
58 : :OControlWizard(_pParent, ModuleRes(RID_DLG_GRIDWIZARD), _rxObjectModel, _rxContext)
59 0 : ,m_bHadDataSelection(sal_True)
60 : {
61 0 : initControlSettings(&m_aSettings);
62 :
63 0 : m_pPrevPage->SetHelpId(HID_GRIDWIZARD_PREVIOUS);
64 0 : m_pNextPage->SetHelpId(HID_GRIDWIZARD_NEXT);
65 0 : m_pCancel->SetHelpId(HID_GRIDWIZARD_CANCEL);
66 0 : m_pFinish->SetHelpId(HID_GRIDWIZARD_FINISH);
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 = sal_False;
73 : }
74 0 : }
75 :
76 : //---------------------------------------------------------------------
77 0 : sal_Bool OGridWizard::approveControl(sal_Int16 _nClassId)
78 : {
79 0 : if (FormComponentType::GRIDCONTROL != _nClassId)
80 0 : return sal_False;
81 :
82 0 : Reference< XGridColumnFactory > xColumnFactory(getContext().xObjectModel, UNO_QUERY);
83 0 : if (!xColumnFactory.is())
84 0 : return sal_False;
85 :
86 0 : return sal_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(String(ModuleRes(RID_STR_DATEPOSTFIX)));
170 :
171 0 : aFormFieldNames.push_back(*pSelectedFields);
172 0 : aColumnServiceNames.push_back(OUString("TimeField"));
173 0 : aColumnLabelPostfixes.push_back(String(ModuleRes(RID_STR_TIMEPOSTFIX)));
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, sal_False);
266 :
267 0 : if (GW_STATE_FIELDSELECTION == _nState)
268 0 : defaultButton(WZB_FINISH);
269 0 : }
270 :
271 : //---------------------------------------------------------------------
272 0 : sal_Bool OGridWizard::leaveState(WizardState _nState)
273 : {
274 0 : if (!OControlWizard::leaveState(_nState))
275 0 : return sal_False;
276 :
277 0 : if (GW_STATE_FIELDSELECTION == _nState)
278 0 : defaultButton(WZB_NEXT);
279 :
280 0 : return sal_True;
281 : }
282 :
283 : //---------------------------------------------------------------------
284 0 : sal_Bool OGridWizard::onFinish()
285 : {
286 0 : if ( !OControlWizard::onFinish() )
287 0 : return sal_False;
288 :
289 0 : implApplySettings();
290 :
291 0 : return sal_True;
292 : }
293 :
294 : //=====================================================================
295 : //= OGridFieldsSelection
296 : //=====================================================================
297 : //---------------------------------------------------------------------
298 0 : OGridFieldsSelection::OGridFieldsSelection( OGridWizard* _pParent )
299 : :OGridPage(_pParent, ModuleRes(RID_PAGE_GW_FIELDSELECTION))
300 : ,m_aFrame (this, ModuleRes(FL_FRAME))
301 : ,m_aExistFieldsLabel (this, ModuleRes(FT_EXISTING_FIELDS))
302 : ,m_aExistFields (this, ModuleRes(LB_EXISTING_FIELDS))
303 : ,m_aSelectOne (this, ModuleRes(PB_FIELDRIGHT))
304 : ,m_aSelectAll (this, ModuleRes(PB_ALLFIELDSRIGHT))
305 : ,m_aDeselectOne (this, ModuleRes(PB_FIELDLEFT))
306 : ,m_aDeselectAll (this, ModuleRes(PB_ALLFIELDSLEFT))
307 : ,m_aSelFieldsLabel (this, ModuleRes(FT_SELECTED_FIELDS))
308 0 : ,m_aSelFields (this, ModuleRes(LB_SELECTED_FIELDS))
309 : {
310 0 : FreeResource();
311 :
312 0 : enableFormDatasourceDisplay();
313 :
314 0 : m_aSelectOne.SetClickHdl(LINK(this, OGridFieldsSelection, OnMoveOneEntry));
315 0 : m_aSelectAll.SetClickHdl(LINK(this, OGridFieldsSelection, OnMoveAllEntries));
316 0 : m_aDeselectOne.SetClickHdl(LINK(this, OGridFieldsSelection, OnMoveOneEntry));
317 0 : m_aDeselectAll.SetClickHdl(LINK(this, OGridFieldsSelection, OnMoveAllEntries));
318 :
319 0 : m_aExistFields.SetSelectHdl(LINK(this, OGridFieldsSelection, OnEntrySelected));
320 0 : m_aSelFields.SetSelectHdl(LINK(this, OGridFieldsSelection, OnEntrySelected));
321 0 : m_aExistFields.SetDoubleClickHdl(LINK(this, OGridFieldsSelection, OnEntryDoubleClicked));
322 0 : m_aSelFields.SetDoubleClickHdl(LINK(this, OGridFieldsSelection, OnEntryDoubleClicked));
323 0 : }
324 :
325 : //---------------------------------------------------------------------
326 0 : void OGridFieldsSelection::ActivatePage()
327 : {
328 0 : OGridPage::ActivatePage();
329 0 : m_aExistFields.GrabFocus();
330 0 : }
331 :
332 : //---------------------------------------------------------------------
333 0 : bool OGridFieldsSelection::canAdvance() const
334 : {
335 0 : return false;
336 : // we're the last page in our wizard
337 : }
338 :
339 : //---------------------------------------------------------------------
340 0 : void OGridFieldsSelection::initializePage()
341 : {
342 0 : OGridPage::initializePage();
343 :
344 0 : const OControlWizardContext& rContext = getContext();
345 0 : fillListBox(m_aExistFields, rContext.aFieldNames);
346 :
347 0 : m_aSelFields.Clear();
348 0 : const OGridSettings& rSettings = getSettings();
349 0 : const OUString* pSelected = rSettings.aSelectedFields.getConstArray();
350 0 : const OUString* pEnd = pSelected + rSettings.aSelectedFields.getLength();
351 0 : for (; pSelected < pEnd; ++pSelected)
352 : {
353 0 : m_aSelFields.InsertEntry(*pSelected);
354 0 : m_aExistFields.RemoveEntry(*pSelected);
355 : }
356 :
357 0 : implCheckButtons();
358 0 : }
359 :
360 : //---------------------------------------------------------------------
361 0 : sal_Bool OGridFieldsSelection::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
362 : {
363 0 : if (!OGridPage::commitPage(_eReason))
364 0 : return sal_False;
365 :
366 0 : OGridSettings& rSettings = getSettings();
367 0 : sal_uInt16 nSelected = m_aSelFields.GetEntryCount();
368 :
369 0 : rSettings.aSelectedFields.realloc(nSelected);
370 0 : OUString* pSelected = rSettings.aSelectedFields.getArray();
371 :
372 0 : for (sal_uInt16 i=0; i<nSelected; ++i, ++pSelected)
373 0 : *pSelected = m_aSelFields.GetEntry(i);
374 :
375 0 : return sal_True;
376 : }
377 :
378 : //---------------------------------------------------------------------
379 0 : void OGridFieldsSelection::implCheckButtons()
380 : {
381 0 : m_aSelectOne.Enable(m_aExistFields.GetSelectEntryCount() != 0);
382 0 : m_aSelectAll.Enable(m_aExistFields.GetEntryCount() != 0);
383 :
384 0 : m_aDeselectOne.Enable(m_aSelFields.GetSelectEntryCount() != 0);
385 0 : m_aDeselectAll.Enable(m_aSelFields.GetEntryCount() != 0);
386 :
387 0 : getDialog()->enableButtons(WZB_FINISH, 0 != m_aSelFields.GetEntryCount());
388 0 : }
389 :
390 : //---------------------------------------------------------------------
391 0 : IMPL_LINK(OGridFieldsSelection, OnEntryDoubleClicked, ListBox*, _pList)
392 : {
393 0 : PushButton* pSimulateButton = &m_aExistFields == _pList ? &m_aSelectOne : &m_aDeselectOne;
394 0 : if (pSimulateButton->IsEnabled())
395 0 : return OnMoveOneEntry( pSimulateButton );
396 : else
397 0 : return 1L;
398 : }
399 :
400 : //---------------------------------------------------------------------
401 0 : IMPL_LINK(OGridFieldsSelection, OnEntrySelected, ListBox*, /*NOTINTERESTEDIN*/)
402 : {
403 0 : implCheckButtons();
404 0 : return 0L;
405 : }
406 :
407 : //---------------------------------------------------------------------
408 0 : IMPL_LINK(OGridFieldsSelection, OnMoveOneEntry, PushButton*, _pButton)
409 : {
410 0 : sal_Bool bMoveRight = (&m_aSelectOne == _pButton);
411 0 : ListBox& rMoveTo = bMoveRight ? m_aSelFields : m_aExistFields;
412 :
413 : // the index of the selected entry
414 0 : sal_uInt16 nSelected = bMoveRight ? m_aExistFields.GetSelectEntryPos() : m_aSelFields.GetSelectEntryPos();
415 : // the (original) relative position of the entry
416 0 : sal_IntPtr nRelativeIndex = reinterpret_cast<sal_IntPtr>(bMoveRight ? m_aExistFields.GetEntryData(nSelected) : m_aSelFields.GetEntryData(nSelected));
417 :
418 0 : sal_uInt16 nInsertPos = LISTBOX_APPEND;
419 0 : if (!bMoveRight)
420 : { // need to determine an insert pos which reflects the original
421 0 : nInsertPos = 0;
422 0 : while (nInsertPos < rMoveTo.GetEntryCount())
423 : {
424 0 : if (reinterpret_cast<sal_IntPtr>(rMoveTo.GetEntryData(nInsertPos)) > nRelativeIndex)
425 0 : break;
426 0 : ++nInsertPos;
427 : }
428 : }
429 :
430 : // the text of the entry to move
431 0 : String sMovingEntry = bMoveRight ? m_aExistFields.GetEntry(nSelected) : m_aSelFields.GetEntry(nSelected);
432 :
433 : // insert the entry
434 0 : nInsertPos = rMoveTo.InsertEntry(sMovingEntry, nInsertPos);
435 : // preserve it's "relative position" entry data
436 0 : rMoveTo.SetEntryData(nInsertPos, reinterpret_cast<void*>(nRelativeIndex));
437 :
438 : // remove the entry from it's old list
439 0 : if (bMoveRight)
440 : {
441 0 : sal_uInt16 nSelectPos = m_aExistFields.GetSelectEntryPos();
442 0 : m_aExistFields.RemoveEntry(nSelected);
443 0 : if ((LISTBOX_ENTRY_NOTFOUND != nSelectPos) && (nSelectPos < m_aExistFields.GetEntryCount()))
444 0 : m_aExistFields.SelectEntryPos(nSelectPos);
445 :
446 0 : m_aExistFields.GrabFocus();
447 : }
448 : else
449 : {
450 0 : sal_uInt16 nSelectPos = m_aSelFields.GetSelectEntryPos();
451 0 : m_aSelFields.RemoveEntry(nSelected);
452 0 : if ((LISTBOX_ENTRY_NOTFOUND != nSelectPos) && (nSelectPos < m_aSelFields.GetEntryCount()))
453 0 : m_aSelFields.SelectEntryPos(nSelectPos);
454 :
455 0 : m_aSelFields.GrabFocus();
456 : }
457 :
458 0 : implCheckButtons();
459 0 : return 0;
460 : }
461 :
462 : //---------------------------------------------------------------------
463 0 : IMPL_LINK(OGridFieldsSelection, OnMoveAllEntries, PushButton*, _pButton)
464 : {
465 0 : sal_Bool bMoveRight = (&m_aSelectAll == _pButton);
466 0 : m_aExistFields.Clear();
467 0 : m_aSelFields.Clear();
468 0 : fillListBox(bMoveRight ? m_aSelFields : m_aExistFields, getContext().aFieldNames);
469 :
470 0 : implCheckButtons();
471 0 : return 0;
472 : }
473 :
474 : //.........................................................................
475 0 : } // namespace dbp
476 : //.........................................................................
477 :
478 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|