1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: directsql.cxx,v $
10 * $Revision: 1.10.158.2 $
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"
34 #ifndef _DBACCESS_UI_DIRECTSQL_HXX_
35 #include "directsql.hxx"
37 #ifndef _DBACCESS_UI_DIRECTSQL_HRC_
38 #include "directsql.hrc"
41 #include "dbu_dlg.hrc"
43 #ifndef _SV_MSGBOX_HXX
44 #include <vcl/msgbox.hxx>
46 #ifndef _COMPHELPER_TYPES_HXX_
47 #include <comphelper/types.hxx>
49 #ifndef _SV_MSGBOX_HXX
50 #include <vcl/msgbox.hxx>
53 #include <vcl/svapp.hxx>
55 #ifndef _VOS_MUTEX_HXX_
56 #include <vos/mutex.hxx>
58 #ifndef TOOLS_DIAGNOSE_EX_H
59 #include <tools/diagnose_ex.h>
62 //........................................................................
65 //........................................................................
67 using namespace ::com::sun::star::uno
;
68 using namespace ::com::sun::star::sdbc
;
69 using namespace ::com::sun::star::lang
;
71 //====================================================================
73 //====================================================================
74 class LargeEntryListBox
: public ListBox
77 LargeEntryListBox( Window
* _pParent
, const ResId
& _rId
);
80 virtual void UserDraw( const UserDrawEvent
& rUDEvt
);
83 //--------------------------------------------------------------------
84 LargeEntryListBox::LargeEntryListBox( Window
* _pParent
, const ResId
& _rId
)
85 :ListBox(_pParent
, _rId
)
87 EnableUserDraw(sal_True
);
90 //--------------------------------------------------------------------
91 void LargeEntryListBox::UserDraw( const UserDrawEvent
& _rUDEvt
)
93 if (LISTBOX_ENTRY_NOTFOUND
== _rUDEvt
.GetItemId())
94 ListBox::UserDraw( _rUDEvt
);
96 _rUDEvt
.GetDevice()->DrawText( _rUDEvt
.GetRect(), GetEntry( _rUDEvt
.GetItemId() ), TEXT_DRAW_LEFT
| TEXT_DRAW_VCENTER
| TEXT_DRAW_ENDELLIPSIS
);
99 //====================================================================
101 //====================================================================
102 DBG_NAME(DirectSQLDialog
)
103 //--------------------------------------------------------------------
104 DirectSQLDialog::DirectSQLDialog( Window
* _pParent
, const Reference
< XConnection
>& _rxConn
)
105 :ModalDialog(_pParent
, ModuleRes(DLG_DIRECTSQL
))
106 ,m_aFrame (this, ModuleRes(FL_SQL
))
107 ,m_aSQLLabel (this, ModuleRes(FT_SQL
))
108 ,m_aSQL (this, ModuleRes(ME_SQL
))
109 ,m_aExecute (this, ModuleRes(PB_EXECUTE
))
110 ,m_aHistoryLabel (this, ModuleRes(FT_HISTORY
))
111 ,m_pSQLHistory(new LargeEntryListBox(this, ModuleRes(LB_HISTORY
)))
112 ,m_aStatusFrame (this, ModuleRes(FL_STATUS
))
113 ,m_aStatus (this, ModuleRes(ME_STATUS
))
114 ,m_aButtonSeparator (this, ModuleRes(FL_BUTTONS
))
115 ,m_aHelp (this, ModuleRes(PB_HELP
))
116 ,m_aClose (this, ModuleRes(PB_CLOSE
))
119 ,m_xConnection(_rxConn
)
121 DBG_CTOR(DirectSQLDialog
,NULL
);
127 m_aExecute
.SetClickHdl(LINK(this, DirectSQLDialog
, OnExecute
));
128 m_aClose
.SetClickHdl(LINK(this, DirectSQLDialog
, OnClose
));
129 m_pSQLHistory
->SetSelectHdl(LINK(this, DirectSQLDialog
, OnListEntrySelected
));
130 m_pSQLHistory
->SetDropDownLineCount(10);
132 // add a dispose listener to the connection
133 Reference
< XComponent
> xConnComp(m_xConnection
, UNO_QUERY
);
134 OSL_ENSURE(xConnComp
.is(), "DirectSQLDialog::DirectSQLDialog: invalid connection!");
136 startComponentListening(xConnComp
);
138 m_aSQL
.SetModifyHdl(LINK(this, DirectSQLDialog
, OnStatementModified
));
139 OnStatementModified(&m_aSQL
);
142 //--------------------------------------------------------------------
143 DirectSQLDialog::~DirectSQLDialog()
146 ::osl::MutexGuard
aGuard(m_aMutex
);
147 stopAllComponentListening();
149 delete m_pSQLHistory
;
151 DBG_DTOR(DirectSQLDialog
,NULL
);
154 //--------------------------------------------------------------------
155 void DirectSQLDialog::_disposing( const EventObject
& _rSource
)
157 ::vos::OGuard
aSolarGuard(Application::GetSolarMutex());
158 ::osl::MutexGuard
aGuard(m_aMutex
);
160 OSL_ENSURE(Reference
< XConnection
>(_rSource
.Source
, UNO_QUERY
).get() == m_xConnection
.get(),
161 "DirectSQLDialog::_disposing: where does this come from?");
165 String
sMessage(ModuleRes(STR_DIRECTSQL_CONNECTIONLOST
));
166 ErrorBox
aError(this, WB_OK
, sMessage
);
170 PostUserEvent(LINK(this, DirectSQLDialog
, OnClose
));
173 //--------------------------------------------------------------------
174 void DirectSQLDialog::addHistoryEntry(const String
& _rStatement
)
176 CHECK_INVARIANTS("DirectSQLDialog::addHistoryEntry");
177 implAddToStatementHistory(_rStatement
);
180 //--------------------------------------------------------------------
181 sal_Int32
DirectSQLDialog::getHistorySize() const
183 CHECK_INVARIANTS("DirectSQLDialog::getHistorySize");
184 return m_aStatementHistory
.size();
187 //--------------------------------------------------------------------
188 void DirectSQLDialog::implEnsureHistoryLimit()
190 CHECK_INVARIANTS("DirectSQLDialog::implEnsureHistoryLimit");
192 if (getHistorySize() <= m_nHistoryLimit
)
196 sal_Int32 nRemoveEntries
= getHistorySize() - m_nHistoryLimit
;
197 while (nRemoveEntries
--)
199 m_aStatementHistory
.pop_front();
200 m_aNormalizedHistory
.pop_front();
201 m_pSQLHistory
->RemoveEntry((USHORT
)0);
205 //--------------------------------------------------------------------
206 void DirectSQLDialog::implAddToStatementHistory(const String
& _rStatement
)
208 CHECK_INVARIANTS("DirectSQLDialog::implAddToStatementHistory");
210 // add the statement to the history
211 m_aStatementHistory
.push_back(_rStatement
);
213 // normalize the statement, and remember the normalized form, too
214 String
sNormalized(_rStatement
);
215 sNormalized
.SearchAndReplaceAll((sal_Unicode
)'\n', ' ');
216 m_aNormalizedHistory
.push_back(sNormalized
);
218 // add the normalized version to the list box
219 m_pSQLHistory
->InsertEntry(sNormalized
);
221 // ensure that we don't exceed the history limit
222 implEnsureHistoryLimit();
226 //--------------------------------------------------------------------
227 const sal_Char
* DirectSQLDialog::impl_CheckInvariants() const
229 if (m_aStatementHistory
.size() != m_aNormalizedHistory
.size())
230 return "statement history is inconsistent!";
233 return "invalid listbox!";
235 if (m_aStatementHistory
.size() != m_pSQLHistory
->GetEntryCount())
236 return "invalid listbox entry count!";
238 if (!m_xConnection
.is())
239 return "have no connection!";
245 //--------------------------------------------------------------------
246 void DirectSQLDialog::implExecuteStatement(const String
& _rStatement
)
248 CHECK_INVARIANTS("DirectSQLDialog::implExecuteStatement");
250 ::osl::MutexGuard
aGuard(m_aMutex
);
255 // create a statement
256 Reference
< XStatement
> xStatement
= m_xConnection
->createStatement();
257 OSL_ENSURE(xStatement
.is(), "DirectSQLDialog::implExecuteStatement: no statement returned by the connection!");
261 xStatement
->execute(_rStatement
);
264 sStatus
= String(ModuleRes(STR_COMMAND_EXECUTED_SUCCESSFULLY
));
266 // dispose the statement
267 ::comphelper::disposeComponent(xStatement
);
269 catch(const SQLException
& e
)
273 catch( const Exception
& )
275 DBG_UNHANDLED_EXCEPTION();
278 // add the status text
279 addStatusText(sStatus
);
282 //--------------------------------------------------------------------
283 void DirectSQLDialog::addStatusText(const String
& _rMessage
)
285 String sAppendMessage
= String::CreateFromInt32(m_nStatusCount
++);
286 sAppendMessage
+= String::CreateFromAscii(": ");
287 sAppendMessage
+= _rMessage
;
288 sAppendMessage
+= String::CreateFromAscii("\n\n");
290 String sCompleteMessage
= m_aStatus
.GetText();
291 sCompleteMessage
+= sAppendMessage
;
292 m_aStatus
.SetText(sCompleteMessage
);
294 m_aStatus
.SetSelection(Selection(sCompleteMessage
.Len(), sCompleteMessage
.Len()));
297 //--------------------------------------------------------------------
298 void DirectSQLDialog::executeCurrent()
300 CHECK_INVARIANTS("DirectSQLDialog::executeCurrent");
302 String sStatement
= m_aSQL
.GetText();
305 implExecuteStatement(sStatement
);
307 // add the statement to the history
308 implAddToStatementHistory(sStatement
);
310 m_aSQL
.SetSelection(Selection());
314 //--------------------------------------------------------------------
315 void DirectSQLDialog::switchToHistory(sal_Int32 _nHistoryPos
, sal_Bool _bUpdateListBox
)
317 CHECK_INVARIANTS("DirectSQLDialog::switchToHistory");
319 if ((_nHistoryPos
>= 0) && (_nHistoryPos
< getHistorySize()))
321 // set the text in the statement editor
322 String sStatement
= m_aStatementHistory
[_nHistoryPos
];
323 m_aSQL
.SetText(sStatement
);
324 OnStatementModified(&m_aSQL
);
328 // selecte the normalized statement in the list box
329 m_pSQLHistory
->SelectEntryPos((USHORT
)_nHistoryPos
);
330 OSL_ENSURE(m_pSQLHistory
->GetSelectEntry() == m_aNormalizedHistory
[_nHistoryPos
],
331 "DirectSQLDialog::switchToHistory: inconsistent listbox entries!");
335 m_aSQL
.SetSelection(Selection(sStatement
.Len(), sStatement
.Len()));
338 OSL_ENSURE(sal_False
, "DirectSQLDialog::switchToHistory: invalid position!");
341 //--------------------------------------------------------------------
342 IMPL_LINK( DirectSQLDialog
, OnStatementModified
, void*, /*NOTINTERESTEDIN*/ )
344 m_aExecute
.Enable(0 != m_aSQL
.GetText().Len());
348 //--------------------------------------------------------------------
349 IMPL_LINK( DirectSQLDialog
, OnClose
, void*, /*NOTINTERESTEDIN*/ )
355 //--------------------------------------------------------------------
356 IMPL_LINK( DirectSQLDialog
, OnExecute
, void*, /*NOTINTERESTEDIN*/ )
362 //--------------------------------------------------------------------
363 IMPL_LINK( DirectSQLDialog
, OnListEntrySelected
, void*, /*NOTINTERESTEDIN*/ )
365 if (!m_pSQLHistory
->IsTravelSelect())
367 const sal_uInt16 nSelected
= m_pSQLHistory
->GetSelectEntryPos();
368 if (LISTBOX_ENTRY_NOTFOUND
!= nSelected
)
369 switchToHistory(nSelected
, sal_False
);
374 //........................................................................
376 //........................................................................