fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / dbaccess / source / ui / dlg / indexdialog.cxx
blob72456c0d17e7080ad965c0129e6dfb0e1fdf4f2f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sal/config.h"
22 #include <set>
24 #include "indexdialog.hxx"
25 #include "dbu_dlg.hrc"
26 #include "dbaccess_helpid.hrc"
27 #include "indexfieldscontrol.hxx"
28 #include "indexcollection.hxx"
29 #include <vcl/layout.hxx>
30 #include <vcl/settings.hxx>
31 #include <vcl/builderfactory.hxx>
32 #include <com/sun/star/sdb/SQLContext.hpp>
33 #include "UITools.hxx"
34 #include <svtools/imgdef.hxx>
35 #include "svtools/treelistentry.hxx"
36 #include "browserids.hxx"
37 #include <connectivity/dbtools.hxx>
38 #include <osl/diagnose.h>
40 const char INDEX_NEW_CMD[] = ".index:createNew";
41 const char INDEX_DROP_CMD[] = ".index:dropCurrent";
42 const char INDEX_RENAME_CMD[] = ".index:renameCurrent";
43 const char INDEX_SAVE_CMD[] = ".index:saveCurrent";
44 const char INDEX_RESET_CMD[] = ".index:resetCurrent";
46 namespace dbaui
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::container;
51 using namespace ::com::sun::star::sdbc;
52 using namespace ::com::sun::star::sdb;
53 using namespace ::com::sun::star::lang;
54 using namespace ::dbtools;
56 // helper
57 bool operator ==(const OIndexField& _rLHS, const OIndexField& _rRHS)
59 return (_rLHS.sFieldName == _rRHS.sFieldName)
60 && (_rLHS.bSortAscending == _rRHS.bSortAscending);
63 bool operator !=(const OIndexField& _rLHS, const OIndexField& _rRHS)
65 return !(_rLHS == _rRHS);
68 bool operator ==(const IndexFields& _rLHS, const IndexFields& _rRHS)
70 if (_rLHS.size() != _rRHS.size())
71 return false;
73 IndexFields::const_iterator aLeft = _rLHS.begin();
74 IndexFields::const_iterator aLeftEnd = _rLHS.end();
75 IndexFields::const_iterator aRight = _rRHS.begin();
76 for (; aLeft != aLeftEnd; ++aLeft, ++aRight)
78 if (*aLeft != *aRight)
79 return false;
82 return true;
85 bool operator !=(const IndexFields& _rLHS, const IndexFields& _rRHS)
87 return !(_rLHS == _rRHS);
90 // DbaIndexList
91 DbaIndexList::DbaIndexList(vcl::Window* _pParent, WinBits nWinBits)
92 :SvTreeListBox(_pParent, nWinBits)
93 ,m_bSuspendSelectHdl(false)
97 bool DbaIndexList::EditedEntry( SvTreeListEntry* _pEntry, const OUString& _rNewText )
99 // first check if this is valid SQL92 name
100 if ( isSQL92CheckEnabled(m_xConnection) )
102 Reference<XDatabaseMetaData> xMeta = m_xConnection->getMetaData();
103 if ( xMeta.is() )
105 OUString sAlias = ::dbtools::convertName2SQLName(_rNewText, xMeta->getExtraNameCharacters());
106 if ( ( xMeta->supportsMixedCaseQuotedIdentifiers() )
108 sAlias != _rNewText
110 !_rNewText.equalsIgnoreAsciiCase(sAlias))
111 return false;
115 if (!SvTreeListBox::EditedEntry(_pEntry, _rNewText))
116 return false;
118 OUString sOldText = GetEntryText(_pEntry);
119 SvTreeListBox::SetEntryText(_pEntry, _rNewText);
121 bool bValid = true;
122 if (m_aEndEditHdl.IsSet())
123 bValid = (0 != m_aEndEditHdl.Call(_pEntry));
125 if (bValid)
126 return true;
128 SvTreeListBox::SetEntryText(_pEntry, sOldText);
130 return false;
133 void DbaIndexList::enableSelectHandler()
135 OSL_ENSURE(m_bSuspendSelectHdl, "DbaIndexList::enableSelectHandler: invalid call (this is not cumulative)!");
136 m_bSuspendSelectHdl = false;
139 void DbaIndexList::disableSelectHandler()
141 OSL_ENSURE(!m_bSuspendSelectHdl, "DbaIndexList::enableSelectHandler: invalid call (this is not cumulative)!");
142 m_bSuspendSelectHdl = true;
145 void DbaIndexList::SelectNoHandlerCall( SvTreeListEntry* _pEntry )
147 disableSelectHandler();
148 Select(_pEntry, true);
149 enableSelectHandler();
152 bool DbaIndexList::Select(SvTreeListEntry* pEntry, bool _bSelect)
154 bool bReturn = SvTreeListBox::Select(pEntry, _bSelect);
156 if (m_aSelectHdl.IsSet() && !m_bSuspendSelectHdl && _bSelect)
157 m_aSelectHdl.Call(this);
159 return bReturn;
162 VCL_BUILDER_FACTORY_ARGS(DbaIndexList, WB_BORDER)
164 // DbaIndexDialog
165 DbaIndexDialog::DbaIndexDialog( vcl::Window* _pParent, const Sequence< OUString >& _rFieldNames,
166 const Reference< XNameAccess >& _rxIndexes,
167 const Reference< XConnection >& _rxConnection,
168 const Reference< XComponentContext >& _rxContext,sal_Int32 _nMaxColumnsInIndex)
169 :ModalDialog( _pParent, "IndexDesignDialog", "dbaccess/ui/indexdesigndialog.ui")
170 ,m_xConnection(_rxConnection)
171 ,m_aGeometrySettings(E_DIALOG, OUString("dbaccess.tabledesign.indexdialog"))
172 ,m_pIndexes(NULL)
173 ,m_pPreviousSelection(NULL)
174 ,m_bEditAgain(false)
175 ,m_xContext(_rxContext)
177 get(m_pActions, "ACTIONS");
179 mnNewCmdId = m_pActions->GetItemId(INDEX_NEW_CMD);
180 mnDropCmdId = m_pActions->GetItemId(INDEX_DROP_CMD);
181 mnRenameCmdId = m_pActions->GetItemId(INDEX_RENAME_CMD);
182 mnSaveCmdId = m_pActions->GetItemId(INDEX_SAVE_CMD);
183 mnResetCmdId = m_pActions->GetItemId(INDEX_RESET_CMD);
185 maScNewCmdImg = m_pActions->GetItemImage(mnNewCmdId);
186 maScDropCmdImg = m_pActions->GetItemImage(mnDropCmdId);
187 maScRenameCmdImg = m_pActions->GetItemImage(mnRenameCmdId);
188 maScSaveCmdImg = m_pActions->GetItemImage(mnSaveCmdId);
189 maScResetCmdImg = m_pActions->GetItemImage(mnResetCmdId);
190 maLcNewCmdImg = get<FixedImage>("image1")->GetImage();
191 maLcDropCmdImg = get<FixedImage>("image2")->GetImage();
192 maLcRenameCmdImg = get<FixedImage>("image3")->GetImage();
193 maLcSaveCmdImg = get<FixedImage>("image4")->GetImage();
194 maLcResetCmdImg = get<FixedImage>("image5")->GetImage();
196 get(m_pIndexList, "INDEX_LIST");
197 Size aSize(LogicToPixel(Size(70, 97), MAP_APPFONT));
198 m_pIndexList->set_width_request(aSize.Width());
199 m_pIndexList->set_height_request(aSize.Height());
200 get(m_pIndexDetails, "INDEX_DETAILS");
201 get(m_pDescriptionLabel, "DESC_LABEL");
202 get(m_pDescription, "DESCRIPTION");
203 get(m_pUnique, "UNIQUE");
204 get(m_pFieldsLabel, "FIELDS_LABEL");
205 get(m_pFields, "FIELDS");
206 aSize = LogicToPixel(Size(128, 61), MAP_APPFONT);
207 m_pFields->set_width_request(aSize.Width());
208 m_pFields->set_height_request(aSize.Height());
209 get(m_pClose, "close");
211 m_pActions->SetSelectHdl(LINK(this, DbaIndexDialog, OnIndexAction));
213 m_pIndexList->SetSelectHdl(LINK(this, DbaIndexDialog, OnIndexSelected));
214 m_pIndexList->SetEndEditHdl(LINK(this, DbaIndexDialog, OnEntryEdited));
215 m_pIndexList->SetSelectionMode(SINGLE_SELECTION);
216 m_pIndexList->SetHighlightRange();
217 m_pIndexList->setConnection(m_xConnection);
219 m_pFields->SetSizePixel(Size(300, 100));
220 m_pFields->Init(_rFieldNames, _nMaxColumnsInIndex, ::dbtools::getBooleanDataSourceSetting( m_xConnection, "AddIndexAppendix" ));
222 setToolBox(m_pActions);
224 m_pIndexes = new OIndexCollection();
227 m_pIndexes->attach(_rxIndexes);
229 catch(SQLException& e)
231 ::dbaui::showError(SQLExceptionInfo(e),_pParent,_rxContext);
233 catch(Exception&)
235 OSL_FAIL("DbaIndexDialog::DbaIndexDialog: could not retrieve basic information from the UNO collection!");
238 fillIndexList();
240 m_pUnique->SetClickHdl(LINK(this, DbaIndexDialog, OnModified));
241 m_pFields->SetModifyHdl(LINK(this, DbaIndexDialog, OnModified));
243 m_pClose->SetClickHdl(LINK(this, DbaIndexDialog, OnCloseDialog));
245 // if all of the indexes have an empty description, we're not interested in displaying it
246 Indexes::const_iterator aCheck;
248 for ( aCheck = m_pIndexes->begin();
249 aCheck != m_pIndexes->end();
250 ++aCheck
253 if (!aCheck->sDescription.isEmpty())
254 break;
257 if (aCheck == m_pIndexes->end())
259 // hide the controls which are necessary for the description
260 m_pDescription->Hide();
261 m_pDescriptionLabel->Hide();
265 void DbaIndexDialog::updateToolbox()
267 m_pActions->EnableItem(mnNewCmdId, !m_pIndexList->IsEditingActive());
269 SvTreeListEntry* pSelected = m_pIndexList->FirstSelected();
270 bool bSelectedAnything = NULL != pSelected;
272 if (pSelected)
274 // is the current entry modified?
275 Indexes::const_iterator aSelectedPos = m_pIndexes->begin() + reinterpret_cast<sal_IntPtr>(pSelected->GetUserData());
276 m_pActions->EnableItem(mnSaveCmdId, aSelectedPos->isModified() || aSelectedPos->isNew());
277 m_pActions->EnableItem(mnResetCmdId, aSelectedPos->isModified() || aSelectedPos->isNew());
278 bSelectedAnything = bSelectedAnything && !aSelectedPos->bPrimaryKey;
280 else
282 m_pActions->EnableItem(mnSaveCmdId, false);
283 m_pActions->EnableItem(mnResetCmdId, false);
285 m_pActions->EnableItem(mnDropCmdId, bSelectedAnything);
286 m_pActions->EnableItem(mnRenameCmdId, bSelectedAnything);
289 void DbaIndexDialog::fillIndexList()
291 Image aPKeyIcon(ModuleRes( IMG_PKEYICON ));
292 // fill the list with the index names
293 m_pIndexList->Clear();
294 Indexes::iterator aIndexLoop = m_pIndexes->begin();
295 Indexes::iterator aEnd = m_pIndexes->end();
296 for (; aIndexLoop != aEnd; ++aIndexLoop)
298 SvTreeListEntry* pNewEntry = NULL;
299 if (aIndexLoop->bPrimaryKey)
300 pNewEntry = m_pIndexList->InsertEntry(aIndexLoop->sName, aPKeyIcon, aPKeyIcon);
301 else
302 pNewEntry = m_pIndexList->InsertEntry(aIndexLoop->sName);
304 pNewEntry->SetUserData(reinterpret_cast< void* >(sal_Int32(aIndexLoop - m_pIndexes->begin())));
307 OnIndexSelected(m_pIndexList);
310 DbaIndexDialog::~DbaIndexDialog( )
312 disposeOnce();
315 void DbaIndexDialog::dispose()
317 setToolBox(NULL);
318 delete m_pIndexes;
319 m_pActions.clear();
320 m_pIndexList.clear();
321 m_pIndexDetails.clear();
322 m_pDescriptionLabel.clear();
323 m_pDescription.clear();
324 m_pUnique.clear();
325 m_pFieldsLabel.clear();
326 m_pFields.clear();
327 m_pClose.clear();
328 ModalDialog::dispose();
331 bool DbaIndexDialog::implCommit(SvTreeListEntry* _pEntry)
333 OSL_ENSURE(_pEntry, "DbaIndexDialog::implCommit: invalid entry!");
335 Indexes::iterator aCommitPos = m_pIndexes->begin() + reinterpret_cast<sal_IntPtr>(_pEntry->GetUserData());
337 // if it's not a new index, remove it
338 // (we can't modify indexes, only drop'n'insert)
339 if (!aCommitPos->isNew())
340 if (!implDropIndex(_pEntry, false))
341 return false;
343 // create the new index
344 SQLExceptionInfo aExceptionInfo;
347 m_pIndexes->commitNewIndex(aCommitPos);
349 catch(SQLContext& e) { aExceptionInfo = SQLExceptionInfo(e); }
350 catch(SQLWarning& e) { aExceptionInfo = SQLExceptionInfo(e); }
351 catch(SQLException& e) { aExceptionInfo = SQLExceptionInfo(e); }
353 // reflect the new selection in the toolbox
354 updateToolbox();
356 if (aExceptionInfo.isValid())
357 showError(aExceptionInfo, this, m_xContext);
358 else
360 m_pUnique->SaveValue();
361 m_pFields->SaveValue();
364 return !aExceptionInfo.isValid();
367 void DbaIndexDialog::OnNewIndex()
369 // commit the current entry, if necessary
370 if (!implCommitPreviouslySelected())
371 return;
373 // get a new unique name for the new index
374 OUString sNewIndexName;
375 const OUString sNewIndexNameBase(ModuleRes(STR_LOGICAL_INDEX_NAME));
376 sal_Int32 i;
378 for ( i = 1; i < 0x7FFFFFFF; ++i )
380 sNewIndexName = sNewIndexNameBase;
381 sNewIndexName += OUString::number(i);
382 if (m_pIndexes->end() == m_pIndexes->find(sNewIndexName))
383 break;
385 if (i == 0x7FFFFFFF)
387 OSL_FAIL("DbaIndexDialog::OnNewIndex: no free index name found!");
388 // can't do anything ... of course we try another base, but this could end with the same result ...
389 return;
392 SvTreeListEntry* pNewEntry = m_pIndexList->InsertEntry(sNewIndexName);
393 m_pIndexes->insert(sNewIndexName);
395 // update the user data on the entries in the list box:
396 // they're iterators of the index collection, and thus they have changed when removing the index
397 for (SvTreeListEntry* pAdjust = m_pIndexList->First(); pAdjust; pAdjust = m_pIndexList->Next(pAdjust))
399 Indexes::iterator aAfterInsertPos = m_pIndexes->find(m_pIndexList->GetEntryText(pAdjust));
400 OSL_ENSURE(aAfterInsertPos != m_pIndexes->end(), "DbaIndexDialog::OnNewIndex: problems with on of the entries!");
401 pAdjust->SetUserData(reinterpret_cast< void* >(sal_Int32(aAfterInsertPos - m_pIndexes->begin())));
404 // select the entry and start in-place editing
405 m_pIndexList->SelectNoHandlerCall(pNewEntry);
406 OnIndexSelected(m_pIndexList);
407 m_pIndexList->EditEntry(pNewEntry);
408 updateToolbox();
411 void DbaIndexDialog::OnDropIndex(bool _bConfirm)
413 // the selected index
414 SvTreeListEntry* pSelected = m_pIndexList->FirstSelected();
415 OSL_ENSURE(pSelected, "DbaIndexDialog::OnDropIndex: invalid call!");
416 if (pSelected)
418 // let the user confirm the drop
419 if (_bConfirm)
421 OUString sConfirm(ModuleRes(STR_CONFIRM_DROP_INDEX));
422 sConfirm = sConfirm.replaceFirst("$name$", m_pIndexList->GetEntryText(pSelected));
423 ScopedVclPtrInstance< MessageDialog > aConfirm(this, sConfirm, VCL_MESSAGE_QUESTION, VCL_BUTTONS_YES_NO);
424 if (RET_YES != aConfirm->Execute())
425 return;
428 // do the drop
429 implDropIndex(pSelected, true);
431 // reflect the new selection in the toolbox
432 updateToolbox();
436 bool DbaIndexDialog::implDropIndex(SvTreeListEntry* _pEntry, bool _bRemoveFromCollection)
438 // do the drop
439 Indexes::iterator aDropPos = m_pIndexes->begin() + reinterpret_cast<sal_IntPtr>(_pEntry->GetUserData());
440 OSL_ENSURE(aDropPos != m_pIndexes->end(), "DbaIndexDialog::OnDropIndex: did not find the index in my collection!");
442 SQLExceptionInfo aExceptionInfo;
443 bool bSuccess = false;
446 if (_bRemoveFromCollection)
447 bSuccess = m_pIndexes->drop(aDropPos);
448 else
449 bSuccess = m_pIndexes->dropNoRemove(aDropPos);
451 catch(SQLContext& e) { aExceptionInfo = SQLExceptionInfo(e); }
452 catch(SQLWarning& e) { aExceptionInfo = SQLExceptionInfo(e); }
453 catch(SQLException& e) { aExceptionInfo = SQLExceptionInfo(e); }
455 if (aExceptionInfo.isValid())
456 showError(aExceptionInfo, this, m_xContext);
457 else if (bSuccess && _bRemoveFromCollection)
459 SvTreeList* pModel = m_pIndexList->GetModel();
461 m_pIndexList->disableSelectHandler();
462 pModel->Remove(_pEntry);
463 m_pIndexList->enableSelectHandler();
465 // update the user data on the entries in the list box:
466 // they're iterators of the index collection, and thus they have changed when removing the index
467 for (SvTreeListEntry* pAdjust = m_pIndexList->First(); pAdjust; pAdjust = m_pIndexList->Next(pAdjust))
469 Indexes::iterator aAfterDropPos = m_pIndexes->find(m_pIndexList->GetEntryText(pAdjust));
470 OSL_ENSURE(aAfterDropPos != m_pIndexes->end(), "DbaIndexDialog::OnDropIndex: problems with on of the remaining entries!");
471 pAdjust->SetUserData(reinterpret_cast< void* >(sal_Int32(aAfterDropPos - m_pIndexes->begin())));
474 // if the remvoved entry was the selected on ...
475 if (m_pPreviousSelection == _pEntry)
476 m_pPreviousSelection = NULL;
478 // the Remove automatically selected another entry (if possible), but we disabled the calling of the handler
479 // to prevent that we missed something ... call the handler directly
480 OnIndexSelected(m_pIndexList);
483 return !aExceptionInfo.isValid();
486 void DbaIndexDialog::OnRenameIndex()
488 // the selected index
489 SvTreeListEntry* pSelected = m_pIndexList->FirstSelected();
490 OSL_ENSURE(pSelected, "DbaIndexDialog::OnRenameIndex: invalid call!");
492 // save the changes made 'til here
493 // Upon leaving the edit mode, the control will be re-initialized with the
494 // settings from the current entry
495 implSaveModified(false);
497 m_pIndexList->EditEntry(pSelected);
498 updateToolbox();
501 void DbaIndexDialog::OnSaveIndex()
503 // the selected index
504 #if OSL_DEBUG_LEVEL > 0
505 SvTreeListEntry* pSelected = m_pIndexList->FirstSelected();
506 OSL_ENSURE( pSelected, "DbaIndexDialog::OnSaveIndex: invalid call!" );
507 #endif
509 implCommitPreviouslySelected();
510 updateToolbox();
513 void DbaIndexDialog::OnResetIndex()
515 // the selected index
516 SvTreeListEntry* pSelected = m_pIndexList->FirstSelected();
517 OSL_ENSURE(pSelected, "DbaIndexDialog::OnResetIndex: invalid call!");
519 Indexes::iterator aResetPos = m_pIndexes->begin() + reinterpret_cast<sal_IntPtr>(pSelected->GetUserData());
521 if (aResetPos->isNew())
523 OnDropIndex(false);
524 return;
527 SQLExceptionInfo aExceptionInfo;
530 m_pIndexes->resetIndex(aResetPos);
532 catch(SQLContext& e) { aExceptionInfo = SQLExceptionInfo(e); }
533 catch(SQLWarning& e) { aExceptionInfo = SQLExceptionInfo(e); }
534 catch(SQLException& e) { aExceptionInfo = SQLExceptionInfo(e); }
536 if (aExceptionInfo.isValid())
537 showError(aExceptionInfo, this, m_xContext);
538 else
539 m_pIndexList->SetEntryText(pSelected, aResetPos->sName);
541 updateControls(pSelected);
542 updateToolbox();
545 IMPL_LINK_NOARG_TYPED( DbaIndexDialog, OnIndexAction, ToolBox*, void )
547 sal_uInt16 nClicked = m_pActions->GetCurItemId();
548 if (nClicked == mnNewCmdId)
549 OnNewIndex();
550 else if (nClicked == mnDropCmdId)
551 OnDropIndex();
552 else if (nClicked == mnRenameCmdId)
553 OnRenameIndex();
554 else if (nClicked == mnSaveCmdId)
555 OnSaveIndex();
556 else if (nClicked == mnResetCmdId)
557 OnResetIndex();
560 IMPL_LINK_NOARG( DbaIndexDialog, OnCloseDialog )
562 if (m_pIndexList->IsEditingActive())
564 OSL_ENSURE(!m_bEditAgain, "DbaIndexDialog::OnCloseDialog: somebody was faster than hell!");
565 // this means somebody entered a new name, which was invalid, which cause us to posted us an event,
566 // and before the event arrived the user clicked onto "close". VERY fast, this user ....
567 m_pIndexList->EndEditing(false);
568 if (m_bEditAgain)
569 // could not commit the new name (started a new - asynchronous - edit trial)
570 return 1L;
573 // the currently selected entry
574 const SvTreeListEntry* pSelected = m_pIndexList->FirstSelected();
575 OSL_ENSURE(pSelected == m_pPreviousSelection, "DbaIndexDialog::OnCloseDialog: inconsistence!");
577 sal_Int32 nResponse = RET_NO;
578 if (pSelected)
580 // the descriptor
581 Indexes::const_iterator aSelected = m_pIndexes->begin() + reinterpret_cast<sal_IntPtr>(pSelected->GetUserData());
583 if (aSelected->isModified() || aSelected->isNew())
585 ScopedVclPtrInstance<MessageDialog> aQuestion(this, "SaveIndexDialog",
586 "dbaccess/ui/saveindexdialog.ui" );
587 nResponse = aQuestion->Execute();
591 switch (nResponse)
593 case RET_YES:
594 if (!implCommitPreviouslySelected())
595 return 1L;
596 break;
597 case RET_NO:
598 break;
599 default:
600 return 1L;
603 EndDialog(RET_OK);
605 return 0L;
608 IMPL_LINK( DbaIndexDialog, OnEditIndexAgain, SvTreeListEntry*, _pEntry )
610 m_bEditAgain = false;
611 m_pIndexList->EditEntry(_pEntry);
612 return 0L;
615 IMPL_LINK( DbaIndexDialog, OnEntryEdited, SvTreeListEntry*, _pEntry )
617 Indexes::iterator aPosition = m_pIndexes->begin() + reinterpret_cast<sal_IntPtr>(_pEntry->GetUserData());
619 OSL_ENSURE(aPosition >= m_pIndexes->begin() && aPosition < m_pIndexes->end(),
620 "DbaIndexDialog::OnEntryEdited: invalid entry!");
622 OUString sNewName = m_pIndexList->GetEntryText(_pEntry);
624 Indexes::const_iterator aSameName = m_pIndexes->find(sNewName);
625 if ((aSameName != aPosition) && (m_pIndexes->end() != aSameName))
627 OUString sError(ModuleRes(STR_INDEX_NAME_ALREADY_USED));
628 sError = sError.replaceFirst("$name$", sNewName);
629 ScopedVclPtrInstance< MessageDialog > aError(this, sError);
630 aError->Execute();
632 updateToolbox();
633 m_bEditAgain = true;
634 PostUserEvent(LINK(this, DbaIndexDialog, OnEditIndexAgain), _pEntry, true);
635 return 0L;
638 aPosition->sName = sNewName;
640 // rename can be done by a drop/insert combination only
641 if (aPosition->isNew())
643 updateToolbox();
644 // no commitment needed here ....
645 return 1L;
648 if (aPosition->sName != aPosition->getOriginalName())
650 aPosition->setModified(true);
651 updateToolbox();
654 return 1L;
657 bool DbaIndexDialog::implSaveModified(bool _bPlausibility)
659 if (m_pPreviousSelection)
661 // try to commit the previously selected index
662 if (m_pFields->IsModified() && !m_pFields->SaveModified())
663 return false;
665 Indexes::iterator aPreviouslySelected = m_pIndexes->begin() + reinterpret_cast<sal_IntPtr>(m_pPreviousSelection->GetUserData());
667 // the unique flag
668 aPreviouslySelected->bUnique = m_pUnique->IsChecked();
669 if (m_pUnique->GetSavedValue() != m_pUnique->GetState())
670 aPreviouslySelected->setModified(true);
672 // the fields
673 m_pFields->commitTo(aPreviouslySelected->aFields);
674 if (m_pFields->GetSavedValue() != aPreviouslySelected->aFields)
675 aPreviouslySelected->setModified(true);
677 // plausibility checks
678 if (_bPlausibility && !implCheckPlausibility(aPreviouslySelected))
679 return false;
682 return true;
685 bool DbaIndexDialog::implCheckPlausibility(const Indexes::const_iterator& _rPos)
687 // need at least one field
688 if (0 == _rPos->aFields.size())
690 ScopedVclPtrInstance< MessageDialog > aError(this, ModuleRes(STR_NEED_INDEX_FIELDS));
691 aError->Execute();
692 m_pFields->GrabFocus();
693 return false;
696 // no double fields
697 std::set< OUString > aExistentFields;
698 for ( IndexFields::const_iterator aFieldCheck = _rPos->aFields.begin();
699 aFieldCheck != _rPos->aFields.end();
700 ++aFieldCheck
703 if (aExistentFields.end() != aExistentFields.find(aFieldCheck->sFieldName))
705 // a column is specified twice ... won't work anyway, so prevent this here and now
706 OUString sMessage(ModuleRes(STR_INDEXDESIGN_DOUBLE_COLUMN_NAME));
707 sMessage = sMessage.replaceFirst("$name$", aFieldCheck->sFieldName);
708 ScopedVclPtrInstance< MessageDialog > aError(this, sMessage);
709 aError->Execute();
710 m_pFields->GrabFocus();
711 return false;
713 aExistentFields.insert(aFieldCheck->sFieldName);
716 return true;
719 bool DbaIndexDialog::implCommitPreviouslySelected()
721 if (m_pPreviousSelection)
723 Indexes::iterator aPreviouslySelected = m_pIndexes->begin() + reinterpret_cast<sal_IntPtr>(m_pPreviousSelection->GetUserData());
725 if (!implSaveModified())
726 return false;
728 // commit the index (if necessary)
729 if (aPreviouslySelected->isModified() && !implCommit(m_pPreviousSelection))
730 return false;
733 return true;
736 IMPL_LINK_NOARG( DbaIndexDialog, OnModified )
738 OSL_ENSURE(m_pPreviousSelection, "DbaIndexDialog, OnModified: invalid call!");
739 Indexes::iterator aPosition = m_pIndexes->begin() + reinterpret_cast<sal_IntPtr>(m_pPreviousSelection->GetUserData());
741 aPosition->setModified(true);
742 updateToolbox();
744 return 1L;
747 void DbaIndexDialog::updateControls(const SvTreeListEntry* _pEntry)
749 if (_pEntry)
751 // the descriptor of the selected index
752 Indexes::const_iterator aSelectedIndex = m_pIndexes->begin() + reinterpret_cast<sal_IntPtr>(_pEntry->GetUserData());
754 // fill the controls
755 m_pUnique->Check(aSelectedIndex->bUnique);
756 m_pUnique->Enable(!aSelectedIndex->bPrimaryKey);
757 m_pUnique->SaveValue();
759 m_pFields->initializeFrom(aSelectedIndex->aFields);
760 m_pFields->Enable(!aSelectedIndex->bPrimaryKey);
761 m_pFields->SaveValue();
763 m_pDescription->SetText(aSelectedIndex->sDescription);
764 m_pDescription->Enable(!aSelectedIndex->bPrimaryKey);
766 m_pDescriptionLabel->Enable(!aSelectedIndex->bPrimaryKey);
768 else
770 m_pUnique->Check(false);
771 m_pFields->initializeFrom(IndexFields());
772 m_pDescription->SetText(OUString());
776 IMPL_LINK( DbaIndexDialog, OnIndexSelected, DbaIndexList*, /*NOTINTERESTEDIN*/ )
778 m_pIndexList->EndSelection();
780 if (m_pIndexList->IsEditingActive())
781 m_pIndexList->EndEditing(false);
783 // commit the old data
784 if (m_pIndexList->FirstSelected() != m_pPreviousSelection)
785 { // (this call may happen in case somebody ended an in-place edit with 'return', so we need to check this before committing)
786 if (!implCommitPreviouslySelected())
788 m_pIndexList->SelectNoHandlerCall(m_pPreviousSelection);
789 return 1L;
793 bool bHaveSelection = (NULL != m_pIndexList->FirstSelected());
795 // disable/enable the detail controls
796 m_pIndexDetails->Enable(bHaveSelection);
797 m_pUnique->Enable(bHaveSelection);
798 m_pDescriptionLabel->Enable(bHaveSelection);
799 m_pFieldsLabel->Enable(bHaveSelection);
800 m_pFields->Enable(bHaveSelection);
802 SvTreeListEntry* pNewSelection = m_pIndexList->FirstSelected();
803 updateControls(pNewSelection);
804 if (bHaveSelection)
805 m_pIndexList->GrabFocus();
807 m_pPreviousSelection = pNewSelection;
809 updateToolbox();
810 return 0L;
812 void DbaIndexDialog::StateChanged( StateChangedType nType )
814 ModalDialog::StateChanged( nType );
816 if ( nType == StateChangedType::ControlBackground )
818 // Check if we need to get new images for normal/high contrast mode
819 checkImageList();
821 else if ( nType == StateChangedType::Text )
823 // The physical toolbar changed its outlook and shows another logical toolbar!
824 // We have to set the correct high contrast mode on the new tbx manager.
825 // pMgr->SetHiContrast( IsHiContrastMode() );
826 checkImageList();
829 void DbaIndexDialog::DataChanged( const DataChangedEvent& rDCEvt )
831 ModalDialog::DataChanged( rDCEvt );
833 if ((( rDCEvt.GetType() == DataChangedEventType::SETTINGS ) ||
834 ( rDCEvt.GetType() == DataChangedEventType::DISPLAY )) &&
835 ( rDCEvt.GetFlags() & AllSettingsFlags::STYLE ))
837 // Check if we need to get new images for normal/high contrast mode
838 checkImageList();
842 void DbaIndexDialog::setImageList(sal_Int16 _eBitmapSet)
844 if ( _eBitmapSet == SFX_SYMBOLS_SIZE_LARGE )
846 m_pActions->SetItemImage(mnNewCmdId, maLcNewCmdImg);
847 m_pActions->SetItemImage(mnDropCmdId, maLcDropCmdImg);
848 m_pActions->SetItemImage(mnRenameCmdId, maLcRenameCmdImg);
849 m_pActions->SetItemImage(mnSaveCmdId, maLcSaveCmdImg);
850 m_pActions->SetItemImage(mnResetCmdId, maLcResetCmdImg);
852 else
854 m_pActions->SetItemImage(mnNewCmdId, maScNewCmdImg);
855 m_pActions->SetItemImage(mnDropCmdId, maScDropCmdImg);
856 m_pActions->SetItemImage(mnRenameCmdId, maScRenameCmdImg);
857 m_pActions->SetItemImage(mnSaveCmdId, maScSaveCmdImg);
858 m_pActions->SetItemImage(mnResetCmdId, maScResetCmdImg);
862 void DbaIndexDialog::resizeControls(const Size&)
866 } // namespace dbaui
868 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */