merge the formfield patch from ooo-build
[ooovba.git] / dbaccess / source / ui / querydesign / querydlg.cxx
blob45adcb828a0b07cc27711522c167df776ee47b1f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: querydlg.cxx,v $
10 * $Revision: 1.19.24.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
33 #ifndef DBAUI_QUERYDLG_HXX
34 #include "querydlg.hxx"
35 #endif
36 #ifndef _DBU_QRY_HRC_
37 #include "dbu_qry.hrc"
38 #endif
39 #ifndef DBAUI_QUERYDLG_HRC
40 #include "querydlg.hrc"
41 #endif
42 #ifndef _TOOLS_DEBUG_HXX
43 #include <tools/debug.hxx>
44 #endif
45 #ifndef TOOLS_DIAGNOSE_EX_H
46 #include <tools/diagnose_ex.h>
47 #endif
48 #ifndef DBAUI_QTABLECONNECTIONDATA_HXX
49 #include "QTableConnectionData.hxx"
50 #endif
51 #ifndef DBAUI_QUERYCONTROLLER_HXX
52 #include "querycontroller.hxx"
53 #endif
54 #ifndef DBAUI_QUERYTABLEVIEW_HXX
55 #include "QueryTableView.hxx"
56 #endif
57 #ifndef DBAUI_QUERYDESIGNVIEW_HXX
58 #include "QueryDesignView.hxx"
59 #endif
60 #ifndef _COM_SUN_STAR_SDBC_XDATABASEMETADATA_HPP_
61 #include <com/sun/star/sdbc/XDatabaseMetaData.hpp>
62 #endif
63 #ifndef DBAUI_RELATIONCONTROL_HXX
64 #include "RelationControl.hxx"
65 #endif
66 #ifndef _SV_MSGBOX_HXX
67 #include <vcl/msgbox.hxx>
68 #endif
70 using namespace dbaui;
71 using namespace ::com::sun::star::uno;
72 using namespace ::com::sun::star::container;
73 using namespace ::com::sun::star::sdbc;
75 namespace dbaui
77 class OJoinControl : public Window
79 public:
80 FixedLine aFL_Join;
81 FixedText aFT_Title;
82 ListBox aLB_JoinType;
83 CheckBox m_aCBNatural;
85 OJoinControl(Window* _pParent,const ResId& _rResId);
87 OJoinControl::OJoinControl(Window* _pParent,const ResId& _rResId)
88 : Window(_pParent,_rResId)
89 ,aFL_Join( this, ResId( FL_JOIN,*_rResId.GetResMgr() ) )
90 ,aFT_Title( this, ResId(FT_LISTBOXTITLE,*_rResId.GetResMgr()) )
91 ,aLB_JoinType( this, ResId(LB_JOINTYPE,*_rResId.GetResMgr()) )
92 ,m_aCBNatural( this, ResId(CB_NATURAL,*_rResId.GetResMgr()) )
94 FreeResource();
96 // -----------------------------------------------------------------------------
97 } // dbaui
98 // -----------------------------------------------------------------------------
99 DBG_NAME(DlgQryJoin)
100 DlgQryJoin::DlgQryJoin( OQueryTableView * pParent,
101 const TTableConnectionData::value_type& _pData,
102 OJoinTableView::OTableWindowMap* _pTableMap,
103 const Reference< XConnection >& _xConnection,
104 BOOL _bAllowTableSelect)
105 :ModalDialog( pParent, ModuleRes(DLG_QRY_JOIN) )
106 ,aML_HelpText( this, ModuleRes(ML_HELPTEXT) )
107 ,aPB_OK( this, ModuleRes( PB_OK ) )
108 ,aPB_CANCEL( this, ModuleRes( PB_CANCEL ) )
109 ,aPB_HELP( this, ModuleRes( PB_HELP ) )
110 ,m_pJoinControl( NULL )
111 ,m_pTableControl( NULL )
112 ,m_pTableMap(_pTableMap)
113 ,m_pTableView(pParent)
114 ,eJoinType(static_cast<OQueryTableConnectionData*>(_pData.get())->GetJoinType())
115 ,m_pOrigConnData(_pData)
116 ,m_xConnection(_xConnection)
118 DBG_CTOR(DlgQryJoin,NULL);
120 aML_HelpText.SetControlBackground( GetSettings().GetStyleSettings().GetFaceColor() );
121 //////////////////////////////////////////////////////////////////////
122 // Connection kopieren
123 m_pConnData.reset(_pData->NewInstance());
124 m_pConnData->CopyFrom(*_pData);
126 m_pTableControl = new OTableListBoxControl(this,ModuleRes(WND_CONTROL),m_pTableMap,this);
128 m_pJoinControl = new OJoinControl(m_pTableControl,ModuleRes(WND_JOIN_CONTROL));
130 m_pJoinControl->Show();
131 m_pJoinControl->m_aCBNatural.Check(static_cast<OQueryTableConnectionData*>(m_pConnData.get())->isNatural());
132 m_pTableControl->Show();
134 if( _bAllowTableSelect )
136 m_pTableControl->Init( m_pConnData );
137 m_pTableControl->fillListBoxes();
139 else
141 m_pTableControl->fillAndDisable(m_pConnData);
142 m_pTableControl->Init( m_pConnData );
145 m_pTableControl->lateUIInit(m_pJoinControl);
147 sal_Bool bSupportFullJoin = sal_False;
148 Reference<XDatabaseMetaData> xMeta;
151 xMeta = m_xConnection->getMetaData();
152 if ( xMeta.is() )
153 bSupportFullJoin = xMeta->supportsFullOuterJoins();
155 catch(SQLException&)
158 sal_Bool bSupportOuterJoin = sal_False;
161 if ( xMeta.is() )
162 bSupportOuterJoin= xMeta->supportsOuterJoins();
164 catch(SQLException&)
168 setJoinType(eJoinType);
170 aPB_OK.SetClickHdl( LINK(this, DlgQryJoin, OKClickHdl) );
172 m_pJoinControl->aLB_JoinType.SetSelectHdl(LINK(this,DlgQryJoin,LBChangeHdl));
173 m_pJoinControl->m_aCBNatural.SetToggleHdl(LINK(this,DlgQryJoin,NaturalToggleHdl));
175 if ( static_cast<OQueryTableView*>(pParent)->getDesignView()->getController().isReadOnly() )
177 m_pJoinControl->aLB_JoinType.Disable();
178 m_pJoinControl->m_aCBNatural.Disable();
179 m_pTableControl->Disable();
181 else
183 const USHORT nCount = m_pJoinControl->aLB_JoinType.GetEntryCount();
184 for (USHORT i = 0; i < nCount; ++i)
186 const long nJoinTyp = reinterpret_cast<long>(m_pJoinControl->aLB_JoinType.GetEntryData(i));
187 if ( !bSupportFullJoin && nJoinTyp == ID_FULL_JOIN )
188 m_pJoinControl->aLB_JoinType.RemoveEntry(i);
189 else if ( !bSupportOuterJoin && (nJoinTyp == ID_LEFT_JOIN || nJoinTyp == ID_RIGHT_JOIN) )
190 m_pJoinControl->aLB_JoinType.RemoveEntry(i);
193 m_pTableControl->NotifyCellChange();
194 m_pTableControl->enableRelation(!static_cast<OQueryTableConnectionData*>(m_pConnData.get())->isNatural() && eJoinType != CROSS_JOIN );
197 FreeResource();
200 //------------------------------------------------------------------------
201 DlgQryJoin::~DlgQryJoin()
203 DBG_DTOR(DlgQryJoin,NULL);
204 delete m_pJoinControl;
205 delete m_pTableControl;
207 // -----------------------------------------------------------------------------
208 IMPL_LINK( DlgQryJoin, LBChangeHdl, ListBox*, /*pListBox*/ )
210 DBG_CHKTHIS(DlgQryJoin,NULL);
211 if (m_pJoinControl->aLB_JoinType.GetSelectEntryPos() == m_pJoinControl->aLB_JoinType.GetSavedValue() )
212 return 1;
214 m_pJoinControl->aLB_JoinType.SaveValue();
215 aML_HelpText.SetText(String());
217 m_pTableControl->enableRelation(true);
219 String sFirstWinName = m_pConnData->getReferencingTable()->GetWinName();
220 String sSecondWinName = m_pConnData->getReferencedTable()->GetWinName();
221 const EJoinType eOldJoinType = eJoinType;
222 USHORT nResId = 0;
223 const USHORT nPos = m_pJoinControl->aLB_JoinType.GetSelectEntryPos();
224 const long nJoinType = reinterpret_cast<long>(m_pJoinControl->aLB_JoinType.GetEntryData(nPos));
225 sal_Bool bAddHint = sal_True;
226 switch ( nJoinType )
228 default:
229 case ID_INNER_JOIN:
230 nResId = STR_QUERY_INNER_JOIN;
231 bAddHint = sal_False;
232 eJoinType = INNER_JOIN;
233 break;
234 case ID_LEFT_JOIN:
235 nResId = STR_QUERY_LEFTRIGHT_JOIN;
236 eJoinType = LEFT_JOIN;
237 break;
238 case ID_RIGHT_JOIN:
240 nResId = STR_QUERY_LEFTRIGHT_JOIN;
241 eJoinType = RIGHT_JOIN;
242 String sTemp = sFirstWinName;
243 sFirstWinName = sSecondWinName;
244 sSecondWinName = sTemp;
246 break;
247 case ID_FULL_JOIN:
248 nResId = STR_QUERY_FULL_JOIN;
249 eJoinType = FULL_JOIN;
250 break;
251 case ID_CROSS_JOIN:
253 nResId = STR_QUERY_CROSS_JOIN;
254 eJoinType = CROSS_JOIN;
256 m_pConnData->ResetConnLines();
257 m_pTableControl->lateInit();
258 m_pJoinControl->m_aCBNatural.Check(FALSE);
259 m_pTableControl->enableRelation(false);
260 ::rtl::OUString sEmpty;
261 m_pConnData->AppendConnLine(sEmpty,sEmpty);
262 aPB_OK.Enable(TRUE);
264 break;
267 m_pJoinControl->m_aCBNatural.Enable(eJoinType != CROSS_JOIN);
269 if ( eJoinType != eOldJoinType && eOldJoinType == CROSS_JOIN )
271 m_pConnData->ResetConnLines();
273 if ( eJoinType != CROSS_JOIN )
275 m_pTableControl->NotifyCellChange();
276 NaturalToggleHdl(&m_pJoinControl->m_aCBNatural);
279 m_pTableControl->Invalidate();
281 String sHelpText = String( ModuleRes( nResId ) );
282 if( nPos )
284 sHelpText.SearchAndReplace( String( RTL_CONSTASCII_STRINGPARAM( "%1" ) ), sFirstWinName );
285 sHelpText.SearchAndReplace( String( RTL_CONSTASCII_STRINGPARAM( "%2" ) ), sSecondWinName );
287 if ( bAddHint )
289 sHelpText += String( RTL_CONSTASCII_STRINGPARAM( "\n" ) );
290 sHelpText += String( ModuleRes( STR_JOIN_TYPE_HINT ) );
293 aML_HelpText.SetText( sHelpText );
294 return 1;
296 // -----------------------------------------------------------------------------
298 IMPL_LINK( DlgQryJoin, OKClickHdl, Button*, /*pButton*/ )
300 DBG_CHKTHIS(DlgQryJoin,NULL);
302 m_pConnData->Update();
303 m_pOrigConnData->CopyFrom( *m_pConnData );
305 EndDialog(RET_OK);
306 return 1;
308 // -----------------------------------------------------------------------------
310 IMPL_LINK( DlgQryJoin, NaturalToggleHdl, CheckBox*, /*pButton*/ )
312 DBG_CHKTHIS(DlgQryJoin,NULL);
313 BOOL bChecked = m_pJoinControl->m_aCBNatural.IsChecked();
314 static_cast<OQueryTableConnectionData*>(m_pConnData.get())->setNatural(bChecked);
315 m_pTableControl->enableRelation(!bChecked);
316 if ( bChecked )
318 m_pConnData->ResetConnLines();
321 Reference<XNameAccess> xReferencedTableColumns(m_pConnData->getReferencedTable()->getColumns());
322 Sequence< ::rtl::OUString> aSeq = m_pConnData->getReferencingTable()->getColumns()->getElementNames();
323 const ::rtl::OUString* pIter = aSeq.getConstArray();
324 const ::rtl::OUString* pEnd = pIter + aSeq.getLength();
325 for(;pIter != pEnd;++pIter)
327 if ( xReferencedTableColumns->hasByName(*pIter) )
328 m_pConnData->AppendConnLine(*pIter,*pIter);
331 catch( const Exception& )
333 DBG_UNHANDLED_EXCEPTION();
335 m_pTableControl->NotifyCellChange();
336 m_pTableControl->Invalidate();
339 return 1;
341 // -----------------------------------------------------------------------------
342 TTableConnectionData::value_type DlgQryJoin::getConnectionData() const
344 return m_pConnData;
346 // -----------------------------------------------------------------------------
347 void DlgQryJoin::setValid(sal_Bool _bValid)
349 //LBChangeHdl(&aLB_JoinType);
351 aPB_OK.Enable(_bValid || eJoinType == CROSS_JOIN );
353 // -----------------------------------------------------------------------------
354 void DlgQryJoin::notifyConnectionChange( )
356 setJoinType( static_cast<OQueryTableConnectionData*>(m_pConnData.get())->GetJoinType() );
357 m_pJoinControl->m_aCBNatural.Check(static_cast<OQueryTableConnectionData*>(m_pConnData.get())->isNatural());
358 NaturalToggleHdl(&m_pJoinControl->m_aCBNatural);
360 // -----------------------------------------------------------------------------
361 void DlgQryJoin::setJoinType(EJoinType _eNewJoinType)
363 eJoinType = _eNewJoinType;
364 m_pJoinControl->m_aCBNatural.Enable(eJoinType != CROSS_JOIN);
366 long nJoinType = 0;
367 switch ( eJoinType )
369 default:
370 case INNER_JOIN:
371 nJoinType = ID_INNER_JOIN;
372 break;
373 case LEFT_JOIN:
374 nJoinType = ID_LEFT_JOIN;
375 break;
376 case RIGHT_JOIN:
377 nJoinType = ID_RIGHT_JOIN;
378 break;
379 case FULL_JOIN:
380 nJoinType = ID_FULL_JOIN;
381 break;
382 case CROSS_JOIN:
383 nJoinType = ID_CROSS_JOIN;
384 break;
387 const USHORT nCount = m_pJoinControl->aLB_JoinType.GetEntryCount();
388 for (USHORT i = 0; i < nCount; ++i)
390 if ( nJoinType == reinterpret_cast<long>(m_pJoinControl->aLB_JoinType.GetEntryData(i)) )
392 m_pJoinControl->aLB_JoinType.SelectEntryPos(i);
393 break;
397 LBChangeHdl(&m_pJoinControl->aLB_JoinType);
399 // -----------------------------------------------------------------------------