Update ooo320-m1
[ooovba.git] / extensions / source / dbpilots / gridwizard.cxx
blob1d903e86d90915302cd3de054b2ae667f8fd2bd3
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: gridwizard.cxx,v $
10 * $Revision: 1.19 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_extensions.hxx"
33 #include "gridwizard.hxx"
34 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
35 #include <com/sun/star/sdbc/DataType.hpp>
36 #include <comphelper/stl_types.hxx>
37 #include <tools/string.hxx>
38 #include <com/sun/star/form/XGridColumnFactory.hpp>
39 #include <com/sun/star/container/XNameContainer.hpp>
40 #include <tools/debug.hxx>
41 #include "dbptools.hxx"
42 #ifndef EXTENSIONS_INC_EXTENSIO_HRC
43 #include "extensio.hrc"
44 #endif
46 #define GW_STATE_DATASOURCE_SELECTION 0
47 #define GW_STATE_FIELDSELECTION 1
49 //.........................................................................
50 namespace dbp
52 //.........................................................................
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::lang;
56 using namespace ::com::sun::star::beans;
57 using namespace ::com::sun::star::sdbc;
58 using namespace ::com::sun::star::container;
59 using namespace ::com::sun::star::form;
60 using namespace ::svt;
62 //=====================================================================
63 //= OGridWizard
64 //=====================================================================
65 //---------------------------------------------------------------------
66 OGridWizard::OGridWizard( Window* _pParent,
67 const Reference< XPropertySet >& _rxObjectModel, const Reference< XMultiServiceFactory >& _rxORB )
68 :OControlWizard(_pParent, ModuleRes(RID_DLG_GRIDWIZARD), _rxObjectModel, _rxORB)
69 ,m_bHadDataSelection(sal_True)
71 initControlSettings(&m_aSettings);
73 m_pPrevPage->SetHelpId(HID_GRIDWIZARD_PREVIOUS);
74 m_pNextPage->SetHelpId(HID_GRIDWIZARD_NEXT);
75 m_pCancel->SetHelpId(HID_GRIDWIZARD_CANCEL);
76 m_pFinish->SetHelpId(HID_GRIDWIZARD_FINISH);
78 // if we do not need the data source selection page ...
79 if (!needDatasourceSelection())
80 { // ... skip it!
81 skip(1);
82 m_bHadDataSelection = sal_False;
86 //---------------------------------------------------------------------
87 sal_Bool OGridWizard::approveControl(sal_Int16 _nClassId)
89 if (FormComponentType::GRIDCONTROL != _nClassId)
90 return sal_False;
92 Reference< XGridColumnFactory > xColumnFactory(getContext().xObjectModel, UNO_QUERY);
93 if (!xColumnFactory.is())
94 return sal_False;
96 return sal_True;
99 //---------------------------------------------------------------------
100 void OGridWizard::implApplySettings()
102 const OControlWizardContext& rContext = getContext();
104 // the factory for the columns
105 Reference< XGridColumnFactory > xColumnFactory(rContext.xObjectModel, UNO_QUERY);
106 DBG_ASSERT(xColumnFactory.is(), "OGridWizard::implApplySettings: should never have made it 'til here!");
107 // (if we're here, what the hell happened in approveControl??)
109 // the container for the columns
110 Reference< XNameContainer > xColumnContainer(rContext.xObjectModel, UNO_QUERY);
111 DBG_ASSERT(xColumnContainer.is(), "OGridWizard::implApplySettings: no container!");
113 if (!xColumnFactory.is() || !xColumnContainer.is())
114 return;
116 static const ::rtl::OUString s_sDataFieldProperty = ::rtl::OUString::createFromAscii("DataField");
117 static const ::rtl::OUString s_sLabelProperty = ::rtl::OUString::createFromAscii("Label");
118 static const ::rtl::OUString s_sWidthProperty = ::rtl::OUString::createFromAscii("Width");
119 static const ::rtl::OUString s_sEmptyString;
121 // collect "descriptors" for the to-be-created (grid)columns
122 DECLARE_STL_VECTOR( ::rtl::OUString, StringArray );
123 StringArray aColumnServiceNames; // service names to be used with the XGridColumnFactory
124 StringArray aColumnLabelPostfixes; // postfixes to append to the column labels
125 StringArray aFormFieldNames; // data field names
127 aColumnServiceNames.reserve(getSettings().aSelectedFields.getLength());
128 aColumnLabelPostfixes.reserve(getSettings().aSelectedFields.getLength());
129 aFormFieldNames.reserve(getSettings().aSelectedFields.getLength());
131 // loop through the selected field names
132 const ::rtl::OUString* pSelectedFields = getSettings().aSelectedFields.getConstArray();
133 const ::rtl::OUString* pEnd = pSelectedFields + getSettings().aSelectedFields.getLength();
134 for (;pSelectedFields < pEnd; ++pSelectedFields)
136 // get the information for the selected column
137 sal_Int32 nFieldType = DataType::OTHER;
138 OControlWizardContext::TNameTypeMap::const_iterator aFind = rContext.aTypes.find(*pSelectedFields);
139 if ( aFind != rContext.aTypes.end() )
140 nFieldType = aFind->second;
142 aFormFieldNames.push_back(*pSelectedFields);
143 switch (nFieldType)
145 case DataType::BIT:
146 case DataType::BOOLEAN:
147 aColumnServiceNames.push_back(::rtl::OUString::createFromAscii("CheckBox"));
148 aColumnLabelPostfixes.push_back(s_sEmptyString);
149 break;
151 case DataType::TINYINT:
152 case DataType::SMALLINT:
153 case DataType::INTEGER:
154 aColumnServiceNames.push_back(::rtl::OUString::createFromAscii("NumericField"));
155 aColumnLabelPostfixes.push_back(s_sEmptyString);
156 break;
158 case DataType::FLOAT:
159 case DataType::REAL:
160 case DataType::DOUBLE:
161 case DataType::NUMERIC:
162 case DataType::DECIMAL:
163 aColumnServiceNames.push_back(::rtl::OUString::createFromAscii("FormattedField"));
164 aColumnLabelPostfixes.push_back(s_sEmptyString);
165 break;
167 case DataType::DATE:
168 aColumnServiceNames.push_back(::rtl::OUString::createFromAscii("DateField"));
169 aColumnLabelPostfixes.push_back(s_sEmptyString);
170 break;
172 case DataType::TIME:
173 aColumnServiceNames.push_back(::rtl::OUString::createFromAscii("TimeField"));
174 aColumnLabelPostfixes.push_back(s_sEmptyString);
175 break;
177 case DataType::TIMESTAMP:
178 aColumnServiceNames.push_back(::rtl::OUString::createFromAscii("DateField"));
179 aColumnLabelPostfixes.push_back(String(ModuleRes(RID_STR_DATEPOSTFIX)));
181 aFormFieldNames.push_back(*pSelectedFields);
182 aColumnServiceNames.push_back(::rtl::OUString::createFromAscii("TimeField"));
183 aColumnLabelPostfixes.push_back(String(ModuleRes(RID_STR_TIMEPOSTFIX)));
184 break;
186 default:
187 aColumnServiceNames.push_back(::rtl::OUString::createFromAscii("TextField"));
188 aColumnLabelPostfixes.push_back(s_sEmptyString);
192 DBG_ASSERT( aFormFieldNames.size() == aColumnServiceNames.size()
193 && aColumnServiceNames.size() == aColumnLabelPostfixes.size(),
194 "OGridWizard::implApplySettings: inconsistent descriptor sequences!");
196 // now loop through the descriptions and create the (grid)columns out of th descriptors
198 Reference< XNameAccess > xExistenceChecker(xColumnContainer.get());
200 ConstStringArrayIterator pColumnServiceName = aColumnServiceNames.begin();
201 ConstStringArrayIterator pColumnLabelPostfix = aColumnLabelPostfixes.begin();
202 ConstStringArrayIterator pFormFieldName = aFormFieldNames.begin();
203 ConstStringArrayIterator pColumnServiceNameEnd = aColumnServiceNames.end();
205 for (;pColumnServiceName < pColumnServiceNameEnd; ++pColumnServiceName, ++pColumnLabelPostfix, ++pFormFieldName)
207 // create a (grid)column for the (resultset)column
210 Reference< XPropertySet > xColumn = xColumnFactory->createColumn(*pColumnServiceName);
212 ::rtl::OUString sColumnName(*pColumnServiceName);
213 disambiguateName(xExistenceChecker, sColumnName);
215 if (xColumn.is())
217 // the data field the column should be bound to
218 xColumn->setPropertyValue(s_sDataFieldProperty, makeAny(*pFormFieldName));
219 // the label
220 xColumn->setPropertyValue(s_sLabelProperty, makeAny(::rtl::OUString(*pFormFieldName) += *pColumnLabelPostfix));
221 // the width (<void/> => column will be auto-sized)
222 xColumn->setPropertyValue(s_sWidthProperty, Any());
224 // insert the column
225 xColumnContainer->insertByName(sColumnName, makeAny(xColumn));
228 catch(Exception&)
230 DBG_ERROR( ::rtl::OString("OGridWizard::implApplySettings: unexpected exception while creating the grid column for field ")
231 += ::rtl::OString(pFormFieldName->getStr(), pFormFieldName->getLength(), gsl_getSystemTextEncoding())
232 += ::rtl::OString("!"));
238 //---------------------------------------------------------------------
239 OWizardPage* OGridWizard::createPage(WizardState _nState)
241 switch (_nState)
243 case GW_STATE_DATASOURCE_SELECTION:
244 return new OTableSelectionPage(this);
245 case GW_STATE_FIELDSELECTION:
246 return new OGridFieldsSelection(this);
249 return NULL;
252 //---------------------------------------------------------------------
253 WizardTypes::WizardState OGridWizard::determineNextState( WizardState _nCurrentState ) const
255 switch (_nCurrentState)
257 case GW_STATE_DATASOURCE_SELECTION:
258 return GW_STATE_FIELDSELECTION;
259 case GW_STATE_FIELDSELECTION:
260 return WZS_INVALID_STATE;
263 return WZS_INVALID_STATE;
266 //---------------------------------------------------------------------
267 void OGridWizard::enterState(WizardState _nState)
269 OControlWizard::enterState(_nState);
271 enableButtons(WZB_PREVIOUS, m_bHadDataSelection ? (GW_STATE_DATASOURCE_SELECTION < _nState) : GW_STATE_FIELDSELECTION < _nState);
272 enableButtons(WZB_NEXT, GW_STATE_FIELDSELECTION != _nState);
273 if (_nState < GW_STATE_FIELDSELECTION)
274 enableButtons(WZB_FINISH, sal_False);
276 if (GW_STATE_FIELDSELECTION == _nState)
277 defaultButton(WZB_FINISH);
280 //---------------------------------------------------------------------
281 sal_Bool OGridWizard::leaveState(WizardState _nState)
283 if (!OControlWizard::leaveState(_nState))
284 return sal_False;
286 if (GW_STATE_FIELDSELECTION == _nState)
287 defaultButton(WZB_NEXT);
289 return sal_True;
292 //---------------------------------------------------------------------
293 sal_Bool OGridWizard::onFinish(sal_Int32 _nResult)
295 if (!OControlWizard::onFinish(_nResult))
296 return sal_False;
298 implApplySettings();
300 return sal_True;
303 //=====================================================================
304 //= OGridFieldsSelection
305 //=====================================================================
306 //---------------------------------------------------------------------
307 OGridFieldsSelection::OGridFieldsSelection( OGridWizard* _pParent )
308 :OGridPage(_pParent, ModuleRes(RID_PAGE_GW_FIELDSELECTION))
309 ,m_aFrame (this, ModuleRes(FL_FRAME))
310 ,m_aExistFieldsLabel (this, ModuleRes(FT_EXISTING_FIELDS))
311 ,m_aExistFields (this, ModuleRes(LB_EXISTING_FIELDS))
312 ,m_aSelectOne (this, ModuleRes(PB_FIELDRIGHT))
313 ,m_aSelectAll (this, ModuleRes(PB_ALLFIELDSRIGHT))
314 ,m_aDeselectOne (this, ModuleRes(PB_FIELDLEFT))
315 ,m_aDeselectAll (this, ModuleRes(PB_ALLFIELDSLEFT))
316 ,m_aSelFieldsLabel (this, ModuleRes(FT_SELECTED_FIELDS))
317 ,m_aSelFields (this, ModuleRes(LB_SELECTED_FIELDS))
319 FreeResource();
321 enableFormDatasourceDisplay();
323 m_aSelectOne.SetClickHdl(LINK(this, OGridFieldsSelection, OnMoveOneEntry));
324 m_aSelectAll.SetClickHdl(LINK(this, OGridFieldsSelection, OnMoveAllEntries));
325 m_aDeselectOne.SetClickHdl(LINK(this, OGridFieldsSelection, OnMoveOneEntry));
326 m_aDeselectAll.SetClickHdl(LINK(this, OGridFieldsSelection, OnMoveAllEntries));
328 m_aExistFields.SetSelectHdl(LINK(this, OGridFieldsSelection, OnEntrySelected));
329 m_aSelFields.SetSelectHdl(LINK(this, OGridFieldsSelection, OnEntrySelected));
330 m_aExistFields.SetDoubleClickHdl(LINK(this, OGridFieldsSelection, OnEntryDoubleClicked));
331 m_aSelFields.SetDoubleClickHdl(LINK(this, OGridFieldsSelection, OnEntryDoubleClicked));
334 //---------------------------------------------------------------------
335 void OGridFieldsSelection::ActivatePage()
337 OGridPage::ActivatePage();
338 m_aExistFields.GrabFocus();
341 //---------------------------------------------------------------------
342 bool OGridFieldsSelection::canAdvance() const
344 return false;
345 // we're the last page in our wizard
348 //---------------------------------------------------------------------
349 void OGridFieldsSelection::initializePage()
351 OGridPage::initializePage();
353 const OControlWizardContext& rContext = getContext();
354 fillListBox(m_aExistFields, rContext.aFieldNames);
356 m_aSelFields.Clear();
357 const OGridSettings& rSettings = getSettings();
358 const ::rtl::OUString* pSelected = rSettings.aSelectedFields.getConstArray();
359 const ::rtl::OUString* pEnd = pSelected + rSettings.aSelectedFields.getLength();
360 for (; pSelected < pEnd; ++pSelected)
362 m_aSelFields.InsertEntry(*pSelected);
363 m_aExistFields.RemoveEntry(*pSelected);
366 implCheckButtons();
369 //---------------------------------------------------------------------
370 sal_Bool OGridFieldsSelection::commitPage( CommitPageReason _eReason )
372 if (!OGridPage::commitPage(_eReason))
373 return sal_False;
375 OGridSettings& rSettings = getSettings();
376 USHORT nSelected = m_aSelFields.GetEntryCount();
378 rSettings.aSelectedFields.realloc(nSelected);
379 ::rtl::OUString* pSelected = rSettings.aSelectedFields.getArray();
381 for (USHORT i=0; i<nSelected; ++i, ++pSelected)
382 *pSelected = m_aSelFields.GetEntry(i);
384 return sal_True;
387 //---------------------------------------------------------------------
388 void OGridFieldsSelection::implCheckButtons()
390 m_aSelectOne.Enable(m_aExistFields.GetSelectEntryCount() != 0);
391 m_aSelectAll.Enable(m_aExistFields.GetEntryCount() != 0);
393 m_aDeselectOne.Enable(m_aSelFields.GetSelectEntryCount() != 0);
394 m_aDeselectAll.Enable(m_aSelFields.GetEntryCount() != 0);
396 getDialog()->enableButtons(WZB_FINISH, 0 != m_aSelFields.GetEntryCount());
399 //---------------------------------------------------------------------
400 IMPL_LINK(OGridFieldsSelection, OnEntryDoubleClicked, ListBox*, _pList)
402 PushButton* pSimulateButton = &m_aExistFields == _pList ? &m_aSelectOne : &m_aDeselectOne;
403 if (pSimulateButton->IsEnabled())
404 return OnMoveOneEntry( pSimulateButton );
405 else
406 return 1L;
409 //---------------------------------------------------------------------
410 IMPL_LINK(OGridFieldsSelection, OnEntrySelected, ListBox*, /*NOTINTERESTEDIN*/)
412 implCheckButtons();
413 return 0L;
416 //---------------------------------------------------------------------
417 IMPL_LINK(OGridFieldsSelection, OnMoveOneEntry, PushButton*, _pButton)
419 sal_Bool bMoveRight = (&m_aSelectOne == _pButton);
420 ListBox& rMoveTo = bMoveRight ? m_aSelFields : m_aExistFields;
422 // the index of the selected entry
423 USHORT nSelected = bMoveRight ? m_aExistFields.GetSelectEntryPos() : m_aSelFields.GetSelectEntryPos();
424 // the (original) relative position of the entry
425 sal_IntPtr nRelativeIndex = reinterpret_cast<sal_IntPtr>(bMoveRight ? m_aExistFields.GetEntryData(nSelected) : m_aSelFields.GetEntryData(nSelected));
427 USHORT nInsertPos = LISTBOX_APPEND;
428 if (!bMoveRight)
429 { // need to determine an insert pos which reflects the original
430 nInsertPos = 0;
431 while (nInsertPos < rMoveTo.GetEntryCount())
433 if (reinterpret_cast<sal_IntPtr>(rMoveTo.GetEntryData(nInsertPos)) > nRelativeIndex)
434 break;
435 ++nInsertPos;
439 // the text of the entry to move
440 String sMovingEntry = bMoveRight ? m_aExistFields.GetEntry(nSelected) : m_aSelFields.GetEntry(nSelected);
442 // insert the entry
443 nInsertPos = rMoveTo.InsertEntry(sMovingEntry, nInsertPos);
444 // preserve it's "relative position" entry data
445 rMoveTo.SetEntryData(nInsertPos, reinterpret_cast<void*>(nRelativeIndex));
447 // remove the entry from it's old list
448 if (bMoveRight)
450 USHORT nSelectPos = m_aExistFields.GetSelectEntryPos();
451 m_aExistFields.RemoveEntry(nSelected);
452 if ((LISTBOX_ENTRY_NOTFOUND != nSelectPos) && (nSelectPos < m_aExistFields.GetEntryCount()))
453 m_aExistFields.SelectEntryPos(nSelectPos);
455 m_aExistFields.GrabFocus();
457 else
459 USHORT nSelectPos = m_aSelFields.GetSelectEntryPos();
460 m_aSelFields.RemoveEntry(nSelected);
461 if ((LISTBOX_ENTRY_NOTFOUND != nSelectPos) && (nSelectPos < m_aSelFields.GetEntryCount()))
462 m_aSelFields.SelectEntryPos(nSelectPos);
464 m_aSelFields.GrabFocus();
467 implCheckButtons();
468 return 0;
471 //---------------------------------------------------------------------
472 IMPL_LINK(OGridFieldsSelection, OnMoveAllEntries, PushButton*, _pButton)
474 sal_Bool bMoveRight = (&m_aSelectAll == _pButton);
475 m_aExistFields.Clear();
476 m_aSelFields.Clear();
477 fillListBox(bMoveRight ? m_aSelFields : m_aExistFields, getContext().aFieldNames);
479 implCheckButtons();
480 return 0;
483 //.........................................................................
484 } // namespace dbp
485 //.........................................................................