Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "WTypeSelect.hxx"
21 : #include "dbustrings.hrc"
22 : #include <tools/diagnose_ex.h>
23 : #include <osl/diagnose.h>
24 : #include "FieldDescriptions.hxx"
25 : #include "WCopyTable.hxx"
26 : #include "dbaccess_helpid.hrc"
27 : #include "dbu_misc.hrc"
28 : #include <tools/stream.hxx>
29 : #include <svtools/svparser.hxx>
30 : #include "UITools.hxx"
31 : #include "sqlmessage.hxx"
32 : #include "FieldControls.hxx"
33 : #include <vcl/layout.hxx>
34 : #include <vcl/builderfactory.hxx>
35 :
36 : #include "dbaccess_slotid.hrc"
37 :
38 : using namespace ::dbaui;
39 : using namespace ::com::sun::star::uno;
40 : using namespace ::com::sun::star::beans;
41 : using namespace ::com::sun::star::container;
42 : using namespace ::com::sun::star::util;
43 : using namespace ::com::sun::star::sdbc;
44 :
45 : // OWizTypeSelectControl
46 0 : OWizTypeSelectControl::OWizTypeSelectControl(vcl::Window* pParent, vcl::Window* pParentTabPage, OTableDesignHelpBar* pHelpBar)
47 0 : : OFieldDescControl(pParent, pHelpBar)
48 : {
49 0 : m_pParentTabPage = pParentTabPage;
50 0 : }
51 :
52 0 : OWizTypeSelectControl::~OWizTypeSelectControl()
53 : {
54 0 : disposeOnce();
55 0 : }
56 :
57 0 : void OWizTypeSelectControl::dispose()
58 : {
59 0 : m_pParentTabPage.clear();
60 0 : OFieldDescControl::dispose();
61 0 : }
62 :
63 0 : void OWizTypeSelectControl::ActivateAggregate( EControlType eType )
64 : {
65 0 : switch(eType )
66 : {
67 : case tpFormat:
68 : case tpDefault:
69 : case tpAutoIncrement:
70 : case tpAutoIncrementValue:
71 0 : break;
72 : default:
73 0 : OFieldDescControl::ActivateAggregate( eType );
74 : }
75 0 : }
76 :
77 0 : void OWizTypeSelectControl::DeactivateAggregate( EControlType eType )
78 : {
79 0 : switch(eType )
80 : {
81 : case tpFormat:
82 : case tpDefault:
83 : case tpAutoIncrement:
84 : case tpAutoIncrementValue:
85 0 : break;
86 : default:
87 0 : OFieldDescControl::DeactivateAggregate( eType );
88 : }
89 0 : }
90 :
91 0 : void OWizTypeSelectControl::CellModified(long nRow, sal_uInt16 nColId )
92 : {
93 : OSL_ENSURE(nRow == -1,"nRow muss -1 sein!");
94 : (void)nRow;
95 :
96 0 : MultiListBox *pListBox = static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pColumnNames;
97 :
98 0 : OFieldDescription* pCurFieldDescr = getCurrentFieldDescData();
99 :
100 0 : sal_uInt16 nPos = pListBox->GetEntryPos( OUString( pCurFieldDescr->GetName() ) );
101 0 : pCurFieldDescr = static_cast< OFieldDescription* >( pListBox->GetEntryData( nPos ) );
102 : OSL_ENSURE( pCurFieldDescr, "OWizTypeSelectControl::CellModified: Columnname/type not found in the listbox!" );
103 0 : if ( !pCurFieldDescr )
104 0 : return;
105 0 : setCurrentFieldDescData( pCurFieldDescr );
106 :
107 0 : OUString sName = pCurFieldDescr->GetName();
108 0 : OUString sNewName;
109 0 : const OPropColumnEditCtrl* pColumnName = getColumnCtrl();
110 0 : if ( pColumnName )
111 0 : sNewName = pColumnName->GetText();
112 :
113 0 : switch(nColId)
114 : {
115 : case FIELD_PROPERTY_COLUMNNAME:
116 : {
117 0 : OCopyTableWizard* pWiz = static_cast<OCopyTableWizard*>(GetParentDialog());
118 : // first we have to check if this name already exists
119 0 : bool bDoubleName = false;
120 0 : bool bCase = true;
121 0 : if ( getMetaData().is() && !getMetaData()->supportsMixedCaseQuotedIdentifiers() )
122 : {
123 0 : bCase = false;
124 0 : sal_uInt16 nCount = pListBox->GetEntryCount();
125 0 : for (sal_uInt16 i=0 ; !bDoubleName && i < nCount ; ++i)
126 : {
127 0 : OUString sEntry(pListBox->GetEntry(i));
128 0 : bDoubleName = sNewName.equalsIgnoreAsciiCase(sEntry);
129 0 : }
130 0 : if ( !bDoubleName && pWiz->shouldCreatePrimaryKey() )
131 0 : bDoubleName = sNewName.equalsIgnoreAsciiCase(pWiz->getPrimaryKeyName());
132 :
133 : }
134 : else
135 0 : bDoubleName = ((pListBox->GetEntryPos(OUString(sNewName)) != LISTBOX_ENTRY_NOTFOUND)
136 0 : || ( pWiz->shouldCreatePrimaryKey()
137 0 : && pWiz->getPrimaryKeyName() == sNewName) );
138 :
139 0 : if ( bDoubleName )
140 : {
141 0 : OUString strMessage = ModuleRes(STR_TABLEDESIGN_DUPLICATE_NAME);
142 0 : strMessage = strMessage.replaceFirst("$column$", sNewName);
143 0 : pWiz->showError(strMessage);
144 0 : pCurFieldDescr->SetName(sName);
145 0 : DisplayData(pCurFieldDescr);
146 0 : static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->setDuplicateName(true);
147 0 : return;
148 : }
149 :
150 0 : OUString sOldName = pCurFieldDescr->GetName();
151 0 : pCurFieldDescr->SetName(sNewName);
152 0 : static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->setDuplicateName(false);
153 :
154 : // now we change the name
155 0 : OCopyTableWizard::TNameMapping::iterator aIter = pWiz->m_mNameMapping.begin();
156 0 : OCopyTableWizard::TNameMapping::iterator aEnd = pWiz->m_mNameMapping.end();
157 :
158 0 : ::comphelper::UStringMixEqual aCase(bCase);
159 0 : for(;aIter != aEnd;++aIter)
160 : {
161 0 : if ( aCase(aIter->second,sName) )
162 : {
163 0 : aIter->second = sNewName;
164 0 : break;
165 : }
166 : }
167 :
168 0 : pListBox->RemoveEntry(nPos);
169 0 : pListBox->InsertEntry(pCurFieldDescr->GetName(),nPos);
170 0 : pListBox->SetEntryData(nPos,pCurFieldDescr);
171 :
172 0 : pWiz->replaceColumn(nPos,pCurFieldDescr,sOldName);
173 : }
174 0 : break;
175 : }
176 0 : saveCurrentFieldDescData();
177 : }
178 :
179 0 : ::com::sun::star::lang::Locale OWizTypeSelectControl::GetLocale() const
180 : {
181 0 : return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pParent->GetLocale();
182 : }
183 :
184 0 : Reference< XNumberFormatter > OWizTypeSelectControl::GetFormatter() const
185 : {
186 0 : return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pParent->GetFormatter();
187 : }
188 :
189 0 : TOTypeInfoSP OWizTypeSelectControl::getTypeInfo(sal_Int32 _nPos)
190 : {
191 0 : return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pParent->getDestTypeInfo(_nPos);
192 : }
193 :
194 0 : const OTypeInfoMap* OWizTypeSelectControl::getTypeInfo() const
195 : {
196 0 : return &static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pParent->getDestTypeInfo();
197 : }
198 :
199 0 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XDatabaseMetaData> OWizTypeSelectControl::getMetaData()
200 : {
201 0 : return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pParent->m_xDestConnection->getMetaData();
202 : }
203 :
204 0 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XConnection> OWizTypeSelectControl::getConnection()
205 : {
206 0 : return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_pParent->m_xDestConnection;
207 : }
208 :
209 0 : bool OWizTypeSelectControl::isAutoIncrementValueEnabled() const
210 : {
211 0 : return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_bAutoIncrementEnabled;
212 : }
213 :
214 0 : OUString OWizTypeSelectControl::getAutoIncrementValue() const
215 : {
216 0 : return static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_sAutoIncrementValue;
217 : }
218 :
219 : #define IMG_PRIMARY_KEY 1
220 0 : OWizTypeSelect::OWizTypeSelect( vcl::Window* pParent, SvStream* _pStream )
221 : :OWizardPage( pParent, "TypeSelect", "dbaccess/ui/typeselectpage.ui")
222 0 : ,m_pTypeControl(VclPtr<OWizTypeSelectControl>::Create(get<VclVBox>("control_container"), this) )
223 : ,m_pParserStream( _pStream )
224 : ,m_nDisplayRow(0)
225 : ,m_bAutoIncrementEnabled(false)
226 0 : ,m_bDuplicateName(false)
227 : {
228 0 : get(m_pColumnNames, "columnnames");
229 0 : m_pColumnNames->SetParentTabPage(this);
230 0 : get(m_pColumns, "columns");
231 0 : get(m_pAutoType, "autotype");
232 0 : get(m_pAutoFt, "autolabel");
233 0 : get(m_pAutoEt, "auto");
234 0 : get(m_pAutoPb, "autobutton");
235 :
236 0 : m_pColumnNames->SetSelectHdl(LINK(this,OWizTypeSelect,ColumnSelectHdl));
237 :
238 0 : ModuleRes aModuleRes(IMG_JOINS);
239 0 : ImageList aImageList(aModuleRes);
240 0 : m_imgPKey = aImageList.GetImage(IMG_PRIMARY_KEY);
241 :
242 :
243 0 : m_pTypeControl->Show();
244 0 : m_pTypeControl->Init();
245 :
246 0 : m_pAutoEt->SetText(OUString("10"));
247 0 : m_pAutoEt->SetDecimalDigits(0);
248 0 : m_pAutoPb->SetClickHdl(LINK(this,OWizTypeSelect,ButtonClickHdl));
249 0 : m_pColumnNames->EnableMultiSelection(true);
250 :
251 : try
252 : {
253 0 : m_pColumnNames->SetPKey( m_pParent->supportsPrimaryKey() );
254 0 : ::dbaui::fillAutoIncrementValue( m_pParent->m_xDestConnection, m_bAutoIncrementEnabled, m_sAutoIncrementValue );
255 : }
256 0 : catch(const Exception&)
257 : {
258 : DBG_UNHANDLED_EXCEPTION();
259 0 : }
260 0 : }
261 :
262 0 : OWizTypeSelect::~OWizTypeSelect()
263 : {
264 0 : disposeOnce();
265 0 : }
266 :
267 0 : void OWizTypeSelect::dispose()
268 : {
269 0 : m_pTypeControl.disposeAndClear();
270 0 : m_pColumnNames.clear();
271 0 : m_pColumns.clear();
272 0 : m_pAutoType.clear();
273 0 : m_pAutoFt.clear();
274 0 : m_pAutoEt.clear();
275 0 : m_pAutoPb.clear();
276 0 : OWizardPage::dispose();
277 0 : }
278 :
279 0 : OUString OWizTypeSelect::GetTitle() const
280 : {
281 0 : return ModuleRes(STR_WIZ_TYPE_SELECT_TITEL);
282 : }
283 :
284 0 : IMPL_LINK( OWizTypeSelect, ColumnSelectHdl, MultiListBox *, /*pListBox*/ )
285 : {
286 0 : OUString aColumnName( m_pColumnNames->GetSelectEntry() );
287 :
288 0 : OFieldDescription* pField = static_cast<OFieldDescription*>(m_pColumnNames->GetEntryData(m_pColumnNames->GetEntryPos(aColumnName)));
289 0 : if(pField)
290 0 : m_pTypeControl->DisplayData(pField);
291 :
292 0 : m_pTypeControl->Enable(m_pColumnNames->GetSelectEntryCount() == 1 );
293 0 : return 0;
294 : }
295 :
296 0 : void OWizTypeSelect::Reset()
297 : {
298 : // restore original state
299 :
300 0 : while(m_pColumnNames->GetEntryCount())
301 0 : m_pColumnNames->RemoveEntry(0);
302 0 : m_pColumnNames->Clear();
303 : sal_Int32 nBreakPos;
304 0 : m_pParent->CheckColumns(nBreakPos);
305 :
306 0 : const ODatabaseExport::TColumnVector& rDestColumns = m_pParent->getDestVector();
307 0 : ODatabaseExport::TColumnVector::const_iterator aIter = rDestColumns.begin();
308 0 : ODatabaseExport::TColumnVector::const_iterator aEnd = rDestColumns.end();
309 0 : for(;aIter != aEnd;++aIter)
310 : {
311 : sal_uInt16 nPos;
312 0 : if((*aIter)->second->IsPrimaryKey())
313 0 : nPos = m_pColumnNames->InsertEntry((*aIter)->first, m_imgPKey );
314 : else
315 0 : nPos = m_pColumnNames->InsertEntry((*aIter)->first);
316 0 : m_pColumnNames->SetEntryData(nPos,(*aIter)->second);
317 : }
318 0 : m_bFirstTime = false;
319 0 : }
320 :
321 0 : void OWizTypeSelect::ActivatePage( )
322 : {
323 0 : bool bOldFirstTime = m_bFirstTime;
324 0 : Reset();
325 0 : m_bFirstTime = bOldFirstTime;
326 :
327 0 : m_pColumnNames->SelectEntryPos(static_cast<sal_uInt16>(m_nDisplayRow));
328 0 : m_nDisplayRow = 0;
329 0 : m_pColumnNames->GetSelectHdl().Call(m_pColumnNames);
330 0 : }
331 :
332 0 : bool OWizTypeSelect::LeavePage()
333 : {
334 0 : OUString aColumnName( m_pColumnNames->GetSelectEntry() );
335 :
336 0 : bool bDuplicateName = false;
337 0 : OFieldDescription* pField = static_cast<OFieldDescription*>(m_pColumnNames->GetEntryData(m_pColumnNames->GetEntryPos(aColumnName)));
338 0 : if ( pField )
339 : {
340 0 : m_pTypeControl->SaveData(pField);
341 0 : bDuplicateName = m_bDuplicateName;
342 : }
343 0 : return !bDuplicateName;
344 : }
345 :
346 0 : void OWizTypeSelect::EnableAuto(bool bEnable)
347 : {
348 0 : m_pAutoFt->Show(bEnable);
349 0 : m_pAutoEt->Show(bEnable);
350 0 : m_pAutoPb->Show(bEnable);
351 0 : m_pAutoType->Show(bEnable);
352 0 : }
353 :
354 0 : IMPL_LINK( OWizTypeSelect, ButtonClickHdl, Button *, /*pButton*/ )
355 : {
356 : sal_Int32 nBreakPos;
357 0 : m_pParent->CheckColumns(nBreakPos);
358 0 : fillColumnList(m_pAutoEt->GetText().toInt32());
359 :
360 0 : ActivatePage();
361 :
362 0 : return 0;
363 : }
364 :
365 0 : OWizTypeSelectList::~OWizTypeSelectList()
366 : {
367 0 : disposeOnce();
368 0 : }
369 :
370 0 : void OWizTypeSelectList::dispose()
371 : {
372 0 : m_pParentTabPage.clear();
373 0 : MultiListBox::dispose();
374 0 : }
375 :
376 0 : bool OWizTypeSelectList::IsPrimaryKeyAllowed() const
377 : {
378 0 : sal_uInt16 nCount = GetSelectEntryCount();
379 : sal_uInt16 j;
380 :
381 0 : for( j = 0; m_bPKey && j < nCount; ++j )
382 : {
383 0 : OFieldDescription* pField = static_cast<OFieldDescription*>(GetEntryData(GetSelectEntryPos(j)));
384 0 : if(!pField || pField->getTypeInfo()->nSearchType == ColumnSearch::NONE)
385 0 : break;
386 : }
387 0 : return j == nCount;
388 : }
389 :
390 0 : void OWizTypeSelectList::setPrimaryKey(OFieldDescription* _pFieldDescr, sal_uInt16 _nPos, bool _bSet)
391 : {
392 0 : OUString sColumnName = GetEntry(_nPos);
393 0 : RemoveEntry(_nPos);
394 0 : _pFieldDescr->SetPrimaryKey(_bSet);
395 0 : if( _bSet )
396 0 : InsertEntry(sColumnName, static_cast<OWizTypeSelect*>(m_pParentTabPage.get())->m_imgPKey,_nPos);
397 0 : else if( _pFieldDescr->getTypeInfo()->bNullable )
398 : {
399 0 : _pFieldDescr->SetControlDefault(Any());
400 0 : InsertEntry(sColumnName,_nPos);
401 : }
402 0 : SetEntryData(_nPos,_pFieldDescr);
403 0 : }
404 :
405 0 : VCL_BUILDER_FACTORY(OWizTypeSelectList)
406 :
407 0 : bool OWizTypeSelectList::PreNotify( NotifyEvent& rEvt )
408 : {
409 0 : bool nDone = false;
410 0 : switch( rEvt.GetType() )
411 : {
412 : case MouseNotifyEvent::MOUSEBUTTONDOWN:
413 : {
414 0 : const MouseEvent* pMEvt = rEvt.GetMouseEvent();
415 0 : if(pMEvt->IsRight() && !pMEvt->GetModifier())
416 0 : nDone = true;
417 : }
418 0 : break;
419 : case MouseNotifyEvent::COMMAND:
420 : {
421 0 : if(!IsPrimaryKeyAllowed())
422 0 : break;
423 :
424 0 : const CommandEvent* pComEvt = rEvt.GetCommandEvent();
425 0 : if(pComEvt->GetCommand() != CommandEventId::ContextMenu)
426 0 : break;
427 : // the place, at which was clicked
428 0 : Point ptWhere(0,0);
429 0 : if (pComEvt->IsMouseEvent())
430 0 : ptWhere = pComEvt->GetMousePosPixel();
431 :
432 0 : PopupMenu aContextMenu(ModuleRes(RID_SBA_RTF_PKEYPOPUP));
433 0 : switch( aContextMenu.Execute( this, ptWhere ) )
434 : {
435 : case SID_TABLEDESIGN_TABED_PRIMARYKEY:
436 : {
437 0 : sal_uInt16 nCount = GetEntryCount();
438 0 : for(sal_uInt16 j = 0 ; j < nCount ; ++j)
439 : {
440 0 : OFieldDescription* pFieldDescr = static_cast<OFieldDescription*>(GetEntryData(j));
441 0 : if( pFieldDescr )
442 : {
443 0 : if(pFieldDescr->IsPrimaryKey() && !IsEntryPosSelected(j))
444 0 : setPrimaryKey(pFieldDescr,j);
445 0 : else if(IsEntryPosSelected(j))
446 : {
447 0 : setPrimaryKey(pFieldDescr,j,!pFieldDescr->IsPrimaryKey());
448 0 : SelectEntryPos(j);
449 : }
450 : }
451 : }
452 0 : GetSelectHdl().Call(this);
453 : }
454 0 : break;
455 : }
456 0 : nDone = true;
457 : }
458 0 : break;
459 : default:
460 0 : break;
461 : }
462 0 : return nDone || MultiListBox::PreNotify(rEvt);
463 : }
464 :
465 0 : void OWizTypeSelect::fillColumnList(sal_uInt32 nRows)
466 : {
467 0 : if(m_pParserStream)
468 : {
469 0 : sal_Size nTell = m_pParserStream->Tell(); // might change seek position of stream
470 :
471 0 : SvParser *pReader = createReader(nRows);
472 0 : if(pReader)
473 : {
474 0 : pReader->AddFirstRef();
475 0 : pReader->CallParser();
476 0 : pReader->ReleaseRef();
477 : }
478 0 : m_pParserStream->Seek(nTell);
479 : }
480 36 : }
481 :
482 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|