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