update dev300-m58
[ooovba.git] / dbaccess / source / ui / misc / WTypeSelect.cxx
blob45cf651acc4ed60f251d0a9713fe18a500bb7d1d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WTypeSelect.cxx,v $
10 * $Revision: 1.30 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
33 #ifndef DBAUI_WIZ_TYPESELECT_HXX
34 #include "WTypeSelect.hxx"
35 #endif
36 #ifndef DBAUI_WIZARD_PAGES_HRC
37 #include "WizardPages.hrc"
38 #endif
39 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
40 #include "dbustrings.hrc"
41 #endif
42 #ifndef _TOOLS_DEBUG_HXX
43 #include <tools/debug.hxx>
44 #endif
45 #ifndef TOOLS_DIAGNOSE_EX_H
46 #include <tools/diagnose_ex.h>
47 #endif
48 #ifndef DBAUI_FIELDDESCRIPTIONS_HXX
49 #include "FieldDescriptions.hxx"
50 #endif
51 #ifndef DBAUI_WIZ_COPYTABLEDIALOG_HXX
52 #include "WCopyTable.hxx"
53 #endif
54 #ifndef _DBA_DBACCESS_HELPID_HRC_
55 #include "dbaccess_helpid.hrc"
56 #endif
57 #ifndef _DBU_MISC_HRC_
58 #include "dbu_misc.hrc"
59 #endif
60 #ifndef _STREAM_HXX
61 #include <tools/stream.hxx>
62 #endif
63 #ifndef _SVPARSER_HXX
64 #include <svtools/svparser.hxx>
65 #endif
66 #ifndef DBAUI_TOOLS_HXX
67 #include "UITools.hxx"
68 #endif
69 #ifndef _DBAUI_SQLMESSAGE_HXX_
70 #include "sqlmessage.hxx"
71 #endif
72 #ifndef DBAUI_FIELDCONTROLS_HXX
73 #include "FieldControls.hxx"
74 #endif
76 using namespace ::dbaui;
77 using namespace ::com::sun::star::uno;
78 using namespace ::com::sun::star::beans;
79 using namespace ::com::sun::star::container;
80 using namespace ::com::sun::star::util;
81 using namespace ::com::sun::star::sdbc;
82 // using namespace ::com::sun::star::sdbcx;
84 //========================================================================
85 // OWizTypeSelectControl
86 DBG_NAME(OWizTypeSelectControl)
87 //========================================================================
88 OWizTypeSelectControl::OWizTypeSelectControl(Window* pParent, const ResId& rResId,OTableDesignHelpBar* pHelpBar)
89 : OFieldDescControl(pParent,rResId,pHelpBar)
91 DBG_CTOR(OWizTypeSelectControl,NULL);
94 // -----------------------------------------------------------------------------
95 OWizTypeSelectControl::~OWizTypeSelectControl()
98 DBG_DTOR(OWizTypeSelectControl,NULL);
100 // -----------------------------------------------------------------------
101 void OWizTypeSelectControl::ActivateAggregate( EControlType eType )
103 switch(eType )
105 case tpFormat:
106 case tpDefault:
107 case tpAutoIncrement:
108 case tpAutoIncrementValue:
109 break;
110 default:
111 OFieldDescControl::ActivateAggregate( eType );
114 // -----------------------------------------------------------------------
115 void OWizTypeSelectControl::DeactivateAggregate( EControlType eType )
117 switch(eType )
119 case tpFormat:
120 case tpDefault:
121 case tpAutoIncrement:
122 case tpAutoIncrementValue:
123 break;
124 default:
125 OFieldDescControl::DeactivateAggregate( eType );
128 // -----------------------------------------------------------------------
129 void OWizTypeSelectControl::CellModified(long nRow, sal_uInt16 nColId )
131 DBG_ASSERT(nRow == -1,"nRow muss -1 sein!");
132 (void)nRow;
134 MultiListBox &aListBox = ((OWizTypeSelect*)GetParent())->m_lbColumnNames;
136 OFieldDescription* pCurFieldDescr = getCurrentFieldDescData();
138 sal_uInt16 nPos = aListBox.GetEntryPos( String( pCurFieldDescr->GetName() ) );
139 pCurFieldDescr = static_cast< OFieldDescription* >( aListBox.GetEntryData( nPos ) );
140 OSL_ENSURE( pCurFieldDescr, "OWizTypeSelectControl::CellModified: Columnname/type not found in the listbox!" );
141 if ( !pCurFieldDescr )
142 return;
143 setCurrentFieldDescData( pCurFieldDescr );
145 ::rtl::OUString sName = pCurFieldDescr->GetName();
146 ::rtl::OUString sNewName;
147 const OPropColumnEditCtrl* pColumnName = getColumnCtrl();
148 if ( pColumnName )
149 sNewName = pColumnName->GetText();
151 switch(nColId)
153 case FIELD_PRPOERTY_COLUMNNAME:
155 OCopyTableWizard* pWiz = static_cast<OCopyTableWizard*>(GetParent()->GetParent());
156 // first we have to check if this name already exists
157 sal_Bool bDoubleName = sal_False;
158 sal_Bool bCase = sal_True;
159 if ( getMetaData().is() && !getMetaData()->supportsMixedCaseQuotedIdentifiers() )
161 bCase = sal_False;
162 USHORT nCount = aListBox.GetEntryCount();
163 for (USHORT i=0 ; !bDoubleName && i < nCount ; ++i)
165 ::rtl::OUString sEntry(aListBox.GetEntry(i));
166 bDoubleName = sNewName.equalsIgnoreAsciiCase(sEntry);
168 if ( !bDoubleName && pWiz->shouldCreatePrimaryKey() )
169 bDoubleName = sNewName.equalsIgnoreAsciiCase(pWiz->getPrimaryKeyName());
172 else
173 bDoubleName = ((aListBox.GetEntryPos(String(sNewName)) != LISTBOX_ENTRY_NOTFOUND)
174 || ( pWiz->shouldCreatePrimaryKey()
175 && pWiz->getPrimaryKeyName() == sNewName) );
177 if ( bDoubleName )
179 String strMessage = String(ModuleRes(STR_TABLEDESIGN_DUPLICATE_NAME));
180 strMessage.SearchAndReplaceAscii("$column$", sNewName);
181 OSQLWarningBox( this, strMessage ).Execute();
182 pCurFieldDescr->SetName(sName);
183 DisplayData(pCurFieldDescr);
184 static_cast<OWizTypeSelect*>(GetParent())->setDuplicateName(sal_True);
185 return;
188 ::rtl::OUString sOldName = pCurFieldDescr->GetName();
189 pCurFieldDescr->SetName(sNewName);
190 static_cast<OWizTypeSelect*>(GetParent())->setDuplicateName(sal_False);
192 // now we change the name
193 OCopyTableWizard::TNameMapping::iterator aIter = pWiz->m_mNameMapping.begin();
194 OCopyTableWizard::TNameMapping::iterator aEnd = pWiz->m_mNameMapping.end();
196 ::comphelper::UStringMixEqual aCase(bCase);
197 for(;aIter != aEnd;++aIter)
199 if ( aCase(aIter->second,sName) )
201 aIter->second = sNewName;
202 break;
206 aListBox.RemoveEntry(nPos);
207 aListBox.InsertEntry(pCurFieldDescr->GetName(),nPos);
208 aListBox.SetEntryData(nPos,pCurFieldDescr);
210 pWiz->replaceColumn(nPos,pCurFieldDescr,sOldName);
212 break;
214 saveCurrentFieldDescData();
216 // -----------------------------------------------------------------------------
217 ::com::sun::star::lang::Locale OWizTypeSelectControl::GetLocale() const
219 return static_cast<OWizTypeSelect*>(GetParent())->m_pParent->GetLocale();
221 // -----------------------------------------------------------------------------
222 Reference< XNumberFormatter > OWizTypeSelectControl::GetFormatter() const
224 return static_cast<OWizTypeSelect*>(GetParent())->m_pParent->GetFormatter();
226 // -----------------------------------------------------------------------------
227 TOTypeInfoSP OWizTypeSelectControl::getTypeInfo(sal_Int32 _nPos)
229 return static_cast<OWizTypeSelect*>(GetParent())->m_pParent->getDestTypeInfo(_nPos);
231 // -----------------------------------------------------------------------------
232 const OTypeInfoMap* OWizTypeSelectControl::getTypeInfo() const
234 return static_cast<OWizTypeSelect*>(GetParent())->m_pParent->getDestTypeInfo();
236 // -----------------------------------------------------------------------------
237 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData> OWizTypeSelectControl::getMetaData()
239 return ((OWizTypeSelect*)GetParent())->m_pParent->m_xDestConnection->getMetaData();
241 // -----------------------------------------------------------------------------
242 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> OWizTypeSelectControl::getConnection()
244 return ((OWizTypeSelect*)GetParent())->m_pParent->m_xDestConnection;
246 // -----------------------------------------------------------------------------
247 sal_Bool OWizTypeSelectControl::isAutoIncrementValueEnabled() const
249 return ((OWizTypeSelect*)GetParent())->m_bAutoIncrementEnabled;
251 // -----------------------------------------------------------------------------
252 ::rtl::OUString OWizTypeSelectControl::getAutoIncrementValue() const
254 return ((OWizTypeSelect*)GetParent())->m_sAutoIncrementValue;
256 // -----------------------------------------------------------------------------
258 //========================================================================
259 DBG_NAME(OWizTypeSelect);
260 #define IMG_PRIMARY_KEY 1
261 //========================================================================
262 OWizTypeSelect::OWizTypeSelect( Window* pParent, SvStream* _pStream )
263 :OWizardPage( pParent, ModuleRes( TAB_WIZ_TYPE_SELECT ))
264 ,m_lbColumnNames( this, ModuleRes( LB_NEW_COLUMN_NAMES ) )
265 ,m_flColumns( this, ModuleRes( FL_COLUMN_NAME ) )
266 ,m_aTypeControl( this, ModuleRes( CONTROL_CONTAINER ))
267 ,m_flAutoType( this, ModuleRes( FL_AUTO_TYPE ) )
268 ,m_ftAuto( this, ModuleRes( FT_AUTO ) )
269 ,m_etAuto( this, ModuleRes( ET_AUTO ) )
270 ,m_pbAuto( this, ModuleRes( PB_AUTO ) )
271 ,m_pParserStream( _pStream )
272 ,m_nDisplayRow(0)
273 ,m_bAutoIncrementEnabled(sal_False)
274 ,m_bDuplicateName(sal_False)
276 DBG_CTOR(OWizTypeSelect,NULL);
277 m_lbColumnNames.SetSelectHdl(LINK(this,OWizTypeSelect,ColumnSelectHdl));
279 ModuleRes aModuleRes(isHiContrast(&m_lbColumnNames) ? IMG_JOINS_H : IMG_JOINS);
280 ImageList aImageList(aModuleRes);
281 m_imgPKey = aImageList.GetImage(IMG_PRIMARY_KEY);
283 m_aTypeControl.Show();
284 m_aTypeControl.Init();
285 m_etAuto.SetText(String::CreateFromAscii("10"));
286 m_etAuto.SetDecimalDigits(0);
287 m_pbAuto.SetClickHdl(LINK(this,OWizTypeSelect,ButtonClickHdl));
288 m_lbColumnNames.EnableMultiSelection(sal_True);
292 m_lbColumnNames.SetPKey( m_pParent->supportsPrimaryKey() );
293 ::dbaui::fillAutoIncrementValue( m_pParent->m_xDestConnection, m_bAutoIncrementEnabled, m_sAutoIncrementValue );
295 catch(const Exception&)
297 DBG_UNHANDLED_EXCEPTION();
300 FreeResource();
302 // -----------------------------------------------------------------------
303 OWizTypeSelect::~OWizTypeSelect()
305 DBG_DTOR(OWizTypeSelect,NULL);
307 // -----------------------------------------------------------------------------
308 String OWizTypeSelect::GetTitle() const
310 DBG_CHKTHIS(OWizTypeSelect,NULL);
311 return String(ModuleRes(STR_WIZ_TYPE_SELECT_TITEL));
313 // -----------------------------------------------------------------------
314 void OWizTypeSelect::Resize()
316 DBG_CHKTHIS(OWizTypeSelect,NULL);
318 // -----------------------------------------------------------------------
319 IMPL_LINK( OWizTypeSelect, ColumnSelectHdl, MultiListBox *, /*pListBox*/ )
321 String aColumnName( m_lbColumnNames.GetSelectEntry() );
323 OFieldDescription* pField = static_cast<OFieldDescription*>(m_lbColumnNames.GetEntryData(m_lbColumnNames.GetEntryPos(aColumnName)));
324 if(pField)
325 m_aTypeControl.DisplayData(pField);
326 return 0;
328 // -----------------------------------------------------------------------
329 void OWizTypeSelect::Reset()
331 // urspr"unglichen zustand wiederherstellen
332 DBG_CHKTHIS(OWizTypeSelect,NULL);
334 while(m_lbColumnNames.GetEntryCount())
335 m_lbColumnNames.RemoveEntry(0);
336 m_lbColumnNames.Clear();
337 sal_Int32 nBreakPos;
338 m_pParent->CheckColumns(nBreakPos);
340 const ODatabaseExport::TColumnVector* pDestColumns = m_pParent->getDestVector();
341 ODatabaseExport::TColumnVector::const_iterator aIter = pDestColumns->begin();
342 ODatabaseExport::TColumnVector::const_iterator aEnd = pDestColumns->end();
343 for(;aIter != aEnd;++aIter)
345 sal_uInt16 nPos;
346 if((*aIter)->second->IsPrimaryKey())
347 nPos = m_lbColumnNames.InsertEntry((*aIter)->first, m_imgPKey );
348 else
349 nPos = m_lbColumnNames.InsertEntry((*aIter)->first);
350 m_lbColumnNames.SetEntryData(nPos,(*aIter)->second);
352 m_bFirstTime = sal_False;
354 // -----------------------------------------------------------------------
355 void OWizTypeSelect::ActivatePage( )
357 DBG_CHKTHIS(OWizTypeSelect,NULL);
358 sal_Bool bOldFirstTime = m_bFirstTime;
359 Reset();
360 m_bFirstTime = bOldFirstTime;
362 m_lbColumnNames.SelectEntryPos(static_cast<USHORT>(m_nDisplayRow));
363 m_nDisplayRow = 0;
364 m_lbColumnNames.GetSelectHdl().Call(&m_lbColumnNames);
366 // -----------------------------------------------------------------------
367 sal_Bool OWizTypeSelect::LeavePage()
369 DBG_CHKTHIS(OWizTypeSelect,NULL);
370 String aColumnName( m_lbColumnNames.GetSelectEntry() );
372 sal_Bool bDuplicateName = sal_False;
373 OFieldDescription* pField = static_cast<OFieldDescription*>(m_lbColumnNames.GetEntryData(m_lbColumnNames.GetEntryPos(aColumnName)));
374 if ( pField )
376 m_aTypeControl.SaveData(pField);
377 bDuplicateName = m_bDuplicateName;
379 return !bDuplicateName;
381 //------------------------------------------------------------------------------
382 void OWizTypeSelect::EnableAuto(sal_Bool bEnable)
384 DBG_CHKTHIS(OWizTypeSelect,NULL);
385 m_ftAuto.Show(bEnable);
386 m_etAuto.Show(bEnable);
387 m_pbAuto.Show(bEnable);
388 m_flAutoType.Show(bEnable);
390 //------------------------------------------------------------------------------
391 IMPL_LINK( OWizTypeSelect, ButtonClickHdl, Button *, /*pButton*/ )
393 DBG_CHKTHIS(OWizTypeSelect,NULL);
394 sal_Int32 nBreakPos;
395 m_pParent->CheckColumns(nBreakPos);
396 fillColumnList(m_etAuto.GetText().ToInt32());
398 ActivatePage();
400 return 0;
402 //------------------------------------------------------------------------
403 sal_Bool OWizTypeSelectList::IsPrimaryKeyAllowed() const
405 DBG_CHKTHIS(OWizTypeSelect,NULL);
406 sal_uInt16 nCount = GetSelectEntryCount();
407 sal_uInt16 j;
409 for( j = 0; m_bPKey && j < nCount; ++j )
411 OFieldDescription* pField = static_cast<OFieldDescription*>(GetEntryData(GetSelectEntryPos(j)));
412 if(!pField || pField->getTypeInfo()->nSearchType == ColumnSearch::NONE)
413 break;
415 return j == nCount;
417 // -----------------------------------------------------------------------------
418 void OWizTypeSelectList::setPrimaryKey(OFieldDescription* _pFieldDescr,sal_uInt16 _nPos,sal_Bool _bSet)
420 String sColumnName = GetEntry(_nPos);
421 RemoveEntry(_nPos);
422 _pFieldDescr->SetPrimaryKey(_bSet);
423 if( _bSet )
424 InsertEntry(sColumnName,((OWizTypeSelect*)GetParent())->m_imgPKey,_nPos);
425 else if( _pFieldDescr->getTypeInfo()->bNullable )
427 _pFieldDescr->SetControlDefault(Any());
428 InsertEntry(sColumnName,_nPos);
430 SetEntryData(_nPos,_pFieldDescr);
432 //------------------------------------------------------------------------
433 long OWizTypeSelectList::PreNotify( NotifyEvent& rEvt )
435 long nDone = 0;
436 switch( rEvt.GetType() )
438 case EVENT_MOUSEBUTTONDOWN:
440 const MouseEvent* pMEvt = rEvt.GetMouseEvent();
441 if(pMEvt->IsRight() && !pMEvt->GetModifier())
442 nDone = 1;
444 break;
445 case EVENT_COMMAND:
447 if(!IsPrimaryKeyAllowed())
448 break;
450 const CommandEvent* pComEvt = rEvt.GetCommandEvent();
451 if(pComEvt->GetCommand() != COMMAND_CONTEXTMENU)
452 break;
453 // die Stelle, an der geklickt wurde
454 Point ptWhere(0,0);
455 if (pComEvt->IsMouseEvent())
456 ptWhere = pComEvt->GetMousePosPixel();
458 PopupMenu aContextMenu(ModuleRes(RID_SBA_RTF_PKEYPOPUP));
459 switch( aContextMenu.Execute( this, ptWhere ) )
461 case SID_TABLEDESIGN_TABED_PRIMARYKEY:
463 String sColumnName;
464 sal_uInt16 nCount = GetEntryCount();
465 for(sal_uInt16 j = 0 ; j < nCount ; ++j)
467 OFieldDescription* pFieldDescr = static_cast<OFieldDescription*>(GetEntryData(j));
468 if( pFieldDescr )
470 if(pFieldDescr->IsPrimaryKey() && !IsEntryPosSelected(j))
471 setPrimaryKey(pFieldDescr,j);
472 else if(IsEntryPosSelected(j))
474 setPrimaryKey(pFieldDescr,j,!pFieldDescr->IsPrimaryKey());
475 SelectEntryPos(j);
479 GetSelectHdl().Call(this);
481 break;
483 nDone = 1;
485 break;
487 return nDone ? nDone : MultiListBox::PreNotify(rEvt);
489 // -----------------------------------------------------------------------------
490 void OWizTypeSelect::fillColumnList(sal_uInt32 nRows)
492 DBG_CHKTHIS(OWizTypeSelect,NULL);
493 if(m_pParserStream)
495 sal_uInt32 nTell = m_pParserStream->Tell(); // verändert vielleicht die Position des Streams
497 SvParser *pReader = createReader(nRows);
498 if(pReader)
500 pReader->AddRef();
501 pReader->CallParser();
502 pReader->ReleaseRef();
504 m_pParserStream->Seek(nTell);
507 // -----------------------------------------------------------------------------