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.beans.PropertyChangeEvent;
26 
27 import com.sun.star.wizards.common.*;
28 import com.sun.star.awt.*;
29 import java.util.ArrayList;
30 
31 public class SortingComponent
32 {
33 
34     com.sun.star.lang.XMultiServiceFactory xMSF;
35     WizardDialog CurUnoDialog;
36     int MaxSortIndex = -1;
37     public String[][] FieldNames;
38     static String sNoSorting;
39     static String sSortCriteriaisduplicate;
40     static String[] sSortHeader = new String[4];
41     static String[] sSortAscend = new String[4];
42     static String[] sSortDescend = new String[4];
43     static short[] bOldSortValues = new short[4];
44     public int MAXSORTCRITERIAINDEX = 3;
45     final int SOFIRSTSORTLST = 0;
46     final int SOSECSORTLST = 1;
47     final int SOTHIRDSORTLST = 2;
48     final int SOFOURTHSORTLST = 3;
49     final int[] SOSORTLST = new int[]
50     {
51         SOFIRSTSORTLST, SOSECSORTLST, SOTHIRDSORTLST, SOFOURTHSORTLST
52     };
53     XListBox[] xSortListBox = new XListBox[4];
54 
55     class ItemListenerImpl implements com.sun.star.awt.XItemListener
56     {
57 
itemStateChanged(ItemEvent EventObject)58         public void itemStateChanged(ItemEvent EventObject)
59         {
60             Helper.setUnoPropertyValue(CurUnoDialog.xDialogModel, PropertyNames.PROPERTY_ENABLED, Boolean.FALSE);
61             int ikey = CurUnoDialog.getControlKey(EventObject.Source, CurUnoDialog.ControlList);
62             enableNextSortListBox(ikey);
63             Helper.setUnoPropertyValue(CurUnoDialog.xDialogModel, PropertyNames.PROPERTY_ENABLED, Boolean.TRUE);
64         }
65 
disposing(com.sun.star.lang.EventObject eventObject)66         public void disposing(com.sun.star.lang.EventObject eventObject)
67         {
68         }
69     }
70 
SortingComponent(WizardDialog CurUnoDialog, int iStep, int iCompPosX, int iCompPosY, int iCompWidth, int FirstHelpIndex)71     public SortingComponent(WizardDialog CurUnoDialog, int iStep, int iCompPosX, int iCompPosY, int iCompWidth, int FirstHelpIndex)
72     {
73         try
74         {
75             this.CurUnoDialog = CurUnoDialog;
76             short curtabindex = UnoDialog.setInitialTabindex(iStep);
77             xMSF = CurUnoDialog.xMSF;
78             Integer IStep = new Integer(iStep);
79             Integer ICompPosX = new Integer(iCompPosX);
80             Integer ICompPosY = new Integer(iCompPosY);
81             Integer ICompWidth = new Integer(iCompWidth);
82 
83             Integer IListBoxPosX = new Integer(iCompPosX + 6);
84             int iOptButtonWidth = 65;
85             Integer IOptButtonWidth = new Integer(iOptButtonWidth);
86             Integer IListBoxWidth = new Integer(iCompWidth - iOptButtonWidth - 12);
87             Integer IOptButtonPosX = new Integer(IListBoxPosX.intValue() + IListBoxWidth.intValue() + 6);
88             getResources();
89             com.sun.star.awt.XWindow[] xListBoxWindow = new com.sun.star.awt.XWindow[4];
90             boolean bDoEnable;
91             String HIDString;
92             int iCurPosY = iCompPosY;
93             for (int i = 0; i < 4; i++)
94             {
95                 bDoEnable = (i < 2);
96                 CurUnoDialog.insertControlModel("com.sun.star.awt.UnoControlFixedLineModel", "lblSort" + Integer.toString(i + 1), new String[]
97                         {
98                             PropertyNames.PROPERTY_ENABLED, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.ORIENTATION, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
99                         }, new Object[]
100                         {
101                                 Boolean.valueOf(bDoEnable), 8, sSortHeader[i], 0, ICompPosX, new Integer(iCurPosY), IStep, new Short(curtabindex++), ICompWidth
102                         });
103 
104                 HIDString = HelpIds.getHelpIdString(FirstHelpIndex);
105                 xSortListBox[i] = CurUnoDialog.insertListBox("lstSort" + Integer.toString(i + 1), SOSORTLST[i], null, new ItemListenerImpl(), new String[]
106                         {
107                             "Dropdown", PropertyNames.PROPERTY_ENABLED, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, "LineCount", PropertyNames.PROPERTY_NAME, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
108                         }, new Object[]
109                         {
110                             true, bDoEnable, 12, HIDString, new Short(UnoDialog.getListBoxLineCount()), "lstSort" + new Integer(i + 1), IListBoxPosX, new Integer(iCurPosY + 14), IStep, new Short(curtabindex++), IListBoxWidth
111                         }); //new Short((short) (17+i*4))
112 
113                 HIDString = HelpIds.getHelpIdString(FirstHelpIndex + 1);
114                 XRadioButton xRadioButtonAsc = CurUnoDialog.insertRadioButton("optAscend" + Integer.toString(i + 1), 0, new String[]
115                         {
116                             PropertyNames.PROPERTY_ENABLED, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STATE, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, "Tag", PropertyNames.PROPERTY_WIDTH
117                         }, new Object[]
118                         {
119                             bDoEnable, 10, HIDString, sSortAscend[i], IOptButtonPosX, new Integer(iCurPosY + 10), new Short((short) 1), IStep, new Short(curtabindex++), PropertyNames.ASC, IOptButtonWidth
120                         }); //, new Short((short) (18+i*4))
121 
122                 HIDString = HelpIds.getHelpIdString(FirstHelpIndex + 2);
123                 XRadioButton xRadioButtonDesc = CurUnoDialog.insertRadioButton("optDescend" + Integer.toString(i + 1), 0, new String[]
124                         {
125                             PropertyNames.PROPERTY_ENABLED, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STATE, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, "Tag", PropertyNames.PROPERTY_WIDTH
126                         }, new Object[]
127                         {
128                             bDoEnable, 10, HIDString, sSortDescend[i], IOptButtonPosX, new Integer(iCurPosY + 24), new Short((short) 0), IStep, new Short(curtabindex++), "DESC", IOptButtonWidth
129                         }); //, new Short((short) (19+i*4))
130                 iCurPosY = iCurPosY + 36;
131                 FirstHelpIndex += 3;
132             }
133         }
134         catch (Exception exception)
135         {
136             exception.printStackTrace(System.out);
137         }
138     }
139 
getResources()140     private boolean getResources()
141     {
142         sSortHeader[0] = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 20);
143         sSortHeader[1] = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 21);
144         sSortHeader[2] = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 51);
145         sSortHeader[3] = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 52);
146         sSortAscend[0] = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 36);
147         sSortAscend[1] = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 53);
148         sSortAscend[2] = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 54);
149         sSortAscend[3] = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 55);
150         sSortDescend[0] = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 37);
151         sSortDescend[1] = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 56);
152         sSortDescend[2] = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 57);
153         sSortDescend[3] = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 58);
154         sSortCriteriaisduplicate = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 74);
155         sNoSorting = CurUnoDialog.m_oResource.getResText(UIConsts.RID_REPORT + 8);
156         return true;
157     }
158 
initialize(String[] _FieldNames, String[][] _SortFieldNames)159     public void initialize(String[] _FieldNames, String[][] _SortFieldNames)
160     {
161         int FieldCount = _FieldNames.length;
162         String[] ViewFieldNames = new String[FieldCount + 1];
163         ViewFieldNames[0] = sNoSorting;
164         System.arraycopy(_FieldNames, 0, ViewFieldNames, 1, FieldCount);
165         short[] SelList = null;
166         for (int i = 0; i < 4; i++)
167         {
168             if (i < _SortFieldNames.length)
169             {
170                 SelList = new short[]
171                         {
172                             (short) (JavaTools.FieldInList(_FieldNames, _SortFieldNames[i][0]) + 1)
173                         };
174             }
175             else
176             {
177                 SelList = new short[]
178                         {
179                             (short) 0
180                         };
181             }
182             CurUnoDialog.setControlProperty("lstSort" + Integer.toString(i + 1), PropertyNames.STRING_ITEM_LIST, ViewFieldNames);
183             CurUnoDialog.setControlProperty("lstSort" + Integer.toString(i + 1), PropertyNames.SELECTED_ITEMS, SelList);
184             toggleSortListBox(i, (i <= _SortFieldNames.length));
185         }
186     }
187 
setMaxSortIndex()188     private void setMaxSortIndex()
189     {
190         MaxSortIndex = -1;
191         for (int i = 0; i <= MAXSORTCRITERIAINDEX; i++)
192         {
193             if (xSortListBox[i].getSelectedItemPos() > 0)
194             {
195                 MaxSortIndex += 1;
196             }
197         }
198     }
199 
200     /**
201      * sets the controls of a Sorting criterion to readonly or not.
202      * @param _index index of the Sorting criterion
203      * @param _breadonly
204      */
setReadOnly(int _index, boolean _breadonly)205     public void setReadOnly(int _index, boolean _breadonly)
206     {
207         CurUnoDialog.setControlProperty("lstSort" + Integer.toString(_index + 1), PropertyNames.READ_ONLY, Boolean.valueOf(_breadonly));
208     }
209 
210     /**
211      *
212      * @param _index the first Sorting criterion in which 'ReadOnly is set to 'false'
213      * @param _bcomplete
214      */
setReadOnlyUntil(int _index, boolean _bcomplete)215     public void setReadOnlyUntil(int _index, boolean _bcomplete)
216     {
217         for (int i = 0; i <= 4; i++)
218         {
219             boolean breadonly = i < _index;
220             setReadOnly(i, breadonly);
221         }
222     }
223 
224     private void enableNextSortListBox(int CurIndex)
225     {
226         try
227         {
228             setMaxSortIndex();
229             boolean bDoEnable = (xSortListBox[CurIndex].getSelectedItemPos() != 0);
230             if (!bDoEnable)
231             {
232                 moveupSortItems(CurIndex, bDoEnable); //disableListBoxesfromIndex(CurIndex);
233             }
234             else
235             {
236                 toggleSortListBox(CurIndex + 1, true);
237             }
238         }
239         catch (Exception exception)
240         {
241             exception.printStackTrace(System.out);
242         }
243     }
244 
245     public String[][] getSortFieldNames()
246     {
247         try
248         {
249             short iCurState;
250             String CurFieldName;
251             setMaxSortIndex();
252             // String[][] SortFieldNames = new String[MaxSortIndex + 1][2];
253             ArrayList<String[]> SortFieldNames = new ArrayList<String[]>();
254             ArrayList<String> SortDescriptions = new ArrayList<String>();
255             for (int i = 0; i <= MaxSortIndex; i++)
256             {
257                 if (!((Boolean) CurUnoDialog.getControlProperty("lstSort" + (i + 1), PropertyNames.READ_ONLY)))
258                 {
259                     CurFieldName = xSortListBox[i].getSelectedItem();
260                     SortDescriptions.add(CurFieldName);
261                     iCurState = ((Short) CurUnoDialog.getControlProperty("optAscend" + Integer.toString(i + 1), PropertyNames.PROPERTY_STATE)).shortValue();
262                     SortFieldNames.add(new String[]{CurFieldName,iCurState == 1 ? PropertyNames.ASC :"DESC" });
263                 }
264             }
265             // When searching for a duplicate entry we can neglect wether the entries are to be sorted ascending or descending
266             // TODO for the future we should deliver a messagebox when two different sorting modes have been applied to one field
267             int iduplicate = JavaTools.getDuplicateFieldIndex(SortDescriptions.toArray(new String[SortDescriptions.size()]));
268             if (iduplicate != -1)
269             {
270                 String sLocSortCriteriaisduplicate = JavaTools.replaceSubString(sSortCriteriaisduplicate, SortFieldNames.get(iduplicate)[0], "<FIELDNAME>");
271                 CurUnoDialog.showMessageBox("WarningBox", VclWindowPeerAttribute.OK, sLocSortCriteriaisduplicate);
272                 CurUnoDialog.vetoableChange(new PropertyChangeEvent(CurUnoDialog, "Steps", 1, 2));
273                 CurUnoDialog.setFocus("lstSort" + (iduplicate + 1));
274                 return new String[][]
275                         {
276                         };
277             }
278             else
279             {
280                 return SortFieldNames.toArray(new String[SortFieldNames.size()][2]);
281             }
282         }
283         catch (Exception exception)
284         {
285             exception.printStackTrace(System.out);
286             return null;
287         }
288     }
289 
290     public void disableListBoxesfromIndex(int CurIndex)
291     {
292         if (CurIndex < MAXSORTCRITERIAINDEX)
293         {
294             for (int i = CurIndex + 1; i <= MAXSORTCRITERIAINDEX; i++)
295             {
296                 toggleSortListBox(i, (false));
297                 if (i < MaxSortIndex)
298                 {
299                     CurUnoDialog.setControlProperty("lstSort" + Integer.toString(i + 2), PropertyNames.SELECTED_ITEMS, new short[]
300                             {
301                                 0
302                             });
303                 }
304                 //          xSortListBox[i+1].selectItemPos((short)0, true);
305             }
306             CurUnoDialog.setFocus("lblSort" + new Integer(CurIndex + 1));
307             MaxSortIndex = CurIndex - 1;
308         }
309     }
310 
311     //  The following code can be reactivated in a future version when task #100799 will be fixed
312     private void moveupSortItems(int CurIndex, boolean bDoEnable)
313     {
314         short iNextItemPos;
315         if ((!bDoEnable) && (MAXSORTCRITERIAINDEX > CurIndex))
316         {
317             for (int i = CurIndex; i < MAXSORTCRITERIAINDEX; i++)
318             {
319                 iNextItemPos = xSortListBox[i + 1].getSelectedItemPos();
320                 if (iNextItemPos != 0)
321                 {
322                     CurUnoDialog.setControlProperty("lstSort" + Integer.toString(i + 1), PropertyNames.SELECTED_ITEMS, new short[]
323                             {
324                                 iNextItemPos
325                             });
326                     CurUnoDialog.setControlProperty("lstSort" + Integer.toString(i + 2), PropertyNames.SELECTED_ITEMS, new short[]
327                             {
328                             });
329                     toggleSortListBox(i, true);
330                     CurUnoDialog.setControlProperty("lstSort" + Integer.toString(i + 2), PropertyNames.SELECTED_ITEMS, new short[]
331                             {
332                                 0
333                             });
334                 }
335             }
336             if (MaxSortIndex < xSortListBox.length - 2)
337             {
338                 toggleSortListBox(MaxSortIndex + 2, false);
339             }
340         }
341         else
342         {
343             toggleSortListBox(CurIndex + 1, bDoEnable);
344         }
345     }
346 
347     private void toggleSortListBox(int CurIndex, boolean bDoEnable)
348     {
349         try
350         {
351             if (CurIndex < xSortListBox.length)
352             {
353                 CurUnoDialog.setControlProperty("lblSort" + Integer.toString(CurIndex + 1), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bDoEnable));
354                 CurUnoDialog.setControlProperty("lstSort" + Integer.toString(CurIndex + 1), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bDoEnable));
355                 CurUnoDialog.setControlProperty("optAscend" + Integer.toString(CurIndex + 1), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bDoEnable));
356                 CurUnoDialog.setControlProperty("optDescend" + Integer.toString(CurIndex + 1), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bDoEnable));
357                 if (!bDoEnable)
358                 {
359                     CurUnoDialog.setControlProperty("lstSort" + Integer.toString(CurIndex + 1), PropertyNames.SELECTED_ITEMS, new short[]
360                             {
361                                 0
362                             });
363                 }
364             }
365         }
366         catch (Exception exception)
367         {
368             exception.printStackTrace(System.out);
369         }
370     }
371 }
372