Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / misc / WColumnSelect.cxx
blobc0aa281d88aeab8944420088f3e50a3394eaed8f
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 "WColumnSelect.hxx"
21 #include "dbu_misc.hrc"
22 #include <osl/diagnose.h>
23 #include "WCopyTable.hxx"
24 #include <com/sun/star/sdbcx/XDataDescriptorFactory.hpp>
25 #include <com/sun/star/sdbcx/XColumnsSupplier.hpp>
26 #include <com/sun/star/sdbcx/XAppend.hpp>
27 #include "moduledbu.hxx"
28 #include <com/sun/star/sdbc/DataType.hpp>
29 #include <com/sun/star/sdbc/ColumnValue.hpp>
30 #include <com/sun/star/sdb/application/CopyTableOperation.hpp>
31 #include "dbustrings.hrc"
32 #include <functional>
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::beans;
36 using namespace ::com::sun::star::container;
37 using namespace ::com::sun::star::sdbc;
38 using namespace ::com::sun::star::sdbcx;
39 using namespace dbaui;
41 namespace CopyTableOperation = ::com::sun::star::sdb::application::CopyTableOperation;
43 OUString OWizColumnSelect::GetTitle() const { return ModuleRes(STR_WIZ_COLUMN_SELECT_TITEL); }
45 OWizardPage::OWizardPage(vcl::Window* pParent, const OString& rID, const OUString& rUIXMLDescription)
46 : TabPage(pParent, rID, rUIXMLDescription)
47 ,m_pParent(static_cast<OCopyTableWizard*>(pParent))
48 ,m_bFirstTime(true)
52 OWizardPage::~OWizardPage()
54 disposeOnce();
57 void OWizardPage::dispose()
59 m_pParent.clear();
60 TabPage::dispose();
63 // OWizColumnSelect
64 OWizColumnSelect::OWizColumnSelect( vcl::Window* pParent)
65 :OWizardPage( pParent, "ApplyColPage", "dbaccess/ui/applycolpage.ui")
67 get(m_pOrgColumnNames, "from");
68 get(m_pColumn_RH, "colrh");
69 get(m_pColumns_RH, "colsrh");
70 get(m_pColumn_LH, "collh");
71 get(m_pColumns_LH, "colslh");
72 get(m_pNewColumnNames, "to");
74 Size aSize(approximate_char_width() * 30, GetTextHeight() * 40);
75 m_pOrgColumnNames->set_width_request(aSize.Width());
76 m_pOrgColumnNames->set_height_request(aSize.Height());
77 m_pNewColumnNames->set_width_request(aSize.Width());
78 m_pNewColumnNames->set_height_request(aSize.Height());
80 m_pColumn_RH->SetClickHdl(LINK(this,OWizColumnSelect,ButtonClickHdl));
81 m_pColumn_LH->SetClickHdl(LINK(this,OWizColumnSelect,ButtonClickHdl));
82 m_pColumns_RH->SetClickHdl(LINK(this,OWizColumnSelect,ButtonClickHdl));
83 m_pColumns_LH->SetClickHdl(LINK(this,OWizColumnSelect,ButtonClickHdl));
85 m_pOrgColumnNames->EnableMultiSelection(true);
86 m_pNewColumnNames->EnableMultiSelection(true);
88 m_pOrgColumnNames->SetDoubleClickHdl(LINK(this,OWizColumnSelect,ListDoubleClickHdl));
89 m_pNewColumnNames->SetDoubleClickHdl(LINK(this,OWizColumnSelect,ListDoubleClickHdl));
92 OWizColumnSelect::~OWizColumnSelect()
94 disposeOnce();
97 void OWizColumnSelect::dispose()
99 while ( m_pNewColumnNames->GetEntryCount() )
101 void* pData = m_pNewColumnNames->GetEntryData(0);
102 if ( pData )
103 delete static_cast<OFieldDescription*>(pData);
105 m_pNewColumnNames->RemoveEntry(0);
107 m_pNewColumnNames->Clear();
108 m_pOrgColumnNames.clear();
109 m_pColumn_RH.clear();
110 m_pColumns_RH.clear();
111 m_pColumn_LH.clear();
112 m_pColumns_LH.clear();
113 m_pNewColumnNames.clear();
114 OWizardPage::dispose();
117 void OWizColumnSelect::Reset()
119 // restore original state
120 clearListBox(*m_pOrgColumnNames);
121 clearListBox(*m_pNewColumnNames);
122 m_pParent->m_mNameMapping.clear();
124 // insert the source columns in the left listbox
125 const ODatabaseExport::TColumnVector& rSrcColumns = m_pParent->getSrcVector();
126 ODatabaseExport::TColumnVector::const_iterator aIter = rSrcColumns.begin();
127 ODatabaseExport::TColumnVector::const_iterator aEnd = rSrcColumns.end();
129 for(;aIter != aEnd;++aIter)
131 const sal_Int32 nPos = m_pOrgColumnNames->InsertEntry((*aIter)->first);
132 m_pOrgColumnNames->SetEntryData(nPos,(*aIter)->second);
135 if(m_pOrgColumnNames->GetEntryCount())
136 m_pOrgColumnNames->SelectEntryPos(0);
138 m_bFirstTime = false;
141 void OWizColumnSelect::ActivatePage( )
143 // if there are no dest columns reset the left side with the origibnal columns
144 if(m_pParent->getDestColumns().empty())
145 Reset();
147 clearListBox(*m_pNewColumnNames);
149 const ODatabaseExport::TColumnVector& rDestColumns = m_pParent->getDestVector();
151 ODatabaseExport::TColumnVector::const_iterator aIter = rDestColumns.begin();
152 ODatabaseExport::TColumnVector::const_iterator aEnd = rDestColumns.end();
153 for(;aIter != aEnd;++aIter)
155 const sal_Int32 nPos = m_pNewColumnNames->InsertEntry((*aIter)->first);
156 m_pNewColumnNames->SetEntryData(nPos,new OFieldDescription(*((*aIter)->second)));
157 m_pOrgColumnNames->RemoveEntry((*aIter)->first);
159 m_pParent->GetOKButton().Enable(m_pNewColumnNames->GetEntryCount() != 0);
160 m_pParent->EnableNextButton(m_pNewColumnNames->GetEntryCount() && m_pParent->getOperation() != CopyTableOperation::AppendData);
161 m_pColumns_RH->GrabFocus();
164 bool OWizColumnSelect::LeavePage()
167 m_pParent->clearDestColumns();
169 for(sal_Int32 i=0 ; i< m_pNewColumnNames->GetEntryCount();++i)
171 OFieldDescription* pField = static_cast<OFieldDescription*>(m_pNewColumnNames->GetEntryData(i));
172 OSL_ENSURE(pField,"The field information can not be null!");
173 m_pParent->insertColumn(i,pField);
176 clearListBox(*m_pNewColumnNames);
178 if ( m_pParent->GetPressedButton() == OCopyTableWizard::WIZARD_NEXT
179 || m_pParent->GetPressedButton() == OCopyTableWizard::WIZARD_FINISH
181 return m_pParent->getDestColumns().size() != 0;
182 else
183 return true;
186 IMPL_LINK_TYPED( OWizColumnSelect, ButtonClickHdl, Button *, pButton, void )
188 ListBox *pLeft = nullptr;
189 ListBox *pRight = nullptr;
190 bool bAll = false;
192 if (pButton == m_pColumn_RH)
194 pLeft = m_pOrgColumnNames;
195 pRight = m_pNewColumnNames;
197 else if(pButton == m_pColumn_LH)
199 pLeft = m_pNewColumnNames;
200 pRight = m_pOrgColumnNames;
202 else if(pButton == m_pColumns_RH)
204 pLeft = m_pOrgColumnNames;
205 pRight = m_pNewColumnNames;
206 bAll = true;
208 else if(pButton == m_pColumns_LH)
210 pLeft = m_pNewColumnNames;
211 pRight = m_pOrgColumnNames;
212 bAll = true;
215 if (!pLeft || !pRight)
216 return;
218 Reference< XDatabaseMetaData > xMetaData( m_pParent->m_xDestConnection->getMetaData() );
219 OUString sExtraChars = xMetaData->getExtraNameCharacters();
220 sal_Int32 nMaxNameLen = m_pParent->getMaxColumnNameLength();
222 ::comphelper::UStringMixEqual aCase(xMetaData->supportsMixedCaseQuotedIdentifiers());
223 ::std::vector< OUString> aRightColumns;
224 fillColumns(pRight,aRightColumns);
226 if(!bAll)
228 for(sal_Int32 i=0; i < pLeft->GetSelectEntryCount(); ++i)
229 moveColumn(pRight,pLeft,aRightColumns,pLeft->GetSelectEntry(i),sExtraChars,nMaxNameLen,aCase);
231 for(sal_Int32 j=pLeft->GetSelectEntryCount(); j ; --j)
232 pLeft->RemoveEntry(pLeft->GetSelectEntry(j-1));
234 else
236 const sal_Int32 nEntries = pLeft->GetEntryCount();
237 for(sal_Int32 i=0; i < nEntries; ++i)
238 moveColumn(pRight,pLeft,aRightColumns,pLeft->GetEntry(i),sExtraChars,nMaxNameLen,aCase);
239 for(sal_Int32 j=pLeft->GetEntryCount(); j ; )
240 pLeft->RemoveEntry(--j);
243 enableButtons();
245 if(m_pOrgColumnNames->GetEntryCount())
246 m_pOrgColumnNames->SelectEntryPos(0);
249 IMPL_LINK_TYPED( OWizColumnSelect, ListDoubleClickHdl, ListBox&, rListBox, void )
251 ListBox *pLeft,*pRight;
252 if(&rListBox == m_pOrgColumnNames)
254 pLeft = m_pOrgColumnNames;
255 pRight = m_pNewColumnNames;
257 else
259 pRight = m_pOrgColumnNames;
260 pLeft = m_pNewColumnNames;
263 // If database is able to process PrimaryKeys, set PrimaryKey
264 Reference< XDatabaseMetaData > xMetaData( m_pParent->m_xDestConnection->getMetaData() );
265 OUString sExtraChars = xMetaData->getExtraNameCharacters();
266 sal_Int32 nMaxNameLen = m_pParent->getMaxColumnNameLength();
268 ::comphelper::UStringMixEqual aCase(xMetaData->supportsMixedCaseQuotedIdentifiers());
269 ::std::vector< OUString> aRightColumns;
270 fillColumns(pRight,aRightColumns);
272 for(sal_Int32 i=0; i < pLeft->GetSelectEntryCount(); ++i)
273 moveColumn(pRight,pLeft,aRightColumns,pLeft->GetSelectEntry(i),sExtraChars,nMaxNameLen,aCase);
274 for(sal_Int32 j=pLeft->GetSelectEntryCount(); j ; )
275 pLeft->RemoveEntry(pLeft->GetSelectEntry(--j));
277 enableButtons();
280 void OWizColumnSelect::clearListBox(ListBox& _rListBox)
282 while(_rListBox.GetEntryCount())
283 _rListBox.RemoveEntry(0);
284 _rListBox.Clear();
287 void OWizColumnSelect::fillColumns(ListBox* pRight,::std::vector< OUString> &_rRightColumns)
289 const sal_Int32 nCount = pRight->GetEntryCount();
290 _rRightColumns.reserve(nCount);
291 for(sal_Int32 i=0; i < nCount; ++i)
292 _rRightColumns.push_back(pRight->GetEntry(i));
295 void OWizColumnSelect::createNewColumn( ListBox* _pListbox,
296 OFieldDescription* _pSrcField,
297 ::std::vector< OUString>& _rRightColumns,
298 const OUString& _sColumnName,
299 const OUString& _sExtraChars,
300 sal_Int32 _nMaxNameLen,
301 const ::comphelper::UStringMixEqual& _aCase)
303 OUString sConvertedName = m_pParent->convertColumnName(TMultiListBoxEntryFindFunctor(&_rRightColumns,_aCase),
304 _sColumnName,
305 _sExtraChars,
306 _nMaxNameLen);
307 OFieldDescription* pNewField = new OFieldDescription(*_pSrcField);
308 pNewField->SetName(sConvertedName);
309 bool bNotConvert = true;
310 pNewField->SetType(m_pParent->convertType(_pSrcField->getSpecialTypeInfo(),bNotConvert));
311 if ( !m_pParent->supportsPrimaryKey() )
312 pNewField->SetPrimaryKey(false);
314 _pListbox->SetEntryData(_pListbox->InsertEntry(sConvertedName),pNewField);
315 _rRightColumns.push_back(sConvertedName);
317 if ( !bNotConvert )
318 m_pParent->showColumnTypeNotSupported(sConvertedName);
321 void OWizColumnSelect::moveColumn( ListBox* _pRight,
322 ListBox* _pLeft,
323 ::std::vector< OUString>& _rRightColumns,
324 const OUString& _sColumnName,
325 const OUString& _sExtraChars,
326 sal_Int32 _nMaxNameLen,
327 const ::comphelper::UStringMixEqual& _aCase)
329 if(_pRight == m_pNewColumnNames)
331 // we copy the column into the new format for the dest
332 OFieldDescription* pSrcField = static_cast<OFieldDescription*>(_pLeft->GetEntryData(_pLeft->GetEntryPos(OUString(_sColumnName))));
333 createNewColumn(_pRight,pSrcField,_rRightColumns,_sColumnName,_sExtraChars,_nMaxNameLen,_aCase);
335 else
337 // find the new column in the dest name mapping to obtain the old column
338 OCopyTableWizard::TNameMapping::const_iterator aIter = ::std::find_if(m_pParent->m_mNameMapping.begin(),m_pParent->m_mNameMapping.end(),
339 [&_aCase, &_sColumnName] (const OCopyTableWizard::TNameMapping::value_type& nameMap) {
340 return _aCase(nameMap.second, _sColumnName);
343 OSL_ENSURE(aIter != m_pParent->m_mNameMapping.end(),"Column must be defined");
344 if ( aIter == m_pParent->m_mNameMapping.end() )
345 return; // do nothing
346 const ODatabaseExport::TColumns& rSrcColumns = m_pParent->getSourceColumns();
347 ODatabaseExport::TColumns::const_iterator aSrcIter = rSrcColumns.find((*aIter).first);
348 if ( aSrcIter != rSrcColumns.end() )
350 // we need also the old position of this column to insert it back on that position again
351 const ODatabaseExport::TColumnVector& rSrcVector = m_pParent->getSrcVector();
352 ODatabaseExport::TColumnVector::const_iterator aPos = ::std::find(rSrcVector.begin(), rSrcVector.end(), aSrcIter);
353 OSL_ENSURE( aPos != rSrcVector.end(),"Invalid position for the iterator here!");
354 ODatabaseExport::TColumnVector::size_type nPos = (aPos - rSrcVector.begin()) - adjustColumnPosition(_pLeft, _sColumnName, (aPos - rSrcVector.begin()), _aCase);
356 _pRight->SetEntryData( _pRight->InsertEntry( (*aIter).first, sal::static_int_cast< sal_uInt16 >(nPos)),aSrcIter->second );
357 _rRightColumns.push_back((*aIter).first);
358 m_pParent->removeColumnNameFromNameMap(_sColumnName);
363 // Simply returning fields back to their original position is
364 // not enough. We need to take into account what fields have
365 // been removed earlier and adjust accordingly. Based on the
366 // algorithm employed in moveColumn().
367 sal_Int32 OWizColumnSelect::adjustColumnPosition( ListBox* _pLeft,
368 const OUString& _sColumnName,
369 ODatabaseExport::TColumnVector::size_type nCurrentPos,
370 const ::comphelper::UStringMixEqual& _aCase)
372 sal_Int32 nAdjustedPos = 0;
374 // if returning all entries to their original position,
375 // then there is no need to adjust the positions.
376 if (m_pColumns_LH->HasFocus())
377 return nAdjustedPos;
379 const sal_Int32 nCount = _pLeft->GetEntryCount();
380 OUString sColumnString;
381 for(sal_Int32 i=0; i < nCount; ++i)
383 sColumnString = _pLeft->GetEntry(i);
384 if(_sColumnName != sColumnString)
386 // find the new column in the dest name mapping to obtain the old column
387 OCopyTableWizard::TNameMapping::const_iterator aIter = ::std::find_if(m_pParent->m_mNameMapping.begin(),m_pParent->m_mNameMapping.end(),
388 [&_aCase, &sColumnString] (const OCopyTableWizard::TNameMapping::value_type& nameMap) {
389 return _aCase(nameMap.second, sColumnString);
392 OSL_ENSURE(aIter != m_pParent->m_mNameMapping.end(),"Column must be defined");
393 const ODatabaseExport::TColumns& rSrcColumns = m_pParent->getSourceColumns();
394 ODatabaseExport::TColumns::const_iterator aSrcIter = rSrcColumns.find((*aIter).first);
395 if ( aSrcIter != rSrcColumns.end() )
397 // we need also the old position of this column to insert it back on that position again
398 const ODatabaseExport::TColumnVector& rSrcVector = m_pParent->getSrcVector();
399 ODatabaseExport::TColumnVector::const_iterator aPos = ::std::find(rSrcVector.begin(), rSrcVector.end(), aSrcIter);
400 ODatabaseExport::TColumnVector::size_type nPos = aPos - rSrcVector.begin();
401 if( nPos < nCurrentPos)
403 nAdjustedPos++;
409 return nAdjustedPos;
412 void OWizColumnSelect::enableButtons()
414 bool bEntries = m_pNewColumnNames->GetEntryCount() != 0;
415 if(!bEntries)
416 m_pParent->m_mNameMapping.clear();
418 m_pParent->GetOKButton().Enable(bEntries);
419 m_pParent->EnableNextButton(bEntries && m_pParent->getOperation() != CopyTableOperation::AppendData);
422 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */