1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include "listcombowizard.hxx"
#include "commonpagesdbp.hxx"
#include <com/sun/star/sdbc/XConnection.hpp>
#include <com/sun/star/sdbcx/XTablesSupplier.hpp>
#include <com/sun/star/container/XNameAccess.hpp>
#include <com/sun/star/form/ListSourceType.hpp>
#include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
#include <tools/debug.hxx>
#include <vcl/msgbox.hxx>
#include <connectivity/dbtools.hxx>
#include "dbpilots.hrc"
#include <comphelper/extract.hxx>
//.........................................................................
namespace dbp
{
//.........................................................................
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::sdbc;
using namespace ::com::sun::star::sdbcx;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::form;
using namespace ::svt;
using namespace ::dbtools;
//=====================================================================
//= OListComboWizard
//=====================================================================
//---------------------------------------------------------------------
OListComboWizard::OListComboWizard( Window* _pParent,
const Reference< XPropertySet >& _rxObjectModel, const Reference< XMultiServiceFactory >& _rxORB )
:OControlWizard(_pParent, ModuleRes(RID_DLG_LISTCOMBOWIZARD), _rxObjectModel, _rxORB)
,m_bListBox(sal_False)
,m_bHadDataSelection(sal_True)
{
initControlSettings(&m_aSettings);
m_pPrevPage->SetHelpId(HID_LISTWIZARD_PREVIOUS);
m_pNextPage->SetHelpId(HID_LISTWIZARD_NEXT);
m_pCancel->SetHelpId(HID_LISTWIZARD_CANCEL);
m_pFinish->SetHelpId(HID_LISTWIZARD_FINISH);
// if we do not need the data source selection page ...
if (!needDatasourceSelection())
{ // ... skip it!
skip(1);
m_bHadDataSelection = sal_False;
}
}
//---------------------------------------------------------------------
sal_Bool OListComboWizard::approveControl(sal_Int16 _nClassId)
{
switch (_nClassId)
{
case FormComponentType::LISTBOX:
m_bListBox = sal_True;
setTitleBase(String(ModuleRes(RID_STR_LISTWIZARD_TITLE)));
return sal_True;
case FormComponentType::COMBOBOX:
m_bListBox = sal_False;
setTitleBase(String(ModuleRes(RID_STR_COMBOWIZARD_TITLE)));
return sal_True;
}
return sal_False;
}
//---------------------------------------------------------------------
OWizardPage* OListComboWizard::createPage(WizardState _nState)
{
switch (_nState)
{
case LCW_STATE_DATASOURCE_SELECTION:
return new OTableSelectionPage(this);
case LCW_STATE_TABLESELECTION:
return new OContentTableSelection(this);
case LCW_STATE_FIELDSELECTION:
return new OContentFieldSelection(this);
case LCW_STATE_FIELDLINK:
return new OLinkFieldsPage(this);
case LCW_STATE_COMBODBFIELD:
return new OComboDBFieldPage(this);
}
return NULL;
}
//---------------------------------------------------------------------
WizardTypes::WizardState OListComboWizard::determineNextState( WizardState _nCurrentState ) const
{
switch (_nCurrentState)
{
case LCW_STATE_DATASOURCE_SELECTION:
return LCW_STATE_TABLESELECTION;
case LCW_STATE_TABLESELECTION:
return LCW_STATE_FIELDSELECTION;
case LCW_STATE_FIELDSELECTION:
return getFinalState();
}
return WZS_INVALID_STATE;
}
//---------------------------------------------------------------------
void OListComboWizard::enterState(WizardState _nState)
{
OControlWizard::enterState(_nState);
enableButtons(WZB_PREVIOUS, m_bHadDataSelection ? (LCW_STATE_DATASOURCE_SELECTION < _nState) : LCW_STATE_TABLESELECTION < _nState);
enableButtons(WZB_NEXT, getFinalState() != _nState);
if (_nState < getFinalState())
enableButtons(WZB_FINISH, sal_False);
if (getFinalState() == _nState)
defaultButton(WZB_FINISH);
}
//---------------------------------------------------------------------
sal_Bool OListComboWizard::leaveState(WizardState _nState)
{
if (!OControlWizard::leaveState(_nState))
return sal_False;
if (getFinalState() == _nState)
defaultButton(WZB_NEXT);
return sal_True;
}
//---------------------------------------------------------------------
void OListComboWizard::implApplySettings()
{
try
{
// for quoting identifiers, we need the connection meta data
Reference< XConnection > xConn = getFormConnection();
DBG_ASSERT(xConn.is(), "OListComboWizard::implApplySettings: no connection, unable to quote!");
Reference< XDatabaseMetaData > xMetaData;
if (xConn.is())
xMetaData = xConn->getMetaData();
// do some quotings
if (xMetaData.is())
{
::rtl::OUString sQuoteString = xMetaData->getIdentifierQuoteString();
if (isListBox()) // only when we have a listbox this should be not empty
getSettings().sLinkedListField = quoteName(sQuoteString, getSettings().sLinkedListField);
::rtl::OUString sCatalog, sSchema, sName;
::dbtools::qualifiedNameComponents( xMetaData, getSettings().sListContentTable, sCatalog, sSchema, sName, ::dbtools::eInDataManipulation );
getSettings().sListContentTable = ::dbtools::composeTableNameForSelect( xConn, sCatalog, sSchema, sName );
getSettings().sListContentField = quoteName(sQuoteString, getSettings().sListContentField);
}
// ListSourceType: SQL
getContext().xObjectModel->setPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ListSourceType")), makeAny((sal_Int32)ListSourceType_SQL));
if (isListBox())
{
// BoundColumn: 1
getContext().xObjectModel->setPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("BoundColumn")), makeAny((sal_Int16)1));
// build the statement to set as list source
String sStatement;
sStatement.AppendAscii("SELECT ");
sStatement += getSettings().sListContentField;
sStatement.AppendAscii(", ");
sStatement += getSettings().sLinkedListField;
sStatement.AppendAscii(" FROM ");
sStatement += getSettings().sListContentTable;
Sequence< ::rtl::OUString > aListSource(1);
aListSource[0] = sStatement;
getContext().xObjectModel->setPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ListSource")), makeAny(aListSource));
}
else
{
// build the statement to set as list source
String sStatement;
sStatement.AppendAscii("SELECT DISTINCT ");
sStatement += getSettings().sListContentField;
sStatement.AppendAscii(" FROM ");
sStatement += getSettings().sListContentTable;
getContext().xObjectModel->setPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ListSource")), makeAny(::rtl::OUString(sStatement)));
}
// the bound field
getContext().xObjectModel->setPropertyValue(::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DataField")), makeAny(::rtl::OUString(getSettings().sLinkedFormField)));
}
catch(Exception&)
{
OSL_FAIL("OListComboWizard::implApplySettings: could not set the property values for the listbox!");
}
}
//---------------------------------------------------------------------
sal_Bool OListComboWizard::onFinish()
{
if ( !OControlWizard::onFinish() )
return sal_False;
implApplySettings();
return sal_True;
}
//=====================================================================
//= OLCPage
//=====================================================================
//---------------------------------------------------------------------
Reference< XNameAccess > OLCPage::getTables(sal_Bool _bNeedIt)
{
Reference< XConnection > xConn = getFormConnection();
DBG_ASSERT(!_bNeedIt || xConn.is(), "OLCPage::getTables: should have an active connection when reaching this page!");
(void)_bNeedIt;
Reference< XTablesSupplier > xSuppTables(xConn, UNO_QUERY);
Reference< XNameAccess > xTables;
if (xSuppTables.is())
xTables = xSuppTables->getTables();
DBG_ASSERT(!_bNeedIt || xTables.is() || !xConn.is(), "OLCPage::getTables: got no tables from the connection!");
return xTables;
}
//---------------------------------------------------------------------
Sequence< ::rtl::OUString > OLCPage::getTableFields(sal_Bool _bNeedIt)
{
Reference< XNameAccess > xTables = getTables(_bNeedIt);
Sequence< ::rtl::OUString > aColumnNames;
if (xTables.is())
{
try
{
// the list table as XColumnsSupplier
Reference< XColumnsSupplier > xSuppCols;
xTables->getByName(getSettings().sListContentTable) >>= xSuppCols;
DBG_ASSERT(!_bNeedIt || xSuppCols.is(), "OLCPage::getTableFields: no columns supplier!");
// the columns
Reference< XNameAccess > xColumns;
if (xSuppCols.is())
xColumns = xSuppCols->getColumns();
// the column names
if (xColumns.is())
aColumnNames = xColumns->getElementNames();
}
catch(Exception&)
{
DBG_ASSERT(!_bNeedIt, "OLinkFieldsPage::initializePage: caught an exception while retrieving the columns!");
}
}
return aColumnNames;
}
//=====================================================================
//= OContentTableSelection
//=====================================================================
//---------------------------------------------------------------------
OContentTableSelection::OContentTableSelection( OListComboWizard* _pParent )
:OLCPage(_pParent, ModuleRes(RID_PAGE_LCW_CONTENTSELECTION_TABLE))
,m_aFrame (this, ModuleRes(FL_FRAME))
,m_aSelectTableLabel (this, ModuleRes(FT_SELECTTABLE_LABEL))
,m_aSelectTable (this, ModuleRes(LB_SELECTTABLE))
{
FreeResource();
enableFormDatasourceDisplay();
m_aSelectTable.SetDoubleClickHdl(LINK(this, OContentTableSelection, OnTableDoubleClicked));
m_aSelectTable.SetSelectHdl(LINK(this, OContentTableSelection, OnTableSelected));
}
//---------------------------------------------------------------------
void OContentTableSelection::ActivatePage()
{
OLCPage::ActivatePage();
m_aSelectTable.GrabFocus();
}
//---------------------------------------------------------------------
bool OContentTableSelection::canAdvance() const
{
if (!OLCPage::canAdvance())
return false;
return 0 != m_aSelectTable.GetSelectEntryCount();
}
//---------------------------------------------------------------------
IMPL_LINK( OContentTableSelection, OnTableSelected, ListBox*, /*_pListBox*/ )
{
updateDialogTravelUI();
return 0L;
}
//---------------------------------------------------------------------
IMPL_LINK( OContentTableSelection, OnTableDoubleClicked, ListBox*, _pListBox )
{
if (_pListBox->GetSelectEntryCount())
getDialog()->travelNext();
return 0L;
}
//---------------------------------------------------------------------
void OContentTableSelection::initializePage()
{
OLCPage::initializePage();
// fill the list with the table name
m_aSelectTable.Clear();
try
{
Reference< XNameAccess > xTables = getTables(sal_True);
Sequence< ::rtl::OUString > aTableNames;
if (xTables.is())
aTableNames = xTables->getElementNames();
fillListBox(m_aSelectTable, aTableNames);
}
catch(Exception&)
{
OSL_FAIL("OContentTableSelection::initializePage: could not retrieve the table names!");
}
m_aSelectTable.SelectEntry(getSettings().sListContentTable);
}
//---------------------------------------------------------------------
sal_Bool OContentTableSelection::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
{
if (!OLCPage::commitPage(_eReason))
return sal_False;
OListComboSettings& rSettings = getSettings();
rSettings.sListContentTable = m_aSelectTable.GetSelectEntry();
if (!rSettings.sListContentTable.Len() && (::svt::WizardTypes::eTravelBackward != _eReason))
// need to select a table
return sal_False;
return sal_True;
}
//=====================================================================
//= OContentFieldSelection
//=====================================================================
//---------------------------------------------------------------------
OContentFieldSelection::OContentFieldSelection( OListComboWizard* _pParent )
:OLCPage(_pParent, ModuleRes(RID_PAGE_LCW_CONTENTSELECTION_FIELD))
,m_aFrame (this, ModuleRes(FL_FRAME))
,m_aTableFields (this, ModuleRes(FT_TABLEFIELDS))
,m_aSelectTableField (this, ModuleRes(LB_SELECTFIELD))
,m_aDisplayedFieldLabel (this, ModuleRes(FT_DISPLAYEDFIELD))
,m_aDisplayedField (this, ModuleRes(ET_DISPLAYEDFIELD))
,m_aInfo (this, ModuleRes(FT_CONTENTFIELD_INFO))
{
m_aInfo.SetText(String(ModuleRes( isListBox() ? STR_FIELDINFO_LISTBOX : STR_FIELDINFO_COMBOBOX)));
FreeResource();
m_aSelectTableField.SetSelectHdl(LINK(this, OContentFieldSelection, OnFieldSelected));
m_aSelectTableField.SetDoubleClickHdl(LINK(this, OContentFieldSelection, OnTableDoubleClicked));
}
//---------------------------------------------------------------------
void OContentFieldSelection::ActivatePage()
{
OLCPage::ActivatePage();
m_aTableFields.GrabFocus();
}
//---------------------------------------------------------------------
void OContentFieldSelection::initializePage()
{
OLCPage::initializePage();
// fill the list of fields
fillListBox(m_aSelectTableField, getTableFields(sal_True));
m_aSelectTableField.SelectEntry(getSettings().sListContentField);
m_aDisplayedField.SetText(getSettings().sListContentField);
}
//---------------------------------------------------------------------
bool OContentFieldSelection::canAdvance() const
{
if (!OLCPage::canAdvance())
return false;
return 0 != m_aSelectTableField.GetSelectEntryCount();
}
//---------------------------------------------------------------------
IMPL_LINK( OContentFieldSelection, OnTableDoubleClicked, ListBox*, /*NOTINTERESTEDIN*/ )
{
if (m_aSelectTableField.GetSelectEntryCount())
getDialog()->travelNext();
return 0L;
}
//---------------------------------------------------------------------
IMPL_LINK( OContentFieldSelection, OnFieldSelected, ListBox*, /*NOTINTERESTEDIN*/ )
{
updateDialogTravelUI();
m_aDisplayedField.SetText(m_aSelectTableField.GetSelectEntry());
return 0L;
}
//---------------------------------------------------------------------
sal_Bool OContentFieldSelection::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
{
if (!OLCPage::commitPage(_eReason))
return sal_False;
getSettings().sListContentField = m_aSelectTableField.GetSelectEntry();
return sal_True;
}
//=====================================================================
//= OLinkFieldsPage
//=====================================================================
//---------------------------------------------------------------------
OLinkFieldsPage::OLinkFieldsPage( OListComboWizard* _pParent )
:OLCPage(_pParent, ModuleRes(RID_PAGE_LCW_FIELDLINK))
,m_aDescription (this, ModuleRes(FT_FIELDLINK_DESC))
,m_aFrame (this, ModuleRes(FL_FRAME))
,m_aValueListFieldLabel (this, ModuleRes(FT_VALUELISTFIELD))
,m_aValueListField (this, ModuleRes(CMB_VALUELISTFIELD))
,m_aTableFieldLabel (this, ModuleRes(FT_TABLEFIELD))
,m_aTableField (this, ModuleRes(CMB_TABLEFIELD))
{
FreeResource();
m_aValueListField.SetModifyHdl(LINK(this, OLinkFieldsPage, OnSelectionModified));
m_aTableField.SetModifyHdl(LINK(this, OLinkFieldsPage, OnSelectionModified));
m_aValueListField.SetSelectHdl(LINK(this, OLinkFieldsPage, OnSelectionModified));
m_aTableField.SetSelectHdl(LINK(this, OLinkFieldsPage, OnSelectionModified));
}
//---------------------------------------------------------------------
void OLinkFieldsPage::ActivatePage()
{
OLCPage::ActivatePage();
m_aValueListField.GrabFocus();
}
//---------------------------------------------------------------------
void OLinkFieldsPage::initializePage()
{
OLCPage::initializePage();
// fill the value list
fillListBox(m_aValueListField, getContext().aFieldNames);
// fill the table field list
fillListBox(m_aTableField, getTableFields(sal_True));
// the initial selections
m_aValueListField.SetText(getSettings().sLinkedFormField);
m_aTableField.SetText(getSettings().sLinkedListField);
implCheckFinish();
}
//---------------------------------------------------------------------
bool OLinkFieldsPage::canAdvance() const
{
// we're on the last page here, no travelNext allowed ...
return false;
}
//---------------------------------------------------------------------
void OLinkFieldsPage::implCheckFinish()
{
sal_Bool bInvalidSelection = (COMBOBOX_ENTRY_NOTFOUND == m_aValueListField.GetEntryPos(m_aValueListField.GetText()));
bInvalidSelection |= (COMBOBOX_ENTRY_NOTFOUND == m_aTableField.GetEntryPos(m_aTableField.GetText()));
getDialog()->enableButtons(WZB_FINISH, !bInvalidSelection);
}
//---------------------------------------------------------------------
IMPL_LINK(OLinkFieldsPage, OnSelectionModified, void*, EMPTYARG)
{
implCheckFinish();
return 0L;
}
//---------------------------------------------------------------------
sal_Bool OLinkFieldsPage::commitPage( ::svt::WizardTypes::CommitPageReason _eReason )
{
if (!OLCPage::commitPage(_eReason))
return sal_False;
getSettings().sLinkedFormField = m_aValueListField.GetText();
getSettings().sLinkedListField = m_aTableField.GetText();
return sal_True;
}
//=====================================================================
//= OComboDBFieldPage
//=====================================================================
//---------------------------------------------------------------------
OComboDBFieldPage::OComboDBFieldPage( OControlWizard* _pParent )
:ODBFieldPage(_pParent)
{
setDescriptionText(String(ModuleRes(RID_STR_COMBOWIZ_DBFIELD)));
}
//---------------------------------------------------------------------
String& OComboDBFieldPage::getDBFieldSetting()
{
return getSettings().sLinkedFormField;
}
//---------------------------------------------------------------------
void OComboDBFieldPage::ActivatePage()
{
ODBFieldPage::ActivatePage();
getDialog()->enableButtons(WZB_FINISH, sal_True);
}
//---------------------------------------------------------------------
bool OComboDBFieldPage::canAdvance() const
{
// we're on the last page here, no travelNext allowed ...
return false;
}
//.........................................................................
} // namespace dbp
//.........................................................................
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|