Version 3.6.0.2, tag libreoffice-3.6.0.2
[LibreOffice.git] / dbaccess / source / ui / misc / WTypeSelect.cxx
blob095dc8939742af7b84f79f9708beef29067fbc76
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "WTypeSelect.hxx"
30 #include "WizardPages.hrc"
31 #include "dbustrings.hrc"
32 #include <tools/diagnose_ex.h>
33 #include <osl/diagnose.h>
34 #include "FieldDescriptions.hxx"
35 #include "WCopyTable.hxx"
36 #include "dbaccess_helpid.hrc"
37 #include "dbu_misc.hrc"
38 #include <tools/stream.hxx>
39 #include <svtools/svparser.hxx>
40 #include "UITools.hxx"
41 #include "sqlmessage.hxx"
42 #include "FieldControls.hxx"
44 #include "dbaccess_slotid.hrc"
46 using namespace ::dbaui;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::beans;
49 using namespace ::com::sun::star::container;
50 using namespace ::com::sun::star::util;
51 using namespace ::com::sun::star::sdbc;
53 //========================================================================
54 // OWizTypeSelectControl
55 DBG_NAME(OWizTypeSelectControl)
56 //========================================================================
57 OWizTypeSelectControl::OWizTypeSelectControl(Window* pParent, const ResId& rResId,OTableDesignHelpBar* pHelpBar)
58 : OFieldDescControl(pParent,rResId,pHelpBar)
60 DBG_CTOR(OWizTypeSelectControl,NULL);
63 // -----------------------------------------------------------------------------
64 OWizTypeSelectControl::~OWizTypeSelectControl()
67 DBG_DTOR(OWizTypeSelectControl,NULL);
69 // -----------------------------------------------------------------------
70 void OWizTypeSelectControl::ActivateAggregate( EControlType eType )
72 switch(eType )
74 case tpFormat:
75 case tpDefault:
76 case tpAutoIncrement:
77 case tpAutoIncrementValue:
78 break;
79 default:
80 OFieldDescControl::ActivateAggregate( eType );
83 // -----------------------------------------------------------------------
84 void OWizTypeSelectControl::DeactivateAggregate( EControlType eType )
86 switch(eType )
88 case tpFormat:
89 case tpDefault:
90 case tpAutoIncrement:
91 case tpAutoIncrementValue:
92 break;
93 default:
94 OFieldDescControl::DeactivateAggregate( eType );
97 // -----------------------------------------------------------------------
98 void OWizTypeSelectControl::CellModified(long nRow, sal_uInt16 nColId )
100 OSL_ENSURE(nRow == -1,"nRow muss -1 sein!");
101 (void)nRow;
103 MultiListBox &aListBox = ((OWizTypeSelect*)GetParent())->m_lbColumnNames;
105 OFieldDescription* pCurFieldDescr = getCurrentFieldDescData();
107 sal_uInt16 nPos = aListBox.GetEntryPos( String( pCurFieldDescr->GetName() ) );
108 pCurFieldDescr = static_cast< OFieldDescription* >( aListBox.GetEntryData( nPos ) );
109 OSL_ENSURE( pCurFieldDescr, "OWizTypeSelectControl::CellModified: Columnname/type not found in the listbox!" );
110 if ( !pCurFieldDescr )
111 return;
112 setCurrentFieldDescData( pCurFieldDescr );
114 ::rtl::OUString sName = pCurFieldDescr->GetName();
115 ::rtl::OUString sNewName;
116 const OPropColumnEditCtrl* pColumnName = getColumnCtrl();
117 if ( pColumnName )
118 sNewName = pColumnName->GetText();
120 switch(nColId)
122 case FIELD_PRPOERTY_COLUMNNAME:
124 OCopyTableWizard* pWiz = static_cast<OCopyTableWizard*>(GetParent()->GetParent());
125 // first we have to check if this name already exists
126 sal_Bool bDoubleName = sal_False;
127 sal_Bool bCase = sal_True;
128 if ( getMetaData().is() && !getMetaData()->supportsMixedCaseQuotedIdentifiers() )
130 bCase = sal_False;
131 sal_uInt16 nCount = aListBox.GetEntryCount();
132 for (sal_uInt16 i=0 ; !bDoubleName && i < nCount ; ++i)
134 ::rtl::OUString sEntry(aListBox.GetEntry(i));
135 bDoubleName = sNewName.equalsIgnoreAsciiCase(sEntry);
137 if ( !bDoubleName && pWiz->shouldCreatePrimaryKey() )
138 bDoubleName = sNewName.equalsIgnoreAsciiCase(pWiz->getPrimaryKeyName());
141 else
142 bDoubleName = ((aListBox.GetEntryPos(String(sNewName)) != LISTBOX_ENTRY_NOTFOUND)
143 || ( pWiz->shouldCreatePrimaryKey()
144 && pWiz->getPrimaryKeyName() == sNewName) );
146 if ( bDoubleName )
148 String strMessage = String(ModuleRes(STR_TABLEDESIGN_DUPLICATE_NAME));
149 strMessage.SearchAndReplaceAscii("$column$", sNewName);
150 pWiz->showError(strMessage);
151 pCurFieldDescr->SetName(sName);
152 DisplayData(pCurFieldDescr);
153 static_cast<OWizTypeSelect*>(GetParent())->setDuplicateName(sal_True);
154 return;
157 ::rtl::OUString sOldName = pCurFieldDescr->GetName();
158 pCurFieldDescr->SetName(sNewName);
159 static_cast<OWizTypeSelect*>(GetParent())->setDuplicateName(sal_False);
161 // now we change the name
162 OCopyTableWizard::TNameMapping::iterator aIter = pWiz->m_mNameMapping.begin();
163 OCopyTableWizard::TNameMapping::iterator aEnd = pWiz->m_mNameMapping.end();
165 ::comphelper::UStringMixEqual aCase(bCase);
166 for(;aIter != aEnd;++aIter)
168 if ( aCase(aIter->second,sName) )
170 aIter->second = sNewName;
171 break;
175 aListBox.RemoveEntry(nPos);
176 aListBox.InsertEntry(pCurFieldDescr->GetName(),nPos);
177 aListBox.SetEntryData(nPos,pCurFieldDescr);
179 pWiz->replaceColumn(nPos,pCurFieldDescr,sOldName);
181 break;
183 saveCurrentFieldDescData();
185 // -----------------------------------------------------------------------------
186 ::com::sun::star::lang::Locale OWizTypeSelectControl::GetLocale() const
188 return static_cast<OWizTypeSelect*>(GetParent())->m_pParent->GetLocale();
190 // -----------------------------------------------------------------------------
191 Reference< XNumberFormatter > OWizTypeSelectControl::GetFormatter() const
193 return static_cast<OWizTypeSelect*>(GetParent())->m_pParent->GetFormatter();
195 // -----------------------------------------------------------------------------
196 TOTypeInfoSP OWizTypeSelectControl::getTypeInfo(sal_Int32 _nPos)
198 return static_cast<OWizTypeSelect*>(GetParent())->m_pParent->getDestTypeInfo(_nPos);
200 // -----------------------------------------------------------------------------
201 const OTypeInfoMap* OWizTypeSelectControl::getTypeInfo() const
203 return static_cast<OWizTypeSelect*>(GetParent())->m_pParent->getDestTypeInfo();
205 // -----------------------------------------------------------------------------
206 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData> OWizTypeSelectControl::getMetaData()
208 return ((OWizTypeSelect*)GetParent())->m_pParent->m_xDestConnection->getMetaData();
210 // -----------------------------------------------------------------------------
211 ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> OWizTypeSelectControl::getConnection()
213 return ((OWizTypeSelect*)GetParent())->m_pParent->m_xDestConnection;
215 // -----------------------------------------------------------------------------
216 sal_Bool OWizTypeSelectControl::isAutoIncrementValueEnabled() const
218 return ((OWizTypeSelect*)GetParent())->m_bAutoIncrementEnabled;
220 // -----------------------------------------------------------------------------
221 ::rtl::OUString OWizTypeSelectControl::getAutoIncrementValue() const
223 return ((OWizTypeSelect*)GetParent())->m_sAutoIncrementValue;
225 // -----------------------------------------------------------------------------
227 //========================================================================
228 DBG_NAME(OWizTypeSelect);
229 #define IMG_PRIMARY_KEY 1
230 //========================================================================
231 OWizTypeSelect::OWizTypeSelect( Window* pParent, SvStream* _pStream )
232 :OWizardPage( pParent, ModuleRes( TAB_WIZ_TYPE_SELECT ))
233 ,m_lbColumnNames( this, ModuleRes( LB_NEW_COLUMN_NAMES ) )
234 ,m_flColumns( this, ModuleRes( FL_COLUMN_NAME ) )
235 ,m_aTypeControl( this, ModuleRes( CONTROL_CONTAINER ))
236 ,m_flAutoType( this, ModuleRes( FL_AUTO_TYPE ) )
237 ,m_ftAuto( this, ModuleRes( FT_AUTO ) )
238 ,m_etAuto( this, ModuleRes( ET_AUTO ) )
239 ,m_pbAuto( this, ModuleRes( PB_AUTO ) )
240 ,m_pParserStream( _pStream )
241 ,m_nDisplayRow(0)
242 ,m_bAutoIncrementEnabled(sal_False)
243 ,m_bDuplicateName(sal_False)
245 DBG_CTOR(OWizTypeSelect,NULL);
246 m_lbColumnNames.SetSelectHdl(LINK(this,OWizTypeSelect,ColumnSelectHdl));
248 ModuleRes aModuleRes(IMG_JOINS);
249 ImageList aImageList(aModuleRes);
250 m_imgPKey = aImageList.GetImage(IMG_PRIMARY_KEY);
252 m_aTypeControl.Show();
253 m_aTypeControl.Init();
254 m_etAuto.SetText(String::CreateFromAscii("10"));
255 m_etAuto.SetDecimalDigits(0);
256 m_pbAuto.SetClickHdl(LINK(this,OWizTypeSelect,ButtonClickHdl));
257 m_lbColumnNames.EnableMultiSelection(sal_True);
261 m_lbColumnNames.SetPKey( m_pParent->supportsPrimaryKey() );
262 ::dbaui::fillAutoIncrementValue( m_pParent->m_xDestConnection, m_bAutoIncrementEnabled, m_sAutoIncrementValue );
264 catch(const Exception&)
266 DBG_UNHANDLED_EXCEPTION();
269 FreeResource();
271 // -----------------------------------------------------------------------
272 OWizTypeSelect::~OWizTypeSelect()
274 DBG_DTOR(OWizTypeSelect,NULL);
276 // -----------------------------------------------------------------------------
277 String OWizTypeSelect::GetTitle() const
279 DBG_CHKTHIS(OWizTypeSelect,NULL);
280 return String(ModuleRes(STR_WIZ_TYPE_SELECT_TITEL));
282 // -----------------------------------------------------------------------
283 void OWizTypeSelect::Resize()
285 DBG_CHKTHIS(OWizTypeSelect,NULL);
287 // -----------------------------------------------------------------------
288 IMPL_LINK( OWizTypeSelect, ColumnSelectHdl, MultiListBox *, /*pListBox*/ )
290 String aColumnName( m_lbColumnNames.GetSelectEntry() );
292 OFieldDescription* pField = static_cast<OFieldDescription*>(m_lbColumnNames.GetEntryData(m_lbColumnNames.GetEntryPos(aColumnName)));
293 if(pField)
294 m_aTypeControl.DisplayData(pField);
296 m_aTypeControl.Enable(m_lbColumnNames.GetSelectEntryCount() == 1 );
297 return 0;
299 // -----------------------------------------------------------------------
300 void OWizTypeSelect::Reset()
302 // urspr"unglichen zustand wiederherstellen
303 DBG_CHKTHIS(OWizTypeSelect,NULL);
305 while(m_lbColumnNames.GetEntryCount())
306 m_lbColumnNames.RemoveEntry(0);
307 m_lbColumnNames.Clear();
308 sal_Int32 nBreakPos;
309 m_pParent->CheckColumns(nBreakPos);
311 const ODatabaseExport::TColumnVector* pDestColumns = m_pParent->getDestVector();
312 ODatabaseExport::TColumnVector::const_iterator aIter = pDestColumns->begin();
313 ODatabaseExport::TColumnVector::const_iterator aEnd = pDestColumns->end();
314 for(;aIter != aEnd;++aIter)
316 sal_uInt16 nPos;
317 if((*aIter)->second->IsPrimaryKey())
318 nPos = m_lbColumnNames.InsertEntry((*aIter)->first, m_imgPKey );
319 else
320 nPos = m_lbColumnNames.InsertEntry((*aIter)->first);
321 m_lbColumnNames.SetEntryData(nPos,(*aIter)->second);
323 m_bFirstTime = sal_False;
325 // -----------------------------------------------------------------------
326 void OWizTypeSelect::ActivatePage( )
328 DBG_CHKTHIS(OWizTypeSelect,NULL);
329 sal_Bool bOldFirstTime = m_bFirstTime;
330 Reset();
331 m_bFirstTime = bOldFirstTime;
333 m_lbColumnNames.SelectEntryPos(static_cast<sal_uInt16>(m_nDisplayRow));
334 m_nDisplayRow = 0;
335 m_lbColumnNames.GetSelectHdl().Call(&m_lbColumnNames);
337 // -----------------------------------------------------------------------
338 sal_Bool OWizTypeSelect::LeavePage()
340 DBG_CHKTHIS(OWizTypeSelect,NULL);
341 String aColumnName( m_lbColumnNames.GetSelectEntry() );
343 sal_Bool bDuplicateName = sal_False;
344 OFieldDescription* pField = static_cast<OFieldDescription*>(m_lbColumnNames.GetEntryData(m_lbColumnNames.GetEntryPos(aColumnName)));
345 if ( pField )
347 m_aTypeControl.SaveData(pField);
348 bDuplicateName = m_bDuplicateName;
350 return !bDuplicateName;
352 //------------------------------------------------------------------------------
353 void OWizTypeSelect::EnableAuto(sal_Bool bEnable)
355 DBG_CHKTHIS(OWizTypeSelect,NULL);
356 m_ftAuto.Show(bEnable);
357 m_etAuto.Show(bEnable);
358 m_pbAuto.Show(bEnable);
359 m_flAutoType.Show(bEnable);
361 //------------------------------------------------------------------------------
362 IMPL_LINK( OWizTypeSelect, ButtonClickHdl, Button *, /*pButton*/ )
364 DBG_CHKTHIS(OWizTypeSelect,NULL);
365 sal_Int32 nBreakPos;
366 m_pParent->CheckColumns(nBreakPos);
367 fillColumnList(m_etAuto.GetText().ToInt32());
369 ActivatePage();
371 return 0;
373 //------------------------------------------------------------------------
374 sal_Bool OWizTypeSelectList::IsPrimaryKeyAllowed() const
376 DBG_CHKTHIS(OWizTypeSelect,NULL);
377 sal_uInt16 nCount = GetSelectEntryCount();
378 sal_uInt16 j;
380 for( j = 0; m_bPKey && j < nCount; ++j )
382 OFieldDescription* pField = static_cast<OFieldDescription*>(GetEntryData(GetSelectEntryPos(j)));
383 if(!pField || pField->getTypeInfo()->nSearchType == ColumnSearch::NONE)
384 break;
386 return j == nCount;
388 // -----------------------------------------------------------------------------
389 void OWizTypeSelectList::setPrimaryKey(OFieldDescription* _pFieldDescr,sal_uInt16 _nPos,sal_Bool _bSet)
391 String sColumnName = GetEntry(_nPos);
392 RemoveEntry(_nPos);
393 _pFieldDescr->SetPrimaryKey(_bSet);
394 if( _bSet )
395 InsertEntry(sColumnName,((OWizTypeSelect*)GetParent())->m_imgPKey,_nPos);
396 else if( _pFieldDescr->getTypeInfo()->bNullable )
398 _pFieldDescr->SetControlDefault(Any());
399 InsertEntry(sColumnName,_nPos);
401 SetEntryData(_nPos,_pFieldDescr);
403 //------------------------------------------------------------------------
404 long OWizTypeSelectList::PreNotify( NotifyEvent& rEvt )
406 long nDone = 0;
407 switch( rEvt.GetType() )
409 case EVENT_MOUSEBUTTONDOWN:
411 const MouseEvent* pMEvt = rEvt.GetMouseEvent();
412 if(pMEvt->IsRight() && !pMEvt->GetModifier())
413 nDone = 1;
415 break;
416 case EVENT_COMMAND:
418 if(!IsPrimaryKeyAllowed())
419 break;
421 const CommandEvent* pComEvt = rEvt.GetCommandEvent();
422 if(pComEvt->GetCommand() != COMMAND_CONTEXTMENU)
423 break;
424 // die Stelle, an der geklickt wurde
425 Point ptWhere(0,0);
426 if (pComEvt->IsMouseEvent())
427 ptWhere = pComEvt->GetMousePosPixel();
429 PopupMenu aContextMenu(ModuleRes(RID_SBA_RTF_PKEYPOPUP));
430 switch( aContextMenu.Execute( this, ptWhere ) )
432 case SID_TABLEDESIGN_TABED_PRIMARYKEY:
434 String sColumnName;
435 sal_uInt16 nCount = GetEntryCount();
436 for(sal_uInt16 j = 0 ; j < nCount ; ++j)
438 OFieldDescription* pFieldDescr = static_cast<OFieldDescription*>(GetEntryData(j));
439 if( pFieldDescr )
441 if(pFieldDescr->IsPrimaryKey() && !IsEntryPosSelected(j))
442 setPrimaryKey(pFieldDescr,j);
443 else if(IsEntryPosSelected(j))
445 setPrimaryKey(pFieldDescr,j,!pFieldDescr->IsPrimaryKey());
446 SelectEntryPos(j);
450 GetSelectHdl().Call(this);
452 break;
454 nDone = 1;
456 break;
458 return nDone ? nDone : MultiListBox::PreNotify(rEvt);
460 // -----------------------------------------------------------------------------
461 void OWizTypeSelect::fillColumnList(sal_uInt32 nRows)
463 DBG_CHKTHIS(OWizTypeSelect,NULL);
464 if(m_pParserStream)
466 sal_uInt32 nTell = m_pParserStream->Tell(); // might change seek position of stream
468 SvParser *pReader = createReader(nRows);
469 if(pReader)
471 pReader->AddRef();
472 pReader->CallParser();
473 pReader->ReleaseRef();
475 m_pParserStream->Seek(nTell);
478 // -----------------------------------------------------------------------------
481 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */