1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 #ifndef DBAUI_WIZ_NAMEMATCHING_HXX
31 #include "WNameMatch.hxx"
32 #endif
33 #ifndef _TOOLS_DEBUG_HXX
34 #include <tools/debug.hxx>
35 #endif
36 #ifndef DBAUI_FIELDDESCRIPTIONS_HXX
37 #include "FieldDescriptions.hxx"
38 #endif
39 #ifndef DBAUI_WIZ_COPYTABLEDIALOG_HXX
40 #include "WCopyTable.hxx"
41 #endif
42 #ifndef _DBA_DBACCESS_HELPID_HRC_
43 #include "dbaccess_helpid.hrc"
44 #endif
45 #ifndef _DBU_MISC_HRC_
46 #include "dbu_misc.hrc"
47 #endif
48 #ifndef DBAUI_WIZARD_PAGES_HRC
49 #include "WizardPages.hrc"
50 #endif
51 #ifndef _SV_SCRBAR_HXX
52 #include <vcl/scrbar.hxx>
53 #endif
54 #ifndef DBAUI_WIZ_COPYTABLEDIALOG_HXX
55 #include "WCopyTable.hxx"
56 #endif
57 #ifndef _COM_SUN_STAR_SDBC_DATATYPE_HPP_
58 #include <com/sun/star/sdbc/DataType.hpp>
59 #endif
60 
61 using namespace ::dbaui;
62 //========================================================================
63 // OWizColumnSelect
64 DBG_NAME(OWizNameMatching)
65 //========================================================================
66 OWizNameMatching::OWizNameMatching( Window* pParent)
67 		:OWizardPage( pParent, ModuleRes( TAB_WIZ_NAME_MATCHING		) )
68 		,m_FT_TABLE_LEFT(		this, ModuleRes( FT_TABLE_LEFT			) )
69 		,m_FT_TABLE_RIGHT(		this, ModuleRes( FT_TABLE_RIGHT			) )
70 		,m_CTRL_LEFT(			this, ModuleRes( CTRL_LEFT				) )
71 		,m_CTRL_RIGHT(			this, ModuleRes( CTRL_RIGHT				) )
72         ,m_ibColumn_up(         this, ModuleRes( IB_COLUMN_UP           ) )
73         ,m_ibColumn_down(       this, ModuleRes( IB_COLUMN_DOWN         ) )
74         ,m_ibColumn_up_right(   this, ModuleRes( IB_COLUMN_UP_RIGHT     ) )
75         ,m_ibColumn_down_right( this, ModuleRes( IB_COLUMN_DOWN_RIGHT   ) )
76 		,m_pbAll(				this, ModuleRes( PB_ALL					) )
77 		,m_pbNone(				this, ModuleRes( PB_NONE					) )
78 
79 {
80 	DBG_CTOR(OWizNameMatching,NULL);
81 
82     m_ibColumn_up.SetClickHdl(LINK(this,OWizNameMatching,ButtonClickHdl));
83     m_ibColumn_down.SetClickHdl(LINK(this,OWizNameMatching,ButtonClickHdl));
84 
85     m_ibColumn_up_right.SetClickHdl(LINK(this,OWizNameMatching,RightButtonClickHdl));
86     m_ibColumn_down_right.SetClickHdl(LINK(this,OWizNameMatching,RightButtonClickHdl));
87 
88 	m_pbAll.SetClickHdl(LINK(this,OWizNameMatching,AllNoneClickHdl));
89 	m_pbNone.SetClickHdl(LINK(this,OWizNameMatching,AllNoneClickHdl));
90 
91 	m_CTRL_LEFT.SetSelectHdl(LINK(this,OWizNameMatching,TableListClickHdl));
92 	m_CTRL_RIGHT.SetSelectHdl(LINK(this,OWizNameMatching,TableListRightSelectHdl));
93 	m_CTRL_RIGHT.EnableCheckButton( NULL );
94 
95 	m_CTRL_LEFT.SetStyle( m_CTRL_LEFT.GetStyle() | WB_FORCE_MAKEVISIBLE );
96 	m_CTRL_RIGHT.SetStyle( m_CTRL_RIGHT.GetStyle() | WB_FORCE_MAKEVISIBLE );
97 
98 	m_sSourceText	= m_FT_TABLE_LEFT.GetText();
99 	m_sSourceText.AppendAscii("\n");
100 	m_sDestText		= m_FT_TABLE_RIGHT.GetText();
101 	m_sDestText.AppendAscii("\n");
102 
103 	// set hiContrast
104 	m_ibColumn_up.SetModeImage(ModuleRes(IMG_SORTUP_H),BMP_COLOR_HIGHCONTRAST);
105 	m_ibColumn_down.SetModeImage(ModuleRes(IMG_SORTDOWN_H),BMP_COLOR_HIGHCONTRAST);
106 	m_ibColumn_up_right.SetModeImage(ModuleRes(IMG_SORTUP_H),BMP_COLOR_HIGHCONTRAST);
107 	m_ibColumn_down_right.SetModeImage(ModuleRes(IMG_SORTDOWN_H),BMP_COLOR_HIGHCONTRAST);
108 
109 	FreeResource();
110 }
111 // -----------------------------------------------------------------------
112 OWizNameMatching::~OWizNameMatching()
113 {
114 	DBG_DTOR(OWizNameMatching,NULL);
115 }
116 
117 // -----------------------------------------------------------------------
118 void OWizNameMatching::Reset()
119 {
120 	// urspr"unglichen zustand wiederherstellen
121 	DBG_CHKTHIS(OWizNameMatching,NULL);
122 	// the left tree contains bitmaps so i need to resize the right one
123 	if(m_bFirstTime)
124 	{
125 		m_CTRL_RIGHT.SetReadOnly(); // sets autoinc to readonly
126 		m_CTRL_RIGHT.SetEntryHeight(m_CTRL_LEFT.GetEntryHeight());
127 		m_CTRL_RIGHT.SetIndent(m_CTRL_LEFT.GetIndent());
128 		m_CTRL_RIGHT.SetSpaceBetweenEntries(m_CTRL_LEFT.GetSpaceBetweenEntries());
129 
130 		m_bFirstTime = sal_False;
131 	}
132 
133 	//	m_CTRL_LEFT.Clear();
134 }
135 // -----------------------------------------------------------------------
136 void OWizNameMatching::ActivatePage( )
137 {
138 	DBG_CHKTHIS(OWizNameMatching,NULL);
139 
140 	// set source table name
141     String aName = m_sSourceText;
142 	aName += String(m_pParent->m_sSourceName);
143 
144 	m_FT_TABLE_LEFT.SetText(aName);
145 
146 	// set dest table name
147     aName = m_sDestText;
148 	aName += String(m_pParent->m_sName);
149 	m_FT_TABLE_RIGHT.SetText(aName);
150 
151 
152 	m_CTRL_LEFT.FillListBox(*m_pParent->getSrcVector());
153 	m_CTRL_RIGHT.FillListBox(*m_pParent->getDestVector());
154 
155 	m_ibColumn_up.Enable( m_CTRL_LEFT.GetEntryCount() > 1 );
156 	m_ibColumn_down.Enable( m_CTRL_LEFT.GetEntryCount() > 1 );
157 
158 	m_ibColumn_up_right.Enable( m_CTRL_RIGHT.GetEntryCount() > 1 );
159 	m_ibColumn_down_right.Enable( m_CTRL_RIGHT.GetEntryCount() > 1 );
160 
161 
162 	m_pParent->EnableButton(OCopyTableWizard::WIZARD_NEXT,sal_False);
163 	m_CTRL_LEFT.GrabFocus();
164 }
165 // -----------------------------------------------------------------------
166 sal_Bool OWizNameMatching::LeavePage()
167 {
168 	DBG_CHKTHIS(OWizNameMatching,NULL);
169 
170 	const ODatabaseExport::TColumnVector* pSrcColumns = m_pParent->getSrcVector();
171 
172 	m_pParent->m_vColumnPos.clear();
173 	m_pParent->m_vColumnTypes.clear();
174 	m_pParent->m_vColumnPos.resize( pSrcColumns->size(), ODatabaseExport::TPositions::value_type( COLUMN_POSITION_NOT_FOUND, COLUMN_POSITION_NOT_FOUND ) );
175 	m_pParent->m_vColumnTypes.resize( pSrcColumns->size(), COLUMN_POSITION_NOT_FOUND );
176 
177 
178 	sal_Int32 nParamPos = 0;
179 	SvLBoxEntry* pLeftEntry = m_CTRL_LEFT.GetModel()->First();
180 	SvLBoxEntry* pRightEntry = m_CTRL_RIGHT.GetModel()->First();
181 	while(pLeftEntry && pRightEntry)
182 	{
183 		OFieldDescription* pSrcField = static_cast<OFieldDescription*>(pLeftEntry->GetUserData());
184 		DBG_ASSERT(pSrcField,"OWizNameMatching: OColumn can not be null!");
185 
186 		ODatabaseExport::TColumnVector::const_iterator aSrcIter	= pSrcColumns->begin();
187         ODatabaseExport::TColumnVector::const_iterator aSrcEnd	= pSrcColumns->end();
188 		for(;aSrcIter != aSrcEnd && (*aSrcIter)->second != pSrcField;++aSrcIter)
189 			;
190 		const sal_Int32 nPos = ::std::distance(pSrcColumns->begin(),aSrcIter);
191 
192 		//	sal_Int32 nPos = m_CTRL_LEFT.GetModel()->GetAbsPos(pLeftEntry);
193 		if(m_CTRL_LEFT.GetCheckButtonState(pLeftEntry) == SV_BUTTON_CHECKED)
194 		{
195 			OFieldDescription* pDestField = static_cast<OFieldDescription*>(pRightEntry->GetUserData());
196 			DBG_ASSERT(pDestField,"OWizNameMatching: OColumn can not be null!");
197 			const ODatabaseExport::TColumnVector* pDestColumns			= m_pParent->getDestVector();
198 			ODatabaseExport::TColumnVector::const_iterator aDestIter	= pDestColumns->begin();
199             ODatabaseExport::TColumnVector::const_iterator aDestEnd	= pDestColumns->end();
200 
201 			for(;aDestIter != aDestEnd && (*aDestIter)->second != pDestField;++aDestIter)
202 				;
203 
204             OSL_ENSURE((nPos) < static_cast<sal_Int32>(m_pParent->m_vColumnPos.size()),"m_pParent->m_vColumnPos: Illegal index for vector");
205 			m_pParent->m_vColumnPos[nPos].first	= ++nParamPos;
206 			m_pParent->m_vColumnPos[nPos].second = ::std::distance(pDestColumns->begin(),aDestIter) + 1;
207 			sal_Bool bNotConvert = sal_True;
208 			TOTypeInfoSP pTypeInfo = m_pParent->convertType((*aDestIter)->second->getSpecialTypeInfo(),bNotConvert);
209 			sal_Int32 nType = ::com::sun::star::sdbc::DataType::VARCHAR;
210 			if ( pTypeInfo.get() )
211 				nType = pTypeInfo->nType;
212 			m_pParent->m_vColumnTypes[nPos] = nType;
213 		}
214 		else
215 		{
216 			m_pParent->m_vColumnPos[nPos].first = COLUMN_POSITION_NOT_FOUND;
217 			m_pParent->m_vColumnPos[nPos].second = COLUMN_POSITION_NOT_FOUND;
218 		}
219 
220 		pLeftEntry = m_CTRL_LEFT.GetModel()->Next(pLeftEntry);
221 		pRightEntry = m_CTRL_RIGHT.GetModel()->Next(pRightEntry);
222 	}
223 
224 	return sal_True;
225 }
226 // -----------------------------------------------------------------------
227 String OWizNameMatching::GetTitle() const { return String(ModuleRes(STR_WIZ_NAME_MATCHING_TITEL)); }
228 // -----------------------------------------------------------------------
229 IMPL_LINK( OWizNameMatching, ButtonClickHdl, Button *, pButton )
230 {
231 	SvLBoxEntry* pEntry = m_CTRL_LEFT.FirstSelected();
232 	if ( pEntry )
233 	{
234 		sal_Int32 nPos		= m_CTRL_LEFT.GetModel()->GetAbsPos(pEntry);
235         if(pButton == &m_ibColumn_up && nPos)
236 			--nPos;
237         else if(pButton == &m_ibColumn_down)
238 			nPos += 2;
239 
240 		m_CTRL_LEFT.ModelIsMoving(pEntry,NULL,nPos);
241 		m_CTRL_LEFT.GetModel()->Move(pEntry,NULL,nPos);
242 		m_CTRL_LEFT.ModelHasMoved(pEntry);
243 
244 		long nThumbPos		= m_CTRL_LEFT.GetVScroll()->GetThumbPos();
245 		long nVisibleSize	= m_CTRL_LEFT.GetVScroll()->GetVisibleSize();
246 
247         if(pButton == &m_ibColumn_down && (nThumbPos+nVisibleSize+1) < nPos)
248 		{
249 			m_CTRL_LEFT.GetVScroll()->DoScrollAction(SCROLL_LINEDOWN);
250 			//	m_CTRL_LEFT.MakeVisible(pEntry,sal_True);
251 		}
252 
253 		TableListClickHdl(&m_CTRL_LEFT);
254 	}
255 
256 
257 	return 0;
258 }
259 //------------------------------------------------------------------------------
260 IMPL_LINK( OWizNameMatching, RightButtonClickHdl, Button *, pButton )
261 {
262 	SvLBoxEntry* pEntry = m_CTRL_RIGHT.FirstSelected();
263 	if ( pEntry )
264 	{
265 		sal_Int32 nPos		= m_CTRL_RIGHT.GetModel()->GetAbsPos(pEntry);
266         if(pButton == &m_ibColumn_up_right && nPos)
267 			--nPos;
268         else if(pButton == &m_ibColumn_down_right)
269 			nPos += 2;
270 
271 		m_CTRL_RIGHT.ModelIsMoving(pEntry,NULL,nPos);
272 		m_CTRL_RIGHT.GetModel()->Move(pEntry,NULL,nPos);
273 		m_CTRL_RIGHT.ModelHasMoved(pEntry);
274 		long nThumbPos		= m_CTRL_RIGHT.GetVScroll()->GetThumbPos();
275 		long nVisibleSize	= m_CTRL_RIGHT.GetVScroll()->GetVisibleSize();
276 
277         if(pButton == &m_ibColumn_down_right && (nThumbPos+nVisibleSize+1) < nPos)
278 			m_CTRL_RIGHT.GetVScroll()->DoScrollAction(SCROLL_LINEDOWN);
279 		TableListRightSelectHdl(&m_CTRL_RIGHT);
280 	}
281 	return 0;
282 }
283 //------------------------------------------------------------------------------
284 IMPL_LINK( OWizNameMatching, TableListClickHdl, void*, /*NOTINTERESTEDIN*/ )
285 {
286 	SvLBoxEntry* pEntry = m_CTRL_LEFT.FirstSelected();
287 	if(pEntry)
288 	{
289 		sal_uLong nPos			= m_CTRL_LEFT.GetModel()->GetAbsPos(pEntry);
290 		SvLBoxEntry* pOldEntry = m_CTRL_RIGHT.FirstSelected();
291 		if(pOldEntry && nPos != m_CTRL_RIGHT.GetModel()->GetAbsPos(pOldEntry))
292 		{
293 			if(pOldEntry)
294 				m_CTRL_RIGHT.Select(pOldEntry,sal_False);
295 			pOldEntry = m_CTRL_RIGHT.GetEntry(nPos);
296 			if(pOldEntry)
297 			{
298 				sal_uLong nNewPos = m_CTRL_LEFT.GetModel()->GetAbsPos(m_CTRL_LEFT.GetFirstEntryInView());
299 				if ( nNewPos - nPos == 1 )
300 					--nNewPos;
301 				m_CTRL_RIGHT.MakeVisible(m_CTRL_RIGHT.GetEntry(nNewPos),sal_True);
302 				m_CTRL_RIGHT.Select(pOldEntry,sal_True);
303 			}
304 		}
305 		else if(!pOldEntry)
306 		{
307 			pOldEntry = m_CTRL_RIGHT.GetEntry(nPos);
308 			if(pOldEntry)
309 			{
310 				m_CTRL_RIGHT.Select(pOldEntry,sal_True);
311 			}
312 		}
313 	}
314 
315 	return 0;
316 }
317 //------------------------------------------------------------------------------
318 IMPL_LINK( OWizNameMatching, TableListRightSelectHdl, void*, /*NOTINTERESTEDIN*/ )
319 {
320 	SvLBoxEntry* pEntry = m_CTRL_RIGHT.FirstSelected();
321 	if(pEntry)
322 	{
323 		sal_uLong nPos			= m_CTRL_RIGHT.GetModel()->GetAbsPos(pEntry);
324 		SvLBoxEntry* pOldEntry = m_CTRL_LEFT.FirstSelected();
325 		if(pOldEntry && nPos != m_CTRL_LEFT.GetModel()->GetAbsPos(pOldEntry))
326 		{
327 			if(pOldEntry)
328 				m_CTRL_LEFT.Select(pOldEntry,sal_False);
329 			pOldEntry = m_CTRL_LEFT.GetEntry(nPos);
330 			if(pOldEntry)
331 			{
332 				sal_uLong nNewPos = m_CTRL_RIGHT.GetModel()->GetAbsPos(m_CTRL_RIGHT.GetFirstEntryInView());
333 				if ( nNewPos - nPos == 1 )
334 					nNewPos--;
335 				m_CTRL_LEFT.MakeVisible(m_CTRL_LEFT.GetEntry(nNewPos),sal_True);
336 				m_CTRL_LEFT.Select(pOldEntry,sal_True);
337 			}
338 		}
339 		else if(!pOldEntry)
340 		{
341 			pOldEntry = m_CTRL_LEFT.GetEntry(nPos);
342 			if(pOldEntry)
343 			{
344 				m_CTRL_LEFT.Select(pOldEntry,sal_True);
345 			}
346 		}
347 	}
348 
349 	return 0;
350 }
351 // -----------------------------------------------------------------------
352 IMPL_LINK( OWizNameMatching, AllNoneClickHdl, Button *, pButton )
353 {
354 	sal_Bool bAll = pButton == &m_pbAll;
355 	SvLBoxEntry* pEntry = m_CTRL_LEFT.First();
356 	while(pEntry)
357 	{
358 		m_CTRL_LEFT.SetCheckButtonState( pEntry, bAll ? SV_BUTTON_CHECKED : SV_BUTTON_UNCHECKED);
359 		pEntry = m_CTRL_LEFT.Next(pEntry);
360 	}
361 
362 	return 0;
363 }
364 // -----------------------------------------------------------------------
365 //========================================================================
366 // class OColumnString
367 //========================================================================
368 class OColumnString : public SvLBoxString
369 {
370 	sal_Bool m_bReadOnly;
371 public:
372 	OColumnString( SvLBoxEntry* pEntry, sal_uInt16 nFlags, const String& rStr,sal_Bool _RO)
373 		:SvLBoxString(pEntry,nFlags,rStr)
374 		,m_bReadOnly(_RO)
375 	{
376 	}
377 
378 	virtual void Paint(const Point& rPos, SvLBox& rDev, sal_uInt16 nFlags, SvLBoxEntry* pEntry);
379 	//virtual void InitViewData( SvLBox* pView,SvLBoxEntry* pEntry, SvViewDataItem* pViewData);
380 };
381 
382 
383 
384 //------------------------------------------------------------------------
385 /*
386 void OColumnString::InitViewData( SvLBox* pView,SvLBoxEntry* pEntry, SvViewDataItem* pViewData)
387 {
388 	SvLBoxString::InitViewData(pView,pEntry,pViewData);
389 }
390 */
391 //------------------------------------------------------------------------
392 void OColumnString::Paint(const Point& rPos, SvLBox& rDev, sal_uInt16 /*nFlags*/, SvLBoxEntry* /*pEntry*/ )
393 {
394 	if(m_bReadOnly)
395 	{
396 		const StyleSettings& rStyleSettings = rDev.GetSettings().GetStyleSettings();
397 		rDev.SetTextColor( rStyleSettings.GetDisableColor() );
398 		rDev.SetTextFillColor( rStyleSettings.GetFieldColor() );
399 	}
400 	rDev.DrawText( rPos, GetText() );
401 }
402 //========================================================================
403 OColumnTreeBox::OColumnTreeBox( Window* pParent, const ResId& rResId )
404 	: OMarkableTreeListBox(pParent,NULL,rResId)
405 {
406 	SetDragDropMode( 0 );
407 	EnableInplaceEditing( sal_False );
408 	SetStyle(GetStyle() | WB_BORDER | WB_HASBUTTONS | WB_HSCROLL);
409 	SetSelectionMode( SINGLE_SELECTION );
410 }
411 //------------------------------------------------------------------------
412 void OColumnTreeBox::InitEntry(SvLBoxEntry* pEntry, const String& rStr, const Image& rImg1, const Image& rImg2, SvLBoxButtonKind eButtonKind)
413 {
414 	DBTreeListBox::InitEntry( pEntry, rStr, rImg1, rImg2, eButtonKind );
415 	SvLBoxString* pString = new OColumnString(pEntry, 0, rStr,sal_False);
416 	if (pString)
417 		pEntry->ReplaceItem( pString, pEntry->ItemCount() - 1 );
418 }
419 //------------------------------------------------------------------------
420 sal_Bool OColumnTreeBox::Select( SvLBoxEntry* pEntry, sal_Bool bSelect )
421 {
422 	if(bSelect)
423 	{
424 		OFieldDescription* pColumn = static_cast<OFieldDescription*>(pEntry->GetUserData());
425 		if(!(pColumn->IsAutoIncrement() && m_bReadOnly))
426 			bSelect = DBTreeListBox::Select( pEntry,bSelect );
427 	}
428 	else
429 		bSelect = DBTreeListBox::Select( pEntry,bSelect );
430 	return bSelect;
431 }
432 //------------------------------------------------------------------------
433 void OColumnTreeBox::FillListBox( const ODatabaseExport::TColumnVector& _rList)
434 {
435 	Clear();
436 	ODatabaseExport::TColumnVector::const_iterator aIter = _rList.begin();
437     ODatabaseExport::TColumnVector::const_iterator aEnd = _rList.end();
438 	for(;aIter != aEnd;++aIter)
439 	{
440 		SvLBoxEntry* pEntry = InsertEntry((*aIter)->first,0,sal_False,LIST_APPEND,(*aIter)->second);
441 		SvButtonState eState = !(m_bReadOnly && (*aIter)->second->IsAutoIncrement()) ? SV_BUTTON_CHECKED : SV_BUTTON_UNCHECKED;
442 		SetCheckButtonState( pEntry, eState );
443 	}
444 }
445 // -----------------------------------------------------------------------------
446 
447 
448 
449 
450 
451