Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / dbaccess / source / ui / querydesign / querydlg.cxx
blob48b21957440193a57743124c2448497254ac3130
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 "querydlg.hxx"
21 #include "dbu_qry.hrc"
22 #include <tools/debug.hxx>
23 #include <tools/diagnose_ex.h>
24 #include "QTableConnectionData.hxx"
25 #include "querycontroller.hxx"
26 #include "QueryTableView.hxx"
27 #include "QueryDesignView.hxx"
28 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
29 #include "RelationControl.hxx"
30 #include <vcl/msgbox.hxx>
31 #include <vcl/settings.hxx>
33 #define ID_INNER_JOIN 1
34 #define ID_LEFT_JOIN 2
35 #define ID_RIGHT_JOIN 3
36 #define ID_FULL_JOIN 4
37 #define ID_CROSS_JOIN 5
39 using namespace dbaui;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::container;
42 using namespace ::com::sun::star::sdbc;
44 DlgQryJoin::DlgQryJoin( OQueryTableView * pParent,
45 const TTableConnectionData::value_type& _pData,
46 OJoinTableView::OTableWindowMap* _pTableMap,
47 const Reference< XConnection >& _xConnection,
48 bool _bAllowTableSelect)
49 : ModalDialog( pParent, "JoinDialog", "dbaccess/ui/joindialog.ui" )
50 , m_pTableControl( nullptr )
51 , m_pTableMap(_pTableMap)
52 , m_pTableView(pParent)
53 , eJoinType(static_cast<OQueryTableConnectionData*>(_pData.get())->GetJoinType())
54 , m_pOrigConnData(_pData)
55 , m_xConnection(_xConnection)
57 get(m_pML_HelpText, "helptext");
58 Size aSize(LogicToPixel(Size(179, 49), MAP_APPFONT));
59 //alternatively loop through the STR_QUERY_* strings with their STR_JOIN_TYPE_HINT
60 //suffix to find the longest entry at runtime
61 m_pML_HelpText->set_height_request(aSize.Height());
62 m_pML_HelpText->set_width_request(aSize.Width());
63 get(m_pLB_JoinType, "type");
64 get(m_pCBNatural, "natural");
65 get(m_pPB_OK, "ok");
67 m_pML_HelpText->SetControlBackground( GetSettings().GetStyleSettings().GetFaceColor() );
68 // Connection kopieren
69 m_pConnData.reset(_pData->NewInstance());
70 m_pConnData->CopyFrom(*_pData);
72 m_pTableControl = new OTableListBoxControl(this, m_pTableMap, this);
74 m_pCBNatural->Check(static_cast<OQueryTableConnectionData*>(m_pConnData.get())->isNatural());
76 if( _bAllowTableSelect )
78 m_pTableControl->Init( m_pConnData );
79 m_pTableControl->fillListBoxes();
81 else
83 m_pTableControl->fillAndDisable(m_pConnData);
84 m_pTableControl->Init( m_pConnData );
87 m_pTableControl->lateUIInit();
89 bool bSupportFullJoin = false;
90 Reference<XDatabaseMetaData> xMeta;
91 try
93 xMeta = m_xConnection->getMetaData();
94 if ( xMeta.is() )
95 bSupportFullJoin = xMeta->supportsFullOuterJoins();
97 catch(SQLException&)
100 bool bSupportOuterJoin = false;
103 if ( xMeta.is() )
104 bSupportOuterJoin= xMeta->supportsOuterJoins();
106 catch(SQLException&)
110 setJoinType(eJoinType);
112 m_pPB_OK->SetClickHdl( LINK(this, DlgQryJoin, OKClickHdl) );
114 m_pLB_JoinType->SetSelectHdl(LINK(this,DlgQryJoin,LBChangeHdl));
115 m_pCBNatural->SetToggleHdl(LINK(this,DlgQryJoin,NaturalToggleHdl));
117 if ( static_cast<OQueryTableView*>(pParent)->getDesignView()->getController().isReadOnly() )
119 m_pLB_JoinType->Disable();
120 m_pCBNatural->Disable();
121 m_pTableControl->Disable();
123 else
125 for (sal_Int32 i = 0; i < m_pLB_JoinType->GetEntryCount();)
127 const sal_IntPtr nJoinTyp = reinterpret_cast<sal_IntPtr>(m_pLB_JoinType->GetEntryData(i));
128 if ( !bSupportFullJoin && nJoinTyp == ID_FULL_JOIN )
129 m_pLB_JoinType->RemoveEntry(i);
130 else if ( !bSupportOuterJoin && (nJoinTyp == ID_LEFT_JOIN || nJoinTyp == ID_RIGHT_JOIN) )
131 m_pLB_JoinType->RemoveEntry(i);
132 else
133 ++i;
136 m_pTableControl->NotifyCellChange();
137 m_pTableControl->enableRelation(!static_cast<OQueryTableConnectionData*>(m_pConnData.get())->isNatural() && eJoinType != CROSS_JOIN );
141 DlgQryJoin::~DlgQryJoin()
143 disposeOnce();
146 void DlgQryJoin::dispose()
148 delete m_pTableControl;
149 m_pML_HelpText.clear();
150 m_pPB_OK.clear();
151 m_pLB_JoinType.clear();
152 m_pCBNatural.clear();
153 m_pTableView.clear();
154 ModalDialog::dispose();
157 IMPL_LINK_NOARG_TYPED( DlgQryJoin, LBChangeHdl, ListBox&, void )
159 if (m_pLB_JoinType->GetSelectEntryPos() == m_pLB_JoinType->GetSavedValue() )
160 return;
162 m_pLB_JoinType->SaveValue();
163 m_pML_HelpText->SetText(OUString());
165 m_pTableControl->enableRelation(true);
167 OUString sFirstWinName = m_pConnData->getReferencingTable()->GetWinName();
168 OUString sSecondWinName = m_pConnData->getReferencedTable()->GetWinName();
169 const EJoinType eOldJoinType = eJoinType;
170 sal_uInt16 nResId = 0;
171 const sal_Int32 nPos = m_pLB_JoinType->GetSelectEntryPos();
172 const sal_IntPtr nJoinType = reinterpret_cast<sal_IntPtr>(m_pLB_JoinType->GetEntryData(nPos));
173 bool bAddHint = true;
174 switch ( nJoinType )
176 default:
177 case ID_INNER_JOIN:
178 nResId = STR_QUERY_INNER_JOIN;
179 bAddHint = false;
180 eJoinType = INNER_JOIN;
181 break;
182 case ID_LEFT_JOIN:
183 nResId = STR_QUERY_LEFTRIGHT_JOIN;
184 eJoinType = LEFT_JOIN;
185 break;
186 case ID_RIGHT_JOIN:
188 nResId = STR_QUERY_LEFTRIGHT_JOIN;
189 eJoinType = RIGHT_JOIN;
190 OUString sTemp = sFirstWinName;
191 sFirstWinName = sSecondWinName;
192 sSecondWinName = sTemp;
194 break;
195 case ID_FULL_JOIN:
196 nResId = STR_QUERY_FULL_JOIN;
197 eJoinType = FULL_JOIN;
198 break;
199 case ID_CROSS_JOIN:
201 nResId = STR_QUERY_CROSS_JOIN;
202 eJoinType = CROSS_JOIN;
204 m_pConnData->ResetConnLines();
205 m_pTableControl->lateInit();
206 m_pCBNatural->Check(false);
207 m_pTableControl->enableRelation(false);
208 m_pConnData->AppendConnLine("","");
209 m_pPB_OK->Enable();
211 break;
214 m_pCBNatural->Enable(eJoinType != CROSS_JOIN);
216 if ( eJoinType != eOldJoinType && eOldJoinType == CROSS_JOIN )
218 m_pConnData->ResetConnLines();
220 if ( eJoinType != CROSS_JOIN )
222 m_pTableControl->NotifyCellChange();
223 NaturalToggleHdl(*m_pCBNatural);
226 m_pTableControl->Invalidate();
228 OUString sHelpText = ModuleRes( nResId );
229 if( nPos )
231 sHelpText = sHelpText.replaceFirst( "%1", sFirstWinName );
232 sHelpText = sHelpText.replaceFirst( "%2", sSecondWinName );
234 if ( bAddHint )
236 sHelpText += "\n";
237 sHelpText += ModuleRes( STR_JOIN_TYPE_HINT );
240 m_pML_HelpText->SetText( sHelpText );
243 IMPL_LINK_NOARG_TYPED( DlgQryJoin, OKClickHdl, Button*, void )
245 m_pConnData->Update();
246 m_pOrigConnData->CopyFrom( *m_pConnData );
248 EndDialog(RET_OK);
251 IMPL_LINK_NOARG_TYPED( DlgQryJoin, NaturalToggleHdl, CheckBox&, void )
253 bool bChecked = m_pCBNatural->IsChecked();
254 static_cast<OQueryTableConnectionData*>(m_pConnData.get())->setNatural(bChecked);
255 m_pTableControl->enableRelation(!bChecked);
256 if ( bChecked )
258 m_pConnData->ResetConnLines();
261 Reference<XNameAccess> xReferencedTableColumns(m_pConnData->getReferencedTable()->getColumns());
262 Sequence< OUString> aSeq = m_pConnData->getReferencingTable()->getColumns()->getElementNames();
263 const OUString* pIter = aSeq.getConstArray();
264 const OUString* pEnd = pIter + aSeq.getLength();
265 for(;pIter != pEnd;++pIter)
267 if ( xReferencedTableColumns->hasByName(*pIter) )
268 m_pConnData->AppendConnLine(*pIter,*pIter);
271 catch( const Exception& )
273 DBG_UNHANDLED_EXCEPTION();
275 m_pTableControl->NotifyCellChange();
276 m_pTableControl->Invalidate();
280 void DlgQryJoin::setValid(bool _bValid)
282 m_pPB_OK->Enable(_bValid || eJoinType == CROSS_JOIN );
285 void DlgQryJoin::notifyConnectionChange( )
287 setJoinType( static_cast<OQueryTableConnectionData*>(m_pConnData.get())->GetJoinType() );
288 m_pCBNatural->Check(static_cast<OQueryTableConnectionData*>(m_pConnData.get())->isNatural());
289 NaturalToggleHdl(*m_pCBNatural);
292 void DlgQryJoin::setJoinType(EJoinType _eNewJoinType)
294 eJoinType = _eNewJoinType;
295 m_pCBNatural->Enable(eJoinType != CROSS_JOIN);
297 sal_IntPtr nJoinType = 0;
298 switch ( eJoinType )
300 default:
301 case INNER_JOIN:
302 nJoinType = ID_INNER_JOIN;
303 break;
304 case LEFT_JOIN:
305 nJoinType = ID_LEFT_JOIN;
306 break;
307 case RIGHT_JOIN:
308 nJoinType = ID_RIGHT_JOIN;
309 break;
310 case FULL_JOIN:
311 nJoinType = ID_FULL_JOIN;
312 break;
313 case CROSS_JOIN:
314 nJoinType = ID_CROSS_JOIN;
315 break;
318 const sal_Int32 nCount = m_pLB_JoinType->GetEntryCount();
319 for (sal_Int32 i = 0; i < nCount; ++i)
321 if ( nJoinType == reinterpret_cast<sal_IntPtr>(m_pLB_JoinType->GetEntryData(i)) )
323 m_pLB_JoinType->SelectEntryPos(i);
324 break;
328 LBChangeHdl(*m_pLB_JoinType);
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */