bump product version to 4.1.6.2
[LibreOffice.git] / dbaccess / source / ui / querydesign / querydlg.cxx
blob90adc949949c6b1bb048a39365965c8e959b2893
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 "querydlg.hrc"
23 #include <tools/debug.hxx>
24 #include <tools/diagnose_ex.h>
25 #include "QTableConnectionData.hxx"
26 #include "querycontroller.hxx"
27 #include "QueryTableView.hxx"
28 #include "QueryDesignView.hxx"
29 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
30 #include "RelationControl.hxx"
31 #include <vcl/msgbox.hxx>
33 using namespace dbaui;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::container;
36 using namespace ::com::sun::star::sdbc;
38 namespace dbaui
40 class OJoinControl : public Window
42 public:
43 FixedLine aFL_Join;
44 FixedText aFT_Title;
45 ListBox aLB_JoinType;
46 CheckBox m_aCBNatural;
48 OJoinControl(Window* _pParent,const ResId& _rResId);
50 OJoinControl::OJoinControl(Window* _pParent,const ResId& _rResId)
51 : Window(_pParent,_rResId)
52 ,aFL_Join( this, ResId( FL_JOIN,*_rResId.GetResMgr() ) )
53 ,aFT_Title( this, ResId(FT_LISTBOXTITLE,*_rResId.GetResMgr()) )
54 ,aLB_JoinType( this, ResId(LB_JOINTYPE,*_rResId.GetResMgr()) )
55 ,m_aCBNatural( this, ResId(CB_NATURAL,*_rResId.GetResMgr()) )
57 FreeResource();
59 // -----------------------------------------------------------------------------
60 } // dbaui
61 // -----------------------------------------------------------------------------
62 DBG_NAME(DlgQryJoin)
63 DlgQryJoin::DlgQryJoin( OQueryTableView * pParent,
64 const TTableConnectionData::value_type& _pData,
65 OJoinTableView::OTableWindowMap* _pTableMap,
66 const Reference< XConnection >& _xConnection,
67 sal_Bool _bAllowTableSelect)
68 :ModalDialog( pParent, ModuleRes(DLG_QRY_JOIN) )
69 ,aML_HelpText( this, ModuleRes(ML_HELPTEXT) )
70 ,aPB_OK( this, ModuleRes( PB_OK ) )
71 ,aPB_CANCEL( this, ModuleRes( PB_CANCEL ) )
72 ,aPB_HELP( this, ModuleRes( PB_HELP ) )
73 ,m_pJoinControl( NULL )
74 ,m_pTableControl( NULL )
75 ,m_pTableMap(_pTableMap)
76 ,m_pTableView(pParent)
77 ,eJoinType(static_cast<OQueryTableConnectionData*>(_pData.get())->GetJoinType())
78 ,m_pOrigConnData(_pData)
79 ,m_xConnection(_xConnection)
81 DBG_CTOR(DlgQryJoin,NULL);
83 aML_HelpText.SetControlBackground( GetSettings().GetStyleSettings().GetFaceColor() );
84 //////////////////////////////////////////////////////////////////////
85 // Connection kopieren
86 m_pConnData.reset(_pData->NewInstance());
87 m_pConnData->CopyFrom(*_pData);
89 m_pTableControl = new OTableListBoxControl(this,ModuleRes(WND_CONTROL),m_pTableMap,this);
91 m_pJoinControl = new OJoinControl(m_pTableControl,ModuleRes(WND_JOIN_CONTROL));
93 m_pJoinControl->Show();
94 m_pJoinControl->m_aCBNatural.Check(static_cast<OQueryTableConnectionData*>(m_pConnData.get())->isNatural());
95 m_pTableControl->Show();
97 if( _bAllowTableSelect )
99 m_pTableControl->Init( m_pConnData );
100 m_pTableControl->fillListBoxes();
102 else
104 m_pTableControl->fillAndDisable(m_pConnData);
105 m_pTableControl->Init( m_pConnData );
108 m_pTableControl->lateUIInit(m_pJoinControl);
110 sal_Bool bSupportFullJoin = sal_False;
111 Reference<XDatabaseMetaData> xMeta;
114 xMeta = m_xConnection->getMetaData();
115 if ( xMeta.is() )
116 bSupportFullJoin = xMeta->supportsFullOuterJoins();
118 catch(SQLException&)
121 sal_Bool bSupportOuterJoin = sal_False;
124 if ( xMeta.is() )
125 bSupportOuterJoin= xMeta->supportsOuterJoins();
127 catch(SQLException&)
131 setJoinType(eJoinType);
133 aPB_OK.SetClickHdl( LINK(this, DlgQryJoin, OKClickHdl) );
135 m_pJoinControl->aLB_JoinType.SetSelectHdl(LINK(this,DlgQryJoin,LBChangeHdl));
136 m_pJoinControl->m_aCBNatural.SetToggleHdl(LINK(this,DlgQryJoin,NaturalToggleHdl));
138 if ( static_cast<OQueryTableView*>(pParent)->getDesignView()->getController().isReadOnly() )
140 m_pJoinControl->aLB_JoinType.Disable();
141 m_pJoinControl->m_aCBNatural.Disable();
142 m_pTableControl->Disable();
144 else
146 const sal_uInt16 nCount = m_pJoinControl->aLB_JoinType.GetEntryCount();
147 for (sal_uInt16 i = 0; i < nCount; ++i)
149 const long nJoinTyp = reinterpret_cast<long>(m_pJoinControl->aLB_JoinType.GetEntryData(i));
150 if ( !bSupportFullJoin && nJoinTyp == ID_FULL_JOIN )
151 m_pJoinControl->aLB_JoinType.RemoveEntry(i);
152 else if ( !bSupportOuterJoin && (nJoinTyp == ID_LEFT_JOIN || nJoinTyp == ID_RIGHT_JOIN) )
153 m_pJoinControl->aLB_JoinType.RemoveEntry(i);
156 m_pTableControl->NotifyCellChange();
157 m_pTableControl->enableRelation(!static_cast<OQueryTableConnectionData*>(m_pConnData.get())->isNatural() && eJoinType != CROSS_JOIN );
160 FreeResource();
163 //------------------------------------------------------------------------
164 DlgQryJoin::~DlgQryJoin()
166 DBG_DTOR(DlgQryJoin,NULL);
167 delete m_pJoinControl;
168 delete m_pTableControl;
170 // -----------------------------------------------------------------------------
171 IMPL_LINK( DlgQryJoin, LBChangeHdl, ListBox*, /*pListBox*/ )
173 DBG_CHKTHIS(DlgQryJoin,NULL);
174 if (m_pJoinControl->aLB_JoinType.GetSelectEntryPos() == m_pJoinControl->aLB_JoinType.GetSavedValue() )
175 return 1;
177 m_pJoinControl->aLB_JoinType.SaveValue();
178 aML_HelpText.SetText(String());
180 m_pTableControl->enableRelation(true);
182 String sFirstWinName = m_pConnData->getReferencingTable()->GetWinName();
183 String sSecondWinName = m_pConnData->getReferencedTable()->GetWinName();
184 const EJoinType eOldJoinType = eJoinType;
185 sal_uInt16 nResId = 0;
186 const sal_uInt16 nPos = m_pJoinControl->aLB_JoinType.GetSelectEntryPos();
187 const long nJoinType = reinterpret_cast<long>(m_pJoinControl->aLB_JoinType.GetEntryData(nPos));
188 sal_Bool bAddHint = sal_True;
189 switch ( nJoinType )
191 default:
192 case ID_INNER_JOIN:
193 nResId = STR_QUERY_INNER_JOIN;
194 bAddHint = sal_False;
195 eJoinType = INNER_JOIN;
196 break;
197 case ID_LEFT_JOIN:
198 nResId = STR_QUERY_LEFTRIGHT_JOIN;
199 eJoinType = LEFT_JOIN;
200 break;
201 case ID_RIGHT_JOIN:
203 nResId = STR_QUERY_LEFTRIGHT_JOIN;
204 eJoinType = RIGHT_JOIN;
205 String sTemp = sFirstWinName;
206 sFirstWinName = sSecondWinName;
207 sSecondWinName = sTemp;
209 break;
210 case ID_FULL_JOIN:
211 nResId = STR_QUERY_FULL_JOIN;
212 eJoinType = FULL_JOIN;
213 break;
214 case ID_CROSS_JOIN:
216 nResId = STR_QUERY_CROSS_JOIN;
217 eJoinType = CROSS_JOIN;
219 m_pConnData->ResetConnLines();
220 m_pTableControl->lateInit();
221 m_pJoinControl->m_aCBNatural.Check(sal_False);
222 m_pTableControl->enableRelation(false);
223 OUString sEmpty;
224 m_pConnData->AppendConnLine(sEmpty,sEmpty);
225 aPB_OK.Enable(sal_True);
227 break;
230 m_pJoinControl->m_aCBNatural.Enable(eJoinType != CROSS_JOIN);
232 if ( eJoinType != eOldJoinType && eOldJoinType == CROSS_JOIN )
234 m_pConnData->ResetConnLines();
236 if ( eJoinType != CROSS_JOIN )
238 m_pTableControl->NotifyCellChange();
239 NaturalToggleHdl(&m_pJoinControl->m_aCBNatural);
242 m_pTableControl->Invalidate();
244 String sHelpText = String( ModuleRes( nResId ) );
245 if( nPos )
247 sHelpText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%1" ) ), sFirstWinName );
248 sHelpText.SearchAndReplace( String( RTL_CONSTASCII_USTRINGPARAM( "%2" ) ), sSecondWinName );
250 if ( bAddHint )
252 sHelpText += String( RTL_CONSTASCII_USTRINGPARAM( "\n" ) );
253 sHelpText += String( ModuleRes( STR_JOIN_TYPE_HINT ) );
256 aML_HelpText.SetText( sHelpText );
257 return 1;
259 // -----------------------------------------------------------------------------
261 IMPL_LINK( DlgQryJoin, OKClickHdl, Button*, /*pButton*/ )
263 DBG_CHKTHIS(DlgQryJoin,NULL);
265 m_pConnData->Update();
266 m_pOrigConnData->CopyFrom( *m_pConnData );
268 EndDialog(RET_OK);
269 return 1;
271 // -----------------------------------------------------------------------------
273 IMPL_LINK( DlgQryJoin, NaturalToggleHdl, CheckBox*, /*pButton*/ )
275 DBG_CHKTHIS(DlgQryJoin,NULL);
276 sal_Bool bChecked = m_pJoinControl->m_aCBNatural.IsChecked();
277 static_cast<OQueryTableConnectionData*>(m_pConnData.get())->setNatural(bChecked);
278 m_pTableControl->enableRelation(!bChecked);
279 if ( bChecked )
281 m_pConnData->ResetConnLines();
284 Reference<XNameAccess> xReferencedTableColumns(m_pConnData->getReferencedTable()->getColumns());
285 Sequence< OUString> aSeq = m_pConnData->getReferencingTable()->getColumns()->getElementNames();
286 const OUString* pIter = aSeq.getConstArray();
287 const OUString* pEnd = pIter + aSeq.getLength();
288 for(;pIter != pEnd;++pIter)
290 if ( xReferencedTableColumns->hasByName(*pIter) )
291 m_pConnData->AppendConnLine(*pIter,*pIter);
294 catch( const Exception& )
296 DBG_UNHANDLED_EXCEPTION();
298 m_pTableControl->NotifyCellChange();
299 m_pTableControl->Invalidate();
302 return 1;
304 // -----------------------------------------------------------------------------
305 TTableConnectionData::value_type DlgQryJoin::getConnectionData() const
307 return m_pConnData;
309 // -----------------------------------------------------------------------------
310 void DlgQryJoin::setValid(sal_Bool _bValid)
312 aPB_OK.Enable(_bValid || eJoinType == CROSS_JOIN );
314 // -----------------------------------------------------------------------------
315 void DlgQryJoin::notifyConnectionChange( )
317 setJoinType( static_cast<OQueryTableConnectionData*>(m_pConnData.get())->GetJoinType() );
318 m_pJoinControl->m_aCBNatural.Check(static_cast<OQueryTableConnectionData*>(m_pConnData.get())->isNatural());
319 NaturalToggleHdl(&m_pJoinControl->m_aCBNatural);
321 // -----------------------------------------------------------------------------
322 void DlgQryJoin::setJoinType(EJoinType _eNewJoinType)
324 eJoinType = _eNewJoinType;
325 m_pJoinControl->m_aCBNatural.Enable(eJoinType != CROSS_JOIN);
327 long nJoinType = 0;
328 switch ( eJoinType )
330 default:
331 case INNER_JOIN:
332 nJoinType = ID_INNER_JOIN;
333 break;
334 case LEFT_JOIN:
335 nJoinType = ID_LEFT_JOIN;
336 break;
337 case RIGHT_JOIN:
338 nJoinType = ID_RIGHT_JOIN;
339 break;
340 case FULL_JOIN:
341 nJoinType = ID_FULL_JOIN;
342 break;
343 case CROSS_JOIN:
344 nJoinType = ID_CROSS_JOIN;
345 break;
348 const sal_uInt16 nCount = m_pJoinControl->aLB_JoinType.GetEntryCount();
349 for (sal_uInt16 i = 0; i < nCount; ++i)
351 if ( nJoinType == reinterpret_cast<long>(m_pJoinControl->aLB_JoinType.GetEntryData(i)) )
353 m_pJoinControl->aLB_JoinType.SelectEntryPos(i);
354 break;
358 LBChangeHdl(&m_pJoinControl->aLB_JoinType);
360 // -----------------------------------------------------------------------------
364 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */