Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / misc / WTypeSelect.cxx
blob4b85643bd96fabc480cb02109c45afdde13251b7
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 const sal_Int32 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 const sal_Int32 nCount = pListBox->GetEntryCount();
125 for (sal_Int32 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::const_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 css::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 css::uno::Reference< css::sdbc::XDatabaseMetaData> OWizTypeSelectControl::getMetaData()
201 return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pParent->m_xDestConnection->getMetaData();
204 css::uno::Reference< css::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("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_NOARG_TYPED( OWizTypeSelect, ColumnSelectHdl, ListBox&, void )
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 );
295 void OWizTypeSelect::Reset()
297 // restore original state
299 while(m_pColumnNames->GetEntryCount())
300 m_pColumnNames->RemoveEntry(0);
301 m_pColumnNames->Clear();
302 sal_Int32 nBreakPos;
303 m_pParent->CheckColumns(nBreakPos);
305 const ODatabaseExport::TColumnVector& rDestColumns = m_pParent->getDestVector();
306 ODatabaseExport::TColumnVector::const_iterator aIter = rDestColumns.begin();
307 ODatabaseExport::TColumnVector::const_iterator aEnd = rDestColumns.end();
308 for(;aIter != aEnd;++aIter)
310 const sal_Int32 nPos = (*aIter)->second->IsPrimaryKey()
311 ? m_pColumnNames->InsertEntry((*aIter)->first, m_imgPKey )
312 : m_pColumnNames->InsertEntry((*aIter)->first);
313 m_pColumnNames->SetEntryData(nPos,(*aIter)->second);
315 m_bFirstTime = false;
318 void OWizTypeSelect::ActivatePage( )
320 bool bOldFirstTime = m_bFirstTime;
321 Reset();
322 m_bFirstTime = bOldFirstTime;
324 m_pColumnNames->SelectEntryPos(static_cast<sal_uInt16>(m_nDisplayRow));
325 m_nDisplayRow = 0;
326 m_pColumnNames->GetSelectHdl().Call(*m_pColumnNames);
329 bool OWizTypeSelect::LeavePage()
331 OUString aColumnName( m_pColumnNames->GetSelectEntry() );
333 bool bDuplicateName = false;
334 OFieldDescription* pField = static_cast<OFieldDescription*>(m_pColumnNames->GetEntryData(m_pColumnNames->GetEntryPos(aColumnName)));
335 if ( pField )
337 m_pTypeControl->SaveData(pField);
338 bDuplicateName = m_bDuplicateName;
340 return !bDuplicateName;
343 void OWizTypeSelect::EnableAuto(bool bEnable)
345 m_pAutoFt->Show(bEnable);
346 m_pAutoEt->Show(bEnable);
347 m_pAutoPb->Show(bEnable);
348 m_pAutoType->Show(bEnable);
351 IMPL_LINK_NOARG_TYPED( OWizTypeSelect, ButtonClickHdl, Button *, void )
353 sal_Int32 nBreakPos;
354 m_pParent->CheckColumns(nBreakPos);
355 fillColumnList(m_pAutoEt->GetText().toInt32());
357 ActivatePage();
360 OWizTypeSelectList::~OWizTypeSelectList()
362 disposeOnce();
365 void OWizTypeSelectList::dispose()
367 m_pParentTabPage.clear();
368 MultiListBox::dispose();
371 bool OWizTypeSelectList::IsPrimaryKeyAllowed() const
373 const sal_Int32 nCount = GetSelectEntryCount();
375 for( sal_Int32 j = 0; m_bPKey && j < nCount; ++j )
377 OFieldDescription* pField = static_cast<OFieldDescription*>(GetEntryData(GetSelectEntryPos(j)));
378 if(!pField || pField->getTypeInfo()->nSearchType == ColumnSearch::NONE)
379 return false;
381 return true;
384 void OWizTypeSelectList::setPrimaryKey(OFieldDescription* _pFieldDescr, sal_uInt16 _nPos, bool _bSet)
386 OUString sColumnName = GetEntry(_nPos);
387 RemoveEntry(_nPos);
388 _pFieldDescr->SetPrimaryKey(_bSet);
389 if( _bSet )
390 InsertEntry(sColumnName, static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_imgPKey,_nPos);
391 else if( _pFieldDescr->getTypeInfo()->bNullable )
393 _pFieldDescr->SetControlDefault(Any());
394 InsertEntry(sColumnName,_nPos);
396 SetEntryData(_nPos,_pFieldDescr);
399 VCL_BUILDER_FACTORY(OWizTypeSelectList)
401 bool OWizTypeSelectList::PreNotify( NotifyEvent& rEvt )
403 bool bDone = false;
404 switch( rEvt.GetType() )
406 case MouseNotifyEvent::MOUSEBUTTONDOWN:
408 const MouseEvent* pMEvt = rEvt.GetMouseEvent();
409 if(pMEvt->IsRight() && !pMEvt->GetModifier())
410 bDone = true;
412 break;
413 case MouseNotifyEvent::COMMAND:
415 if(!IsPrimaryKeyAllowed())
416 break;
418 const CommandEvent* pComEvt = rEvt.GetCommandEvent();
419 if(pComEvt->GetCommand() != CommandEventId::ContextMenu)
420 break;
421 // the place, at which was clicked
422 Point ptWhere(0,0);
423 if (pComEvt->IsMouseEvent())
424 ptWhere = pComEvt->GetMousePosPixel();
426 PopupMenu aContextMenu(ModuleRes(RID_SBA_RTF_PKEYPOPUP));
427 // Should primary key checkbox be checked?
428 const sal_Int32 nCount = GetEntryCount();
429 bool bCheckOk = false;
430 for(sal_Int32 j = 0 ; j < nCount ; ++j)
432 OFieldDescription* pFieldDescr = static_cast<OFieldDescription*>(GetEntryData(j));
433 // if at least one of the fields is selected but not in the primary key,
434 // or is in the primary key but not selected, then don't check the
435 // primary key checkbox.
436 if( pFieldDescr && pFieldDescr->IsPrimaryKey() != IsEntryPosSelected(j) )
438 bCheckOk = false;
439 break;
441 if (!bCheckOk && IsEntryPosSelected(j))
442 bCheckOk = true;
445 if (bCheckOk)
446 aContextMenu.CheckItem( SID_TABLEDESIGN_TABED_PRIMARYKEY );
448 switch( aContextMenu.Execute( this, ptWhere ) )
450 case SID_TABLEDESIGN_TABED_PRIMARYKEY:
452 for(sal_Int32 j = 0 ; j < nCount ; ++j)
454 OFieldDescription* pFieldDescr = static_cast<OFieldDescription*>(GetEntryData(j));
455 if( pFieldDescr )
457 if(!bCheckOk && IsEntryPosSelected(j))
459 setPrimaryKey(pFieldDescr,j,true);
460 SelectEntryPos(j);
462 else
464 setPrimaryKey(pFieldDescr,j);
468 GetSelectHdl().Call(*this);
470 break;
472 bDone = true;
474 break;
475 default:
476 break;
478 return bDone || MultiListBox::PreNotify(rEvt);
481 void OWizTypeSelect::fillColumnList(sal_uInt32 nRows)
483 if(m_pParserStream)
485 sal_Size nTell = m_pParserStream->Tell(); // might change seek position of stream
487 SvParser *pReader = createReader(nRows);
488 if(pReader)
490 pReader->AddFirstRef();
491 pReader->CallParser();
492 pReader->ReleaseRef();
494 m_pParserStream->Seek(nTell);
498 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */