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