-
Notifications
You must be signed in to change notification settings - Fork 7
/
BBjGridExWidget.bbj
671 lines (538 loc) · 21.7 KB
/
BBjGridExWidget.bbj
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
use com.basiscomponents.db.ResultSet
use com.basiscomponents.db.DataRow
use com.basiscomponents.db.tree.DataTree
use java.sql.Types
use ::BBjWidget/BBjWidget.bbj::BBjWidget
class public BBjGridExWidgetSelectRowEvent
field public BBjVector SelectedRows! = new BBjVector()
method public BBjNumber getSelectedRowCount()
methodret #SelectedRows!.size()
methodend
classend
class public BBjGridExWidgetRowDoubleClickEvent
field public BBjNumber Row
classend
class public BBjGridExWidgetColumnState
field public BBjString String$
classend
rem /**
rem * A sample BBj Widget Implementation
rem */
class public BBjGridExWidget extends BBjWidget
field private BBjHtmlView HTMLView!
field private Boolean IsReady! = BBjAPI.FALSE
field private BBjVector backlog! = new BBjVector()
field private ResultSet RS!
field private BBjString URL$
field private DataTree TREE!
field private BBjString GroupColumnLabel$ = "Group"
field private BBjNumber SelectionMode
field private BBjNumber MultipleSelection
field private BBjNumber FitToGrid
field private BBjNumber EnableFilter
field private DataRow ColumnDefinition!
rem --------------------- constructors -------------------------------------
rem /**
rem * disabled default constructor
rem */
method private BBjGridExWidget()
methodend
rem /**
rem * The constructor that creates the widget on wnd!
rem * @param BBjWindow wnd!: parent window
rem * @param BBjInt id: the control ID
rem * @param BBjInt x: x-location
rem * @param BBjInt y: y-location
rem * @param BBjInt w: width
rem * @param BBjInt h: height
rem */
method public BBjGridExWidget(BBjWindow wnd!, BBjInt id!, BBjInt x!, BBjInt y!, BBjInt w!, BBjInt h!)
#super!.create(wnd!,id!,x!,y!,w!,h!)
methodend
rem ---------------------- methods from BBjWidget --------------------------------
rem /**
rem * @Override
rem * This method is called whenever the widget needs to be rendered
rem * @param Boolean f_init!: if TRUE the control is rendered for the first time so this method has to perform initial rendering
rem */
method public void redraw(Boolean f_init!)
declare BBjHtmlView htmlview!
if (f_init!) then
dummy$=stbl("!OPTIONS","JAVAFX_HTMLVIEW=true")
html$="<html><body><div id='eventTransporterDiv' onClick='bbj_grid_widget_post_event(event);'></div><div id=""grid"" style=""height: 100%"" class=""ag-fresh""></div></body></html>"
htmlview! = #getCanvas().addHtmlView(101,0,0,#getCanvas().getWidth(),#getCanvas().getHeight(),html$)
htmlview!.setCallback(BBjAPI.ON_PAGE_LOADED,#this!,"onLoad")
htmlview!.setCallback(BBjAPI.ON_NATIVE_JAVASCRIPT,#this!,"onNativeEvent")
htmlview!.setOpaque(0)
#HTMLView!=htmlview!
if INFO(3,6)<>"5" then
#HTMLView!.setLocation(-9,-9)
fi
fi
if INFO(3,6)="5" then
#HTMLView!.setSize(#getCanvas().getWidth(),#getCanvas().getHeight())
else
#HTMLView!.setSize(#getCanvas().getWidth()+16,#getCanvas().getHeight()+16)
fi
methodend
rem /**
rem * Event Handler for Native JavaScript Event (from the Grid)
rem * Determines and dispatches the actual event
rem *
rem * @param BBjNativeJavaScriptEvent ev!: the js event
rem *
rem */
method public void onNativeEvent ( BBjNativeJavaScriptEvent ev!)
n$=str(ev!.getEventMap())
if pos("grid-select-row"=N$)>0 then
fire_ev! = new BBjGridExWidgetSelectRowEvent()
if pos("nodes="=n$)>0 then
x$=n$(pos("nodes="=n$)+6)
x$=x$(pos("{"=x$)+1)
x$=x$(1,pos("}"=x$)-1)
v! = new BBjVector()
while pos(","=x$)>0
x1$=x$(1,pos(","=x$)-1)
x$=x$(pos(","=x$)+1)
x1$=x1$(pos(":"=x1$)+2)
x1$=x1$(1,len(x1$)-1)
v!.addItem(x1$)
wend
if (x$>"") then
x$=x$(pos(":"=x$)+2)
x$=x$(1,len(x$)-1)
v!.addItem(x$)
fi
fi
fire_ev!.setSelectedRows(v!)
#fireEvent(#ON_GRID_SELECT_ROW(),fire_ev!)
fi
if pos("grid-row-doubleclick"=N$)>0 then
fire_ev! = new BBjGridExWidgetRowDoubleClickEvent()
if pos("nodes="=n$)>0 then
x$=n$(pos("nodes="=n$)+6)
x$=x$(1,pos(","=x$)-1)
fire_ev!.setRow(num(x$))
fi
#fireEvent(#ON_GRID_DOUBLE_CLICK(),fire_ev!)
fi
methodend
method private void performGridDataUpdate()
if (! #IsReady!) methodret
if (#RS! <> null()) then
data$=#RS!.toJson()
cdef$=#getAgGridColumnDefinition(0)
else
if (#TREE! <> null()) then
data$=#TREE!.toJson()
cdef$=#getAgGridColumnDefinition(1)
fi
fi
opt$="{
: 'enableColResize': true,
: 'enableSorting': true,
: 'animateRows': true,
: 'enableColResize': true,
: 'allowContextMenuWithControlKey': false,"
if (#EnableFilter)
opt$=opt$+"'enableFilter': false,'floatingFilter': true,"
fi
if (#SelectionMode = BBjGrid.GRID_SELECT_ROW) then
if (#MultipleSelection >0 )
opt$=opt$+"'rowSelection': 'multiple',"
else
opt$=opt$+"'rowSelection': 'single',"
fi
fi
opt$=opt$+"'columnDefs':"+cdef$
opt$=opt$+"}"
#HTMLView!.executeScript("bbj_grid_widget_set_data("+data$+","+opt$+")")
methodend
method private BBjString getAgGridColumnDefinition(BBjInt fGroup%)
declare DataRow cd!
cd! = #ColumnDefinition!
if cd! = null() or cd!.getFieldNames().size()=0 then
if #RS! <> null() and #RS!.size()>0 then
cd! = #RS!.get(0)
else
if #TREE! <> null() then
cd! = #TREE!.getFirstRecord()
fi
fi
fi
cdef$= "["
first=1
if fGroup%>0 then
cdef$=cdef$+"{""headerName"":"""+#GroupColumnLabel$+""",""field"":""__node__name"",""cellRenderer"": ""group"",""pinned"":""left""}"
first=0
fi
f! = cd!.getFieldNames()
it! = f!.iterator()
while it!.hasNext()
f$=it!.next()
if first then
first = 0
else
cdef$=cdef$+","
fi
label$=""
label$=cd!.getFieldAttribute(f$,"LABEL",err=*next)
if label$="" then
label$=f$
fi
cdef$=cdef$+"{""headerName"":"""+label$+""",""field"":"""+f$+""""
pin$=""
pin$=cd!.getFieldAttribute(f$,"PINNED",err=*next)
if pin$>"" then
cdef$=cdef$+",""pinned"":"""+pin$+""""
fi
width$=""
width$=cd!.getFieldAttribute(f$,"WIDTH",err=*next)
if width$>"" then
cdef$=cdef$+",'width':'"+width$+"'"
fi
switch cd!.getFieldType(f$)
case Types.INTEGER
case Types.DECIMAL
case Types.DOUBLE
cdef$=cdef$+",""type"":""numericColumn"",""filter"":""number"""
break
case Types.DATE
case Types.TIMESTAMP
cdef$=cdef$+",""filter"":""date"""
break
case default
cdef$=cdef$+",""filter"":""text"""
break
swend
bgc$=""
bgc$=cd!.getFieldAttribute(f$,"BGCOLOR",err=*next)
fgc$=""
fgc$=cd!.getFieldAttribute(f$,"FGCOLOR",err=*next)
align$=""
align$=cd!.getFieldAttribute(f$,"ALIGN",err=*next)
f=0
if (bgc$>"" OR fgc$>"" or align$>"") then
cdef$=cdef$+",'cellStyleDefaults': {"
if (fgc$>"") then
cdef$=cdef$+"'FGCOLOR': '"+fgc$+"'"
f=1
fi
if f>0 then
cdef$=cdef$+","
fi
if bgc$>"" then
cdef$=cdef$+"'BGCOLOR': '"+bgc$+"'"
f=1
fi
if f>0 then
cdef$=cdef$+","
fi
if align$>"" then
cdef$=cdef$+"'ALIGN':'"+align$+"'"
f=1
fi
cdef$=cdef$+"}"
fi
cdef$=cdef$+"}"
wend
cdef$=cdef$+"]"
methodret cdef$
methodend
rem /**
rem * callback method after initial load
rem * does last initialization work
rem *
rem * @param BBjEvent ev!: the onLoad event
rem */
method public void onLoad(BBjEvent ev!)
#HTMLView!.clearCallback(#HTMLView!.ON_PAGE_LOADED); rem ' only do this once
ch=unt
rem loading the patched ag-grid in BUI until they will have fixed their code for the getDocument method
if INFO(3,6)="5" then
rem open (ch)"BBjGridExWidget/js/ag-grid-enterprise.js"
open (ch)"BBjGridExWidget/js/ag-grid.js"
read record (ch,siz=5512000)script$
close (ch)
#HTMLView!.injectScript(script$)
#HTMLView!.injectScript(script$,1)
else
rem open (ch)"BBjGridExWidget/js/ag-grid-enterprise.js"
open (ch)"BBjGridExWidget/js/ag-grid.min.js"
read record (ch,siz=5512000)script$
close (ch)
#HTMLView!.injectScript(script$,1)
fi
ch=unt
open (ch)"BBjGridExWidget/js/BBjGridExWidget.js"
read record (ch,siz=1521000)script$
close (ch)
if INFO(3,6)="5" then
#HTMLView!.injectScript(script$)
#HTMLView!.injectScript(script$,1)
else
p_script! = script$
p_script! = p_script!.replaceAll("\$doc","document")
p_script! = p_script!.replaceAll("\$wnd","window")
#HTMLView!.injectScript(str(p_script!),1)
#HTMLView!.injectScript(str(p_script!))
fi
#IsReady! = BBjAPI.TRUE
if #RS! <> null() or #TREE! <> null() or #URL$>"" then
#performGridDataUpdate()
fi
it! = #backlog!.iterator()
while it!.hasNext()
s$=it!.next()
#HTMLView!.executeScript(s$)
wend
#backlog! = null()
methodend
rem /**
rem * experimental function that shows the developer console
rem * (only in GUI, for BUI it's a NOOP)
rem */
method public void showDeveloperConsole()
if (info(3,6)<>"5") then
#executeScript("if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}")
fi
methodend
method private Object executeScript(BBjString s$)
if #IsReady! then
methodret #HTMLView!.executeScript(s$)
else
#backlog!.addItem(s$)
fi
methodret null()
methodend
rem --------------------api methods--------------------------------
rem /**
rem * set the data into the grid
rem * @param ResultSet: the com.basiscomponents.db.ResultSet with the data
rem */
method public void setData( ResultSet rs!)
#RS! = rs!
#TREE! = null()
#URL$=""
#performGridDataUpdate()
methodend
rem /**
rem * set the data into the grid, to create a tree grid
rem * @param DataTree: the com.basiscomponents.db.tree.DataTree with the data
rem * organized in hiearchy of ResultSets
rem * @see Demo/TreeDemo.bbj https://github.com/BasisHub/BBjGridExWidget/blob/master/Demo/TreeDemo.bbj
rem */
method public void setData( DataTree tree!)
#RS! = null()
#TREE! = tree!
#URL$=""
#performGridDataUpdate()
methodend
rem /**
rem * clear all columns
rem */
method public void clearColumnDefinitions()
#ColumnDefinition! = null()
methodend
rem /**
rem * add a column to the grid
rem * @param BBjString field$: the field name that matches the ResultSet
rem */
method public void addColumn(BBjString Field$)
#addColumn(Field$,Field$,12,0)
methodend
rem /**
rem * add a column to the grid
rem * @param BBjString field$: the field name that matches the ResultSet
rem * @param BBjString label$: the column header
rem */
method public void addColumn(BBjString Field$, BBjString Label$)
#addColumn(Field$,Label$, 12, 0)
methodend
rem /**
rem * add a column to the grid
rem * @param BBjString field$: the field name that matches the ResultSet
rem * @param BBjString label$: the column header
rem * @param BBjNumber Type!: the column Type (java.sql.Types)
rem * @see https://docs.oracle.com/javase/8/docs/api/java/sql/Types.html
rem *
rem */
method public void addColumn(BBjString Field$, BBjString Label$, BBjNumber Type!)
#addColumn(Field$,Label$, Type!, 0)
methodend
rem /**
rem * add a column to the grid
rem * @param BBjString field$: the field name that matches the ResultSet
rem * @param BBjString label$: the column header
rem * @param BBjNumber Type!: the column Type (java.sql.Types)
rem * @param BBjNumber Editable!: 1 = column is editable
rem * @see https://docs.oracle.com/javase/8/docs/api/java/sql/Types.html
rem *
rem */
method public void addColumn(BBjString Field$, BBjString Label$, BBjNumber Type!, BBjNumber Editable!)
if #ColumnDefinition! = null() then
#ColumnDefinition! = new DataRow()
fi
#ColumnDefinition!.setFieldValue(Field$,Type!,"")
#ColumnDefinition!.setFieldAttribute(Field$,"LABEL",Label$)
#ColumnDefinition!.setFieldAttribute(Field$,"EDITABLE",STR(Editable!))
methodend
rem /**
rem * pin a column of the grid to the left or the right margin
rem * @param BBjString field$: the field name of the column
rem * @param BBjString pin$: "right" or "left"
rem *
rem */
method public void pinColumn(BBjString Field$, BBjString pin$)
pin$=cvs(pin$,8)
if pin$="left" or pin$="right" then
#ColumnDefinition!.setFieldAttribute(Field$,"PINNED",pin$,err=*next)
else
#ColumnDefinition!.removeFieldAttribute(Field$,"PINNED",err=*next)
fi
methodend
rem /**
rem * set the grid to scroll horizontally or fit into the client area
rem * @param BBjNumber fit!: 1 = fit all columns into the width
rem *
rem */
method public void setFitToGrid(BBjNumber fit!)
#FitToGrid=fit!
ret! = #executeScript("bbj_grid_widget_fit_grid("+str(fit!)+")")
methodend
rem /**
rem * set the font color of a column
rem * @param BBjString Field$: the field name of the column
rem * @param BBjColor color!: the color for the column
rem */
method public void setColumnForeColor(BBjString Field$,BBjColor color!)
#ColumnDefinition!.setFieldAttribute(Field$,"FGCOLOR","#"+hta(chr(color!.getRed()))+hta(chr(color!.getGreen()))+hta(chr(color!.getBlue())),err=*next)
methodend
rem /**
rem * set the background color of a column
rem * @param BBjString Field$: the field name of the column
rem * @param BBjColor color!: the color for the column
rem */
method public void setColumnBackColor(BBjString Field$,BBjColor color!)
#ColumnDefinition!.setFieldAttribute(Field$,"BGCOLOR","#"+hta(chr(color!.getRed()))+hta(chr(color!.getGreen()))+hta(chr(color!.getBlue())),err=*next)
methodend
rem /**
rem * set the width of a column
rem * @param BBjString Field$: the field name of the column
rem * @param BBjNumber width!: the column width
rem */
method public void setColumnWidth(BBjString Field$, BBjNumber w!)
#ColumnDefinition!.setFieldAttribute(Field$,"WIDTH",STR(w!),err=*next)
methodend
rem /**
rem * set the alignment of a column
rem * @param BBjString Field$: the field name of the column
rem * @param BBjNumber align: the column alignment
rem *
rem * valid alignments:
rem *
rem * @see GRID_ALIGN_LEFT()
rem * @see GRID_ALIGN_CENTER()
rem * @see GRID_ALIGN_RIGHT()
rem */
method public void setColumnAlignment(BBjString Field$, BBjNumber align)
switch align
case BBjGrid.GRID_ALIGN_LEFT
#ColumnDefinition!.setFieldAttribute(Field$,"ALIGN","left",err=*next)
break
case BBjGrid.GRID_ALIGN_CENTER
#ColumnDefinition!.setFieldAttribute(Field$,"ALIGN","center",err=*next)
break
case BBjGrid.GRID_ALIGN_RIGHT
#ColumnDefinition!.setFieldAttribute(Field$,"ALIGN","right",err=*next)
break
swend
methodend
rem /**
rem * set the selection mode of the grid
rem * @param BBjNumber mode: the selection mode, one of
rem * @see GRID_SELECT_ROW()
rem * @see GRID_SELECT_CELL()
rem */
method public void setSelectionMode(BBjNumber mode)
#SelectionMode=mode
methodend
rem /**
rem * Allow selection of multiple cells or rows
rem * @param BBjNumber multiple: 1 allows multiple selection, 0 denies
rem */
method public void setMultipleSelection(BBjNumber multiple)
#MultipleSelection=multiple
methodend
rem /**
rem * Allow filters in the grid
rem * @param BBjNumber enable: 1 shows the auto filter widgets in the toolbar
rem */
method public void setEnableFilter(BBjNumber enable)
#EnableFilter=enable
methodend
rem /**
rem * set the label for the group column if the grid is a tree-grid
rem * defaults to "Group"
rem * @param label$ - the label
rem */
method public void setGroupColumnLabel(BBjString label$)
#GroupColumnLabel$ = label$
methodend
rem /**
rem * get the label for the group column if the grid is a tree-grid
rem * @returns label$
rem */
method public BBjString getGroupColumnLabel()
return #GroupColumnLabel$
methodend
method public BBjGridExWidgetColumnState getColumnsState()
state! = new BBjGridExWidgetColumnState()
json_string$ = str(#HTMLView!.executeScript("bbj_grid_widget_get_state()"))
state!.setString(json_string$)
methodret state!
methodend
method public void setColumnsState(BBjGridExWidgetColumnState state!)
json_state$ = state!.getString()
#HTMLView!.executeScript("bbj_grid_widget_set_state("+json_state$+")")
methodend
rem /**
rem * @return constant value to define left aligned column
rem */
method public static BBjNumber GRID_ALIGN_LEFT()
methodret BBjGrid.GRID_ALIGN_LEFT
methodend
rem /**
rem * @return constant value to define right aligned column
rem */
method public static BBjNumber GRID_ALIGN_RIGHT()
methodret BBjGrid.GRID_ALIGN_RIGHT
methodend
rem /**
rem * @return constant value to define centered column
rem */
method public static BBjNumber GRID_ALIGN_CENTER()
methodret BBjGrid.GRID_ALIGN_CENTER
methodend
method public static BBjNumber GRID_SELECT_ROW()
methodret BBjGrid.GRID_SELECT_ROW
methodend
rem /**
rem * @return constant value to define cell selection mode
rem */
method public static BBjNumber GRID_SELECT_CELL()
methodret BBjGrid.GRID_SELECT_CELL
methodend
rem /**
rem * @return constant value to define row selection event
rem */
method public static BBjNumber ON_GRID_SELECT_ROW()
methodret BBjAPI.ON_GRID_SELECT_ROW
methodend
rem /**
rem * @return constant value to define row double click event
rem */
method public static BBjNumber ON_GRID_DOUBLE_CLICK()
methodret BBjAPI.ON_GRID_DOUBLE_CLICK
methodend
classend