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