merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / dbpilots / listcombowizard.cxx
blob162c609bfa9f41b0f1bec9060047b931be61a06b
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: listcombowizard.cxx,v $
10 * $Revision: 1.22 $
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 "listcombowizard.hxx"
34 #include "commonpagesdbp.hxx"
35 #include <com/sun/star/sdbc/XConnection.hpp>
36 #include <com/sun/star/sdbcx/XTablesSupplier.hpp>
37 #include <com/sun/star/container/XNameAccess.hpp>
38 #include <com/sun/star/form/ListSourceType.hpp>
39 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
40 #include <tools/debug.hxx>
41 #include <vcl/msgbox.hxx>
42 #include <connectivity/dbtools.hxx>
43 #ifndef EXTENSIONS_INC_EXTENSIO_HRC
44 #include "extensio.hrc"
45 #endif
46 #include <comphelper/extract.hxx>
48 //.........................................................................
49 namespace dbp
51 //.........................................................................
53 using namespace ::com::sun::star::uno;
54 using namespace ::com::sun::star::lang;
55 using namespace ::com::sun::star::beans;
56 using namespace ::com::sun::star::sdbc;
57 using namespace ::com::sun::star::sdbcx;
58 using namespace ::com::sun::star::container;
59 using namespace ::com::sun::star::form;
60 using namespace ::svt;
61 using namespace ::dbtools;
63 //=====================================================================
64 //= OListComboWizard
65 //=====================================================================
66 //---------------------------------------------------------------------
67 OListComboWizard::OListComboWizard( Window* _pParent,
68 const Reference< XPropertySet >& _rxObjectModel, const Reference< XMultiServiceFactory >& _rxORB )
69 :OControlWizard(_pParent, ModuleRes(RID_DLG_LISTCOMBOWIZARD), _rxObjectModel, _rxORB)
70 ,m_bListBox(sal_False)
71 ,m_bHadDataSelection(sal_True)
73 initControlSettings(&m_aSettings);
75 m_pPrevPage->SetHelpId(HID_LISTWIZARD_PREVIOUS);
76 m_pNextPage->SetHelpId(HID_LISTWIZARD_NEXT);
77 m_pCancel->SetHelpId(HID_LISTWIZARD_CANCEL);
78 m_pFinish->SetHelpId(HID_LISTWIZARD_FINISH);
80 // if we do not need the data source selection page ...
81 if (!needDatasourceSelection())
82 { // ... skip it!
83 skip(1);
84 m_bHadDataSelection = sal_False;
88 //---------------------------------------------------------------------
89 sal_Bool OListComboWizard::approveControl(sal_Int16 _nClassId)
91 switch (_nClassId)
93 case FormComponentType::LISTBOX:
94 m_bListBox = sal_True;
95 setTitleBase(String(ModuleRes(RID_STR_LISTWIZARD_TITLE)));
96 return sal_True;
97 case FormComponentType::COMBOBOX:
98 m_bListBox = sal_False;
99 setTitleBase(String(ModuleRes(RID_STR_COMBOWIZARD_TITLE)));
100 return sal_True;
102 return sal_False;
105 //---------------------------------------------------------------------
106 OWizardPage* OListComboWizard::createPage(WizardState _nState)
108 switch (_nState)
110 case LCW_STATE_DATASOURCE_SELECTION:
111 return new OTableSelectionPage(this);
112 case LCW_STATE_TABLESELECTION:
113 return new OContentTableSelection(this);
114 case LCW_STATE_FIELDSELECTION:
115 return new OContentFieldSelection(this);
116 case LCW_STATE_FIELDLINK:
117 return new OLinkFieldsPage(this);
118 case LCW_STATE_COMBODBFIELD:
119 return new OComboDBFieldPage(this);
122 return NULL;
125 //---------------------------------------------------------------------
126 WizardTypes::WizardState OListComboWizard::determineNextState( WizardState _nCurrentState ) const
128 switch (_nCurrentState)
130 case LCW_STATE_DATASOURCE_SELECTION:
131 return LCW_STATE_TABLESELECTION;
132 case LCW_STATE_TABLESELECTION:
133 return LCW_STATE_FIELDSELECTION;
134 case LCW_STATE_FIELDSELECTION:
135 return getFinalState();
138 return WZS_INVALID_STATE;
141 //---------------------------------------------------------------------
142 void OListComboWizard::enterState(WizardState _nState)
144 OControlWizard::enterState(_nState);
146 enableButtons(WZB_PREVIOUS, m_bHadDataSelection ? (LCW_STATE_DATASOURCE_SELECTION < _nState) : LCW_STATE_TABLESELECTION < _nState);
147 enableButtons(WZB_NEXT, getFinalState() != _nState);
148 if (_nState < getFinalState())
149 enableButtons(WZB_FINISH, sal_False);
151 if (getFinalState() == _nState)
152 defaultButton(WZB_FINISH);
155 //---------------------------------------------------------------------
156 sal_Bool OListComboWizard::leaveState(WizardState _nState)
158 if (!OControlWizard::leaveState(_nState))
159 return sal_False;
161 if (getFinalState() == _nState)
162 defaultButton(WZB_NEXT);
164 return sal_True;
167 //---------------------------------------------------------------------
168 void OListComboWizard::implApplySettings()
172 // for quoting identifiers, we need the connection meta data
173 Reference< XConnection > xConn = getFormConnection();
174 DBG_ASSERT(xConn.is(), "OListComboWizard::implApplySettings: no connection, unable to quote!");
175 Reference< XDatabaseMetaData > xMetaData;
176 if (xConn.is())
177 xMetaData = xConn->getMetaData();
179 // do some quotings
180 if (xMetaData.is())
182 ::rtl::OUString sQuoteString = xMetaData->getIdentifierQuoteString();
183 if (isListBox()) // only when we have a listbox this should be not empty
184 getSettings().sLinkedListField = quoteName(sQuoteString, getSettings().sLinkedListField);
186 ::rtl::OUString sCatalog, sSchema, sName;
187 ::dbtools::qualifiedNameComponents( xMetaData, getSettings().sListContentTable, sCatalog, sSchema, sName, ::dbtools::eInDataManipulation );
188 getSettings().sListContentTable = ::dbtools::composeTableNameForSelect( xConn, sCatalog, sSchema, sName );
190 getSettings().sListContentField = quoteName(sQuoteString, getSettings().sListContentField);
193 // ListSourceType: SQL
194 getContext().xObjectModel->setPropertyValue(::rtl::OUString::createFromAscii("ListSourceType"), makeAny((sal_Int32)ListSourceType_SQL));
196 if (isListBox())
198 // BoundColumn: 1
199 getContext().xObjectModel->setPropertyValue(::rtl::OUString::createFromAscii("BoundColumn"), makeAny((sal_Int16)1));
201 // build the statement to set as list source
202 String sStatement;
203 sStatement.AppendAscii("SELECT ");
204 sStatement += getSettings().sListContentField;
205 sStatement.AppendAscii(", ");
206 sStatement += getSettings().sLinkedListField;
207 sStatement.AppendAscii(" FROM ");
208 sStatement += getSettings().sListContentTable;
209 Sequence< ::rtl::OUString > aListSource(1);
210 aListSource[0] = sStatement;
211 getContext().xObjectModel->setPropertyValue(::rtl::OUString::createFromAscii("ListSource"), makeAny(aListSource));
213 else
215 // build the statement to set as list source
216 String sStatement;
217 sStatement.AppendAscii("SELECT DISTINCT ");
218 sStatement += getSettings().sListContentField;
219 sStatement.AppendAscii(" FROM ");
220 sStatement += getSettings().sListContentTable;
221 getContext().xObjectModel->setPropertyValue(::rtl::OUString::createFromAscii("ListSource"), makeAny(::rtl::OUString(sStatement)));
224 // the bound field
225 getContext().xObjectModel->setPropertyValue(::rtl::OUString::createFromAscii("DataField"), makeAny(::rtl::OUString(getSettings().sLinkedFormField)));
227 catch(Exception&)
229 DBG_ERROR("OListComboWizard::implApplySettings: could not set the property values for the listbox!");
233 //---------------------------------------------------------------------
234 sal_Bool OListComboWizard::onFinish(sal_Int32 _nResult)
236 if (!OControlWizard::onFinish(_nResult))
237 return sal_False;
239 if (RET_OK != _nResult)
240 return sal_True;
242 implApplySettings();
243 return sal_True;
246 //=====================================================================
247 //= OLCPage
248 //=====================================================================
249 //---------------------------------------------------------------------
250 Reference< XNameAccess > OLCPage::getTables(sal_Bool _bNeedIt)
252 Reference< XConnection > xConn = getFormConnection();
253 DBG_ASSERT(!_bNeedIt || xConn.is(), "OLCPage::getTables: should have an active connection when reaching this page!");
254 (void)_bNeedIt;
256 Reference< XTablesSupplier > xSuppTables(xConn, UNO_QUERY);
257 Reference< XNameAccess > xTables;
258 if (xSuppTables.is())
259 xTables = xSuppTables->getTables();
261 DBG_ASSERT(!_bNeedIt || xTables.is() || !xConn.is(), "OLCPage::getTables: got no tables from the connection!");
263 return xTables;
266 //---------------------------------------------------------------------
267 Sequence< ::rtl::OUString > OLCPage::getTableFields(sal_Bool _bNeedIt)
269 Reference< XNameAccess > xTables = getTables(_bNeedIt);
270 Sequence< ::rtl::OUString > aColumnNames;
271 if (xTables.is())
275 // the list table as XColumnsSupplier
276 Reference< XColumnsSupplier > xSuppCols;
277 xTables->getByName(getSettings().sListContentTable) >>= xSuppCols;
278 DBG_ASSERT(!_bNeedIt || xSuppCols.is(), "OLCPage::getTableFields: no columns supplier!");
280 // the columns
281 Reference< XNameAccess > xColumns;
282 if (xSuppCols.is())
283 xColumns = xSuppCols->getColumns();
285 // the column names
286 if (xColumns.is())
287 aColumnNames = xColumns->getElementNames();
289 catch(Exception&)
291 DBG_ASSERT(!_bNeedIt, "OLinkFieldsPage::initializePage: caught an exception while retrieving the columns!");
294 return aColumnNames;
297 //=====================================================================
298 //= OContentTableSelection
299 //=====================================================================
300 //---------------------------------------------------------------------
301 OContentTableSelection::OContentTableSelection( OListComboWizard* _pParent )
302 :OLCPage(_pParent, ModuleRes(RID_PAGE_LCW_CONTENTSELECTION_TABLE))
303 ,m_aFrame (this, ModuleRes(FL_FRAME))
304 ,m_aSelectTableLabel (this, ModuleRes(FT_SELECTTABLE_LABEL))
305 ,m_aSelectTable (this, ModuleRes(LB_SELECTTABLE))
307 FreeResource();
309 enableFormDatasourceDisplay();
311 m_aSelectTable.SetDoubleClickHdl(LINK(this, OContentTableSelection, OnTableDoubleClicked));
312 m_aSelectTable.SetSelectHdl(LINK(this, OContentTableSelection, OnTableSelected));
315 //---------------------------------------------------------------------
316 void OContentTableSelection::ActivatePage()
318 OLCPage::ActivatePage();
319 m_aSelectTable.GrabFocus();
322 //---------------------------------------------------------------------
323 bool OContentTableSelection::canAdvance() const
325 if (!OLCPage::canAdvance())
326 return false;
328 return 0 != m_aSelectTable.GetSelectEntryCount();
331 //---------------------------------------------------------------------
332 IMPL_LINK( OContentTableSelection, OnTableSelected, ListBox*, /*_pListBox*/ )
334 updateDialogTravelUI();
335 return 0L;
338 //---------------------------------------------------------------------
339 IMPL_LINK( OContentTableSelection, OnTableDoubleClicked, ListBox*, _pListBox )
341 if (_pListBox->GetSelectEntryCount())
342 getDialog()->travelNext();
343 return 0L;
346 //---------------------------------------------------------------------
347 void OContentTableSelection::initializePage()
349 OLCPage::initializePage();
351 // fill the list with the table name
352 m_aSelectTable.Clear();
355 Reference< XNameAccess > xTables = getTables(sal_True);
356 Sequence< ::rtl::OUString > aTableNames;
357 if (xTables.is())
358 aTableNames = xTables->getElementNames();
359 fillListBox(m_aSelectTable, aTableNames);
361 catch(Exception&)
363 DBG_ERROR("OContentTableSelection::initializePage: could not retrieve the table names!");
366 m_aSelectTable.SelectEntry(getSettings().sListContentTable);
369 //---------------------------------------------------------------------
370 sal_Bool OContentTableSelection::commitPage( CommitPageReason _eReason )
372 if (!OLCPage::commitPage(_eReason))
373 return sal_False;
375 OListComboSettings& rSettings = getSettings();
376 rSettings.sListContentTable = m_aSelectTable.GetSelectEntry();
377 if (!rSettings.sListContentTable.Len() && (eTravelBackward != _eReason))
378 // need to select a table
379 return sal_False;
381 return sal_True;
384 //=====================================================================
385 //= OContentFieldSelection
386 //=====================================================================
387 //---------------------------------------------------------------------
388 OContentFieldSelection::OContentFieldSelection( OListComboWizard* _pParent )
389 :OLCPage(_pParent, ModuleRes(RID_PAGE_LCW_CONTENTSELECTION_FIELD))
390 ,m_aFrame (this, ModuleRes(FL_FRAME))
391 ,m_aTableFields (this, ModuleRes(FT_TABLEFIELDS))
392 ,m_aSelectTableField (this, ModuleRes(LB_SELECTFIELD))
393 ,m_aDisplayedFieldLabel (this, ModuleRes(FT_DISPLAYEDFIELD))
394 ,m_aDisplayedField (this, ModuleRes(ET_DISPLAYEDFIELD))
395 ,m_aInfo (this, ModuleRes(FT_CONTENTFIELD_INFO))
397 m_aInfo.SetText(String(ModuleRes( isListBox() ? STR_FIELDINFO_LISTBOX : STR_FIELDINFO_COMBOBOX)));
398 FreeResource();
399 m_aSelectTableField.SetSelectHdl(LINK(this, OContentFieldSelection, OnFieldSelected));
400 m_aSelectTableField.SetDoubleClickHdl(LINK(this, OContentFieldSelection, OnTableDoubleClicked));
403 //---------------------------------------------------------------------
404 void OContentFieldSelection::ActivatePage()
406 OLCPage::ActivatePage();
407 m_aTableFields.GrabFocus();
410 //---------------------------------------------------------------------
411 void OContentFieldSelection::initializePage()
413 OLCPage::initializePage();
415 // fill the list of fields
416 fillListBox(m_aSelectTableField, getTableFields(sal_True));
418 m_aSelectTableField.SelectEntry(getSettings().sListContentField);
419 m_aDisplayedField.SetText(getSettings().sListContentField);
422 //---------------------------------------------------------------------
423 bool OContentFieldSelection::canAdvance() const
425 if (!OLCPage::canAdvance())
426 return false;
428 return 0 != m_aSelectTableField.GetSelectEntryCount();
431 //---------------------------------------------------------------------
432 IMPL_LINK( OContentFieldSelection, OnTableDoubleClicked, ListBox*, /*NOTINTERESTEDIN*/ )
434 if (m_aSelectTableField.GetSelectEntryCount())
435 getDialog()->travelNext();
436 return 0L;
439 //---------------------------------------------------------------------
440 IMPL_LINK( OContentFieldSelection, OnFieldSelected, ListBox*, /*NOTINTERESTEDIN*/ )
442 updateDialogTravelUI();
443 m_aDisplayedField.SetText(m_aSelectTableField.GetSelectEntry());
444 return 0L;
447 //---------------------------------------------------------------------
448 sal_Bool OContentFieldSelection::commitPage( CommitPageReason _eReason )
450 if (!OLCPage::commitPage(_eReason))
451 return sal_False;
453 getSettings().sListContentField = m_aSelectTableField.GetSelectEntry();
455 return sal_True;
458 //=====================================================================
459 //= OLinkFieldsPage
460 //=====================================================================
461 //---------------------------------------------------------------------
462 OLinkFieldsPage::OLinkFieldsPage( OListComboWizard* _pParent )
463 :OLCPage(_pParent, ModuleRes(RID_PAGE_LCW_FIELDLINK))
464 ,m_aDescription (this, ModuleRes(FT_FIELDLINK_DESC))
465 ,m_aFrame (this, ModuleRes(FL_FRAME))
466 ,m_aValueListFieldLabel (this, ModuleRes(FT_VALUELISTFIELD))
467 ,m_aValueListField (this, ModuleRes(CMB_VALUELISTFIELD))
468 ,m_aTableFieldLabel (this, ModuleRes(FT_TABLEFIELD))
469 ,m_aTableField (this, ModuleRes(CMB_TABLEFIELD))
471 FreeResource();
473 m_aValueListField.SetModifyHdl(LINK(this, OLinkFieldsPage, OnSelectionModified));
474 m_aTableField.SetModifyHdl(LINK(this, OLinkFieldsPage, OnSelectionModified));
475 m_aValueListField.SetSelectHdl(LINK(this, OLinkFieldsPage, OnSelectionModified));
476 m_aTableField.SetSelectHdl(LINK(this, OLinkFieldsPage, OnSelectionModified));
479 //---------------------------------------------------------------------
480 void OLinkFieldsPage::ActivatePage()
482 OLCPage::ActivatePage();
483 m_aValueListField.GrabFocus();
486 //---------------------------------------------------------------------
487 void OLinkFieldsPage::initializePage()
489 OLCPage::initializePage();
491 // fill the value list
492 fillListBox(m_aValueListField, getContext().aFieldNames);
493 // fill the table field list
494 fillListBox(m_aTableField, getTableFields(sal_True));
496 // the initial selections
497 m_aValueListField.SetText(getSettings().sLinkedFormField);
498 m_aTableField.SetText(getSettings().sLinkedListField);
500 implCheckFinish();
503 //---------------------------------------------------------------------
504 bool OLinkFieldsPage::canAdvance() const
506 // we're on the last page here, no travelNext allowed ...
507 return false;
510 //---------------------------------------------------------------------
511 void OLinkFieldsPage::implCheckFinish()
513 sal_Bool bInvalidSelection = (COMBOBOX_ENTRY_NOTFOUND == m_aValueListField.GetEntryPos(m_aValueListField.GetText()));
514 bInvalidSelection |= (COMBOBOX_ENTRY_NOTFOUND == m_aTableField.GetEntryPos(m_aTableField.GetText()));
515 getDialog()->enableButtons(WZB_FINISH, !bInvalidSelection);
518 //---------------------------------------------------------------------
519 IMPL_LINK(OLinkFieldsPage, OnSelectionModified, void*, EMPTYARG)
521 implCheckFinish();
522 return 0L;
525 //---------------------------------------------------------------------
526 sal_Bool OLinkFieldsPage::commitPage( CommitPageReason _eReason )
528 if (!OLCPage::commitPage(_eReason))
529 return sal_False;
531 getSettings().sLinkedFormField = m_aValueListField.GetText();
532 getSettings().sLinkedListField = m_aTableField.GetText();
534 return sal_True;
537 //=====================================================================
538 //= OComboDBFieldPage
539 //=====================================================================
540 //---------------------------------------------------------------------
541 OComboDBFieldPage::OComboDBFieldPage( OControlWizard* _pParent )
542 :ODBFieldPage(_pParent)
544 setDescriptionText(String(ModuleRes(RID_STR_COMBOWIZ_DBFIELD)));
547 //---------------------------------------------------------------------
548 String& OComboDBFieldPage::getDBFieldSetting()
550 return getSettings().sLinkedFormField;
553 //---------------------------------------------------------------------
554 void OComboDBFieldPage::ActivatePage()
556 ODBFieldPage::ActivatePage();
557 getDialog()->enableButtons(WZB_FINISH, sal_True);
560 //---------------------------------------------------------------------
561 bool OComboDBFieldPage::canAdvance() const
563 // we're on the last page here, no travelNext allowed ...
564 return false;
567 //.........................................................................
568 } // namespace dbp
569 //.........................................................................