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 com.sun.star.wizards.common.*;
26 import com.sun.star.wizards.db.*;
27 // import com.sun.star.awt.XWindow;
28 import com.sun.star.lang.IllegalArgumentException;
29 import com.sun.star.sdb.CommandType;
30 // import com.sun.star.sdbc.SQLException;
31 import com.sun.star.uno.AnyConverter;
32 // import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.awt.*;
34 import com.sun.star.beans.PropertyValue;
35 import java.text.Collator;
36 import java.util.Comparator;
37 
38 public class CommandFieldSelection extends FieldSelection implements Comparator
39 {
40 
41     private CommandMetaData CurDBMetaData;
42     private XListBox xTableListBox;
43     private XFixedText xlblTable;
44     // private String SFILLUPFIELDSLISTBOX = "fillUpFieldsListbox";
45     private String sTableListBoxName;
46     private String sTableLabelName;
47     private String sQueryPrefix;
48     private String sTablePrefix;
49     private short m_iSelPos = -1;
50     private short iOldSelPos = -1;
51     private boolean bpreselectCommand = true;
52     private boolean bgetQueries;
53     // boolean AppendMode;
54     private WizardDialog oWizardDialog;
55     private Collator aCollator = null;
56 
57     class ItemListenerImpl implements com.sun.star.awt.XItemListener
58     {
59 
itemStateChanged(com.sun.star.awt.ItemEvent EventObject)60         public void itemStateChanged(com.sun.star.awt.ItemEvent EventObject)
61         {
62             short[] SelItems = (short[]) CurUnoDialog.getControlProperty(sTableListBoxName, PropertyNames.SELECTED_ITEMS);
63             if (SelItems.length > 0)
64             {
65                 iOldSelPos = m_iSelPos;
66                 m_iSelPos = SelItems[0];
67                 if ((m_iSelPos > -1) && (m_iSelPos != iOldSelPos))
68                 {
69                     if (!AppendMode)
70                     {
71                         oWizardDialog.enablefromStep(IStep.intValue() + 1, false);
72                     }
73                     fillUpFieldsListbox();
74                 }
75             }
76         }
77 
disposing(com.sun.star.lang.EventObject eventObject)78         public void disposing(com.sun.star.lang.EventObject eventObject)
79         {
80         }
81     }
82 
83     /**
84      * instantiates a CommandFieldSelection with a preselected command
85      * @param _CurUnoDialog
86      * @param _CurDBMetaData
87      * @param iStep
88      * @param _iHeight
89      * @param _reslblFields
90      * @param _reslblSelFields
91      * @param _reslblTables
92      * @param _bgetQueries
93      * @param _ifirstHID
94      */
CommandFieldSelection(WizardDialog _CurUnoDialog, CommandMetaData _CurDBMetaData, int iStep, int _iHeight, String _reslblFields, String _reslblSelFields, String _reslblTables, boolean _bgetQueries, int _ifirstHID)95     public CommandFieldSelection(WizardDialog _CurUnoDialog, CommandMetaData _CurDBMetaData, int iStep, int _iHeight, String _reslblFields, String _reslblSelFields, String _reslblTables, boolean _bgetQueries, int _ifirstHID)
96     {
97         super(_CurUnoDialog, iStep, 95, 57, 210, _iHeight, _reslblFields, _reslblSelFields, (_ifirstHID + 1), true);
98         insertControls(_CurDBMetaData, _bgetQueries, _reslblTables);
99         oWizardDialog = (WizardDialog) CurUnoDialog;
100     }
101 
102     /**
103      * instantiates a CommandFieldSelection with a preselected command
104      * @param _CurUnoDialog
105      * @param _CurDBMetaData
106      * @param _iHeight
107      * @param _reslblFields
108      * @param _reslblSelFields
109      * @param _reslblTables
110      * @param _bgetQueries
111      * @param _ifirstHID
112      */
CommandFieldSelection(UnoDialog _CurUnoDialog, CommandMetaData _CurDBMetaData, int _iHeight, String _reslblFields, String _reslblSelFields, String _reslblTables, boolean _bgetQueries, int _ifirstHID)113     public CommandFieldSelection(UnoDialog _CurUnoDialog, CommandMetaData _CurDBMetaData, int _iHeight, String _reslblFields, String _reslblSelFields, String _reslblTables, boolean _bgetQueries, int _ifirstHID)
114     {
115         super(_CurUnoDialog, 1, 95, 57, 210, _iHeight, _reslblFields, _reslblSelFields, (_ifirstHID + 1), true);
116         insertControls(_CurDBMetaData, _bgetQueries, _reslblTables);
117         oWizardDialog = (WizardDialog) CurUnoDialog;
118     }
119 
insertControls(CommandMetaData _CurDBMetaData, boolean _bgetQueries, String _reslblTables)120     private void insertControls(CommandMetaData _CurDBMetaData, boolean _bgetQueries, String _reslblTables)
121     {
122         try
123         {
124             this.AppendMode = !_bgetQueries;
125             this.bgetQueries = _bgetQueries;
126             this.CurDBMetaData = _CurDBMetaData;
127             toggleListboxControls(Boolean.FALSE);
128             sTableLabelName = "lblTables_" + super.sIncSuffix;
129             sTableListBoxName = "lstTables_" + super.sIncSuffix;
130             sTablePrefix = getTablePrefix();
131             sQueryPrefix = getQueryPrefix();
132             Integer LabelWidth = new Integer(getListboxWidth().intValue() + 6);
133             // Label 'Tables or Queries'
134             xlblTable = CurUnoDialog.insertLabel(sTableLabelName,
135                     new String[]
136                     {
137                         PropertyNames.PROPERTY_ENABLED, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
138                     },
139                     new Object[]
140                     {
141                         Boolean.FALSE, 8, _reslblTables, 95, 27, IStep, new Short((short) 3), LabelWidth
142                     });
143             // DropDown Listbox TableNames
144             xTableListBox = CurUnoDialog.insertListBox(sTableListBoxName, 0, null, new ItemListenerImpl(),
145                     new String[]
146                     {
147                         "Dropdown", PropertyNames.PROPERTY_ENABLED, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, "LineCount", PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
148                     },
149                     new Object[]
150                     {
151                         Boolean.TRUE, Boolean.FALSE, 12, HelpIds.getHelpIdString(super.FirstHelpIndex - 1), new Short(UnoDialog.getListBoxLineCount()), 95, 37, IStep, new Short((short) 4), getListboxWidth()
152                     });
153             // XWindow xTableListBoxWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, xTableListBox);
154             fillupCommandListBox();
155         }
156         catch (Exception exception)
157         {
158             exception.printStackTrace(System.out);
159         }
160     }
161 
162     /**
163      * @return Returns the sQueryPrefix.
164      */
getQueryPrefix()165     public String getQueryPrefix()
166     {
167         if (sQueryPrefix == null)
168         {
169             sQueryPrefix = CurUnoDialog.m_oResource.getResText(UIConsts.RID_QUERY + 22);
170         }
171         return sQueryPrefix;
172     }
173 
getCommandPrefix(int _nCommandType)174     private String getCommandPrefix(int _nCommandType)
175     {
176         if (_nCommandType == CommandType.TABLE)
177         {
178             return getTablePrefix();
179         }
180         else if (_nCommandType == CommandType.QUERY)
181         {
182             return getQueryPrefix();
183         }
184         else
185         {
186             return PropertyNames.EMPTY_STRING;
187         }
188     }
189 
190     /**
191      * @return Returns the sTablePrefix.
192      */
getTablePrefix()193     public String getTablePrefix()
194     {
195         if (sTablePrefix == null)
196         {
197             sTablePrefix = CurUnoDialog.m_oResource.getResText(UIConsts.RID_QUERY + 21);
198         }
199         return sTablePrefix;
200     }
201 
getselectedItemPos()202     private short getselectedItemPos()
203     {
204         short[] iSelPoses = ((short[]) Helper.getUnoPropertyValue(UnoDialog.getModel(xTableListBox), PropertyNames.SELECTED_ITEMS));
205         if (iSelPoses.length > 0)
206         {
207             return iSelPoses[0];
208         }
209         else
210         {
211             if (this.bpreselectCommand)
212             {
213                 String[] sItemList = ((String[]) Helper.getUnoPropertyValue(UnoDialog.getModel(xTableListBox), PropertyNames.STRING_ITEM_LIST));
214                 if (sItemList.length > 0)
215                 {
216                     return (short) 0;
217                 }
218             }
219             return (short) -1;
220         }
221     }
222 
fillUpFieldsListbox()223     public void fillUpFieldsListbox()
224     {
225         try
226         {
227             boolean binitialize = false;
228             String curCommandName = PropertyNames.EMPTY_STRING;
229             //As the peer of the control might not yet exist we have to query the model for the SelectedItems
230             short iSelPos = getselectedItemPos();
231             // String[] sLocList = (String[]) CurUnoDialog.getControlProperty(sTableListBoxName, PropertyNames.STRING_ITEM_LIST);
232             final String sSelectedTableName = xTableListBox.getItem(iSelPos);
233             if (!bgetQueries)
234             {
235                 curCommandName = sSelectedTableName; // sLocList[iSelPos];
236                 CurDBMetaData.setTableByName(curCommandName);
237                 binitialize = CurDBMetaData.getFieldNamesOfCommand(curCommandName, CommandType.TABLE, AppendMode);
238             }
239             else
240             {
241                 if (sSelectedTableName.startsWith(sTablePrefix))
242                 {
243                     CurDBMetaData.setCommandType(CommandType.TABLE);
244                     curCommandName = JavaTools.replaceSubString(sSelectedTableName, PropertyNames.EMPTY_STRING, sTablePrefix);
245                     CurDBMetaData.setTableByName(curCommandName);
246                     binitialize = CurDBMetaData.getFieldNamesOfCommand(curCommandName, CommandType.TABLE, AppendMode);
247                 }
248                 else
249                 {
250                     CurDBMetaData.setCommandType(CommandType.QUERY);
251                     curCommandName = JavaTools.replaceSubString(sSelectedTableName, PropertyNames.EMPTY_STRING, sQueryPrefix);
252                     CurDBMetaData.setQueryByName(curCommandName);
253                     binitialize = CurDBMetaData.getFieldNamesOfCommand(curCommandName, CommandType.QUERY, AppendMode);
254                 }
255             }
256             if (binitialize)
257             {
258                 CurDBMetaData.setCommandName(curCommandName);
259                 if (CurDBMetaData.m_aAllFieldNames != null)
260                 {
261                     if (CurDBMetaData.m_aAllFieldNames.length > 0)
262                     {
263                         initialize(CurDBMetaData.m_aAllFieldNames, AppendMode, CurDBMetaData.getMaxColumnsInSelect());
264                         return;
265                     }
266                 }
267             }
268             emptyFieldsListBoxes();
269         }
270         catch (Exception exception)
271         {
272             exception.printStackTrace(System.out);
273         }
274     }
275 
276     /** returns the selected entry index in the commandListbox
277      *
278      * @return
279      * @throws com.sun.star.wizards.common.TerminateWizardException
280      */
fillupCommandListBox()281     private short fillupCommandListBox() throws com.sun.star.wizards.common.TerminateWizardException
282     {
283         short[] iSelArray = new short[0];
284         boolean bgetFields = false;
285         String[] ContentList = new String[0];
286         // CurDBMetaData.initCommandNames();
287         if (bgetQueries)
288         {
289             ContentList = new String[CurDBMetaData.getTableNames().length + CurDBMetaData.getQueryNames().length];
290             System.arraycopy(CurDBMetaData.getQueryNames(), 0, ContentList, CurDBMetaData.getTableNames().length, CurDBMetaData.getQueryNames().length);
291             ContentList = setPrefixinArray(ContentList, sQueryPrefix, CurDBMetaData.getTableNames().length, CurDBMetaData.getQueryNames().length);
292         }
293         else
294         {
295             ContentList = new String[CurDBMetaData.getTableNames().length];
296         }
297         System.arraycopy(CurDBMetaData.getTableNames(), 0, ContentList, 0, CurDBMetaData.getTableNames().length);
298         if (bgetQueries)
299         {
300             ContentList = setPrefixinArray(ContentList, sTablePrefix, 0, CurDBMetaData.getTableNames().length);
301         }
302         java.util.Arrays.sort(ContentList, this);
303         Helper.setUnoPropertyValue(UnoDialog.getModel(xTableListBox), PropertyNames.STRING_ITEM_LIST, ContentList);
304         short iSelPos = getselectedItemPos();
305         if (bpreselectCommand)
306         {
307             if (iSelPos > -1)
308             {
309                 bgetFields = true;
310                 iSelArray = new short[]
311                         {
312                             iSelPos
313                         };
314             }
315         }
316         else
317         {
318             emptyFieldsListBoxes();
319             iSelArray = new short[] {iSelPos};
320         }
321         Helper.setUnoPropertyValue(UnoDialog.getModel(xTableListBox), PropertyNames.SELECTED_ITEMS, iSelArray);
322         toggleCommandListBox(true);
323         if (bgetFields)
324         {
325             fillUpFieldsListbox();
326         }
327         return iSelPos;
328     }
329 
getCollator()330     private Collator getCollator()
331     {
332         if (this.aCollator == null)
333         {
334             com.sun.star.lang.Locale aOfficeLocale = Configuration.getOfficeLocale(this.CurDBMetaData.xMSF);
335             java.util.Locale aJavaLocale = new java.util.Locale(aOfficeLocale.Language, aOfficeLocale.Country, aOfficeLocale.Variant);
336             //Get the Collator for US English and set its strength to PRIMARY
337             this.aCollator = Collator.getInstance(aJavaLocale);
338             aCollator.setStrength(Collator.TERTIARY);
339         }
340         return aCollator;
341     }
342 
compare(Object _oObject1, Object _oObject2)343     public int compare(Object _oObject1, Object _oObject2)
344     {
345         return this.getCollator().compare(_oObject1, _oObject2);
346     }
347 
setPrefixinArray(String[] _ContentList, String _sprefix, int _startindex, int _nlen)348     private String[] setPrefixinArray(String[] _ContentList, String _sprefix, int _startindex, int _nlen)
349     {
350         for (int i = _startindex; i < _startindex + _nlen; i++)
351         {
352             _ContentList[i] = _sprefix + _ContentList[i];
353         }
354         return _ContentList;
355     }
356 
toggleCommandListBox(String[] _NewItems)357     public void toggleCommandListBox(String[] _NewItems)
358     {
359         boolean bdoenable = !(QueryMetaData.getIncludedCommandNames(_NewItems).length >= CurDBMetaData.getMaxTablesInSelect());
360         toggleCommandListBox(bdoenable);
361     }
362 
toggleCommandListBox(boolean _bdoenable)363     public void toggleCommandListBox(boolean _bdoenable)
364     {
365         Helper.setUnoPropertyValue(UnoDialog.getModel(xTableListBox), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(_bdoenable));
366         Helper.setUnoPropertyValue(UnoDialog.getModel(xlblTable), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(_bdoenable));
367     }
368 
getSelectedCommandName()369     public String getSelectedCommandName()
370     {
371         String sCommandname = xTableListBox.getSelectedItem();
372         if (sCommandname.startsWith(this.sTablePrefix))
373         {
374             return sCommandname.substring(sTablePrefix.length());
375         }
376         else if ((sCommandname.startsWith(this.sQueryPrefix)))
377         {
378             return sCommandname.substring(sQueryPrefix.length());
379         }
380         else
381         {
382             return sCommandname;
383         }
384     }
385 
getSelectedCommandType()386     public int getSelectedCommandType()
387     {
388         String sCommandname = xTableListBox.getSelectedItem();
389         if (sCommandname.startsWith(this.sTablePrefix))
390         {
391             return CommandType.TABLE;
392         }
393         else
394         {
395             return CommandType.QUERY;
396         }
397     }
398 
preselectCommand(PropertyValue[] _aPropertyValue, boolean _bReadOnly)399     public void preselectCommand(PropertyValue[] _aPropertyValue, boolean _bReadOnly)
400     {
401         try
402         {
403             if (Properties.hasPropertyValue(_aPropertyValue, PropertyNames.COMMAND_TYPE))
404             {
405                 int nCommandType = AnyConverter.toInt(Properties.getPropertyValue(_aPropertyValue, PropertyNames.COMMAND_TYPE));
406                 String sCommand = AnyConverter.toString(Properties.getPropertyValue(_aPropertyValue, PropertyNames.COMMAND));
407                 if (sCommand != null)
408                 {
409                     preselectCommand(sCommand, nCommandType, _bReadOnly);
410                 }
411             }
412         }
413         catch (IllegalArgumentException e)
414         {
415             e.printStackTrace();
416         }
417     }
418 
preselectCommand(String _selitem, int _nCommandType, boolean _bReadOnly)419     public void preselectCommand(String _selitem, int _nCommandType, boolean _bReadOnly)
420     {
421         if (_selitem.length() > 0)
422         {
423             String[] sitems = (String[]) Helper.getUnoPropertyValue(UnoDialog.getModel(xTableListBox), PropertyNames.STRING_ITEM_LIST);
424             String sPrefix = getCommandPrefix(_nCommandType);
425             short iselpos = (short) JavaTools.FieldInList(sitems, sPrefix + _selitem);
426             if (iselpos > -1)
427             {
428                 Helper.setUnoPropertyValue(UnoDialog.getModel(xTableListBox), PropertyNames.SELECTED_ITEMS, new short[]
429                         {
430                             iselpos
431                         });
432             }
433             this.fillUpFieldsListbox();
434         }
435         else
436         {
437             Helper.setUnoPropertyValue(UnoDialog.getModel(xTableListBox), PropertyNames.SELECTED_ITEMS, new short[]
438                     {
439                     });
440         }
441         if (_bReadOnly)
442         {
443             Helper.setUnoPropertyValue(UnoDialog.getModel(xTableListBox), PropertyNames.READ_ONLY, Boolean.valueOf(_selitem.length() > 0));
444         }
445     }
446 
preselectCommand(String _selitem, boolean _bReadOnly)447     public void preselectCommand(String _selitem, boolean _bReadOnly)
448     {
449         if (_selitem.length() > 0)
450         {
451             String[] sitems = (String[]) Helper.getUnoPropertyValue(UnoDialog.getModel(xTableListBox), PropertyNames.STRING_ITEM_LIST);
452             short iselpos = (short) JavaTools.FieldInList(sitems, getTablePrefix() + _selitem);
453             if (iselpos > -1)
454             {
455                 Helper.setUnoPropertyValue(UnoDialog.getModel(xTableListBox), PropertyNames.SELECTED_ITEMS, new short[] { iselpos });
456             }
457             this.fillUpFieldsListbox();
458         }
459         else
460         {
461             Helper.setUnoPropertyValue(UnoDialog.getModel(xTableListBox), PropertyNames.SELECTED_ITEMS, new short[] {} );
462             this.fillUpFieldsListbox();
463         }
464         if (_bReadOnly)
465         {
466             Helper.setUnoPropertyValue(UnoDialog.getModel(xTableListBox), PropertyNames.READ_ONLY, Boolean.valueOf(_selitem.length() > 0));
467         }
468         toggleListboxButtons((short)-1,(short)-1);
469     }
470 }
471