Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / misc / WNameMatch.cxx
blob68c91aa29aeee41596f64a283bc983dc1e879249
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 "WNameMatch.hxx"
21 #include <osl/diagnose.h>
22 #include "FieldDescriptions.hxx"
23 #include "WCopyTable.hxx"
24 #include "dbaccess_helpid.hrc"
25 #include "dbu_misc.hrc"
26 #include <vcl/scrbar.hxx>
27 #include <vcl/settings.hxx>
28 #include <vcl/builderfactory.hxx>
29 #include "svtools/treelistentry.hxx"
30 #include <com/sun/star/sdbc/DataType.hpp>
31 #include <o3tl/make_unique.hxx>
32 using namespace ::dbaui;
33 // OWizColumnSelect
34 OWizNameMatching::OWizNameMatching( vcl::Window* pParent)
35 :OWizardPage( pParent, "NameMatching", "dbaccess/ui/namematchingpage.ui" )
36 , m_aImgUp(ModuleRes(IMG_UP))
37 , m_aImgDown(ModuleRes(IMG_DOWN))
39 get(m_pTABLE_LEFT, "leftlabel");
40 get(m_pTABLE_RIGHT, "rightlabel");
41 get(m_pCTRL_LEFT, "left");
42 get(m_pCTRL_RIGHT, "right");
43 get(m_pColumn_up, "up");
44 m_pColumn_up->SetModeImage(m_aImgUp);
45 get(m_pColumn_down, "down");
46 m_pColumn_down->SetModeImage(m_aImgDown);
47 get(m_pColumn_up_right, "up_right");
48 m_pColumn_up_right->SetModeImage(m_aImgUp);
49 get(m_pColumn_down_right, "down_right");
50 m_pColumn_down_right->SetModeImage(m_aImgDown);
51 get(m_pAll, "all");
52 get(m_pNone, "none");
54 m_pColumn_up->SetClickHdl(LINK(this,OWizNameMatching,ButtonClickHdl));
55 m_pColumn_down->SetClickHdl(LINK(this,OWizNameMatching,ButtonClickHdl));
57 m_pColumn_up_right->SetClickHdl(LINK(this,OWizNameMatching,RightButtonClickHdl));
58 m_pColumn_down_right->SetClickHdl(LINK(this,OWizNameMatching,RightButtonClickHdl));
60 m_pAll->SetClickHdl(LINK(this,OWizNameMatching,AllNoneClickHdl));
61 m_pNone->SetClickHdl(LINK(this,OWizNameMatching,AllNoneClickHdl));
63 m_pCTRL_LEFT->SetSelectHdl(LINK(this,OWizNameMatching,TableListClickHdl));
64 m_pCTRL_RIGHT->SetSelectHdl(LINK(this,OWizNameMatching,TableListRightSelectHdl));
65 m_pCTRL_RIGHT->EnableCheckButton( nullptr );
67 m_pCTRL_LEFT->SetStyle( m_pCTRL_LEFT->GetStyle() | WB_FORCE_MAKEVISIBLE );
68 m_pCTRL_RIGHT->SetStyle( m_pCTRL_RIGHT->GetStyle() | WB_FORCE_MAKEVISIBLE );
70 m_sSourceText = m_pTABLE_LEFT->GetText();
71 m_sSourceText += "\n";
72 m_sDestText = m_pTABLE_RIGHT->GetText();
73 m_sDestText += "\n";
76 OWizNameMatching::~OWizNameMatching()
78 disposeOnce();
81 void OWizNameMatching::dispose()
83 m_pTABLE_LEFT.clear();
84 m_pTABLE_RIGHT.clear();
85 m_pCTRL_LEFT.clear();
86 m_pCTRL_RIGHT.clear();
87 m_pColumn_up.clear();
88 m_pColumn_down.clear();
89 m_pColumn_up_right.clear();
90 m_pColumn_down_right.clear();
91 m_pAll.clear();
92 m_pNone.clear();
93 OWizardPage::dispose();
96 void OWizNameMatching::Reset()
98 // urspr"unglichen zustand wiederherstellen
99 // the left tree contains bitmaps so i need to resize the right one
100 if(m_bFirstTime)
102 m_pCTRL_RIGHT->SetReadOnly(); // sets autoinc to readonly
103 m_pCTRL_RIGHT->SetEntryHeight(m_pCTRL_LEFT->GetEntryHeight());
104 m_pCTRL_RIGHT->SetIndent(m_pCTRL_LEFT->GetIndent());
105 m_pCTRL_RIGHT->SetSpaceBetweenEntries(m_pCTRL_LEFT->GetSpaceBetweenEntries());
107 m_bFirstTime = false;
112 void OWizNameMatching::ActivatePage( )
115 // set source table name
116 OUString aName = m_sSourceText;
117 aName += m_pParent->m_sSourceName;
119 m_pTABLE_LEFT->SetText(aName);
121 // set dest table name
122 aName = m_sDestText;
123 aName += m_pParent->m_sName;
124 m_pTABLE_RIGHT->SetText(aName);
126 m_pCTRL_LEFT->FillListBox(m_pParent->getSrcVector());
127 m_pCTRL_RIGHT->FillListBox(m_pParent->getDestVector());
129 m_pColumn_up->Enable( m_pCTRL_LEFT->GetEntryCount() > 1 );
130 m_pColumn_down->Enable( m_pCTRL_LEFT->GetEntryCount() > 1 );
132 m_pColumn_up_right->Enable( m_pCTRL_RIGHT->GetEntryCount() > 1 );
133 m_pColumn_down_right->Enable( m_pCTRL_RIGHT->GetEntryCount() > 1 );
135 m_pParent->EnableNextButton(false);
136 m_pCTRL_LEFT->GrabFocus();
139 bool OWizNameMatching::LeavePage()
142 const ODatabaseExport::TColumnVector& rSrcColumns = m_pParent->getSrcVector();
144 m_pParent->m_vColumnPos.clear();
145 m_pParent->m_vColumnTypes.clear();
146 m_pParent->m_vColumnPos.resize( rSrcColumns.size(), ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND, COLUMN_POSITION_NOT_FOUND ) );
147 m_pParent->m_vColumnTypes.resize( rSrcColumns.size(), COLUMN_POSITION_NOT_FOUND );
149 sal_Int32 nParamPos = 0;
150 SvTreeListEntry* pLeftEntry = m_pCTRL_LEFT->GetModel()->First();
151 SvTreeListEntry* pRightEntry = m_pCTRL_RIGHT->GetModel()->First();
152 while(pLeftEntry && pRightEntry)
154 OFieldDescription* pSrcField = static_cast<OFieldDescription*>(pLeftEntry->GetUserData());
155 OSL_ENSURE(pSrcField,"OWizNameMatching: OColumn can not be null!");
157 ODatabaseExport::TColumnVector::const_iterator aSrcIter = rSrcColumns.begin();
158 ODatabaseExport::TColumnVector::const_iterator aSrcEnd = rSrcColumns.end();
159 for(;aSrcIter != aSrcEnd && (*aSrcIter)->second != pSrcField;++aSrcIter)
161 const sal_Int32 nPos = ::std::distance(rSrcColumns.begin(),aSrcIter);
163 if(m_pCTRL_LEFT->GetCheckButtonState(pLeftEntry) == SvButtonState::Checked)
165 OFieldDescription* pDestField = static_cast<OFieldDescription*>(pRightEntry->GetUserData());
166 OSL_ENSURE(pDestField,"OWizNameMatching: OColumn can not be null!");
167 const ODatabaseExport::TColumnVector& rDestColumns = m_pParent->getDestVector();
168 ODatabaseExport::TColumnVector::const_iterator aDestIter = rDestColumns.begin();
169 ODatabaseExport::TColumnVector::const_iterator aDestEnd = rDestColumns.end();
171 for(;aDestIter != aDestEnd && (*aDestIter)->second != pDestField;++aDestIter)
174 OSL_ENSURE((nPos) < static_cast<sal_Int32>(m_pParent->m_vColumnPos.size()),"m_pParent->m_vColumnPos: Illegal index for vector");
175 m_pParent->m_vColumnPos[nPos].first = ++nParamPos;
176 m_pParent->m_vColumnPos[nPos].second = ::std::distance(rDestColumns.begin(),aDestIter) + 1;
178 TOTypeInfoSP pTypeInfo;
180 assert(aDestIter != aDestEnd);
181 if (aDestIter != aDestEnd)
183 bool bNotConvert = true;
184 pTypeInfo = m_pParent->convertType((*aDestIter)->second->getSpecialTypeInfo(), bNotConvert);
187 sal_Int32 nType = css::sdbc::DataType::VARCHAR;
188 if ( pTypeInfo.get() )
189 nType = pTypeInfo->nType;
190 m_pParent->m_vColumnTypes[nPos] = nType;
192 else
194 m_pParent->m_vColumnPos[nPos].first = COLUMN_POSITION_NOT_FOUND;
195 m_pParent->m_vColumnPos[nPos].second = COLUMN_POSITION_NOT_FOUND;
198 pLeftEntry = m_pCTRL_LEFT->GetModel()->Next(pLeftEntry);
199 pRightEntry = m_pCTRL_RIGHT->GetModel()->Next(pRightEntry);
202 return true;
205 OUString OWizNameMatching::GetTitle() const { return ModuleRes(STR_WIZ_NAME_MATCHING_TITEL); }
207 IMPL_LINK_TYPED( OWizNameMatching, ButtonClickHdl, Button *, pButton, void )
209 SvTreeListEntry* pEntry = m_pCTRL_LEFT->FirstSelected();
210 if ( pEntry )
212 sal_Int32 nPos = m_pCTRL_LEFT->GetModel()->GetAbsPos(pEntry);
213 if(pButton == m_pColumn_up && nPos)
214 --nPos;
215 else if(pButton == m_pColumn_down)
216 nPos += 2;
218 m_pCTRL_LEFT->ModelIsMoving(pEntry,nullptr,nPos);
219 m_pCTRL_LEFT->GetModel()->Move(pEntry,nullptr,nPos);
220 m_pCTRL_LEFT->ModelHasMoved(pEntry);
222 long nThumbPos = m_pCTRL_LEFT->GetVScroll()->GetThumbPos();
223 long nVisibleSize = m_pCTRL_LEFT->GetVScroll()->GetVisibleSize();
225 if(pButton == m_pColumn_down && (nThumbPos+nVisibleSize+1) < nPos)
227 m_pCTRL_LEFT->GetVScroll()->DoScrollAction(SCROLL_LINEDOWN);
230 TableListClickHdl(m_pCTRL_LEFT);
234 IMPL_LINK_TYPED( OWizNameMatching, RightButtonClickHdl, Button *, pButton, void )
236 SvTreeListEntry* pEntry = m_pCTRL_RIGHT->FirstSelected();
237 if ( pEntry )
239 sal_Int32 nPos = m_pCTRL_RIGHT->GetModel()->GetAbsPos(pEntry);
240 if(pButton == m_pColumn_up_right && nPos)
241 --nPos;
242 else if(pButton == m_pColumn_down_right)
243 nPos += 2;
245 m_pCTRL_RIGHT->ModelIsMoving(pEntry,nullptr,nPos);
246 m_pCTRL_RIGHT->GetModel()->Move(pEntry,nullptr,nPos);
247 m_pCTRL_RIGHT->ModelHasMoved(pEntry);
248 long nThumbPos = m_pCTRL_RIGHT->GetVScroll()->GetThumbPos();
249 long nVisibleSize = m_pCTRL_RIGHT->GetVScroll()->GetVisibleSize();
251 if(pButton == m_pColumn_down_right && (nThumbPos+nVisibleSize+1) < nPos)
252 m_pCTRL_RIGHT->GetVScroll()->DoScrollAction(SCROLL_LINEDOWN);
253 TableListRightSelectHdl(m_pCTRL_RIGHT);
257 IMPL_LINK_NOARG_TYPED( OWizNameMatching, TableListClickHdl, SvTreeListBox*, void )
259 SvTreeListEntry* pEntry = m_pCTRL_LEFT->FirstSelected();
260 if(pEntry)
262 sal_uLong nPos = m_pCTRL_LEFT->GetModel()->GetAbsPos(pEntry);
263 SvTreeListEntry* pOldEntry = m_pCTRL_RIGHT->FirstSelected();
264 if(pOldEntry && nPos != m_pCTRL_RIGHT->GetModel()->GetAbsPos(pOldEntry))
266 if(pOldEntry)
267 m_pCTRL_RIGHT->Select(pOldEntry,false);
268 pOldEntry = m_pCTRL_RIGHT->GetEntry(nPos);
269 if(pOldEntry)
271 sal_uLong nNewPos = m_pCTRL_LEFT->GetModel()->GetAbsPos(m_pCTRL_LEFT->GetFirstEntryInView());
272 if ( nNewPos - nPos == 1 )
273 --nNewPos;
274 m_pCTRL_RIGHT->MakeVisible(m_pCTRL_RIGHT->GetEntry(nNewPos), true);
275 m_pCTRL_RIGHT->Select(pOldEntry);
278 else if(!pOldEntry)
280 pOldEntry = m_pCTRL_RIGHT->GetEntry(nPos);
281 if(pOldEntry)
283 m_pCTRL_RIGHT->Select(pOldEntry);
289 IMPL_LINK_NOARG_TYPED( OWizNameMatching, TableListRightSelectHdl, SvTreeListBox*, void )
291 SvTreeListEntry* pEntry = m_pCTRL_RIGHT->FirstSelected();
292 if(pEntry)
294 sal_uLong nPos = m_pCTRL_RIGHT->GetModel()->GetAbsPos(pEntry);
295 SvTreeListEntry* pOldEntry = m_pCTRL_LEFT->FirstSelected();
296 if(pOldEntry && nPos != m_pCTRL_LEFT->GetModel()->GetAbsPos(pOldEntry))
298 if(pOldEntry)
299 m_pCTRL_LEFT->Select(pOldEntry,false);
300 pOldEntry = m_pCTRL_LEFT->GetEntry(nPos);
301 if(pOldEntry)
303 sal_uLong nNewPos = m_pCTRL_RIGHT->GetModel()->GetAbsPos(m_pCTRL_RIGHT->GetFirstEntryInView());
304 if ( nNewPos - nPos == 1 )
305 nNewPos--;
306 m_pCTRL_LEFT->MakeVisible(m_pCTRL_LEFT->GetEntry(nNewPos), true);
307 m_pCTRL_LEFT->Select(pOldEntry);
310 else if(!pOldEntry)
312 pOldEntry = m_pCTRL_LEFT->GetEntry(nPos);
313 if(pOldEntry)
315 m_pCTRL_LEFT->Select(pOldEntry);
321 IMPL_LINK_TYPED( OWizNameMatching, AllNoneClickHdl, Button *, pButton, void )
323 bool bAll = pButton == m_pAll;
324 SvTreeListEntry* pEntry = m_pCTRL_LEFT->First();
325 while(pEntry)
327 m_pCTRL_LEFT->SetCheckButtonState( pEntry, bAll ? SvButtonState::Checked : SvButtonState::Unchecked);
328 pEntry = m_pCTRL_LEFT->Next(pEntry);
332 // class OColumnString
333 class OColumnString : public SvLBoxString
335 bool m_bReadOnly;
336 public:
337 OColumnString( const OUString& rStr, bool RO )
338 :SvLBoxString(rStr)
339 ,m_bReadOnly(RO)
343 virtual void Paint(const Point& rPos, SvTreeListBox& rDev, vcl::RenderContext& rRenderContext,
344 const SvViewDataEntry* pView, const SvTreeListEntry& rEntry) override;
347 void OColumnString::Paint(const Point& rPos, SvTreeListBox& /*rDev*/, vcl::RenderContext& rRenderContext,
348 const SvViewDataEntry* /*pView*/, const SvTreeListEntry& /*rEntry*/)
350 rRenderContext.Push(PushFlags::TEXTCOLOR | PushFlags::TEXTFILLCOLOR);
351 if(m_bReadOnly)
353 const StyleSettings& rStyleSettings = rRenderContext.GetSettings().GetStyleSettings();
354 rRenderContext.SetTextColor(rStyleSettings.GetDisableColor());
355 rRenderContext.SetTextFillColor(rStyleSettings.GetFieldColor());
357 rRenderContext.DrawText(rPos, GetText());
358 rRenderContext.Pop();
361 OColumnTreeBox::OColumnTreeBox( vcl::Window* pParent, WinBits nBits )
362 : OMarkableTreeListBox(pParent, nBits)
363 , m_bReadOnly(false)
365 SetDragDropMode( DragDropMode::NONE );
366 EnableInplaceEditing( false );
367 SetStyle(GetStyle() | WB_BORDER | WB_HASBUTTONS | WB_HSCROLL | nBits);
368 SetSelectionMode( SINGLE_SELECTION );
371 VCL_BUILDER_FACTORY(OColumnTreeBox)
373 void OColumnTreeBox::InitEntry(SvTreeListEntry* pEntry, const OUString& rStr, const Image& rImg1, const Image& rImg2, SvLBoxButtonKind eButtonKind)
375 DBTreeListBox::InitEntry(pEntry, rStr, rImg1, rImg2, eButtonKind);
376 pEntry->ReplaceItem(o3tl::make_unique<OColumnString>(rStr,false), pEntry->ItemCount() - 1);
379 bool OColumnTreeBox::Select( SvTreeListEntry* pEntry, bool bSelect )
381 if(bSelect)
383 OFieldDescription* pColumn = static_cast<OFieldDescription*>(pEntry->GetUserData());
384 if(!(pColumn->IsAutoIncrement() && m_bReadOnly))
385 bSelect = DBTreeListBox::Select( pEntry,bSelect );
387 else
388 bSelect = DBTreeListBox::Select( pEntry,bSelect );
389 return bSelect;
392 void OColumnTreeBox::FillListBox( const ODatabaseExport::TColumnVector& _rList)
394 Clear();
395 ODatabaseExport::TColumnVector::const_iterator aIter = _rList.begin();
396 ODatabaseExport::TColumnVector::const_iterator aEnd = _rList.end();
397 for(;aIter != aEnd;++aIter)
399 SvTreeListEntry* pEntry = InsertEntry((*aIter)->first, nullptr, false, TREELIST_APPEND, (*aIter)->second);
400 SvButtonState eState = !(m_bReadOnly && (*aIter)->second->IsAutoIncrement()) ? SvButtonState::Checked : SvButtonState::Unchecked;
401 SetCheckButtonState( pEntry, eState );
405 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */