summaryrefslogtreecommitdiff
path: root/dbaccess/source/ui/misc/WTypeSelect.cxx
blob: 06fcf50d0a46d4c9dafddb418342eb8cada4ba38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <WTypeSelect.hxx>
#include <stringconstants.hxx>
#include <bitmaps.hlst>
#include <tools/diagnose_ex.h>
#include <osl/diagnose.h>
#include <FieldDescriptions.hxx>
#include <WCopyTable.hxx>
#include <strings.hrc>
#include <tools/stream.hxx>
#include <svtools/svparser.hxx>
#include <UITools.hxx>
#include <core_resource.hxx>
#include <sqlmessage.hxx>
#include <FieldControls.hxx>
#include <vcl/layout.hxx>
#include <vcl/builderfactory.hxx>

#include <dbaccess_slotid.hrc>

using namespace ::dbaui;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::beans;
using namespace ::com::sun::star::container;
using namespace ::com::sun::star::util;
using namespace ::com::sun::star::sdbc;

// OWizTypeSelectControl
OWizTypeSelectControl::OWizTypeSelectControl(TabPageParent pParent, OWizTypeSelect* pParentTabPage)
    : OFieldDescControl(pParent, nullptr)
    , m_xParentTabPage(pParentTabPage)
{
}

OWizTypeSelectControl::~OWizTypeSelectControl()
{
    disposeOnce();
}

void OWizTypeSelectControl::dispose()
{
    m_xParentTabPage.clear();
    OFieldDescControl::dispose();
}

void OWizTypeSelectControl::ActivateAggregate( EControlType eType )
{
    switch(eType )
    {
        case tpFormat:
        case tpDefault:
        case tpAutoIncrement:
        case tpAutoIncrementValue:
            break;
        default:
            OFieldDescControl::ActivateAggregate( eType );
    }
}

void OWizTypeSelectControl::DeactivateAggregate( EControlType eType )
{
    switch(eType )
    {
        case tpFormat:
        case tpDefault:
        case tpAutoIncrement:
        case tpAutoIncrementValue:
            break;
        default:
            OFieldDescControl::DeactivateAggregate( eType );
    }
}

void OWizTypeSelectControl::CellModified(long nRow, sal_uInt16 nColId )
{
    OSL_ENSURE(nRow == -1,"nRow must be -1!");

    weld::TreeView* pListBox = m_xParentTabPage->m_xColumnNames->GetWidget();

    OFieldDescription* pCurFieldDescr = getCurrentFieldDescData();

    const sal_Int32 nPos = pListBox->find_text(pCurFieldDescr->GetName());
    pCurFieldDescr = reinterpret_cast< OFieldDescription* >( pListBox->get_id(nPos).toInt64() );
    OSL_ENSURE( pCurFieldDescr, "OWizTypeSelectControl::CellModified: Columnname/type not found in the listbox!" );
    if ( !pCurFieldDescr )
        return;
    setCurrentFieldDescData( pCurFieldDescr );

    OUString sName = pCurFieldDescr->GetName();
    OUString sNewName;
    const OPropColumnEditCtrl* pColumnName = getColumnCtrl();
    if ( pColumnName )
        sNewName = pColumnName->get_text();

    switch(nColId)
    {
        case FIELD_PROPERTY_COLUMNNAME:
            {
                OCopyTableWizard* pWiz = m_xParentTabPage->m_pParent;
                // first we have to check if this name already exists
                bool bDoubleName = false;
                bool bCase = true;
                if ( getMetaData().is() && !getMetaData()->supportsMixedCaseQuotedIdentifiers() )
                {
                    bCase = false;
                    const sal_Int32 nCount = pListBox->n_children();
                    for (sal_Int32 i=0 ; !bDoubleName && i < nCount ; ++i)
                    {
                        OUString sEntry(pListBox->get_text(i));
                        bDoubleName = sNewName.equalsIgnoreAsciiCase(sEntry);
                    }
                    if ( !bDoubleName && pWiz->shouldCreatePrimaryKey() )
                        bDoubleName = sNewName.equalsIgnoreAsciiCase(pWiz->getPrimaryKeyName());

                }
                else
                    bDoubleName =  ((pListBox->find_text(sNewName) != -1)
                                    || ( pWiz->shouldCreatePrimaryKey()
                                        &&  pWiz->getPrimaryKeyName() == sNewName) );

                if ( bDoubleName )
                {
                    OUString strMessage = DBA_RES(STR_TABLEDESIGN_DUPLICATE_NAME);
                    strMessage = strMessage.replaceFirst("$column$", sNewName);
                    pWiz->showError(strMessage);
                    pCurFieldDescr->SetName(sName);
                    DisplayData(pCurFieldDescr);
                    m_xParentTabPage->setDuplicateName(true);
                    return;
                }

                OUString sOldName = pCurFieldDescr->GetName();
                pCurFieldDescr->SetName(sNewName);
                m_xParentTabPage->setDuplicateName(false);

                // now we change the name

                ::comphelper::UStringMixEqual aCase(bCase);
                for (auto & elem : pWiz->m_mNameMapping)
                {
                    if ( aCase(elem.second,sName) )
                    {
                        elem.second = sNewName;
                        break;
                    }
                }

                pListBox->remove(nPos);
                pListBox->insert_text(nPos, pCurFieldDescr->GetName());
                pListBox->set_id(nPos, OUString::number(reinterpret_cast<sal_Int64>(pCurFieldDescr)));

                pWiz->replaceColumn(nPos,pCurFieldDescr,sOldName);
            }
            break;
    }
    saveCurrentFieldDescData();
}

css::lang::Locale  OWizTypeSelectControl::GetLocale() const
{
    return m_xParentTabPage->m_pParent->GetLocale();
}

Reference< XNumberFormatter > OWizTypeSelectControl::GetFormatter() const
{
    return m_xParentTabPage->m_pParent->GetFormatter();
}

TOTypeInfoSP    OWizTypeSelectControl::getTypeInfo(sal_Int32 _nPos)
{
    return m_xParentTabPage->m_pParent->getDestTypeInfo(_nPos);
}

const OTypeInfoMap* OWizTypeSelectControl::getTypeInfo() const
{
    return &m_xParentTabPage->m_pParent->getDestTypeInfo();
}

css::uno::Reference< css::sdbc::XDatabaseMetaData> OWizTypeSelectControl::getMetaData()
{
    return m_xParentTabPage->m_pParent->m_xDestConnection->getMetaData();
}

css::uno::Reference< css::sdbc::XConnection> OWizTypeSelectControl::getConnection()
{
    return m_xParentTabPage->m_pParent->m_xDestConnection;
}

bool OWizTypeSelectControl::isAutoIncrementValueEnabled() const
{
    return m_xParentTabPage->m_bAutoIncrementEnabled;
}

OUString OWizTypeSelectControl::getAutoIncrementValue() const
{
    return m_xParentTabPage->m_sAutoIncrementValue;
}

OWizTypeSelect::OWizTypeSelect(OCopyTableWizard* pWizard, TabPageParent pParent, SvStream* pStream)
    : OWizardPage(pWizard, pParent, "dbaccess/ui/typeselectpage.ui", "TypeSelect")
    , m_xColumnNames(new OWizTypeSelectList(m_xBuilder->weld_tree_view("columnnames")))
    , m_xColumns(m_xBuilder->weld_label("columns"))
    , m_xControlContainer(m_xBuilder->weld_container("control_container"))
    , m_xTypeControl(VclPtr<OWizTypeSelectControl>::Create(TabPageParent(m_xControlContainer.get(), pWizard), this))
    , m_xAutoType(m_xBuilder->weld_label("autotype"))
    , m_xAutoFt(m_xBuilder->weld_label("autolabel"))
    , m_xAutoEt(m_xBuilder->weld_spin_button("auto"))
    , m_xAutoPb(m_xBuilder->weld_button("autobutton"))
    , m_pParserStream(pStream)
    , m_nDisplayRow(0)
    , m_bAutoIncrementEnabled(false)
    , m_bDuplicateName(false)
{
    m_xColumnNames->SetParentTabPage(this);

    m_xColumnNames->connect_changed(LINK(this,OWizTypeSelect,ColumnSelectHdl));

    m_xTypeControl->Show();
    m_xTypeControl->Init();

    m_xAutoEt->set_text("10");
    m_xAutoEt->set_digits(0);
    m_xAutoPb->connect_clicked(LINK(this,OWizTypeSelect,ButtonClickHdl));
    m_xColumnNames->set_selection_mode(SelectionMode::Multiple);

    try
    {
        m_xColumnNames->SetPKey( m_pParent->supportsPrimaryKey() );
        ::dbaui::fillAutoIncrementValue( m_pParent->m_xDestConnection, m_bAutoIncrementEnabled, m_sAutoIncrementValue );
    }
    catch(const Exception&)
    {
        DBG_UNHANDLED_EXCEPTION("dbaccess");
    }
}

OWizTypeSelect::~OWizTypeSelect()
{
    disposeOnce();
}

void OWizTypeSelect::dispose()
{
    m_xTypeControl.disposeAndClear();
    OWizardPage::dispose();
}

OUString OWizTypeSelect::GetTitle() const
{
    return DBA_RES(STR_WIZ_TYPE_SELECT_TITEL);
}

IMPL_LINK_NOARG(OWizTypeSelect, ColumnSelectHdl, weld::TreeView&, void)
{
    OFieldDescription* pField = reinterpret_cast<OFieldDescription*>(m_xColumnNames->get_selected_id().toInt64());
    if (pField)
        m_xTypeControl->DisplayData(pField);

    m_xTypeControl->Enable(m_xColumnNames->count_selected_rows() == 1);
}

void OWizTypeSelect::Reset()
{
    // restore original state
    m_xColumnNames->clear();
    sal_Int32 nCount(0), nBreakPos;
    m_pParent->CheckColumns(nBreakPos);

    const ODatabaseExport::TColumnVector& rDestColumns = m_pParent->getDestVector();
    for (auto const& column : rDestColumns)
    {
        OUString sId(OUString::number(reinterpret_cast<sal_Int64>(column->second)));
        m_xColumnNames->append(sId, column->first);
        if (column->second->IsPrimaryKey())
            m_xColumnNames->set_image(nCount, BMP_PRIMARY_KEY);
        ++nCount;
    }
    m_bFirstTime = false;
}

void OWizTypeSelect::ActivatePage( )
{
    bool bOldFirstTime = m_bFirstTime;
    Reset();
    m_bFirstTime = bOldFirstTime;

    m_xColumnNames->select(m_nDisplayRow);
    m_nDisplayRow = 0;
    ColumnSelectHdl(*m_xColumnNames->GetWidget());
}

bool OWizTypeSelect::LeavePage()
{
    bool bDuplicateName = false;
    OFieldDescription* pField = reinterpret_cast<OFieldDescription*>(m_xColumnNames->get_selected_id().toInt64());
    if ( pField )
    {
        m_xTypeControl->SaveData(pField);
        bDuplicateName = m_bDuplicateName;
    }
    return !bDuplicateName;
}

void OWizTypeSelect::EnableAuto(bool bEnable)
{
    m_xAutoFt->set_visible(bEnable);
    m_xAutoEt->set_visible(bEnable);
    m_xAutoPb->set_visible(bEnable);
    m_xAutoType->set_visible(bEnable);
}

IMPL_LINK_NOARG(OWizTypeSelect, ButtonClickHdl, weld::Button&, void)
{
    sal_Int32 nBreakPos;
    m_pParent->CheckColumns(nBreakPos);

    // fill column list
    sal_uInt32 nRows = m_xAutoEt->get_text().toInt32();
    if(m_pParserStream)
    {
        sal_uInt64 const nTell = m_pParserStream->Tell(); // might change seek position of stream

        createReaderAndCallParser(nRows);
        m_pParserStream->Seek(nTell);
    }

    ActivatePage();
}

OWizTypeSelectList::OWizTypeSelectList(std::unique_ptr<weld::TreeView> xControl)
    : m_xControl(std::move(xControl))
    , m_bPKey(false)
    , m_xParentTabPage(nullptr)
{
    m_xControl->connect_popup_menu(LINK(this, OWizTypeSelectList, CommandHdl));
}

bool OWizTypeSelectList::IsPrimaryKeyAllowed() const
{
    auto aRows = m_xControl->get_selected_rows();
    std::sort(aRows.begin(), aRows.end());

    const sal_Int32 nCount = aRows.size();

    for( sal_Int32 j = 0; m_bPKey && j < nCount; ++j )
    {
        OFieldDescription* pField = reinterpret_cast<OFieldDescription*>(m_xControl->get_id(aRows[j]).toInt64());
        if(!pField || pField->getTypeInfo()->nSearchType == ColumnSearch::NONE)
            return false;
    }
    return true;
}

void OWizTypeSelectList::setPrimaryKey(OFieldDescription* _pFieldDescr, sal_uInt16 _nPos, bool _bSet)
{
    _pFieldDescr->SetPrimaryKey(_bSet);
    if( _bSet )
    {
        OUString sImage(BMP_PRIMARY_KEY);
        m_xControl->set_image(_nPos, sImage);
    }
    else if( _pFieldDescr->getTypeInfo()->bNullable )
    {
        _pFieldDescr->SetControlDefault(Any());
        m_xControl->set_image(_nPos, OUString());
    }
}

IMPL_LINK(OWizTypeSelectList, CommandHdl, const CommandEvent&, rCEvt, bool)
{
    if (rCEvt.GetCommand() != CommandEventId::ContextMenu)
        return false;
    if (!IsPrimaryKeyAllowed())
        return false;

    std::unique_ptr<weld::Builder> xBuilder(Application::CreateBuilder(m_xControl.get(), "dbaccess/ui/keymenu.ui"));
    auto xContextMenu = xBuilder->weld_menu("menu");
    // Should primary key checkbox be checked?
    const sal_Int32 nCount = m_xControl->n_children();
    bool bCheckOk = false;
    for(sal_Int32 j = 0 ; j < nCount ; ++j)
    {
        OFieldDescription* pFieldDescr = reinterpret_cast<OFieldDescription*>(m_xControl->get_id(j).toInt64());
        // if at least one of the fields is selected but not in the primary key,
        // or is in the primary key but not selected, then don't check the
        // primary key checkbox.
        if( pFieldDescr && pFieldDescr->IsPrimaryKey() != m_xControl->is_selected(j) )
        {
            bCheckOk = false;
            break;
        }
        if (!bCheckOk && m_xControl->is_selected(j))
            bCheckOk = true;
    }

    if (bCheckOk)
        xContextMenu->set_active("primarykey", true);

    OString sCommand(xContextMenu->popup_at_rect(m_xControl.get(), tools::Rectangle(rCEvt.GetMousePosPixel(), Size(1,1))));
    if (sCommand == "primarykey")
    {
        for (sal_Int32 j = 0 ; j < nCount; ++j)
        {
            OFieldDescription* pFieldDescr = reinterpret_cast<OFieldDescription*>(m_xControl->get_id(j).toInt64());
            if (pFieldDescr)
            {
                if(!bCheckOk && m_xControl->is_selected(j))
                {
                    setPrimaryKey(pFieldDescr,j,true);
                }
                else
                {
                    setPrimaryKey(pFieldDescr,j);
                }
            }
        }
        m_aChangeHdl.Call(*m_xControl);
    }

    return true;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */