Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "paramdialog.hxx"
21 : #include "paramdialog.hrc"
22 : #include "dbu_dlg.hrc"
23 : #include "commontypes.hxx"
24 : #include "moduledbu.hxx"
25 : #include <com/sun/star/util/NumberFormatter.hpp>
26 : #include <com/sun/star/sdbc/DataType.hpp>
27 : #include <comphelper/processfactory.hxx>
28 : #include <connectivity/dbtools.hxx>
29 : #include "dbustrings.hrc"
30 : #include <vcl/svapp.hxx>
31 : #include <vcl/msgbox.hxx>
32 : #include <osl/diagnose.h>
33 : #include <tools/diagnose_ex.h>
34 : #include "localresaccess.hxx"
35 : #include <unotools/syslocale.hxx>
36 :
37 : #define EF_VISITED 0x0001
38 : #define EF_DIRTY 0x0002
39 :
40 : namespace dbaui
41 : {
42 :
43 : using namespace ::com::sun::star::uno;
44 : using namespace ::com::sun::star::lang;
45 : using namespace ::com::sun::star::beans;
46 : using namespace ::com::sun::star::container;
47 : using namespace ::com::sun::star::sdbc;
48 : using namespace ::com::sun::star::util;
49 : using namespace ::connectivity;
50 :
51 : // OParameterDialog
52 :
53 :
54 0 : OParameterDialog::OParameterDialog(
55 : Window* pParent, const Reference< XIndexAccess > & rParamContainer,
56 : const Reference< XConnection > & _rxConnection, const Reference< XComponentContext >& rxContext)
57 : :ModalDialog( pParent, ModuleRes(DLG_PARAMETERS))
58 : ,m_aNamesFrame (this, ModuleRes(FL_PARAMS))
59 : ,m_aAllParams (this, ModuleRes(LB_ALLPARAMS))
60 : ,m_aValueFrame (this, ModuleRes(FT_VALUE))
61 : ,m_aParam (this, ModuleRes(ET_PARAM))
62 : ,m_aTravelNext (this, ModuleRes(BT_TRAVELNEXT))
63 : ,m_aOKBtn (this, ModuleRes(BT_OK))
64 : ,m_aCancelBtn (this, ModuleRes(BT_CANCEL))
65 : ,m_nCurrentlySelected(LISTBOX_ENTRY_NOTFOUND)
66 : ,m_xConnection(_rxConnection)
67 0 : ,m_aPredicateInput( rxContext, _rxConnection, getParseContext() )
68 0 : ,m_bNeedErrorOnCurrent(sal_True)
69 : {
70 :
71 0 : if (rxContext.is())
72 0 : m_xFormatter = Reference< XNumberFormatter>( NumberFormatter::create( rxContext ), UNO_QUERY_THROW);
73 : else {
74 : OSL_FAIL("OParameterDialog::OParameterDialog: need a service factory!");
75 : }
76 :
77 0 : Reference< XNumberFormatsSupplier > xNumberFormats = ::dbtools::getNumberFormats(m_xConnection, true);
78 0 : if (!xNumberFormats.is())
79 0 : ::comphelper::disposeComponent(m_xFormatter);
80 : else
81 0 : m_xFormatter->attachNumberFormatsSupplier(xNumberFormats);
82 : try
83 : {
84 : OSL_ENSURE(rParamContainer->getCount(), "OParameterDialog::OParameterDialog : can't handle empty containers !");
85 :
86 0 : m_aFinalValues.realloc(rParamContainer->getCount());
87 0 : PropertyValue* pValues = m_aFinalValues.getArray();
88 :
89 0 : for (sal_Int32 i = 0, nCount = rParamContainer->getCount(); i<nCount; ++i, ++pValues)
90 : {
91 0 : Reference< XPropertySet > xParamAsSet;
92 0 : rParamContainer->getByIndex(i) >>= xParamAsSet;
93 : OSL_ENSURE(xParamAsSet.is(),"Parameter is null!");
94 0 : if(!xParamAsSet.is())
95 0 : continue;
96 0 : pValues->Name = ::comphelper::getString(xParamAsSet->getPropertyValue(PROPERTY_NAME));
97 0 : m_aAllParams.InsertEntry(pValues->Name);
98 :
99 0 : if (!pValues->Value.hasValue())
100 : // it won't have a value, 'cause it's default constructed. But may be later we support
101 : // initializing this dialog with values
102 0 : pValues->Value = makeAny( OUString() );
103 : // default the values to an empty string
104 :
105 0 : m_aVisitedParams.push_back(0);
106 : // not visited, not dirty
107 0 : }
108 :
109 0 : m_xParams = rParamContainer;
110 : }
111 0 : catch(Exception&)
112 : {
113 : DBG_UNHANDLED_EXCEPTION();
114 : }
115 :
116 0 : Construct();
117 :
118 0 : m_aResetVisitFlag.SetTimeoutHdl(LINK(this, OParameterDialog, OnVisitedTimeout));
119 :
120 0 : FreeResource();
121 0 : }
122 :
123 0 : OParameterDialog::~OParameterDialog()
124 : {
125 0 : if (m_aResetVisitFlag.IsActive())
126 0 : m_aResetVisitFlag.Stop();
127 :
128 0 : }
129 :
130 0 : void OParameterDialog::Construct()
131 : {
132 0 : m_aAllParams.SetSelectHdl(LINK(this, OParameterDialog, OnEntrySelected));
133 0 : m_aParam.SetLoseFocusHdl(LINK(this, OParameterDialog, OnValueLoseFocus));
134 0 : m_aParam.SetModifyHdl(LINK(this, OParameterDialog, OnValueModified));
135 0 : m_aTravelNext.SetClickHdl(LINK(this, OParameterDialog, OnButtonClicked));
136 0 : m_aOKBtn.SetClickHdl(LINK(this, OParameterDialog, OnButtonClicked));
137 0 : m_aCancelBtn.SetClickHdl(LINK(this, OParameterDialog, OnButtonClicked));
138 :
139 0 : if (m_aAllParams.GetEntryCount())
140 : {
141 0 : m_aAllParams.SelectEntryPos(0);
142 0 : LINK(this, OParameterDialog, OnEntrySelected).Call(&m_aAllParams);
143 :
144 0 : if (m_aAllParams.GetEntryCount() == 1)
145 : {
146 0 : m_aTravelNext.Enable(false);
147 : }
148 :
149 0 : if (m_aAllParams.GetEntryCount() > 1)
150 : {
151 0 : m_aOKBtn.SetStyle(m_aOKBtn.GetStyle() & ~WB_DEFBUTTON);
152 0 : m_aTravelNext.SetStyle(m_aTravelNext.GetStyle() | WB_DEFBUTTON);
153 : }
154 : }
155 :
156 0 : m_aParam.GrabFocus();
157 0 : }
158 :
159 0 : IMPL_LINK(OParameterDialog, OnValueLoseFocus, Control*, /*pSource*/)
160 : {
161 0 : if (m_nCurrentlySelected != LISTBOX_ENTRY_NOTFOUND)
162 : {
163 0 : if ( ( m_aVisitedParams[ m_nCurrentlySelected ] & EF_DIRTY ) == 0 )
164 : // nothing to do, the value isn't dirty
165 0 : return 0L;
166 : }
167 :
168 0 : Reference< XPropertySet > xParamAsSet;
169 0 : m_xParams->getByIndex(m_nCurrentlySelected) >>= xParamAsSet;
170 0 : if (xParamAsSet.is())
171 : {
172 0 : if (m_xConnection.is() && m_xFormatter.is())
173 : {
174 0 : OUString sParamValue( m_aParam.GetText() );
175 0 : sal_Bool bValid = m_aPredicateInput.normalizePredicateString( sParamValue, xParamAsSet );
176 0 : m_aParam.SetText( sParamValue );
177 0 : if ( bValid )
178 : {
179 : // with this the value isn't dirty anymore
180 0 : if (m_nCurrentlySelected != LISTBOX_ENTRY_NOTFOUND)
181 0 : m_aVisitedParams[m_nCurrentlySelected] &= ~EF_DIRTY;
182 : }
183 : else
184 : {
185 0 : if (!m_bNeedErrorOnCurrent)
186 0 : return 1L;
187 :
188 0 : m_bNeedErrorOnCurrent = sal_False; // will be reset in OnValueModified
189 :
190 0 : OUString sName;
191 : try
192 : {
193 0 : sName = ::comphelper::getString(xParamAsSet->getPropertyValue(PROPERTY_NAME));
194 : }
195 0 : catch(Exception&)
196 : {
197 : DBG_UNHANDLED_EXCEPTION();
198 : }
199 :
200 0 : OUString sMessage;
201 : {
202 0 : LocalResourceAccess aDummy(DLG_PARAMETERS, RSC_MODALDIALOG);
203 0 : sMessage = ModuleRes(STR_COULD_NOT_CONVERT_PARAM);
204 : }
205 0 : sMessage = sMessage.replaceAll( "$name$", sName );
206 0 : ErrorBox(NULL, WB_OK, sMessage).Execute();
207 0 : m_aParam.GrabFocus();
208 0 : return 1L;
209 0 : }
210 : }
211 : }
212 :
213 0 : return 0L;
214 : }
215 :
216 0 : IMPL_LINK(OParameterDialog, OnButtonClicked, PushButton*, pButton)
217 : {
218 0 : if (&m_aCancelBtn == pButton)
219 : {
220 : // no interpreting of the given values anymore ....
221 0 : m_aParam.SetLoseFocusHdl(Link()); // no direct call from the control anymore ...
222 0 : m_bNeedErrorOnCurrent = sal_False; // in case of any indirect calls -> no error message
223 0 : m_aCancelBtn.SetClickHdl(Link());
224 0 : m_aCancelBtn.Click();
225 : }
226 0 : else if (&m_aOKBtn == pButton)
227 : {
228 : // transfer the current values into the Any
229 0 : if (LINK(this, OParameterDialog, OnEntrySelected).Call(&m_aAllParams) != 0L)
230 : { // there was an error interpreting the current text
231 0 : m_bNeedErrorOnCurrent = sal_True;
232 : // we're are out of the complex web :) of direct and indirect calls to OnValueLoseFocus now,
233 : // so the next time it is called we need an error message, again ....
234 : // (TODO : there surely are better solutions for this ...)
235 0 : return 1L;
236 : }
237 :
238 0 : if (m_xParams.is())
239 : {
240 : // write the parameters
241 : try
242 : {
243 0 : PropertyValue* pValues = m_aFinalValues.getArray();
244 0 : for (sal_Int32 i = 0, nCount = m_xParams->getCount(); i<nCount; ++i, ++pValues)
245 : {
246 0 : Reference< XPropertySet > xParamAsSet;
247 0 : m_xParams->getByIndex(i) >>= xParamAsSet;
248 :
249 0 : OUString sValue;
250 0 : pValues->Value >>= sValue;
251 0 : pValues->Value <<= OUString( m_aPredicateInput.getPredicateValue( sValue, xParamAsSet, false ) );
252 0 : }
253 : }
254 0 : catch(Exception&)
255 : {
256 : DBG_UNHANDLED_EXCEPTION();
257 : }
258 :
259 : }
260 : // to close the dialog (which is more code than a simple EndDialog)
261 0 : m_aOKBtn.SetClickHdl(Link());
262 0 : m_aOKBtn.Click();
263 : }
264 0 : else if (&m_aTravelNext == pButton)
265 : {
266 0 : sal_Int32 nCurrent = m_aAllParams.GetSelectEntryPos();
267 0 : sal_Int32 nCount = m_aAllParams.GetEntryCount();
268 : OSL_ENSURE(static_cast<size_t>(nCount) == m_aVisitedParams.size(), "OParameterDialog::OnButtonClicked : inconsistent lists !");
269 :
270 : // search the next entry in list we haven't visited yet
271 0 : sal_Int32 nNext = (nCurrent + 1) % nCount;
272 0 : while ((nNext != nCurrent) && ( m_aVisitedParams[nNext] & EF_VISITED ))
273 0 : nNext = (nNext + 1) % nCount;
274 :
275 0 : if ( m_aVisitedParams[nNext] & EF_VISITED )
276 : // there is no such "not visited yet" entry -> simpy take the next one
277 0 : nNext = (nCurrent + 1) % nCount;
278 :
279 0 : m_aAllParams.SelectEntryPos(nNext);
280 0 : LINK(this, OParameterDialog, OnEntrySelected).Call(&m_aAllParams);
281 0 : m_bNeedErrorOnCurrent = sal_True;
282 : // we're are out of the complex web :) of direct and indirect calls to OnValueLoseFocus now,
283 : // so the next time it is called we need an error message, again ....
284 : // (TODO : there surely are better solutions for this ...)
285 : }
286 :
287 0 : return 0L;
288 : }
289 :
290 0 : IMPL_LINK(OParameterDialog, OnEntrySelected, ListBox*, /*pList*/)
291 : {
292 0 : if (m_aResetVisitFlag.IsActive())
293 : {
294 0 : LINK(this, OParameterDialog, OnVisitedTimeout).Call(&m_aResetVisitFlag);
295 0 : m_aResetVisitFlag.Stop();
296 : }
297 : // save the old values
298 0 : if (m_nCurrentlySelected != LISTBOX_ENTRY_NOTFOUND)
299 : {
300 : // do the transformation of the current text
301 0 : if (LINK(this, OParameterDialog, OnValueLoseFocus).Call(&m_aParam) != 0L)
302 : { // there was an error interpreting the text
303 0 : m_aAllParams.SelectEntryPos(m_nCurrentlySelected);
304 0 : return 1L;
305 : }
306 :
307 0 : m_aFinalValues[m_nCurrentlySelected].Value <<= OUString(m_aParam.GetText());
308 : }
309 :
310 : // initialize the controls with the new values
311 0 : sal_Int32 nSelected = m_aAllParams.GetSelectEntryPos();
312 : OSL_ENSURE(nSelected != LISTBOX_ENTRY_NOTFOUND, "OParameterDialog::OnEntrySelected : no current entry !");
313 :
314 0 : m_aParam.SetText(::comphelper::getString(m_aFinalValues[nSelected].Value));
315 0 : m_nCurrentlySelected = nSelected;
316 :
317 : // with this the value isn't dirty
318 : OSL_ENSURE(static_cast<size_t>(m_nCurrentlySelected) < m_aVisitedParams.size(), "OParameterDialog::OnEntrySelected : invalid current entry !");
319 0 : m_aVisitedParams[m_nCurrentlySelected] &= ~EF_DIRTY;
320 :
321 0 : m_aResetVisitFlag.SetTimeout(1000);
322 0 : m_aResetVisitFlag.Start();
323 :
324 0 : return 0L;
325 : }
326 :
327 0 : IMPL_LINK(OParameterDialog, OnVisitedTimeout, Timer*, /*pTimer*/)
328 : {
329 : OSL_ENSURE(m_nCurrentlySelected != LISTBOX_ENTRY_NOTFOUND, "OParameterDialog::OnVisitedTimeout : invalid call !");
330 :
331 : // mark the currently selected entry as visited
332 : OSL_ENSURE(static_cast<size_t>(m_nCurrentlySelected) < m_aVisitedParams.size(), "OParameterDialog::OnVisitedTimeout : invalid entry !");
333 0 : m_aVisitedParams[m_nCurrentlySelected] |= EF_VISITED;
334 :
335 : // was it the last "not visited yet" entry ?
336 0 : ByteVector::const_iterator aIter;
337 0 : for ( aIter = m_aVisitedParams.begin();
338 0 : aIter < m_aVisitedParams.end();
339 : ++aIter
340 : )
341 : {
342 0 : if (((*aIter) & EF_VISITED) == 0)
343 0 : break;
344 : }
345 0 : if (aIter == m_aVisitedParams.end())
346 : { // yes, there isn't another one -> change the "default button"
347 0 : m_aTravelNext.SetStyle(m_aTravelNext.GetStyle() & ~WB_DEFBUTTON);
348 0 : m_aOKBtn.SetStyle(m_aOKBtn.GetStyle() | WB_DEFBUTTON);
349 :
350 : // set to focus to one of the buttons temporary (with this their "default"-state is really updated)
351 0 : Window* pOldFocus = Application::GetFocusWindow();
352 :
353 : // if the old focus window is the value edit do some preparations ...
354 0 : Selection aSel;
355 0 : if (pOldFocus == &m_aParam)
356 : {
357 0 : m_aParam.SetLoseFocusHdl(Link());
358 0 : aSel = m_aParam.GetSelection();
359 : }
360 0 : m_aTravelNext.GrabFocus();
361 0 : if (pOldFocus)
362 0 : pOldFocus->GrabFocus();
363 :
364 : // restore the settings for the value edit
365 0 : if (pOldFocus == &m_aParam)
366 : {
367 0 : m_aParam.SetLoseFocusHdl(LINK(this, OParameterDialog, OnValueLoseFocus));
368 0 : m_aParam.SetSelection(aSel);
369 : }
370 : }
371 :
372 0 : return 0L;
373 : }
374 :
375 0 : IMPL_LINK(OParameterDialog, OnValueModified, Control*, /*pBox*/)
376 : {
377 : // mark the currently selected entry as dirty
378 : OSL_ENSURE(static_cast<size_t>(m_nCurrentlySelected) < m_aVisitedParams.size(), "OParameterDialog::OnValueModified : invalid entry !");
379 0 : m_aVisitedParams[m_nCurrentlySelected] |= EF_DIRTY;
380 :
381 0 : m_bNeedErrorOnCurrent = sal_True;
382 :
383 0 : return 0L;
384 : }
385 :
386 : } // namespace dbaui
387 :
388 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|