1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "directsql.hxx"
31 #include "directsql.hrc"
32 #include "dbu_dlg.hrc"
33 #include <vcl/msgbox.hxx>
34 #include <comphelper/types.hxx>
35 #include <vcl/svapp.hxx>
36 #include <osl/mutex.hxx>
37 #include <tools/diagnose_ex.h>
38 #include <rtl/strbuf.hxx>
40 //........................................................................
43 //........................................................................
45 using namespace ::com::sun::star::uno
;
46 using namespace ::com::sun::star::sdbc
;
47 using namespace ::com::sun::star::lang
;
49 using ::rtl::OStringBuffer
;
51 //====================================================================
53 //====================================================================
54 class LargeEntryListBox
: public ListBox
57 LargeEntryListBox( Window
* _pParent
, const ResId
& _rId
);
60 virtual void UserDraw( const UserDrawEvent
& rUDEvt
);
63 //--------------------------------------------------------------------
64 LargeEntryListBox::LargeEntryListBox( Window
* _pParent
, const ResId
& _rId
)
65 :ListBox(_pParent
, _rId
)
67 EnableUserDraw(sal_True
);
70 //--------------------------------------------------------------------
71 void LargeEntryListBox::UserDraw( const UserDrawEvent
& _rUDEvt
)
73 if (LISTBOX_ENTRY_NOTFOUND
== _rUDEvt
.GetItemId())
74 ListBox::UserDraw( _rUDEvt
);
76 _rUDEvt
.GetDevice()->DrawText( _rUDEvt
.GetRect(), GetEntry( _rUDEvt
.GetItemId() ), TEXT_DRAW_LEFT
| TEXT_DRAW_VCENTER
| TEXT_DRAW_ENDELLIPSIS
);
79 //====================================================================
81 //====================================================================
82 DBG_NAME(DirectSQLDialog
)
83 //--------------------------------------------------------------------
84 DirectSQLDialog::DirectSQLDialog( Window
* _pParent
, const Reference
< XConnection
>& _rxConn
)
85 :ModalDialog(_pParent
, ModuleRes(DLG_DIRECTSQL
))
86 ,m_aFrame (this, ModuleRes(FL_SQL
))
87 ,m_aSQLLabel (this, ModuleRes(FT_SQL
))
88 ,m_aSQL (this, ModuleRes(ME_SQL
))
89 ,m_aExecute (this, ModuleRes(PB_EXECUTE
))
90 ,m_aHistoryLabel (this, ModuleRes(FT_HISTORY
))
91 ,m_pSQLHistory(new LargeEntryListBox(this, ModuleRes(LB_HISTORY
)))
92 ,m_aStatusFrame (this, ModuleRes(FL_STATUS
))
93 ,m_aStatus (this, ModuleRes(ME_STATUS
))
94 ,m_aButtonSeparator (this, ModuleRes(FL_BUTTONS
))
95 ,m_aHelp (this, ModuleRes(PB_HELP
))
96 ,m_aClose (this, ModuleRes(PB_CLOSE
))
99 ,m_xConnection(_rxConn
)
101 DBG_CTOR(DirectSQLDialog
,NULL
);
107 m_aExecute
.SetClickHdl(LINK(this, DirectSQLDialog
, OnExecute
));
108 m_aClose
.SetClickHdl(LINK(this, DirectSQLDialog
, OnClose
));
109 m_pSQLHistory
->SetSelectHdl(LINK(this, DirectSQLDialog
, OnListEntrySelected
));
110 m_pSQLHistory
->SetDropDownLineCount(10);
112 // add a dispose listener to the connection
113 Reference
< XComponent
> xConnComp(m_xConnection
, UNO_QUERY
);
114 OSL_ENSURE(xConnComp
.is(), "DirectSQLDialog::DirectSQLDialog: invalid connection!");
116 startComponentListening(xConnComp
);
118 m_aSQL
.SetModifyHdl(LINK(this, DirectSQLDialog
, OnStatementModified
));
119 OnStatementModified(&m_aSQL
);
122 //--------------------------------------------------------------------
123 DirectSQLDialog::~DirectSQLDialog()
126 ::osl::MutexGuard
aGuard(m_aMutex
);
127 stopAllComponentListening();
129 delete m_pSQLHistory
;
131 DBG_DTOR(DirectSQLDialog
,NULL
);
134 //--------------------------------------------------------------------
135 void DirectSQLDialog::_disposing( const EventObject
& _rSource
)
137 SolarMutexGuard aSolarGuard
;
138 ::osl::MutexGuard
aGuard(m_aMutex
);
140 OSL_ENSURE(Reference
< XConnection
>(_rSource
.Source
, UNO_QUERY
).get() == m_xConnection
.get(),
141 "DirectSQLDialog::_disposing: where does this come from?");
145 String
sMessage(ModuleRes(STR_DIRECTSQL_CONNECTIONLOST
));
146 ErrorBox
aError(this, WB_OK
, sMessage
);
150 PostUserEvent(LINK(this, DirectSQLDialog
, OnClose
));
153 //--------------------------------------------------------------------
154 sal_Int32
DirectSQLDialog::getHistorySize() const
156 CHECK_INVARIANTS("DirectSQLDialog::getHistorySize");
157 return m_aStatementHistory
.size();
160 //--------------------------------------------------------------------
161 void DirectSQLDialog::implEnsureHistoryLimit()
163 CHECK_INVARIANTS("DirectSQLDialog::implEnsureHistoryLimit");
165 if (getHistorySize() <= m_nHistoryLimit
)
169 sal_Int32 nRemoveEntries
= getHistorySize() - m_nHistoryLimit
;
170 while (nRemoveEntries
--)
172 m_aStatementHistory
.pop_front();
173 m_aNormalizedHistory
.pop_front();
174 m_pSQLHistory
->RemoveEntry((sal_uInt16
)0);
178 //--------------------------------------------------------------------
179 void DirectSQLDialog::implAddToStatementHistory(const String
& _rStatement
)
181 CHECK_INVARIANTS("DirectSQLDialog::implAddToStatementHistory");
183 // add the statement to the history
184 m_aStatementHistory
.push_back(_rStatement
);
186 // normalize the statement, and remember the normalized form, too
187 String
sNormalized(_rStatement
);
188 sNormalized
.SearchAndReplaceAll((sal_Unicode
)'\n', ' ');
189 m_aNormalizedHistory
.push_back(sNormalized
);
191 // add the normalized version to the list box
192 m_pSQLHistory
->InsertEntry(sNormalized
);
194 // ensure that we don't exceed the history limit
195 implEnsureHistoryLimit();
199 //--------------------------------------------------------------------
200 const sal_Char
* DirectSQLDialog::impl_CheckInvariants() const
202 if (m_aStatementHistory
.size() != m_aNormalizedHistory
.size())
203 return "statement history is inconsistent!";
206 return "invalid listbox!";
208 if (m_aStatementHistory
.size() != m_pSQLHistory
->GetEntryCount())
209 return "invalid listbox entry count!";
211 if (!m_xConnection
.is())
212 return "have no connection!";
218 //--------------------------------------------------------------------
219 void DirectSQLDialog::implExecuteStatement(const String
& _rStatement
)
221 CHECK_INVARIANTS("DirectSQLDialog::implExecuteStatement");
223 ::osl::MutexGuard
aGuard(m_aMutex
);
228 // create a statement
229 Reference
< XStatement
> xStatement
= m_xConnection
->createStatement();
230 OSL_ENSURE(xStatement
.is(), "DirectSQLDialog::implExecuteStatement: no statement returned by the connection!");
234 xStatement
->execute(_rStatement
);
237 sStatus
= String(ModuleRes(STR_COMMAND_EXECUTED_SUCCESSFULLY
));
239 // dispose the statement
240 ::comphelper::disposeComponent(xStatement
);
242 catch(const SQLException
& e
)
246 catch( const Exception
& )
248 DBG_UNHANDLED_EXCEPTION();
251 // add the status text
252 addStatusText(sStatus
);
255 //--------------------------------------------------------------------
256 void DirectSQLDialog::addStatusText(const String
& _rMessage
)
258 String sAppendMessage
= String::CreateFromInt32(m_nStatusCount
++);
259 sAppendMessage
+= String::CreateFromAscii(": ");
260 sAppendMessage
+= _rMessage
;
261 sAppendMessage
+= String::CreateFromAscii("\n\n");
263 String sCompleteMessage
= m_aStatus
.GetText();
264 sCompleteMessage
+= sAppendMessage
;
265 m_aStatus
.SetText(sCompleteMessage
);
267 m_aStatus
.SetSelection(Selection(sCompleteMessage
.Len(), sCompleteMessage
.Len()));
270 //--------------------------------------------------------------------
271 void DirectSQLDialog::executeCurrent()
273 CHECK_INVARIANTS("DirectSQLDialog::executeCurrent");
275 String sStatement
= m_aSQL
.GetText();
278 implExecuteStatement(sStatement
);
280 // add the statement to the history
281 implAddToStatementHistory(sStatement
);
283 m_aSQL
.SetSelection(Selection());
287 //--------------------------------------------------------------------
288 void DirectSQLDialog::switchToHistory(sal_Int32 _nHistoryPos
, sal_Bool _bUpdateListBox
)
290 CHECK_INVARIANTS("DirectSQLDialog::switchToHistory");
292 if ((_nHistoryPos
>= 0) && (_nHistoryPos
< getHistorySize()))
294 // set the text in the statement editor
295 String sStatement
= m_aStatementHistory
[_nHistoryPos
];
296 m_aSQL
.SetText(sStatement
);
297 OnStatementModified(&m_aSQL
);
301 // selecte the normalized statement in the list box
302 m_pSQLHistory
->SelectEntryPos((sal_uInt16
)_nHistoryPos
);
303 OSL_ENSURE(m_pSQLHistory
->GetSelectEntry() == m_aNormalizedHistory
[_nHistoryPos
],
304 "DirectSQLDialog::switchToHistory: inconsistent listbox entries!");
308 m_aSQL
.SetSelection(Selection(sStatement
.Len(), sStatement
.Len()));
311 OSL_FAIL("DirectSQLDialog::switchToHistory: invalid position!");
314 //--------------------------------------------------------------------
315 IMPL_LINK( DirectSQLDialog
, OnStatementModified
, void*, /*NOTINTERESTEDIN*/ )
317 m_aExecute
.Enable(0 != m_aSQL
.GetText().Len());
321 //--------------------------------------------------------------------
322 IMPL_LINK( DirectSQLDialog
, OnClose
, void*, /*NOTINTERESTEDIN*/ )
328 //--------------------------------------------------------------------
329 IMPL_LINK( DirectSQLDialog
, OnExecute
, void*, /*NOTINTERESTEDIN*/ )
335 //--------------------------------------------------------------------
336 IMPL_LINK( DirectSQLDialog
, OnListEntrySelected
, void*, /*NOTINTERESTEDIN*/ )
338 if (!m_pSQLHistory
->IsTravelSelect())
340 const sal_uInt16 nSelected
= m_pSQLHistory
->GetSelectEntryPos();
341 if (LISTBOX_ENTRY_NOTFOUND
!= nSelected
)
342 switchToHistory(nSelected
, sal_False
);
347 //........................................................................
349 //........................................................................
351 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */