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 package com.sun.star.wizards.ui;
24 
25 // import java.util.Vector;
26 import com.sun.star.awt.TextEvent;
27 import com.sun.star.awt.VclWindowPeerAttribute;
28 import com.sun.star.awt.XControl;
29 import com.sun.star.awt.XListBox;
30 import com.sun.star.awt.XRadioButton;
31 import com.sun.star.beans.PropertyAttribute;
32 import com.sun.star.beans.PropertyValue;
33 import com.sun.star.beans.XPropertyContainer;
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.lang.EventObject;
36 import com.sun.star.lang.IllegalArgumentException;
37 import com.sun.star.lang.XMultiServiceFactory;
38 import com.sun.star.sdb.SQLFilterOperator;
39 import com.sun.star.sdbc.DataType;
40 import com.sun.star.uno.Any;
41 import com.sun.star.uno.AnyConverter;
42 import com.sun.star.uno.Exception;
43 import com.sun.star.uno.Type;
44 import com.sun.star.uno.TypeClass;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.XInterface;
47 import com.sun.star.wizards.common.NumberFormatter;
48 import com.sun.star.wizards.common.Helper;
49 import com.sun.star.wizards.common.HelpIds;
50 import com.sun.star.wizards.common.JavaTools;
51 import com.sun.star.wizards.common.PropertyNames;
52 import com.sun.star.wizards.db.FieldColumn;
53 import com.sun.star.wizards.db.QueryMetaData;
54 import com.sun.star.wizards.db.SQLQueryComposer;
55 import java.util.logging.Level;
56 import java.util.logging.Logger;
57 
58 public class FilterComponent
59 {
60 
61     private Integer IStep;
62     private int RowCount;
63     private static String[] sLogicOperators;
64     private XRadioButton optMatchAny;
65     private String slblFieldNames;
66     private String slblOperators;
67     private String slblValue;
68     WizardDialog CurUnoDialog;
69     private int BaseID = 2300;
70     private String sIncSuffix;
71     private ControlRow[] oControlRows;
72     private String sDuplicateCondition;
73     final int SOOPTORMODE = 100;
74     final int SOOPTANDMODE = 101;
75     QueryMetaData oQueryMetaData;
76     int iDateTimeFormat;
77     int iDateFormat;
78     int iTimeFormat;
79     private PropertyValue[][] filterconditions;
80     private short curtabindex;
81     XMultiServiceFactory xMSF;
82     final int SO_FIRSTFIELDNAME = 1;
83     final int SO_SECONDFIELDNAME = 2;
84     final int SO_THIRDFIELDNAME = 3;
85     final int SO_FOURTHFIELDNAME = 4;
86     int[] SO_FIELDNAMELIST = new int[]
87     {
88         SO_FIRSTFIELDNAME, SO_SECONDFIELDNAME, SO_THIRDFIELDNAME, SO_FOURTHFIELDNAME
89     };
90     final int SO_FIRSTCONDITION = 5;
91     final int SO_SECONDCONDITION = 6;
92     final int SO_THIRDCONDITION = 7;
93     final int SO_FOURTHCONDITION = 8;
94     int[] SO_CONDITIONLIST = new int[]
95     {
96         SO_FIRSTCONDITION, SO_SECONDCONDITION, SO_THIRDCONDITION, SO_FOURTHCONDITION
97     };
98     final int SO_FIRSTTEXTFIELD = 1;
99     final int SO_SECONDTEXTFIELD = 2;
100     final int SO_THIRDTEXTFIELD = 3;
101     final int SO_FOURTHTEXTFIELD = 4;
102     int[] SO_TEXTFIELDLIST = new int[]
103     {
104         SO_FIRSTTEXTFIELD, SO_SECONDTEXTFIELD, SO_THIRDTEXTFIELD, SO_FOURTHTEXTFIELD
105     };
106     final int SO_FIRSTBOOLFIELDNAME = 256 + 1;
107     final int SO_SECONDBOOLFIELDNAME = 256 + 2;
108     final int SO_THIRDBOOLFIELDNAME = 256 + 3;
109     final int SO_FOURTHBOOLFIELDNAME = 256 + 4;
110     int SO_BOOLEANLIST[] =
111 
112 
113     {
114         SO_FIRSTBOOLFIELDNAME, SO_SECONDBOOLFIELDNAME, SO_THIRDBOOLFIELDNAME, SO_FOURTHBOOLFIELDNAME
115     };
116     final int SO_OPTQUERYMODE = 5;
117     int SOI_MATCHALL = 0;
118     int SOI_MATCHANY = 1;
119     int curHelpID;
120 
121     class ItemListenerImpl implements com.sun.star.awt.XItemListener
122     {
123 
itemStateChanged(com.sun.star.awt.ItemEvent EventObject)124         public void itemStateChanged(com.sun.star.awt.ItemEvent EventObject)
125         {
126             int iKey = CurUnoDialog.getControlKey(EventObject.Source, CurUnoDialog.ControlList);
127             String sControlName = PropertyNames.EMPTY_STRING;
128             switch (iKey)
129             {
130                 case SO_FIRSTFIELDNAME:
131                 case SO_SECONDFIELDNAME:
132                 case SO_THIRDFIELDNAME:
133                 case SO_FOURTHFIELDNAME:
134                     sControlName = getControlName(EventObject.Source);
135                     String sControlNameSuffix = sIncSuffix + "_" + getIndexNumber(sControlName);
136                     XListBox xCurFieldListBox = UnoRuntime.queryInterface(XListBox.class, CurUnoDialog.xDlgContainer.getControl(sControlName));
137                     String CurDisplayFieldName = xCurFieldListBox.getSelectedItem();
138                     FieldColumn CurFieldColumn = new FieldColumn(oQueryMetaData, CurDisplayFieldName);
139 
140                     String sControlNameTextValue = "txtValue" + sControlNameSuffix;
141                     XControl xValueControl = CurUnoDialog.xDlgContainer.getControl(sControlNameTextValue);
142                     XInterface xValueModel = (XInterface) UnoDialog.getModel(xValueControl);
143                     Helper.setUnoPropertyValue(xValueModel, "TreatAsNumber", Boolean.valueOf(CurFieldColumn.isNumberFormat()));
144                     final NumberFormatter aNumberFormatter = oQueryMetaData.getNumberFormatter();
145                     aNumberFormatter.setNumberFormat(xValueModel, CurFieldColumn.getDBFormatKey(), aNumberFormatter);
146 
147                     break;
148                 case SO_FIRSTCONDITION:
149                 case SO_SECONDCONDITION:
150                 case SO_THIRDCONDITION:
151                 case SO_FOURTHCONDITION:
152                     sControlName = getControlName(EventObject.Source);
153                     break;
154                 case SOOPTORMODE:
155                 case SOOPTANDMODE:
156                     return;
157 
158                 case SO_FIRSTBOOLFIELDNAME:
159                 case SO_SECONDBOOLFIELDNAME:
160                 case SO_THIRDBOOLFIELDNAME:
161                 case SO_FOURTHBOOLFIELDNAME:
162                     sControlName = getControlName(EventObject.Source);
163                     break;
164 
165                 default:
166                     break;
167             }
168             togglefollowingControlRow(sControlName);
169         }
170 
disposing(com.sun.star.lang.EventObject eventObject)171         public void disposing(com.sun.star.lang.EventObject eventObject)
172         {
173         }
174     }
175 
176     class TextListenerImpl implements com.sun.star.awt.XTextListener
177     {
178 
textChanged(TextEvent EventObject)179         public void textChanged(TextEvent EventObject)
180         {
181             String sName = getControlName(EventObject.Source);
182             togglefollowingControlRow(sName);
183         }
184 
disposing(EventObject eventObject)185         public void disposing(EventObject eventObject)
186         {
187         }
188 
actionPerformed(com.sun.star.awt.ActionEvent actionEvent)189         public void actionPerformed(com.sun.star.awt.ActionEvent actionEvent)
190         {
191             getfilterstate();
192         }
193     }
194 
getIndexNumber(String _sStr)195     public static String getIndexNumber(String _sStr)
196     {
197         return _sStr.substring(_sStr.length() - 1, _sStr.length());
198     }
199 
200     /**
201      * Enable the next ControlRow if the Condition is complete in the current line
202      *
203      * @param _scurName every control name ends with something like _1, _2, _3 this is the index number
204      *        we try to interpret to check which line we currently use. (number - 1)
205      */
togglefollowingControlRow(String _scurName)206     private void togglefollowingControlRow(String _scurName)
207     {
208         // String sNameSuffix = sIncSuffix + "_" + _scurName.substring(_scurName.length() - 1, _scurName.length());
209         int Index = Integer.valueOf(getIndexNumber(_scurName)).intValue();
210         if (Index < oControlRows.length)
211         {
212             boolean bValue = oControlRows[Index - 1].isConditionComplete();
213             oControlRows[Index].setEnabled(bValue);
214         }
215     }
216 
getControlName(Object _oSourceevent)217     private String getControlName(Object _oSourceevent)
218     {
219         try
220         {
221             XControl xControl = UnoRuntime.queryInterface(XControl.class, _oSourceevent);
222             XPropertySet xPSet = UnoRuntime.queryInterface(XPropertySet.class, xControl.getModel());
223             return AnyConverter.toString(xPSet.getPropertyValue(PropertyNames.PROPERTY_NAME));
224         }
225         catch (Exception e)
226         {
227             e.printStackTrace(System.out);
228         }
229         return PropertyNames.EMPTY_STRING;
230     }
231 
getFilterConditions()232     public PropertyValue[][] getFilterConditions()
233     {
234         // ControlRow CurControlRow;
235         // getfilterstate();
236         int nFilterCount = getFilterCount();
237         if (nFilterCount > 0)
238         {
239             final SQLQueryComposer composer = oQueryMetaData.getSQLQueryComposer();
240             try
241             {
242                 final String serviceName = "com.sun.star.beans.PropertyBag";
243                 final XPropertyContainer column = UnoRuntime.queryInterface(XPropertyContainer.class, oQueryMetaData.xMSF.createInstance(serviceName));
244 
245                 column.addProperty("Type", PropertyAttribute.BOUND, DataType.VARCHAR);
246                 column.addProperty(PropertyNames.PROPERTY_NAME, PropertyAttribute.BOUND, PropertyNames.EMPTY_STRING);
247                 column.addProperty("Value", (short) (PropertyAttribute.MAYBEVOID | PropertyAttribute.REMOVABLE), null);
248                 final XPropertySet columnSet = UnoRuntime.queryInterface(XPropertySet.class, column);
249 
250                 if (composer.getQuery().length() == 0)
251                 {
252                     StringBuilder sql = new StringBuilder();
253                     sql.append(composer.getSelectClause(true));
254                     sql.append(' ');
255                     sql.append(composer.getFromClause());
256                     composer.getQueryComposer().setElementaryQuery(sql.toString());
257                 }
258                 composer.getQueryComposer().setStructuredFilter(new PropertyValue[][]
259                         {
260                         });
261                 for (int i = 0; i < RowCount; i++)
262                 {
263                     ControlRow currentControlRow = oControlRows[i];
264                     if (currentControlRow.isEnabled() && currentControlRow.isConditionComplete())
265                     {
266                         String sFieldName = currentControlRow.getSelectedFieldName();
267                         int nOperator = (int) currentControlRow.getSelectedOperator();
268                         FieldColumn aFieldColumn = oQueryMetaData.getFieldColumnByDisplayName(sFieldName);
269                         columnSet.setPropertyValue(PropertyNames.PROPERTY_NAME, aFieldColumn.getFieldName());
270                         columnSet.setPropertyValue("Type", aFieldColumn.getXColumnPropertySet().getPropertyValue("Type"));
271                         Object value = currentControlRow.getValue();
272                         switch (aFieldColumn.getFieldType())
273                         {
274                             case DataType.TIMESTAMP:
275                             case DataType.DATE:
276                                 value = ((Double) value) - oQueryMetaData.getNullDateCorrection();
277                                 break;
278                         }
279                         column.removeProperty("Value");
280                         final short operator = currentControlRow.getSelectedOperator();
281                         if ((operator == SQLFilterOperator.SQLNULL)
282                                 || (operator == SQLFilterOperator.NOT_SQLNULL)
283                                 || AnyConverter.isVoid(value))
284                         {
285                             column.addProperty("Value", (short) (PropertyAttribute.MAYBEVOID | PropertyAttribute.REMOVABLE), "");
286                             value = new Any(new Type(TypeClass.VOID), null);
287                         }
288                         else
289                         {
290                             column.addProperty("Value", (short) (PropertyAttribute.MAYBEVOID | PropertyAttribute.REMOVABLE), value);
291                         }
292                         columnSet.setPropertyValue("Value", value);
293                         composer.getQueryComposer().appendFilterByColumn(columnSet, getfilterstate() == this.SOI_MATCHALL, nOperator);
294                     }
295                 }
296                 filterconditions = composer.getNormalizedStructuredFilter();
297                 int[] iduplicate = JavaTools.getDuplicateFieldIndex(filterconditions);
298                 if (iduplicate[0] != -1)
299                 {
300                     PropertyValue aduplicatecondition = filterconditions[iduplicate[0]][iduplicate[1]];
301                     String smsgDuplicateCondition = getDisplayCondition(sDuplicateCondition, aduplicatecondition, null);
302                     CurUnoDialog.showMessageBox("WarningBox", VclWindowPeerAttribute.OK, smsgDuplicateCondition);
303                     CurUnoDialog.vetoableChange(new java.beans.PropertyChangeEvent(CurUnoDialog, "Steps", Integer.valueOf(1), Integer.valueOf(2)));
304                     return new PropertyValue[][]
305                             {
306                             };
307                 }
308             }
309             catch (Exception ex)
310             {
311                 Logger.getLogger(FilterComponent.class.getName()).log(Level.SEVERE, null, ex);
312             }
313         }
314         else
315         {
316             filterconditions = new PropertyValue[0][0];
317         }
318         return filterconditions;
319     }
320 
getDisplayCondition(String _BaseString, PropertyValue _filtercondition, QueryMetaData _CurDBMetaData)321     public static String getDisplayCondition(String _BaseString, PropertyValue _filtercondition, QueryMetaData _CurDBMetaData)
322     {
323         try
324         {
325             String FieldName;
326             if (_CurDBMetaData != null)
327             {
328                 FieldColumn CurDBFieldColumn = _CurDBMetaData.getFieldColumnByDisplayName(_filtercondition.Name);
329                 FieldName = CurDBFieldColumn.getFieldTitle();
330             }
331             else
332             {
333                 FieldName = _filtercondition.Name;
334             }
335             String sreturn = JavaTools.replaceSubString(_BaseString, FieldName, "<FIELDNAME>");
336             String soperator = sLogicOperators[_filtercondition.Handle - 1];
337             sreturn = JavaTools.replaceSubString(sreturn, soperator, "<LOGICOPERATOR>");
338             String sDisplayValue = PropertyNames.EMPTY_STRING;
339             if ((_filtercondition.Handle != SQLFilterOperator.SQLNULL)
340                     && (_filtercondition.Handle != SQLFilterOperator.NOT_SQLNULL)
341                     && !AnyConverter.isVoid(_filtercondition.Value))
342             {
343                 sDisplayValue = AnyConverter.toString(_filtercondition.Value);
344             }
345             sreturn = JavaTools.replaceSubString(sreturn, sDisplayValue, "<VALUE>");
346             return sreturn;
347         }
348         catch (IllegalArgumentException e)
349         {
350             e.printStackTrace(System.out);
351         }
352         return PropertyNames.EMPTY_STRING;
353     }
354 
getfilterstate()355     private int getfilterstate()
356     {
357         boolean bisany = true;
358         int ifilterstate = SOI_MATCHALL;
359         bisany = (this.optMatchAny.getState());
360         if (bisany)
361         {
362             ifilterstate = SOI_MATCHANY;
363         }
364         else
365         {
366             ifilterstate = SOI_MATCHALL;
367         }
368         return ifilterstate;
369     }
370 
getFilterCount()371     private int getFilterCount()
372     {
373         int a = 0;
374         for (int i = 0; i < RowCount; i++)
375         {
376             if (oControlRows[i].isEnabled() && oControlRows[i].isConditionComplete())
377             {
378                 a++;
379             }
380         }
381         return a;
382     }
383 
384     /** Creates a new instance of FilterComponent
385      * @param CurUnoDialog
386      * @param _xMSF
387      * @param iStep
388      * @param iPosX
389      * @param iPosY
390      * @param iWidth
391      * @param FilterCount
392      * @param _oQueryMetaData
393      * @param _firstHelpID
394      */
FilterComponent(WizardDialog CurUnoDialog, XMultiServiceFactory _xMSF, int iStep, int iPosX, int iPosY, int iWidth, int FilterCount, QueryMetaData _oQueryMetaData, int _firstHelpID)395     public FilterComponent(WizardDialog CurUnoDialog, XMultiServiceFactory _xMSF, int iStep, int iPosX, int iPosY, int iWidth, int FilterCount, QueryMetaData _oQueryMetaData, int _firstHelpID)
396     {
397         this.curHelpID = _firstHelpID;
398         this.xMSF = _xMSF;
399         this.IStep = Integer.valueOf(iStep);
400 
401         curtabindex = UnoDialog.setInitialTabindex(iStep);
402         this.CurUnoDialog = CurUnoDialog;
403         this.RowCount = FilterCount;
404         this.oQueryMetaData = _oQueryMetaData;
405         boolean bEnabled;
406         sIncSuffix = com.sun.star.wizards.common.Desktop.getIncrementSuffix(CurUnoDialog.getDlgNameAccess(), "optMatchAll");
407 
408         String soptMatchAll = CurUnoDialog.m_oResource.getResText(BaseID + 9);
409         String soptMatchAny = CurUnoDialog.m_oResource.getResText(BaseID + 10);
410         slblFieldNames = CurUnoDialog.m_oResource.getResText(BaseID + 17);
411         slblOperators = CurUnoDialog.m_oResource.getResText(BaseID + 24);
412         slblValue = CurUnoDialog.m_oResource.getResText(BaseID + 25);
413         sLogicOperators = CurUnoDialog.m_oResource.getResArray(BaseID + 26, 10 /* 7 */); // =, <>, <, >, <=, >=, like, !like, is null, !is null
414 
415         sDuplicateCondition = CurUnoDialog.m_oResource.getResText(BaseID + 89);
416 
417         // create Radiobuttons
418         // * match all
419         // * match one
420         CurUnoDialog.insertRadioButton("optMatchAll" + sIncSuffix, SOOPTANDMODE, new ItemListenerImpl(),
421                 new String[]
422                 {
423                     PropertyNames.PROPERTY_HEIGHT,
424                     PropertyNames.PROPERTY_HELPURL,
425                     PropertyNames.PROPERTY_LABEL,
426                     PropertyNames.PROPERTY_POSITION_X,
427                     PropertyNames.PROPERTY_POSITION_Y,
428                     PropertyNames.PROPERTY_STATE,
429                     PropertyNames.PROPERTY_STEP,
430                     PropertyNames.PROPERTY_TABINDEX,
431                     PropertyNames.PROPERTY_WIDTH
432                 },
433                 new Object[]
434                 {
435                     Integer.valueOf(9),
436                     HelpIds.getHelpIdString(curHelpID++),
437                     soptMatchAll,
438                     Integer.valueOf(iPosX),
439                     Integer.valueOf(iPosY),
440                     Short.valueOf((short) 1),
441                     IStep,
442                     Short.valueOf(curtabindex++),
443                     Integer.valueOf(203)
444                 });
445         optMatchAny = CurUnoDialog.insertRadioButton("optMatchAny" + sIncSuffix, SOOPTORMODE, new ItemListenerImpl(),
446                 new String[]
447                 {
448                     PropertyNames.PROPERTY_HEIGHT,
449                     PropertyNames.PROPERTY_HELPURL,
450                     PropertyNames.PROPERTY_LABEL,
451                     PropertyNames.PROPERTY_POSITION_X,
452                     PropertyNames.PROPERTY_POSITION_Y,
453                     PropertyNames.PROPERTY_STEP,
454                     PropertyNames.PROPERTY_TABINDEX,
455                     PropertyNames.PROPERTY_WIDTH
456                 },
457                 new Object[]
458                 {
459                     Integer.valueOf(9),
460                     HelpIds.getHelpIdString(curHelpID++),
461                     soptMatchAny,
462                     Integer.valueOf(iPosX),
463                     Integer.valueOf(iPosY + 12),
464                     IStep,
465                     Short.valueOf(curtabindex++),
466                     Integer.valueOf(203)
467                 });
468         getfilterstate();
469 
470         oControlRows = new ControlRow[FilterCount];
471         for (int i = 0; i < FilterCount; i++)
472         {
473             bEnabled = (i == 0);
474             oControlRows[i] = new ControlRow(iPosX, iPosY + 20, i, bEnabled, (this.curHelpID + (i * 3)));
475             iPosY += 43;
476         }
477     }
478 
initialize(PropertyValue[][] _filterconditions, String[] _fieldnames)479     public void initialize(PropertyValue[][] _filterconditions, String[] _fieldnames)
480     {
481         int i;
482         for (i = 0; i < RowCount; i++)
483         {
484             oControlRows[i].setFieldNames(_fieldnames);
485             // oControlRows[i].setFieldNames(aFieldNamesWithAdditionalEmpty);
486         }
487         this.filterconditions = _filterconditions;
488         PropertyValue[] curfilterconditions;
489         // int a;
490         if (_filterconditions.length == 1)
491         {
492             curfilterconditions = filterconditions[0];
493             for (i = 0; i < curfilterconditions.length; i++)
494             {
495                 oControlRows[i].setCondition(filterconditions[0][i]);
496             }
497         }
498         else
499         {
500             for (i = 0; i < filterconditions.length; i++)
501             {
502                 oControlRows[i].setCondition(filterconditions[i][0]);
503             }
504         }
505         while (i < oControlRows.length)
506         {
507             oControlRows[i].settovoid();
508             boolean bdoenable;
509             if (i > 0)
510             {
511                 bdoenable = oControlRows[i - 1].isConditionComplete();
512             }
513             else
514             {
515                 bdoenable = true;
516             }
517             oControlRows[i].setEnabled(bdoenable);
518             i++;
519         }
520     }
521 
addNumberFormats()522     public void addNumberFormats()
523     {
524         iDateFormat = oQueryMetaData.getNumberFormatter().defineNumberFormat("YYYY-MM-DD");
525         iTimeFormat = oQueryMetaData.getNumberFormatter().defineNumberFormat("HH:MM:SS");
526         iDateTimeFormat = oQueryMetaData.getNumberFormatter().defineNumberFormat("YYYY-MM-DD HH:MM:SS");
527     }
528 
529     // -------------------------------------------------------------------------
530     //
531     //
532     // -------------------------------------------------------------------------
533     final class ControlRow
534     {
535 
536         private final static int SOLSTFIELDNAME = 3;
537         private final static int SOLSTOPERATOR = 4;
538         private final static int SOTXTVALUE = 5;
539         protected XInterface[] ControlElements = new XInterface[6];
540         private boolean m_bEnabled;
541         String[] FieldNames;
542 
ControlRow(int iCompPosX, int iCompPosY, int Index, boolean _bEnabled, int _firstRowHelpID)543         protected ControlRow(int iCompPosX, int iCompPosY, int Index, boolean _bEnabled, int _firstRowHelpID)
544         {
545             int nFieldWidth = 71;
546             int nOperatorWidth = 70;
547             int nValueWidth = 44;
548 
549             int nPosX1 = iCompPosX + 10;
550             int nPosX2 = iCompPosX + 10 + nFieldWidth + 6; // 87
551             int nPosX3 = iCompPosX + 10 + nFieldWidth + 6 + nOperatorWidth + 6;
552 
553 
554             try
555             {
556                 String sCompSuffix = sIncSuffix + "_" + String.valueOf(Index + 1);
557                 m_bEnabled = _bEnabled;
558 
559                 // Label Field
560                 ControlElements[0] = CurUnoDialog.insertLabel("lblFieldNames" + sCompSuffix,
561                         new String[]
562                         {
563                             PropertyNames.PROPERTY_ENABLED,
564                             PropertyNames.PROPERTY_HEIGHT,
565                             PropertyNames.PROPERTY_LABEL,
566                             PropertyNames.PROPERTY_POSITION_X,
567                             PropertyNames.PROPERTY_POSITION_Y,
568                             PropertyNames.PROPERTY_STEP,
569                             PropertyNames.PROPERTY_TABINDEX,
570                             PropertyNames.PROPERTY_WIDTH
571                         },
572                         new Object[]
573                         {
574                             Boolean.valueOf(isEnabled()),
575                             Integer.valueOf(9),
576                             slblFieldNames,
577                             Integer.valueOf(nPosX1),
578                             Integer.valueOf(iCompPosY + 13),
579                             IStep,
580                             Short.valueOf(curtabindex++),
581                             Integer.valueOf(nFieldWidth)
582                         });
583 
584                 // Label Operator
585                 ControlElements[1] = CurUnoDialog.insertLabel("lblOperators" + sCompSuffix,
586                         new String[]
587                         {
588                             PropertyNames.PROPERTY_ENABLED,
589                             PropertyNames.PROPERTY_HEIGHT,
590                             PropertyNames.PROPERTY_LABEL,
591                             PropertyNames.PROPERTY_POSITION_X,
592                             PropertyNames.PROPERTY_POSITION_Y,
593                             PropertyNames.PROPERTY_STEP,
594                             PropertyNames.PROPERTY_TABINDEX,
595                             PropertyNames.PROPERTY_WIDTH
596                         },
597                         new Object[]
598                         {
599                             Boolean.valueOf(isEnabled()),
600                             Integer.valueOf(9),
601                             slblOperators,
602                             Integer.valueOf(nPosX2),
603                             Integer.valueOf(iCompPosY + 13),
604                             IStep,
605                             Short.valueOf(curtabindex++),
606                             Integer.valueOf(nOperatorWidth)
607                         });
608 
609                 // Label Value
610                 ControlElements[2] = CurUnoDialog.insertLabel("lblValue" + sCompSuffix,
611                         new String[]
612                         {
613                             PropertyNames.PROPERTY_ENABLED,
614                             PropertyNames.PROPERTY_HEIGHT,
615                             PropertyNames.PROPERTY_LABEL,
616                             PropertyNames.PROPERTY_POSITION_X,
617                             PropertyNames.PROPERTY_POSITION_Y,
618                             PropertyNames.PROPERTY_STEP,
619                             PropertyNames.PROPERTY_TABINDEX,
620                             PropertyNames.PROPERTY_WIDTH
621                         },
622                         new Object[]
623                         {
624                             Boolean.valueOf(isEnabled()),
625                             Integer.valueOf(9),
626                             slblValue,
627                             Integer.valueOf(nPosX3),
628                             Integer.valueOf(iCompPosY + 13),
629                             IStep,
630                             Short.valueOf(curtabindex++),
631                             Integer.valueOf(nValueWidth)
632                         });
633 
634                 // Listbox Fields
635                 ControlElements[SOLSTFIELDNAME] = CurUnoDialog.insertListBox("lstFieldName" + sCompSuffix, SO_FIELDNAMELIST[Index], null, new ItemListenerImpl(),
636                         new String[]
637                         {
638                             PropertyNames.PROPERTY_ENABLED,
639                             "Dropdown",
640                             PropertyNames.PROPERTY_HEIGHT,
641                             PropertyNames.PROPERTY_HELPURL,
642                             "LineCount",
643                             PropertyNames.PROPERTY_POSITION_X,
644                             PropertyNames.PROPERTY_POSITION_Y,
645                             PropertyNames.PROPERTY_STEP,
646                             PropertyNames.PROPERTY_TABINDEX,
647                             PropertyNames.PROPERTY_WIDTH
648                         },
649                         new Object[]
650                         {
651                             Boolean.valueOf(isEnabled()),
652                             Boolean.TRUE,
653                             Integer.valueOf(13),
654                             HelpIds.getHelpIdString(_firstRowHelpID++),
655                             Short.valueOf(UnoDialog.getListBoxLineCount() /* 7 */),
656                             Integer.valueOf(nPosX1),
657                             Integer.valueOf(iCompPosY + 23),
658                             IStep,
659                             Short.valueOf(curtabindex++),
660                             Integer.valueOf(nFieldWidth)
661                         });
662 
663                 // Listbox Operators
664                 ControlElements[SOLSTOPERATOR] = CurUnoDialog.insertListBox("lstOperator" + sCompSuffix, SO_CONDITIONLIST[Index], null, new ItemListenerImpl(),
665                         new String[]
666                         {
667                             PropertyNames.PROPERTY_ENABLED,
668                             "Dropdown",
669                             PropertyNames.PROPERTY_HEIGHT,
670                             PropertyNames.PROPERTY_HELPURL,
671                             "LineCount",
672                             PropertyNames.PROPERTY_POSITION_X,
673                             PropertyNames.PROPERTY_POSITION_Y,
674                             PropertyNames.PROPERTY_STEP,
675                             PropertyNames.STRING_ITEM_LIST,
676                             PropertyNames.PROPERTY_TABINDEX,
677                             PropertyNames.PROPERTY_WIDTH
678                         },
679                         new Object[]
680                         {
681                             Boolean.valueOf(isEnabled()),
682                             Boolean.TRUE,
683                             Integer.valueOf(13),
684                             HelpIds.getHelpIdString(_firstRowHelpID++),
685                             Short.valueOf((short) sLogicOperators.length /* 7 */),
686                             Integer.valueOf(nPosX2),
687                             Integer.valueOf(iCompPosY + 23),
688                             IStep,
689                             sLogicOperators,
690                             Short.valueOf(curtabindex++),
691                             Integer.valueOf(nOperatorWidth)
692                         });
693                 ControlElements[SOTXTVALUE] = CurUnoDialog.insertFormattedField("txtValue" + sCompSuffix, SO_TEXTFIELDLIST[Index], new TextListenerImpl(),
694                         new String[]
695                         {
696                             PropertyNames.PROPERTY_ENABLED,
697                             PropertyNames.PROPERTY_HEIGHT,
698                             PropertyNames.PROPERTY_HELPURL,
699                             PropertyNames.PROPERTY_POSITION_X,
700                             PropertyNames.PROPERTY_POSITION_Y,
701                             PropertyNames.PROPERTY_STEP,
702                             PropertyNames.PROPERTY_TABINDEX,
703                             PropertyNames.PROPERTY_WIDTH
704                         },
705                         new Object[]
706                         {
707                             Boolean.valueOf(isEnabled()),
708                             Integer.valueOf(13),
709                             HelpIds.getHelpIdString(_firstRowHelpID++),
710                             Integer.valueOf(nPosX3),
711                             Integer.valueOf(iCompPosY + 23),
712                             IStep,
713                             Short.valueOf(curtabindex++),
714                             Integer.valueOf(nValueWidth)
715                         });
716             }
717             catch (Exception exception)
718             {
719                 exception.printStackTrace(System.out);
720             }
721         }
722 
723         /**
724          * @returns true if the current condition is complete, all needed fields are filled with values.
725          * So we can enable the next.
726          */
isConditionComplete()727         boolean isConditionComplete()
728         {
729             try
730             {
731                 short[] SelFields = (short[]) AnyConverter.toArray(Helper.getUnoPropertyValue(UnoDialog.getModel(ControlElements[SOLSTFIELDNAME]), PropertyNames.SELECTED_ITEMS));
732                 if (SelFields.length > 0)
733                 {
734                     int nSelOperator = getSelectedOperator();
735                     // short[] SelOperator = (short[]) AnyConverter.toArray(Helper.getUnoPropertyValue(UnoDialog.getModel(ControlElements[SOLSTOPERATOR]), PropertyNames.SELECTED_ITEMS));
736                     if (nSelOperator == com.sun.star.sdb.SQLFilterOperator.SQLNULL
737                             || /* is null */ nSelOperator == com.sun.star.sdb.SQLFilterOperator.NOT_SQLNULL) /* is not null */
738 
739                     {
740                         // disable value field
741                         Helper.setUnoPropertyValue(UnoDialog.getModel(ControlElements[2]), PropertyNames.PROPERTY_ENABLED, Boolean.FALSE);
742                         Helper.setUnoPropertyValue(UnoDialog.getModel(ControlElements[SOTXTVALUE]), PropertyNames.PROPERTY_ENABLED, Boolean.FALSE);
743 
744                         return true;
745                     }
746                     else
747                     {
748                         // enable value field
749                         Helper.setUnoPropertyValue(UnoDialog.getModel(ControlElements[2]), PropertyNames.PROPERTY_ENABLED, Boolean.TRUE);
750                         Helper.setUnoPropertyValue(UnoDialog.getModel(ControlElements[SOTXTVALUE]), PropertyNames.PROPERTY_ENABLED, Boolean.TRUE);
751                     }
752                     if (nSelOperator != -1)
753                     {
754                         Object oValue = Helper.getUnoPropertyValue(UnoDialog.getModel(ControlElements[SOTXTVALUE]), "EffectiveValue");
755                         if (!AnyConverter.isVoid(oValue))
756                         {
757                             String sValue = (String.valueOf(oValue));
758                             return (!sValue.equals(PropertyNames.EMPTY_STRING));
759                         }
760                     }
761                 }
762                 return false;
763             }
764             catch (Exception exception)
765             {
766                 exception.printStackTrace(System.out);
767                 return false;
768             }
769         }
770 
setCondition(PropertyValue _filtercondition)771         protected void setCondition(PropertyValue _filtercondition)
772         {
773             try
774             {
775                 XListBox xFieldsListBox = UnoRuntime.queryInterface(XListBox.class, ControlElements[SOLSTFIELDNAME]);
776                 xFieldsListBox.selectItem(_filtercondition.Name, true);
777                 XListBox xOperatorListBox = UnoRuntime.queryInterface(XListBox.class, ControlElements[SOLSTOPERATOR]);
778                 xOperatorListBox.selectItemPos((short) (_filtercondition.Handle - 1), true);
779 
780                 if (AnyConverter.isString(_filtercondition.Value))
781                 {
782                     String sValue = AnyConverter.toString(_filtercondition.Value);
783                     if (sValue.indexOf("{D '") > -1)
784                     {
785                         sValue = JavaTools.replaceSubString(sValue, PropertyNames.EMPTY_STRING, "{D '");
786                         sValue = JavaTools.replaceSubString(sValue, PropertyNames.EMPTY_STRING, "' }");
787                         try
788                         {
789                             oQueryMetaData.getNumberFormatter().convertStringToNumber(iDateFormat, sValue);
790                         }
791                         catch (java.lang.Exception ex)
792                         {
793                             Logger.getLogger(FilterComponent.class.getName()).log(Level.SEVERE, null, ex);
794                         }
795                     }
796                     else if (sValue.indexOf("{T '") > -1)
797                     {
798                         sValue = JavaTools.replaceSubString(sValue, PropertyNames.EMPTY_STRING, "{T '");
799                         sValue = JavaTools.replaceSubString(sValue, PropertyNames.EMPTY_STRING, "' }");
800                         try
801                         {
802                             oQueryMetaData.getNumberFormatter().convertStringToNumber(iTimeFormat, sValue);
803                         }
804                         catch (java.lang.Exception ex)
805                         {
806                             Logger.getLogger(FilterComponent.class.getName()).log(Level.SEVERE, null, ex);
807                         }
808                     }
809                 }
810                 else if (AnyConverter.isBoolean(_filtercondition.Value))
811                 {
812                     boolean b = AnyConverter.toBoolean(_filtercondition.Value);
813                     double nValue = 0.0;
814                     if (b)
815                     {
816                         nValue = 1.0;
817                     }
818                     Helper.setUnoPropertyValue(UnoDialog.getModel(ControlElements[SOTXTVALUE]), "EffectiveValue", nValue);
819                 }
820                 else
821                 {
822                     Helper.setUnoPropertyValue(UnoDialog.getModel(ControlElements[SOTXTVALUE]), "EffectiveValue", _filtercondition.Value);
823                 }
824             }
825             catch (Exception e)
826             {
827                 e.printStackTrace(System.out);
828             }
829         }
830 
setFieldNames(String[] _FieldNames)831         protected void setFieldNames(String[] _FieldNames)
832         {
833             Helper.setUnoPropertyValue(UnoDialog.getModel(ControlElements[SOLSTFIELDNAME]), PropertyNames.STRING_ITEM_LIST, _FieldNames);
834             FieldNames = _FieldNames;
835         }
836 
isEnabled()837         protected boolean isEnabled()
838         {
839             return m_bEnabled;
840         }
841 
settovoid()842         private void settovoid()
843         {
844             WizardDialog.deselectListBox(ControlElements[SOLSTFIELDNAME]);
845             WizardDialog.deselectListBox(ControlElements[SOLSTOPERATOR]);
846             Helper.setUnoPropertyValue(UnoDialog.getModel(ControlElements[SOTXTVALUE]), "EffectiveValue", com.sun.star.uno.Any.VOID);
847         }
848 
setEnabled(boolean _bEnabled)849         private void setEnabled(boolean _bEnabled)
850         {
851             // enable all Controls Fieldname, operator, value
852             for (int i = 0; i < ControlElements.length; i++)
853             {
854                 Helper.setUnoPropertyValue(UnoDialog.getModel(ControlElements[i]), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(_bEnabled));
855             }
856             m_bEnabled = _bEnabled;
857             if (isEnabled())
858             {
859                 short[] iselected = new short[]
860                 {
861                 };
862                 try
863                 {
864                     iselected = (short[]) AnyConverter.toArray(Helper.getUnoPropertyValue(UnoDialog.getModel(ControlElements[SOLSTOPERATOR]), PropertyNames.SELECTED_ITEMS));
865                 }
866                 catch (Exception e)
867                 {
868                     e.printStackTrace(System.out);
869                 }
870                 if ((iselected.length) == 0)
871                 {
872                     Helper.setUnoPropertyValue(UnoDialog.getModel(ControlElements[SOLSTOPERATOR]), PropertyNames.SELECTED_ITEMS, new short[]
873                             {
874                                 0
875                             });
876                 }
877             }
878             else if (!isConditionComplete())
879             {
880                 WizardDialog.deselectListBox(ControlElements[SOLSTOPERATOR]);
881             }
882         }
883 
getSelectedFieldName()884         protected String getSelectedFieldName()
885         {
886             try
887             {
888                 short[] SelFields = (short[]) AnyConverter.toArray(Helper.getUnoPropertyValue(UnoDialog.getModel(ControlElements[SOLSTFIELDNAME]), PropertyNames.SELECTED_ITEMS));
889                 return FieldNames[SelFields[0]];
890             }
891             catch (Exception exception)
892             {
893                 exception.printStackTrace(System.out);
894             }
895             return null;
896         }
897 
898         // TODO: check if it is really useful to match the indices of the listbox the API constants
899         // =, <>, <, >, <=, >=, like, !like, is null, !is null
getSelectedOperator()900         protected short getSelectedOperator()
901         {
902             try
903             {
904                 short[] SelFields = (short[]) AnyConverter.toArray(Helper.getUnoPropertyValue(UnoDialog.getModel(ControlElements[SOLSTOPERATOR]), PropertyNames.SELECTED_ITEMS));
905                 switch (SelFields[0])
906                 {
907                     case 0:
908                         return com.sun.star.sdb.SQLFilterOperator.EQUAL;
909                     case 1:
910                         return com.sun.star.sdb.SQLFilterOperator.NOT_EQUAL;
911                     case 2:
912                         return com.sun.star.sdb.SQLFilterOperator.LESS;
913                     case 3:
914                         return com.sun.star.sdb.SQLFilterOperator.GREATER;
915                     case 4:
916                         return com.sun.star.sdb.SQLFilterOperator.LESS_EQUAL;
917                     case 5:
918                         return com.sun.star.sdb.SQLFilterOperator.GREATER_EQUAL;
919                     case 6:
920                         return com.sun.star.sdb.SQLFilterOperator.LIKE;
921                     case 7:
922                         return com.sun.star.sdb.SQLFilterOperator.NOT_LIKE;
923                     case 8:
924                         return com.sun.star.sdb.SQLFilterOperator.SQLNULL;
925                     case 9:
926                         return com.sun.star.sdb.SQLFilterOperator.NOT_SQLNULL;
927                     default:
928                         return -1;
929                 }
930             }
931             catch (Exception exception)
932             {
933                 exception.printStackTrace(System.out);
934             }
935             return -1;
936         }
937 
938         // TODO make a difference between Text and Numbers
getValue()939         protected Object getValue()
940         {
941             return (Helper.getUnoPropertyValue(UnoDialog.getModel(ControlElements[SOTXTVALUE]), "EffectiveValue"));
942         }
943 
getText()944         protected Object getText()
945         {
946             return (Helper.getUnoPropertyValue(UnoDialog.getModel(ControlElements[SOTXTVALUE]), "Text"));
947         }
948 
getDateTimeString(boolean bgetDate)949         protected String getDateTimeString(boolean bgetDate)
950         {
951             double dblValue = ((Double) getValue()).doubleValue();
952             NumberFormatter oNumberFormatter = oQueryMetaData.getNumberFormatter();
953             return oNumberFormatter.convertNumberToString(iDateTimeFormat, dblValue);
954         }
955     }
956 }
957