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.table;
24 
25 import com.sun.star.awt.FontDescriptor;
26 import com.sun.star.awt.ItemEvent;
27 import com.sun.star.awt.XButton;
28 import com.sun.star.awt.XItemListener;
29 import com.sun.star.awt.XListBox;
30 import com.sun.star.awt.XTextComponent;
31 import com.sun.star.beans.XPropertySet;
32 import com.sun.star.lang.EventObject;
33 import com.sun.star.uno.Exception;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.wizards.common.Desktop;
36 import com.sun.star.wizards.common.Helper;
37 import com.sun.star.wizards.common.PropertyNames;
38 import com.sun.star.wizards.db.TableDescriptor;
39 import com.sun.star.wizards.ui.UIConsts;
40 import com.sun.star.wizards.ui.UnoDialog;
41 
42 public class FieldFormatter implements XItemListener
43 {
44 
45     TableWizard CurUnoDialog;
46     TableDescriptor curTableDescriptor;
47     Object oColumnDescriptorModel;
48     XTextComponent txtfieldname;
49     XListBox xlstFieldNames;
50     XButton btnplus;
51     XButton btnminus;
52     XButton btnShiftUp;
53     XButton btnShiftDown;
54     short curtabindex;
55     String TOGGLEBUTTONS = "toggleButtons";
56     String ADDFIELDNAME = "addFieldName";
57     String REMOVEFIELDNAME = "removeFieldName";
58     String MODIFYFIELDNAME = "modifyFieldName";
59     String[] fieldnames;
60     String suntitled;
61     Integer IFieldFormatStep;
62 
FieldFormatter(TableWizard _CurUnoDialog, TableDescriptor _curTableDescriptor)63     public FieldFormatter(TableWizard _CurUnoDialog, TableDescriptor _curTableDescriptor)
64     {
65         this.CurUnoDialog = _CurUnoDialog;
66         curtabindex = (short) (TableWizard.SOFIELDSFORMATPAGE * 100);
67         IFieldFormatStep = new Integer(TableWizard.SOFIELDSFORMATPAGE);
68         String sFieldName = CurUnoDialog.m_oResource.getResText(UIConsts.RID_TABLE + 23);
69         String sFieldNames = CurUnoDialog.m_oResource.getResText(UIConsts.RID_TABLE + 25);
70         String sfieldinfo = CurUnoDialog.m_oResource.getResText(UIConsts.RID_TABLE + 20);
71         String sbtnplushelptext = CurUnoDialog.m_oResource.getResText(UIConsts.RID_TABLE + 45);
72         String sbtnminushelptext = CurUnoDialog.m_oResource.getResText(UIConsts.RID_TABLE + 46);
73 
74         suntitled = CurUnoDialog.m_oResource.getResText(UIConsts.RID_TABLE + 43);
75 
76         CurUnoDialog.insertLabel("lblFieldNames",
77                 new String[]
78                 {
79                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
80                 },
81                 new Object[]
82                 {
83                     UIConsts.INTEGERS[8], sFieldNames, 91, 27, IFieldFormatStep, new Short(curtabindex++), 90
84                 });
85 
86         try
87         {
88             xlstFieldNames = CurUnoDialog.insertListBox("lstfieldnames", 3, null, this,
89                     new String[]
90                     {
91                         PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
92                     },
93                     new Object[]
94                     {
95                         133, "HID:WIZARDS_HID_DLGTABLE_LB_SELFIELDNAMES", 92, 37, IFieldFormatStep, new Short(curtabindex++), 62
96                     });
97         }
98         catch (Exception e)
99         {
100             e.printStackTrace(System.out);
101         }
102 
103         FontDescriptor oFontDesc = new FontDescriptor();
104         oFontDesc.Name = "StarSymbol";
105 
106         btnShiftUp = CurUnoDialog.insertButton("btnShiftUp", "shiftFieldNameUp", this,
107                 new String[]
108                 {
109                     PropertyNames.PROPERTY_ENABLED, PropertyNames.FONT_DESCRIPTOR, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
110                 },
111                 new Object[]
112                 {
113                     Boolean.FALSE, oFontDesc, 14, "HID:WIZARDS_HID_DLGTABLE_CMDMOVEFIELDUP", String.valueOf((char) 8743), 158, 139, IFieldFormatStep, new Short(curtabindex++), 14
114                 });
115 
116         btnShiftDown = CurUnoDialog.insertButton("btnShiftDown", "shiftFieldNameDown", this,
117                 new String[]
118                 {
119                     PropertyNames.PROPERTY_ENABLED, PropertyNames.FONT_DESCRIPTOR, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
120                 },
121                 new Object[]
122                 {
123                     Boolean.FALSE, oFontDesc, 14, "HID:WIZARDS_HID_DLGTABLE_CMDMOVEFIELDDOWN", String.valueOf((char) 8744), 158, 156, IFieldFormatStep, new Short(curtabindex++), 14
124                 });
125         oFontDesc = new FontDescriptor();
126         oFontDesc.Weight = com.sun.star.awt.FontWeight.BOLD;
127         oFontDesc.Height = (short) 13;
128         btnminus = CurUnoDialog.insertButton("btnminus", "removeFieldName", this,
129                 new String[]
130                 {
131                     PropertyNames.FONT_DESCRIPTOR, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
132                 },
133                 new Object[]
134                 {
135                     oFontDesc, 14, "HID:WIZARDS_HID_DLGTABLE_CMDMINUS", "-", 118, 175, IFieldFormatStep, new Short(curtabindex++), 14
136                 });
137 
138         btnplus = CurUnoDialog.insertButton("btnplus", "addFieldName", this,
139                 new String[]
140                 {
141                     PropertyNames.FONT_DESCRIPTOR, PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
142                 },
143                 new Object[]
144                 {
145                     oFontDesc, 14, "HID:WIZARDS_HID_DLGTABLE_CMDPLUS", "+", 137, 175, IFieldFormatStep, new Short(curtabindex++), 14
146                 });
147 
148         CurUnoDialog.insertControlModel("com.sun.star.awt.UnoControlFixedLineModel", "ColDescriptorHeader",
149                 new String[]
150                 {
151                     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
152                 },
153                 new Object[]
154                 {
155                     8, sfieldinfo, 0, 158, 27, IFieldFormatStep, new Short(curtabindex++), 165
156                 });
157 
158 
159         CurUnoDialog.insertLabel("lblFieldName",
160                 new String[]
161                 {
162                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
163                 },
164                 new Object[]
165                 {
166                     UIConsts.INTEGERS[8], sFieldName, 158, 39, IFieldFormatStep, new Short(curtabindex++), 94
167                 });
168 
169         txtfieldname = CurUnoDialog.insertTextField("txtfieldname", MODIFYFIELDNAME, this,
170                 new String[]
171                 {
172                     PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_HELPURL, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, "Text", PropertyNames.PROPERTY_WIDTH
173                 },
174                 new Object[]
175                 {
176                     UIConsts.INTEGER_12, "HID:WIZARDS_HID_DLGTABLE_COLNAME", 274, 37, IFieldFormatStep, new Short(curtabindex++), PropertyNames.EMPTY_STRING, 50
177                 });
178         txtfieldname.addTextListener(CurUnoDialog);
179         CurUnoDialog.getPeerConfiguration().setAccessibleName(btnplus, sbtnplushelptext);
180         CurUnoDialog.getPeerConfiguration().setAccessibleName(btnminus, sbtnminushelptext);
181     }
182 
initialize(TableDescriptor _curTableDescriptor, String[] _fieldnames)183     public void initialize(TableDescriptor _curTableDescriptor, String[] _fieldnames)
184     {
185         if (oColumnDescriptorModel == null)
186         {
187             oColumnDescriptorModel = CurUnoDialog.insertControlModel("com.sun.star.sdb.ColumnDescriptorControlModel", "oColumnDescriptor",
188                     new String[]
189                     {
190                         PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH, "EditWidth"
191                     }, // PropertyNames.PROPERTY_HELPURL
192                     new Object[]
193                     {
194                         85, 158, 49, IFieldFormatStep, new Short(curtabindex++), 166, 50
195                     });  //, "HID:WIZARDS_HID_DLGTABLE_COLMODIFIER"
196             curTableDescriptor = _curTableDescriptor;
197             Helper.setUnoPropertyValue(oColumnDescriptorModel, PropertyNames.ACTIVE_CONNECTION, _curTableDescriptor.DBConnection);
198             txtfieldname.setMaxTextLen((short) this.curTableDescriptor.getMaxColumnNameLength());
199         }
200         else
201         {
202             int nStep = ((Integer) Helper.getUnoPropertyValue(oColumnDescriptorModel, PropertyNames.PROPERTY_STEP)).intValue();
203             if (nStep > IFieldFormatStep.intValue())
204             {
205                 Helper.setUnoPropertyValue(oColumnDescriptorModel, PropertyNames.PROPERTY_STEP, IFieldFormatStep);
206                 CurUnoDialog.repaintDialogStep();
207             }
208         }
209         Helper.setUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.STRING_ITEM_LIST, _fieldnames);
210         Helper.setUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.SELECTED_ITEMS, new short[]
211                 {
212                     0
213                 });
214         updateColumnDescriptor(_fieldnames[0], curTableDescriptor.getByName(_fieldnames[0]));
215         toggleButtons();
216         CurUnoDialog.setFocus("lstfieldnames");
217     }
218 
toggleButtons()219     public void toggleButtons()
220     {
221         boolean benableShiftUpButton = false;
222         boolean benableShiftDownButton = false;
223         boolean bfieldnameisselected = Helper.getUnoArrayPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.SELECTED_ITEMS) != null;
224         int ilistcount = /* xlstFieldNames.getItemCount();*/ UnoDialog.getListBoxItemCount(xlstFieldNames);
225         boolean blistispopulated = (ilistcount > 0);
226         if (bfieldnameisselected)
227         {
228             int iselpos = xlstFieldNames.getSelectedItemPos();
229             benableShiftUpButton = (iselpos != 0);
230             benableShiftDownButton = (iselpos != ilistcount - 1);
231         }
232         Helper.setUnoPropertyValue(UnoDialog.getModel(btnShiftUp), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(benableShiftUpButton));
233         Helper.setUnoPropertyValue(UnoDialog.getModel(btnShiftDown), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(benableShiftDownButton));
234         Helper.setUnoPropertyValue(UnoDialog.getModel(btnminus), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(blistispopulated));
235         CurUnoDialog.setcompleted(TableWizard.SOFIELDSFORMATPAGE, blistispopulated);
236     }
237 
addFieldName()238     public void addFieldName()
239     {
240         String snewfieldname = Desktop.getUniqueName(xlstFieldNames.getItems(), suntitled, PropertyNames.EMPTY_STRING);
241         short icount = xlstFieldNames.getItemCount();
242         if (CurUnoDialog.verifyfieldcount(icount))
243         {
244             xlstFieldNames.addItem(snewfieldname, icount);
245             Helper.setUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.SELECTED_ITEMS, new short[]
246                     {
247                         icount
248                     });
249             toggleButtons();
250             FieldDescription curfielddescription = new FieldDescription(snewfieldname);
251             CurUnoDialog.fielditems.put(snewfieldname, curfielddescription);
252             curTableDescriptor.addColumn(curfielddescription.getPropertyValues());
253             updateColumnDescriptor(snewfieldname, curTableDescriptor.getByName(snewfieldname));
254             CurUnoDialog.setControlVisible("oColumnDescriptor", true);
255             CurUnoDialog.repaintDialogStep();
256         }
257     }
258 
removeFieldName()259     public void removeFieldName()
260     {
261         String[] fieldnames = (String[]) Helper.getUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.STRING_ITEM_LIST);
262         short ipos = UnoDialog.getSelectedItemPos(xlstFieldNames);
263         String fieldname = fieldnames[ipos];
264         xlstFieldNames.removeItems(ipos, (short) 1);
265         CurUnoDialog.fielditems.remove(fieldname);
266         int ilistcount = /* xlstFieldNames.getItemCount();*/ UnoDialog.getListBoxItemCount(xlstFieldNames);
267         if ((ipos) < ilistcount)
268         {
269             Helper.setUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.SELECTED_ITEMS, new short[]
270                     {
271                         ipos
272                     });
273         }
274         else
275         {
276             if (ilistcount > -1)
277             {
278                 ipos = (short) ((short) ilistcount - (short) 1);
279                 Helper.setUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.SELECTED_ITEMS, new short[]
280                         {
281                             ipos
282                         });
283             }
284         }
285         curTableDescriptor.dropColumnbyName(fieldname);
286         fieldnames = (String[]) Helper.getUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.STRING_ITEM_LIST);
287         boolean benable = ((ipos > -1) && (ipos < fieldnames.length));
288         if (benable)
289         {
290             String snewfieldname = fieldnames[ipos];
291             updateColumnDescriptor(snewfieldname, curTableDescriptor.getByName(snewfieldname));
292             toggleButtons();
293         }
294         else
295         {
296             Helper.setUnoPropertyValue(UnoDialog.getModel(txtfieldname), "Text", PropertyNames.EMPTY_STRING);
297             Helper.setUnoPropertyValue(UnoDialog.getModel(btnminus), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(benable));
298             CurUnoDialog.setcompleted(TableWizard.SOFIELDSFORMATPAGE, benable);
299         }
300         Helper.setUnoPropertyValue(UnoDialog.getModel(btnminus), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(benable));
301         CurUnoDialog.setControlVisible("oColumnDescriptor", benable);
302         CurUnoDialog.repaintDialogStep();
303     }
304 
modifyFieldName()305     public void modifyFieldName()
306     {
307         String newfieldname = txtfieldname.getText();
308         String oldfieldname = xlstFieldNames.getSelectedItem();
309         if (!newfieldname.equals(oldfieldname))
310         {
311             if (curTableDescriptor.modifyColumnName(oldfieldname, newfieldname))
312             {
313                 Object oColumn = Helper.getUnoPropertyValue(oColumnDescriptorModel, "Column");
314                 Helper.setUnoPropertyValue(oColumn, PropertyNames.PROPERTY_NAME, newfieldname);
315                 FieldDescription curfielddescription = (FieldDescription) CurUnoDialog.fielditems.get(oldfieldname);
316                 CurUnoDialog.fielditems.remove(oldfieldname);
317                 curfielddescription.setName(newfieldname);
318                 CurUnoDialog.fielditems.put(newfieldname, curfielddescription);
319                 String[] fieldnames = xlstFieldNames.getItems();
320                 short ipos = xlstFieldNames.getSelectedItemPos();
321                 fieldnames[ipos] = newfieldname;
322                 Helper.setUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.STRING_ITEM_LIST, fieldnames);
323                 Helper.setUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.SELECTED_ITEMS, new short[]
324                         {
325                             ipos
326                         });
327             }
328         }
329     }
330 
shiftFieldNameUp()331     public void shiftFieldNameUp()
332     {
333         short ipos = xlstFieldNames.getSelectedItemPos();
334         String[] snewlist = shiftArrayItem(xlstFieldNames.getItems(), ipos, -1);
335         Helper.setUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.STRING_ITEM_LIST, snewlist);
336         if ((ipos - 1) > -1)
337         {
338             Helper.setUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.SELECTED_ITEMS, new short[]
339                     {
340                         (short) (ipos - 1)
341                     });
342             curTableDescriptor.moveColumn(ipos, ipos - 1);
343         }
344         toggleButtons();
345     }
346 
shiftFieldNameDown()347     public void shiftFieldNameDown()
348     {
349         short ipos = xlstFieldNames.getSelectedItemPos();
350         String[] snewlist = shiftArrayItem(xlstFieldNames.getItems(), ipos, 1);
351         Helper.setUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.STRING_ITEM_LIST, snewlist);
352         if ((ipos + 1) < xlstFieldNames.getItemCount())
353         {
354             Helper.setUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.SELECTED_ITEMS, new short[]
355                     {
356                         (short) (ipos + 1)
357                     });
358             curTableDescriptor.moveColumn(ipos, ipos + 1);
359         }
360         toggleButtons();
361     }
362 
shiftArrayItem(String[] _slist, int _oldindex, int _shiftcount)363     public String[] shiftArrayItem(String[] _slist, int _oldindex, int _shiftcount)
364     {
365         int newindex = _oldindex + _shiftcount;
366         if ((newindex >= 0) && (newindex < _slist.length))
367         {
368             String buffer = _slist[newindex];
369             _slist[newindex] = _slist[_oldindex];
370             _slist[_oldindex] = buffer;
371         }
372         return _slist;
373     }
374 
updateColumnofColumnDescriptor()375     public boolean updateColumnofColumnDescriptor()
376     {
377         Object oColumn = Helper.getUnoPropertyValue(oColumnDescriptorModel, "Column");
378         XPropertySet xColPropertySet = UnoRuntime.queryInterface(XPropertySet.class, oColumn);
379         if (xColPropertySet != null)
380         {
381             curTableDescriptor.modifyColumn(txtfieldname.getText(), xColPropertySet);
382             return true;
383         }
384         return false;
385     }
386 
updateColumnDescriptor(String _ColumnName, XPropertySet _xColumn)387     private void updateColumnDescriptor(String _ColumnName, XPropertySet _xColumn)
388     {
389         updateColumnofColumnDescriptor();
390         XPropertySet xNewPropertySet = curTableDescriptor.clonePropertySet(_ColumnName, _xColumn);
391         if (xNewPropertySet != null)
392         {
393             Helper.setUnoPropertyValue(oColumnDescriptorModel, "Column", xNewPropertySet);
394         }
395         txtfieldname.setText(_ColumnName);
396     }
397 
clonePropertySet(XPropertySet _xPropertySet)398     public XPropertySet clonePropertySet(XPropertySet _xPropertySet)
399     {
400         return null;
401     }
402 
itemStateChanged(ItemEvent arg0)403     public void itemStateChanged(ItemEvent arg0)
404     {
405         String fieldname = xlstFieldNames.getSelectedItem();
406         XPropertySet xColumn = curTableDescriptor.getByName(fieldname);
407         updateColumnDescriptor(fieldname, xColumn);
408         toggleButtons();
409     }
410 
iscompleted()411     public boolean iscompleted()
412     {
413         String[] sfieldnames = (String[]) Helper.getUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.STRING_ITEM_LIST);
414         return sfieldnames.length > 0;
415     }
416 
getFieldNames()417     public String[] getFieldNames()
418     {
419         return (String[]) Helper.getUnoPropertyValue(UnoDialog.getModel(xlstFieldNames), PropertyNames.STRING_ITEM_LIST);
420     }
421 
422     /* (non-Javadoc)
423      * @see com.sun.star.lang.XEventListener#disposing(com.sun.star.lang.EventObject)
424      */
disposing(EventObject arg0)425     public void disposing(EventObject arg0)
426     {
427         // TODO Auto-generated method stub
428     }
429 }
430