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