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 0 : Reference< XPropertySet > xParamAsSet;
185 0 : m_xParams->getByIndex(m_nCurrentlySelected) >>= xParamAsSet;
186 0 : if (xParamAsSet.is())
187 : {
188 0 : if (m_xConnection.is() && m_xFormatter.is())
189 : {
190 0 : OUString sParamValue( m_aParam.GetText() );
191 0 : sal_Bool bValid = m_aPredicateInput.normalizePredicateString( sParamValue, xParamAsSet );
192 0 : m_aParam.SetText( sParamValue );
193 0 : if ( bValid )
194 : {
195 : // with this the value isn't dirty anymore
196 0 : if (m_nCurrentlySelected != LISTBOX_ENTRY_NOTFOUND)
197 0 : m_aVisitedParams[m_nCurrentlySelected] &= ~EF_DIRTY;
198 : }
199 : else
200 : {
201 0 : if (!m_bNeedErrorOnCurrent)
202 0 : return 1L;
203 :
204 0 : m_bNeedErrorOnCurrent = sal_False; // will be reset in OnValueModified
205 :
206 0 : OUString sName;
207 : try
208 : {
209 0 : sName = ::comphelper::getString(xParamAsSet->getPropertyValue(PROPERTY_NAME));
210 : }
211 0 : catch(Exception&)
212 : {
213 : DBG_UNHANDLED_EXCEPTION();
214 : }
215 :
216 0 : String sMessage;
217 : {
218 0 : LocalResourceAccess aDummy(DLG_PARAMETERS, RSC_MODALDIALOG);
219 0 : sMessage = String(ModuleRes(STR_COULD_NOT_CONVERT_PARAM));
220 : }
221 0 : sMessage.SearchAndReplaceAll( OUString("$name$"), sName );
222 0 : ErrorBox(NULL, WB_OK, sMessage).Execute();
223 0 : m_aParam.GrabFocus();
224 0 : return 1L;
225 0 : }
226 : }
227 : }
228 :
229 0 : return 0L;
230 : }
231 :
232 : //------------------------------------------------------------------------------
233 0 : IMPL_LINK(OParameterDialog, OnButtonClicked, PushButton*, pButton)
234 : {
235 0 : if (&m_aCancelBtn == pButton)
236 : {
237 : // no interpreting of the given values anymore ....
238 0 : m_aParam.SetLoseFocusHdl(Link()); // no direct call from the control anymore ...
239 0 : m_bNeedErrorOnCurrent = sal_False; // in case of any indirect calls -> no error message
240 0 : m_aCancelBtn.SetClickHdl(Link());
241 0 : m_aCancelBtn.Click();
242 : }
243 0 : else if (&m_aOKBtn == pButton)
244 : {
245 : // transfer the current values into the Any
246 0 : if (LINK(this, OParameterDialog, OnEntrySelected).Call(&m_aAllParams) != 0L)
247 : { // there was an error interpreting the current text
248 0 : m_bNeedErrorOnCurrent = sal_True;
249 : // we're are out of the complex web :) of direct and indirect calls to OnValueLoseFocus now,
250 : // so the next time it is called we need an error message, again ....
251 : // (TODO : there surely are better solutions for this ...)
252 0 : return 1L;
253 : }
254 :
255 0 : if (m_xParams.is())
256 : {
257 : // write the parameters
258 : try
259 : {
260 0 : PropertyValue* pValues = m_aFinalValues.getArray();
261 0 : for (sal_Int32 i = 0, nCount = m_xParams->getCount(); i<nCount; ++i, ++pValues)
262 : {
263 0 : Reference< XPropertySet > xParamAsSet;
264 0 : m_xParams->getByIndex(i) >>= xParamAsSet;
265 :
266 0 : OUString sValue;
267 0 : pValues->Value >>= sValue;
268 0 : pValues->Value <<= OUString( m_aPredicateInput.getPredicateValue( sValue, xParamAsSet, sal_False ) );
269 0 : }
270 : }
271 0 : catch(Exception&)
272 : {
273 : DBG_UNHANDLED_EXCEPTION();
274 : }
275 :
276 : }
277 : // to close the dialog (which is more code than a simple EndDialog)
278 0 : m_aOKBtn.SetClickHdl(Link());
279 0 : m_aOKBtn.Click();
280 : }
281 0 : else if (&m_aTravelNext == pButton)
282 : {
283 0 : sal_uInt16 nCurrent = m_aAllParams.GetSelectEntryPos();
284 0 : sal_uInt16 nCount = m_aAllParams.GetEntryCount();
285 : OSL_ENSURE(nCount == m_aVisitedParams.size(), "OParameterDialog::OnButtonClicked : inconsistent lists !");
286 :
287 : // search the next entry in list we haven't visited yet
288 0 : sal_uInt16 nNext = (nCurrent + 1) % nCount;
289 0 : while ((nNext != nCurrent) && ( m_aVisitedParams[nNext] & EF_VISITED ))
290 0 : nNext = (nNext + 1) % nCount;
291 :
292 0 : if ( m_aVisitedParams[nNext] & EF_VISITED )
293 : // there is no such "not visited yet" entry -> simpy take the next one
294 0 : nNext = (nCurrent + 1) % nCount;
295 :
296 0 : m_aAllParams.SelectEntryPos(nNext);
297 0 : LINK(this, OParameterDialog, OnEntrySelected).Call(&m_aAllParams);
298 0 : m_bNeedErrorOnCurrent = sal_True;
299 : // we're are out of the complex web :) of direct and indirect calls to OnValueLoseFocus now,
300 : // so the next time it is called we need an error message, again ....
301 : // (TODO : there surely are better solutions for this ...)
302 : }
303 :
304 0 : return 0L;
305 : }
306 :
307 : //------------------------------------------------------------------------------
308 0 : IMPL_LINK(OParameterDialog, OnEntrySelected, ListBox*, /*pList*/)
309 : {
310 0 : if (m_aResetVisitFlag.IsActive())
311 : {
312 0 : LINK(this, OParameterDialog, OnVisitedTimeout).Call(&m_aResetVisitFlag);
313 0 : m_aResetVisitFlag.Stop();
314 : }
315 : // save the old values
316 0 : if (m_nCurrentlySelected != LISTBOX_ENTRY_NOTFOUND)
317 : {
318 : // do the transformation of the current text
319 0 : if (LINK(this, OParameterDialog, OnValueLoseFocus).Call(&m_aParam) != 0L)
320 : { // there was an error interpreting the text
321 0 : m_aAllParams.SelectEntryPos(m_nCurrentlySelected);
322 0 : return 1L;
323 : }
324 :
325 0 : m_aFinalValues[m_nCurrentlySelected].Value <<= OUString(m_aParam.GetText());
326 : }
327 :
328 : // initialize the controls with the new values
329 0 : sal_uInt16 nSelected = m_aAllParams.GetSelectEntryPos();
330 : OSL_ENSURE(nSelected != LISTBOX_ENTRY_NOTFOUND, "OParameterDialog::OnEntrySelected : no current entry !");
331 :
332 0 : m_aParam.SetText(::comphelper::getString(m_aFinalValues[nSelected].Value));
333 0 : m_nCurrentlySelected = nSelected;
334 :
335 : // with this the value isn't dirty
336 : OSL_ENSURE(m_nCurrentlySelected < m_aVisitedParams.size(), "OParameterDialog::OnEntrySelected : invalid current entry !");
337 0 : m_aVisitedParams[m_nCurrentlySelected] &= ~EF_DIRTY;
338 :
339 0 : m_aResetVisitFlag.SetTimeout(1000);
340 0 : m_aResetVisitFlag.Start();
341 :
342 0 : return 0L;
343 : }
344 :
345 : //------------------------------------------------------------------------------
346 0 : IMPL_LINK(OParameterDialog, OnVisitedTimeout, Timer*, /*pTimer*/)
347 : {
348 : OSL_ENSURE(m_nCurrentlySelected != LISTBOX_ENTRY_NOTFOUND, "OParameterDialog::OnVisitedTimeout : invalid call !");
349 :
350 : // mark the currently selected entry as visited
351 : OSL_ENSURE(m_nCurrentlySelected < m_aVisitedParams.size(), "OParameterDialog::OnVisitedTimeout : invalid entry !");
352 0 : m_aVisitedParams[m_nCurrentlySelected] |= EF_VISITED;
353 :
354 : // was it the last "not visited yet" entry ?
355 0 : ConstByteVectorIterator aIter;
356 0 : for ( aIter = m_aVisitedParams.begin();
357 0 : aIter < m_aVisitedParams.end();
358 : ++aIter
359 : )
360 : {
361 0 : if (((*aIter) & EF_VISITED) == 0)
362 0 : break;
363 : }
364 0 : if (aIter == m_aVisitedParams.end())
365 : { // yes, there isn't another one -> change the "default button"
366 0 : m_aTravelNext.SetStyle(m_aTravelNext.GetStyle() & ~WB_DEFBUTTON);
367 0 : m_aOKBtn.SetStyle(m_aOKBtn.GetStyle() | WB_DEFBUTTON);
368 :
369 : // set to focus to one of the buttons temporary (with this their "default"-state is really updated)
370 0 : Window* pOldFocus = Application::GetFocusWindow();
371 :
372 : // if the old focus window is the value edit do some preparations ...
373 0 : Selection aSel;
374 0 : if (pOldFocus == &m_aParam)
375 : {
376 0 : m_aParam.SetLoseFocusHdl(Link());
377 0 : aSel = m_aParam.GetSelection();
378 : }
379 0 : m_aTravelNext.GrabFocus();
380 0 : if (pOldFocus)
381 0 : pOldFocus->GrabFocus();
382 :
383 : // restore the settings for the value edit
384 0 : if (pOldFocus == &m_aParam)
385 : {
386 0 : m_aParam.SetLoseFocusHdl(LINK(this, OParameterDialog, OnValueLoseFocus));
387 0 : m_aParam.SetSelection(aSel);
388 : }
389 : }
390 :
391 0 : return 0L;
392 : }
393 :
394 : //------------------------------------------------------------------------------
395 0 : IMPL_LINK(OParameterDialog, OnValueModified, Control*, /*pBox*/)
396 : {
397 : // mark the currently selected entry as dirty
398 : OSL_ENSURE(m_nCurrentlySelected < m_aVisitedParams.size(), "OParameterDialog::OnValueModified : invalid entry !");
399 0 : m_aVisitedParams[m_nCurrentlySelected] |= EF_DIRTY;
400 :
401 0 : m_bNeedErrorOnCurrent = sal_True;
402 :
403 0 : return 0L;
404 : }
405 :
406 :
407 : //.........................................................................
408 12 : } // namespace dbaui
409 : //.........................................................................
410 :
411 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|