Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / misc / WTypeSelect.cxx
blobbcd2b85f4746dc5227bad8dbbde5760dcf85c93e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "WTypeSelect.hxx"
21 #include "dbustrings.hrc"
22 #include <tools/diagnose_ex.h>
23 #include <osl/diagnose.h>
24 #include "FieldDescriptions.hxx"
25 #include "WCopyTable.hxx"
26 #include "dbaccess_helpid.hrc"
27 #include "dbu_misc.hrc"
28 #include <tools/stream.hxx>
29 #include <svtools/svparser.hxx>
30 #include "UITools.hxx"
31 #include "sqlmessage.hxx"
32 #include "FieldControls.hxx"
33 #include <vcl/layout.hxx>
34 #include <vcl/builderfactory.hxx>
36 #include "dbaccess_slotid.hrc"
38 using namespace ::dbaui;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::container;
42 using namespace ::com::sun::star::util;
43 using namespace ::com::sun::star::sdbc;
45 // OWizTypeSelectControl
46 OWizTypeSelectControl::OWizTypeSelectControl(vcl::Window* pParent, vcl::Window* pParentTabPage, OTableDesignHelpBar* pHelpBar)
47 : OFieldDescControl(pParent, pHelpBar)
49 m_pParentTabPage = pParentTabPage;
52 OWizTypeSelectControl::~OWizTypeSelectControl()
54 disposeOnce();
57 void OWizTypeSelectControl::dispose()
59 m_pParentTabPage.clear();
60 OFieldDescControl::dispose();
63 void OWizTypeSelectControl::ActivateAggregate( EControlType eType )
65 switch(eType )
67 case tpFormat:
68 case tpDefault:
69 case tpAutoIncrement:
70 case tpAutoIncrementValue:
71 break;
72 default:
73 OFieldDescControl::ActivateAggregate( eType );
77 void OWizTypeSelectControl::DeactivateAggregate( EControlType eType )
79 switch(eType )
81 case tpFormat:
82 case tpDefault:
83 case tpAutoIncrement:
84 case tpAutoIncrementValue:
85 break;
86 default:
87 OFieldDescControl::DeactivateAggregate( eType );
91 void OWizTypeSelectControl::CellModified(long nRow, sal_uInt16 nColId )
93 OSL_ENSURE(nRow == -1,"nRow muss -1 sein!");
94 (void)nRow;
96 MultiListBox *pListBox = static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pColumnNames;
98 OFieldDescription* pCurFieldDescr = getCurrentFieldDescData();
100 sal_uInt16 nPos = pListBox->GetEntryPos( OUString( pCurFieldDescr->GetName() ) );
101 pCurFieldDescr = static_cast< OFieldDescription* >( pListBox->GetEntryData( nPos ) );
102 OSL_ENSURE( pCurFieldDescr, "OWizTypeSelectControl::CellModified: Columnname/type not found in the listbox!" );
103 if ( !pCurFieldDescr )
104 return;
105 setCurrentFieldDescData( pCurFieldDescr );
107 OUString sName = pCurFieldDescr->GetName();
108 OUString sNewName;
109 const OPropColumnEditCtrl* pColumnName = getColumnCtrl();
110 if ( pColumnName )
111 sNewName = pColumnName->GetText();
113 switch(nColId)
115 case FIELD_PROPERTY_COLUMNNAME:
117 OCopyTableWizard* pWiz = static_cast<OCopyTableWizard*>(GetParentDialog());
118 // first we have to check if this name already exists
119 bool bDoubleName = false;
120 bool bCase = true;
121 if ( getMetaData().is() && !getMetaData()->supportsMixedCaseQuotedIdentifiers() )
123 bCase = false;
124 sal_uInt16 nCount = pListBox->GetEntryCount();
125 for (sal_uInt16 i=0 ; !bDoubleName && i < nCount ; ++i)
127 OUString sEntry(pListBox->GetEntry(i));
128 bDoubleName = sNewName.equalsIgnoreAsciiCase(sEntry);
130 if ( !bDoubleName && pWiz->shouldCreatePrimaryKey() )
131 bDoubleName = sNewName.equalsIgnoreAsciiCase(pWiz->getPrimaryKeyName());
134 else
135 bDoubleName = ((pListBox->GetEntryPos(OUString(sNewName)) != LISTBOX_ENTRY_NOTFOUND)
136 || ( pWiz->shouldCreatePrimaryKey()
137 && pWiz->getPrimaryKeyName() == sNewName) );
139 if ( bDoubleName )
141 OUString strMessage = ModuleRes(STR_TABLEDESIGN_DUPLICATE_NAME);
142 strMessage = strMessage.replaceFirst("$column$", sNewName);
143 pWiz->showError(strMessage);
144 pCurFieldDescr->SetName(sName);
145 DisplayData(pCurFieldDescr);
146 static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->setDuplicateName(true);
147 return;
150 OUString sOldName = pCurFieldDescr->GetName();
151 pCurFieldDescr->SetName(sNewName);
152 static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->setDuplicateName(false);
154 // now we change the name
155 OCopyTableWizard::TNameMapping::iterator aIter = pWiz->m_mNameMapping.begin();
156 OCopyTableWizard::TNameMapping::iterator aEnd = pWiz->m_mNameMapping.end();
158 ::comphelper::UStringMixEqual aCase(bCase);
159 for(;aIter != aEnd;++aIter)
161 if ( aCase(aIter->second,sName) )
163 aIter->second = sNewName;
164 break;
168 pListBox->RemoveEntry(nPos);
169 pListBox->InsertEntry(pCurFieldDescr->GetName(),nPos);
170 pListBox->SetEntryData(nPos,pCurFieldDescr);
172 pWiz->replaceColumn(nPos,pCurFieldDescr,sOldName);
174 break;
176 saveCurrentFieldDescData();
179 ::com::sun::star::lang::Locale OWizTypeSelectControl::GetLocale() const
181 return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pParent->GetLocale();
184 Reference< XNumberFormatter > OWizTypeSelectControl::GetFormatter() const
186 return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pParent->GetFormatter();
189 TOTypeInfoSP OWizTypeSelectControl::getTypeInfo(sal_Int32 _nPos)
191 return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pParent->getDestTypeInfo(_nPos);
194 const OTypeInfoMap* OWizTypeSelectControl::getTypeInfo() const
196 return &static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pParent->getDestTypeInfo();
199 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData> OWizTypeSelectControl::getMetaData()
201 return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pParent->m_xDestConnection->getMetaData();
204 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> OWizTypeSelectControl::getConnection()
206 return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pParent->m_xDestConnection;
209 bool OWizTypeSelectControl::isAutoIncrementValueEnabled() const
211 return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_bAutoIncrementEnabled;
214 OUString OWizTypeSelectControl::getAutoIncrementValue() const
216 return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_sAutoIncrementValue;
219 #define IMG_PRIMARY_KEY 1
220 OWizTypeSelect::OWizTypeSelect( vcl::Window* pParent, SvStream* _pStream )
221 :OWizardPage( pParent, "TypeSelect", "dbaccess/ui/typeselectpage.ui")
222 ,m_pTypeControl(VclPtr<OWizTypeSelectControl>::Create(get<VclVBox>("control_container"), this) )
223 ,m_pParserStream( _pStream )
224 ,m_nDisplayRow(0)
225 ,m_bAutoIncrementEnabled(false)
226 ,m_bDuplicateName(false)
228 get(m_pColumnNames, "columnnames");
229 m_pColumnNames->SetParentTabPage(this);
230 get(m_pColumns, "columns");
231 get(m_pAutoType, "autotype");
232 get(m_pAutoFt, "autolabel");
233 get(m_pAutoEt, "auto");
234 get(m_pAutoPb, "autobutton");
236 m_pColumnNames->SetSelectHdl(LINK(this,OWizTypeSelect,ColumnSelectHdl));
238 ModuleRes aModuleRes(IMG_JOINS);
239 ImageList aImageList(aModuleRes);
240 m_imgPKey = aImageList.GetImage(IMG_PRIMARY_KEY);
243 m_pTypeControl->Show();
244 m_pTypeControl->Init();
246 m_pAutoEt->SetText(OUString("10"));
247 m_pAutoEt->SetDecimalDigits(0);
248 m_pAutoPb->SetClickHdl(LINK(this,OWizTypeSelect,ButtonClickHdl));
249 m_pColumnNames->EnableMultiSelection(true);
253 m_pColumnNames->SetPKey( m_pParent->supportsPrimaryKey() );
254 ::dbaui::fillAutoIncrementValue( m_pParent->m_xDestConnection, m_bAutoIncrementEnabled, m_sAutoIncrementValue );
256 catch(const Exception&)
258 DBG_UNHANDLED_EXCEPTION();
262 OWizTypeSelect::~OWizTypeSelect()
264 disposeOnce();
267 void OWizTypeSelect::dispose()
269 m_pTypeControl.disposeAndClear();
270 m_pColumnNames.clear();
271 m_pColumns.clear();
272 m_pAutoType.clear();
273 m_pAutoFt.clear();
274 m_pAutoEt.clear();
275 m_pAutoPb.clear();
276 OWizardPage::dispose();
279 OUString OWizTypeSelect::GetTitle() const
281 return ModuleRes(STR_WIZ_TYPE_SELECT_TITEL);
284 IMPL_LINK( OWizTypeSelect, ColumnSelectHdl, MultiListBox *, /*pListBox*/ )
286 OUString aColumnName( m_pColumnNames->GetSelectEntry() );
288 OFieldDescription* pField = static_cast<OFieldDescription*>(m_pColumnNames->GetEntryData(m_pColumnNames->GetEntryPos(aColumnName)));
289 if(pField)
290 m_pTypeControl->DisplayData(pField);
292 m_pTypeControl->Enable(m_pColumnNames->GetSelectEntryCount() == 1 );
293 return 0;
296 void OWizTypeSelect::Reset()
298 // restore original state
300 while(m_pColumnNames->GetEntryCount())
301 m_pColumnNames->RemoveEntry(0);
302 m_pColumnNames->Clear();
303 sal_Int32 nBreakPos;
304 m_pParent->CheckColumns(nBreakPos);
306 const ODatabaseExport::TColumnVector& rDestColumns = m_pParent->getDestVector();
307 ODatabaseExport::TColumnVector::const_iterator aIter = rDestColumns.begin();
308 ODatabaseExport::TColumnVector::const_iterator aEnd = rDestColumns.end();
309 for(;aIter != aEnd;++aIter)
311 sal_uInt16 nPos;
312 if((*aIter)->second->IsPrimaryKey())
313 nPos = m_pColumnNames->InsertEntry((*aIter)->first, m_imgPKey );
314 else
315 nPos = m_pColumnNames->InsertEntry((*aIter)->first);
316 m_pColumnNames->SetEntryData(nPos,(*aIter)->second);
318 m_bFirstTime = false;
321 void OWizTypeSelect::ActivatePage( )
323 bool bOldFirstTime = m_bFirstTime;
324 Reset();
325 m_bFirstTime = bOldFirstTime;
327 m_pColumnNames->SelectEntryPos(static_cast<sal_uInt16>(m_nDisplayRow));
328 m_nDisplayRow = 0;
329 m_pColumnNames->GetSelectHdl().Call(m_pColumnNames);
332 bool OWizTypeSelect::LeavePage()
334 OUString aColumnName( m_pColumnNames->GetSelectEntry() );
336 bool bDuplicateName = false;
337 OFieldDescription* pField = static_cast<OFieldDescription*>(m_pColumnNames->GetEntryData(m_pColumnNames->GetEntryPos(aColumnName)));
338 if ( pField )
340 m_pTypeControl->SaveData(pField);
341 bDuplicateName = m_bDuplicateName;
343 return !bDuplicateName;
346 void OWizTypeSelect::EnableAuto(bool bEnable)
348 m_pAutoFt->Show(bEnable);
349 m_pAutoEt->Show(bEnable);
350 m_pAutoPb->Show(bEnable);
351 m_pAutoType->Show(bEnable);
354 IMPL_LINK( OWizTypeSelect, ButtonClickHdl, Button *, /*pButton*/ )
356 sal_Int32 nBreakPos;
357 m_pParent->CheckColumns(nBreakPos);
358 fillColumnList(m_pAutoEt->GetText().toInt32());
360 ActivatePage();
362 return 0;
365 OWizTypeSelectList::~OWizTypeSelectList()
367 disposeOnce();
370 void OWizTypeSelectList::dispose()
372 m_pParentTabPage.clear();
373 MultiListBox::dispose();
376 bool OWizTypeSelectList::IsPrimaryKeyAllowed() const
378 sal_uInt16 nCount = GetSelectEntryCount();
379 sal_uInt16 j;
381 for( j = 0; m_bPKey && j < nCount; ++j )
383 OFieldDescription* pField = static_cast<OFieldDescription*>(GetEntryData(GetSelectEntryPos(j)));
384 if(!pField || pField->getTypeInfo()->nSearchType == ColumnSearch::NONE)
385 break;
387 return j == nCount;
390 void OWizTypeSelectList::setPrimaryKey(OFieldDescription* _pFieldDescr, sal_uInt16 _nPos, bool _bSet)
392 OUString sColumnName = GetEntry(_nPos);
393 RemoveEntry(_nPos);
394 _pFieldDescr->SetPrimaryKey(_bSet);
395 if( _bSet )
396 InsertEntry(sColumnName, static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_imgPKey,_nPos);
397 else if( _pFieldDescr->getTypeInfo()->bNullable )
399 _pFieldDescr->SetControlDefault(Any());
400 InsertEntry(sColumnName,_nPos);
402 SetEntryData(_nPos,_pFieldDescr);
405 VCL_BUILDER_FACTORY(OWizTypeSelectList)
407 bool OWizTypeSelectList::PreNotify( NotifyEvent& rEvt )
409 bool nDone = false;
410 switch( rEvt.GetType() )
412 case MouseNotifyEvent::MOUSEBUTTONDOWN:
414 const MouseEvent* pMEvt = rEvt.GetMouseEvent();
415 if(pMEvt->IsRight() && !pMEvt->GetModifier())
416 nDone = true;
418 break;
419 case MouseNotifyEvent::COMMAND:
421 if(!IsPrimaryKeyAllowed())
422 break;
424 const CommandEvent* pComEvt = rEvt.GetCommandEvent();
425 if(pComEvt->GetCommand() != CommandEventId::ContextMenu)
426 break;
427 // the place, at which was clicked
428 Point ptWhere(0,0);
429 if (pComEvt->IsMouseEvent())
430 ptWhere = pComEvt->GetMousePosPixel();
432 PopupMenu aContextMenu(ModuleRes(RID_SBA_RTF_PKEYPOPUP));
433 // Should primary key checkbox be checked?
434 const sal_Int32 nCount = GetEntryCount();
435 bool bCheckOk = false;
436 for(sal_Int32 j = 0 ; j < nCount ; ++j)
438 OFieldDescription* pFieldDescr = static_cast<OFieldDescription*>(GetEntryData(j));
439 // if at least one of the fields is selected but not in the primary key,
440 // or is in the primary key but not selected, then don't check the
441 // primary key checkbox.
442 if( pFieldDescr && pFieldDescr->IsPrimaryKey() != IsEntryPosSelected(j) )
444 bCheckOk = false;
445 break;
447 if (!bCheckOk && IsEntryPosSelected(j))
448 bCheckOk = true;
451 if (bCheckOk)
452 aContextMenu.CheckItem( SID_TABLEDESIGN_TABED_PRIMARYKEY, true );
454 switch( aContextMenu.Execute( this, ptWhere ) )
456 case SID_TABLEDESIGN_TABED_PRIMARYKEY:
458 for(sal_Int32 j = 0 ; j < nCount ; ++j)
460 OFieldDescription* pFieldDescr = static_cast<OFieldDescription*>(GetEntryData(j));
461 if( pFieldDescr )
463 if(!bCheckOk && IsEntryPosSelected(j))
465 setPrimaryKey(pFieldDescr,j,true);
466 SelectEntryPos(j);
468 else
470 setPrimaryKey(pFieldDescr,j);
474 GetSelectHdl().Call(this);
476 break;
478 nDone = true;
480 break;
481 default:
482 break;
484 return nDone || MultiListBox::PreNotify(rEvt);
487 void OWizTypeSelect::fillColumnList(sal_uInt32 nRows)
489 if(m_pParserStream)
491 sal_Size nTell = m_pParserStream->Tell(); // might change seek position of stream
493 SvParser *pReader = createReader(nRows);
494 if(pReader)
496 pReader->AddFirstRef();
497 pReader->CallParser();
498 pReader->ReleaseRef();
500 m_pParserStream->Seek(nTell);
504 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */