1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
package com.sun.star.wizards.table;
import com.sun.star.awt.ActionEvent;
import com.sun.star.awt.ItemEvent;
import com.sun.star.awt.XCheckBox;
import com.sun.star.awt.XFixedText;
import com.sun.star.awt.XListBox;
import com.sun.star.awt.XRadioButton;
import com.sun.star.beans.XPropertySet;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.uno.AnyConverter;
import com.sun.star.wizards.common.Helper;
import com.sun.star.wizards.common.JavaTools;
import com.sun.star.wizards.common.PropertyNames;
import com.sun.star.wizards.db.TableDescriptor;
import com.sun.star.wizards.ui.FieldSelection;
import com.sun.star.wizards.ui.UIConsts;
import com.sun.star.wizards.ui.UnoDialog;
import com.sun.star.wizards.ui.XFieldSelectionListener;
import com.sun.star.wizards.ui.event.XActionListenerAdapter;
import com.sun.star.wizards.ui.event.XItemListenerAdapter;
public class PrimaryKeyHandler implements XFieldSelectionListener
{
private final TableWizard CurUnoDialog;
private final XRadioButton optAddAutomatically;
private final XRadioButton optUseExisting;
private final XRadioButton optUseSeveral;
private final XCheckBox chkcreatePrimaryKey;
private final XCheckBox chkApplyAutoValueExisting;
private final XCheckBox chkApplyAutoValueAutomatic;
private final XListBox lstSinglePrimeKey;
private final XFixedText lblPrimeFieldName;
private final FieldSelection curPrimaryKeySelection;
private String[] fieldnames;
private final TableDescriptor curTableDescriptor;
private final boolean bAutoPrimaryKeysupportsAutoIncrmentation;
private static final String SAUTOMATICKEYFIELDNAME = "ID";
public PrimaryKeyHandler(TableWizard _CurUnoDialog, TableDescriptor _curTableDescriptor)
{
this.CurUnoDialog = _CurUnoDialog;
curTableDescriptor = _curTableDescriptor;
bAutoPrimaryKeysupportsAutoIncrmentation = curTableDescriptor.oTypeInspector.isAutoIncrementationSupported();
short curtabindex = (short) ((TableWizard.SOPRIMARYKEYPAGE * 100) - 20);
Integer IPRIMEKEYSTEP = Integer.valueOf(TableWizard.SOPRIMARYKEYPAGE);
final String sExplanations = CurUnoDialog.m_oResource.getResText("RID_TABLE_26");
final String screatePrimaryKey = CurUnoDialog.m_oResource.getResText("RID_TABLE_27");
final String slblPrimeFieldName = CurUnoDialog.m_oResource.getResText("RID_TABLE_31");
final String sApplyAutoValue = CurUnoDialog.m_oResource.getResText("RID_TABLE_33");
final String sAddAutomatically = CurUnoDialog.m_oResource.getResText("RID_TABLE_28");
final String sUseExisting = CurUnoDialog.m_oResource.getResText("RID_TABLE_29");
final String sUseSeveral = CurUnoDialog.m_oResource.getResText("RID_TABLE_30");
final String slblAvailableFields = CurUnoDialog.m_oResource.getResText("RID_QUERY_4");
final String slblSelPrimaryFields = CurUnoDialog.m_oResource.getResText("RID_TABLE_32");
CurUnoDialog.insertLabel("lblExplanation",
new String[]
{
PropertyNames.PROPERTY_HEIGHT, PropertyNames.PROPERTY_LABEL, PropertyNames.PROPERTY_MULTILINE, PropertyNames.PROPERTY_POSITION_X, PropertyNames.PROPERTY_POSITION_Y, PropertyNames.PROPERTY_STEP, PropertyNames.PROPERTY_TABINDEX, PropertyNames.PROPERTY_WIDTH
},
new Object[]
{
40, sExplanations, Boolean.TRUE, 91, 27, IPRIMEKEYSTEP, Short.valueOf(curtabindex++), 233
});
chkcreatePrimaryKey = CurUnoDialog.insertCheckBox("chkcreatePrimaryKey", new XItemListenerAdapter() {
@Override
public void itemStateChanged(ItemEvent event) {
togglePrimeKeyFields();
}
},
new String[]
{
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, PropertyNames.PROPERTY_WIDTH
},
new Object[]
{
UIConsts.INTEGERS[8], "HID:WIZARDS_HID_DLGTABLE_CHK_USEPRIMEKEY", screatePrimaryKey, 97, 70, Short.valueOf((short) 1), IPRIMEKEYSTEP, Short.valueOf(curtabindex++), 160
});
optAddAutomatically = CurUnoDialog.insertRadioButton("optAddAutomatically", new XItemListenerAdapter() {
@Override
public void itemStateChanged(ItemEvent event) {
togglePrimeKeyFields();
}
},
new String[]
{
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, PropertyNames.PROPERTY_WIDTH
},
new Object[]
{
UIConsts.INTEGERS[8], "HID:WIZARDS_HID_DLGTABLE_OPT_PK_AUTOMATIC", sAddAutomatically, 106, 82, Short.valueOf((short) 1), IPRIMEKEYSTEP, Short.valueOf(curtabindex++), 200
});
optUseExisting = CurUnoDialog.insertRadioButton("optUseExisting", new XItemListenerAdapter() {
@Override
public void itemStateChanged(ItemEvent event) {
togglePrimeKeyFields();
}
},
new String[]
{
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
}, //94
new Object[]
{
UIConsts.INTEGERS[8], "HID:WIZARDS_HID_DLGTABLE_OPT_PK_SINGLE", sUseExisting, 106, 104, IPRIMEKEYSTEP, Short.valueOf(curtabindex++), 200
});
optUseSeveral = CurUnoDialog.insertRadioButton("optUseSeveral", new XItemListenerAdapter() {
@Override
public void itemStateChanged(ItemEvent event) {
togglePrimeKeyFields();
}
},
new String[]
{
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
},
new Object[]
{
UIConsts.INTEGERS[8], "HID:WIZARDS_HID_DLGTABLE_OPT_PK_SEVERAL", sUseSeveral, 106, 132, IPRIMEKEYSTEP, Short.valueOf(curtabindex++), 200
});
chkApplyAutoValueAutomatic = CurUnoDialog.insertCheckBox("chkApplyAutoValueAutomatic", new XItemListenerAdapter() {
@Override
public void itemStateChanged(ItemEvent event) {
togglePrimeKeyFields();
}
},
new String[]
{
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
}, //107
new Object[]
{
UIConsts.INTEGERS[8], "HID:WIZARDS_HID_DLGTABLE_CK_PK_AUTOVALUE_AUTOMATIC", sApplyAutoValue, 116, 92, IPRIMEKEYSTEP, Short.valueOf(curtabindex++), 68
});
lblPrimeFieldName = CurUnoDialog.insertLabel("lblPrimeFieldName",
new String[]
{
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
},
new Object[]
{
Boolean.FALSE, UIConsts.INTEGERS[8], slblPrimeFieldName, 116, 117, IPRIMEKEYSTEP, Short.valueOf(curtabindex++), 46
});
lstSinglePrimeKey = CurUnoDialog.insertListBox("lstSinglePrimeKey", new XActionListenerAdapter() {
@Override
public void actionPerformed(ActionEvent event) {
onPrimeKeySelected();
}
}, null,
new String[]
{
"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
},
new Object[]
{
Boolean.TRUE,
Boolean.FALSE,
12,
"HID:WIZARDS_HID_DLGTABLE_LB_PK_FIELDNAME",
Short.valueOf(UnoDialog.getListBoxLineCount()),
162,
115,
IPRIMEKEYSTEP,
Short.valueOf(curtabindex++),
80
});
chkApplyAutoValueExisting = CurUnoDialog.insertCheckBox("chkApplyAutoValueExisting", new XItemListenerAdapter() {
@Override
public void itemStateChanged(ItemEvent event) {
togglePrimeKeyFields();
}
},
new String[]
{
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
}, //107
new Object[]
{
UIConsts.INTEGERS[8], "HID:WIZARDS_HID_DLGTABLE_CK_PK_AUTOVALUE", sApplyAutoValue, 248, 117, IPRIMEKEYSTEP, Short.valueOf(curtabindex++), 66
});
curPrimaryKeySelection = new FieldSelection(CurUnoDialog, IPRIMEKEYSTEP.intValue(), 116, 142, 208, 47, slblAvailableFields, slblSelPrimaryFields, 41234, false);
curPrimaryKeySelection.addFieldSelectionListener(this);
}
public void initialize()
{
// boolean breselect;
fieldnames = curTableDescriptor.getNonBinaryFieldNames();
String[] skeyfieldnames = curPrimaryKeySelection.getSelectedFieldNames();
curPrimaryKeySelection.initialize(fieldnames, false);
if (skeyfieldnames != null && skeyfieldnames.length > 0)
{
String[] snewkeyfieldnames = JavaTools.removeOutdatedFields(skeyfieldnames, fieldnames);
curPrimaryKeySelection.setSelectedFieldNames(snewkeyfieldnames);
}
String selfield = lstSinglePrimeKey.getSelectedItem();
Helper.setUnoPropertyValue(UnoDialog.getModel(lstSinglePrimeKey), PropertyNames.STRING_ITEM_LIST, fieldnames);
if (selfield != null)
{
if (JavaTools.FieldInList(fieldnames, selfield) > -1)
{
lstSinglePrimeKey.selectItem(selfield, true);
}
}
togglePrimeKeyFields();
}
public boolean iscompleted()
{
if (chkcreatePrimaryKey.getState() == 0)
{
return true;
}
if (this.optAddAutomatically.getState())
{
return true;
}
if (optUseExisting.getState())
{
fieldnames = curTableDescriptor.getNonBinaryFieldNames();
String selfield = lstSinglePrimeKey.getSelectedItem();
if (selfield != null)
{
return (JavaTools.FieldInList(fieldnames, selfield) > -1);
}
}
if (optUseSeveral.getState())
{
fieldnames = curTableDescriptor.getNonBinaryFieldNames();
String[] skeyfieldnames = curPrimaryKeySelection.getSelectedFieldNames();
String[] snewkeyfieldnames = JavaTools.removeOutdatedFields(skeyfieldnames, fieldnames);
return (snewkeyfieldnames.length > 0);
}
return false;
}
private void togglePrimeKeyFields()
{
boolean bdoEnable = (this.chkcreatePrimaryKey.getState() == 1);
Helper.setUnoPropertyValue(UnoDialog.getModel(optAddAutomatically), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bdoEnable));
Helper.setUnoPropertyValue(UnoDialog.getModel(chkApplyAutoValueAutomatic), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bAutoPrimaryKeysupportsAutoIncrmentation && bdoEnable));
Helper.setUnoPropertyValue(UnoDialog.getModel(optUseExisting), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bdoEnable));
Helper.setUnoPropertyValue(UnoDialog.getModel(optUseSeveral), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bdoEnable));
//toggle subcontrols of the radiobuttons...
toggleAutomaticAutoValueCheckBox();
boolean benableSinglePrimekeyControls = bdoEnable && optUseExisting.getState();
toggleSinglePrimeKeyFields(benableSinglePrimekeyControls);
boolean benableSeveralPrimekeyControls = bdoEnable && optUseSeveral.getState();
curPrimaryKeySelection.toggleListboxControls(Boolean.valueOf(benableSeveralPrimekeyControls));
// toggle the following steps of the dialog...
if (!bdoEnable)
{
CurUnoDialog.setcompleted(TableWizard.SOPRIMARYKEYPAGE, true);
}
else
{
if (benableSeveralPrimekeyControls)
{
CurUnoDialog.setcompleted(TableWizard.SOPRIMARYKEYPAGE, (curPrimaryKeySelection.getSelectedFieldNames().length > 0));
}
else if (benableSinglePrimekeyControls)
{
CurUnoDialog.setcompleted(TableWizard.SOPRIMARYKEYPAGE, UnoDialog.isListBoxSelected(lstSinglePrimeKey)); //.getSelectedItemPos() != -1);
}
else if (optAddAutomatically.getState())
{
CurUnoDialog.setcompleted(TableWizard.SOPRIMARYKEYPAGE, true);
}
}
}
private boolean isAutoIncrementatable(String _fieldname)
{
try
{
XPropertySet xColPropertySet = curTableDescriptor.getByName(_fieldname);
if (xColPropertySet != null && curTableDescriptor.getDBDataTypeInspector() != null)
{
return curTableDescriptor.getDBDataTypeInspector().isAutoIncrementable(xColPropertySet);
}
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
return false;
}
public boolean isAutomaticMode()
{
boolean bisAutomaticMode = false;
if (chkcreatePrimaryKey.getState() == 1)
{
bisAutomaticMode = ((Short) Helper.getUnoPropertyValue(UnoDialog.getModel(optAddAutomatically), PropertyNames.PROPERTY_STATE)).shortValue() == (short) 1;
}
return bisAutomaticMode;
}
public String getAutomaticFieldName()
{
return SAUTOMATICKEYFIELDNAME;
}
public boolean isAutoIncremented()
{
boolean bischecked = false;
if (chkcreatePrimaryKey.getState() == 1)
{
boolean bisAutomaticMode = ((Short) Helper.getUnoPropertyValue(UnoDialog.getModel(optAddAutomatically), PropertyNames.PROPERTY_STATE)).shortValue() == (short) 1;
boolean bisExistingMode = ((Short) Helper.getUnoPropertyValue(UnoDialog.getModel(optUseExisting), PropertyNames.PROPERTY_STATE)).shortValue() == (short) 1;
if (bisAutomaticMode)
{
bischecked = chkApplyAutoValueAutomatic.getState() == (short) 1;
}
else if (bisExistingMode)
{
bischecked = chkApplyAutoValueExisting.getState() == (short) 1;
}
}
return bischecked;
}
private void onPrimeKeySelected()
{
try
{
String selfieldname = lstSinglePrimeKey.getSelectedItem();
boolean bdoenable = isAutoIncrementatable(selfieldname);
CurUnoDialog.setcompleted(TableWizard.SOPRIMARYKEYPAGE, lstSinglePrimeKey.getSelectedItemPos() != -1);
Helper.setUnoPropertyValue(UnoDialog.getModel(chkApplyAutoValueExisting), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bdoenable));
XPropertySet xColPropertySet = curTableDescriptor.getByName(selfieldname);
boolean bIsAutoIncremented = ((Boolean) xColPropertySet.getPropertyValue("IsAutoIncrement")).booleanValue();
if (bIsAutoIncremented)
{
Helper.setUnoPropertyValue(UnoDialog.getModel(chkApplyAutoValueExisting), PropertyNames.PROPERTY_STATE, Short.valueOf((short) 1));
}
else
{
Helper.setUnoPropertyValue(UnoDialog.getModel(chkApplyAutoValueExisting), PropertyNames.PROPERTY_STATE, Short.valueOf((short) 0));
}
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
}
private void toggleAutomaticAutoValueCheckBox()
{
try
{
boolean bisAutomaticMode = AnyConverter.toBoolean(Helper.getUnoPropertyValue(UnoDialog.getModel(optAddAutomatically), PropertyNames.PROPERTY_ENABLED));
boolean bdoenable = bAutoPrimaryKeysupportsAutoIncrmentation && optAddAutomatically.getState() && bisAutomaticMode;
Helper.setUnoPropertyValue(UnoDialog.getModel(chkApplyAutoValueAutomatic), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bdoenable));
}
catch (IllegalArgumentException e)
{
e.printStackTrace(System.err);
}
}
private void toggleSinglePrimeKeyFields(boolean _bdoenable)
{
Helper.setUnoPropertyValue(UnoDialog.getModel(lblPrimeFieldName), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(_bdoenable));
Helper.setUnoPropertyValue(UnoDialog.getModel(lstSinglePrimeKey), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(_bdoenable));
Helper.setUnoPropertyValue(UnoDialog.getModel(chkApplyAutoValueExisting), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(_bdoenable));
boolean bdoenableAutoValueCheckBox = (isAutoIncrementatable(lstSinglePrimeKey.getSelectedItem()) && _bdoenable);
Helper.setUnoPropertyValue(UnoDialog.getModel(chkApplyAutoValueExisting), PropertyNames.PROPERTY_ENABLED, Boolean.valueOf(bdoenableAutoValueCheckBox));
}
public String[] getPrimaryKeyFields()
{
if (chkcreatePrimaryKey.getState() == 0)
{
return null;
}
if (fieldnames == null)
{
initialize();
}
if (optUseSeveral.getState())
{
return curPrimaryKeySelection.getSelectedFieldNames();
}
else if (optUseExisting.getState())
{
return new String[]
{
lstSinglePrimeKey.getSelectedItem()
};
}
else if (optAddAutomatically.getState())
{
return new String[]
{
SAUTOMATICKEYFIELDNAME
};
}
return null;
}
public void moveItemDown(String Selitem)
{
}
public void moveItemUp(String Selitem)
{
}
public void setID(String sIncSuffix)
{
}
public void shiftFromLeftToRight(String[] SelItems, String[] NewItems)
{
CurUnoDialog.setcompleted(TableWizard.SOPRIMARYKEYPAGE, (curPrimaryKeySelection.getSelectedFieldNames().length > 0));
}
public void shiftFromRightToLeft(String[] OldSelItems, String[] NewItems)
{
CurUnoDialog.setcompleted(TableWizard.SOPRIMARYKEYPAGE, (curPrimaryKeySelection.getSelectedFieldNames().length > 0));
}
}
|