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