1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WTypeSelect.cxx,v $
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"
36 #ifndef DBAUI_WIZARD_PAGES_HRC
37 #include "WizardPages.hrc"
39 #ifndef DBACCESS_SHARED_DBUSTRINGS_HRC
40 #include "dbustrings.hrc"
42 #ifndef _TOOLS_DEBUG_HXX
43 #include <tools/debug.hxx>
45 #ifndef TOOLS_DIAGNOSE_EX_H
46 #include <tools/diagnose_ex.h>
48 #ifndef DBAUI_FIELDDESCRIPTIONS_HXX
49 #include "FieldDescriptions.hxx"
51 #ifndef DBAUI_WIZ_COPYTABLEDIALOG_HXX
52 #include "WCopyTable.hxx"
54 #ifndef _DBA_DBACCESS_HELPID_HRC_
55 #include "dbaccess_helpid.hrc"
57 #ifndef _DBU_MISC_HRC_
58 #include "dbu_misc.hrc"
61 #include <tools/stream.hxx>
64 #include <svtools/svparser.hxx>
66 #ifndef DBAUI_TOOLS_HXX
67 #include "UITools.hxx"
69 #ifndef _DBAUI_SQLMESSAGE_HXX_
70 #include "sqlmessage.hxx"
72 #ifndef DBAUI_FIELDCONTROLS_HXX
73 #include "FieldControls.hxx"
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
)
107 case tpAutoIncrement
:
108 case tpAutoIncrementValue
:
111 OFieldDescControl::ActivateAggregate( eType
);
114 // -----------------------------------------------------------------------
115 void OWizTypeSelectControl::DeactivateAggregate( EControlType eType
)
121 case tpAutoIncrement
:
122 case tpAutoIncrementValue
:
125 OFieldDescControl::DeactivateAggregate( eType
);
128 // -----------------------------------------------------------------------
129 void OWizTypeSelectControl::CellModified(long nRow
, sal_uInt16 nColId
)
131 DBG_ASSERT(nRow
== -1,"nRow muss -1 sein!");
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
)
143 setCurrentFieldDescData( pCurFieldDescr
);
145 ::rtl::OUString sName
= pCurFieldDescr
->GetName();
146 ::rtl::OUString sNewName
;
147 const OPropColumnEditCtrl
* pColumnName
= getColumnCtrl();
149 sNewName
= pColumnName
->GetText();
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() )
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());
173 bDoubleName
= ((aListBox
.GetEntryPos(String(sNewName
)) != LISTBOX_ENTRY_NOTFOUND
)
174 || ( pWiz
->shouldCreatePrimaryKey()
175 && pWiz
->getPrimaryKeyName() == sNewName
) );
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
);
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
;
206 aListBox
.RemoveEntry(nPos
);
207 aListBox
.InsertEntry(pCurFieldDescr
->GetName(),nPos
);
208 aListBox
.SetEntryData(nPos
,pCurFieldDescr
);
210 pWiz
->replaceColumn(nPos
,pCurFieldDescr
,sOldName
);
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
)
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();
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
)));
325 m_aTypeControl
.DisplayData(pField
);
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();
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
)
346 if((*aIter
)->second
->IsPrimaryKey())
347 nPos
= m_lbColumnNames
.InsertEntry((*aIter
)->first
, m_imgPKey
);
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
;
360 m_bFirstTime
= bOldFirstTime
;
362 m_lbColumnNames
.SelectEntryPos(static_cast<USHORT
>(m_nDisplayRow
));
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
)));
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
);
395 m_pParent
->CheckColumns(nBreakPos
);
396 fillColumnList(m_etAuto
.GetText().ToInt32());
402 //------------------------------------------------------------------------
403 sal_Bool
OWizTypeSelectList::IsPrimaryKeyAllowed() const
405 DBG_CHKTHIS(OWizTypeSelect
,NULL
);
406 sal_uInt16 nCount
= GetSelectEntryCount();
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
)
417 // -----------------------------------------------------------------------------
418 void OWizTypeSelectList::setPrimaryKey(OFieldDescription
* _pFieldDescr
,sal_uInt16 _nPos
,sal_Bool _bSet
)
420 String sColumnName
= GetEntry(_nPos
);
422 _pFieldDescr
->SetPrimaryKey(_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
)
436 switch( rEvt
.GetType() )
438 case EVENT_MOUSEBUTTONDOWN
:
440 const MouseEvent
* pMEvt
= rEvt
.GetMouseEvent();
441 if(pMEvt
->IsRight() && !pMEvt
->GetModifier())
447 if(!IsPrimaryKeyAllowed())
450 const CommandEvent
* pComEvt
= rEvt
.GetCommandEvent();
451 if(pComEvt
->GetCommand() != COMMAND_CONTEXTMENU
)
453 // die Stelle, an der geklickt wurde
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
:
464 sal_uInt16 nCount
= GetEntryCount();
465 for(sal_uInt16 j
= 0 ; j
< nCount
; ++j
)
467 OFieldDescription
* pFieldDescr
= static_cast<OFieldDescription
*>(GetEntryData(j
));
470 if(pFieldDescr
->IsPrimaryKey() && !IsEntryPosSelected(j
))
471 setPrimaryKey(pFieldDescr
,j
);
472 else if(IsEntryPosSelected(j
))
474 setPrimaryKey(pFieldDescr
,j
,!pFieldDescr
->IsPrimaryKey());
479 GetSelectHdl().Call(this);
487 return nDone
? nDone
: MultiListBox::PreNotify(rEvt
);
489 // -----------------------------------------------------------------------------
490 void OWizTypeSelect::fillColumnList(sal_uInt32 nRows
)
492 DBG_CHKTHIS(OWizTypeSelect
,NULL
);
495 sal_uInt32 nTell
= m_pParserStream
->Tell(); // verändert vielleicht die Position des Streams
497 SvParser
*pReader
= createReader(nRows
);
501 pReader
->CallParser();
502 pReader
->ReleaseRef();
504 m_pParserStream
->Seek(nTell
);
507 // -----------------------------------------------------------------------------