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