/
WebCellBasedView.java
6419 lines (5715 loc) · 210 KB
/
WebCellBasedView.java
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file belongs to the Servoy development and deployment environment, Copyright (C) 1997-2010 Servoy BV
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Affero General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along
with this program; if not, see http:/www.gnu.org/licenses or write to the Free
Software Foundation,Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
*/
package com.servoy.j2db.server.headlessclient.dataui;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.SortedMap;
import java.util.StringTokenizer;
import java.util.TreeMap;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.text.html.CSS;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.IRequestTarget;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.Page;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.ResourceReference;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxCallDecorator;
import org.apache.wicket.ajax.calldecorator.AjaxCallDecorator;
import org.apache.wicket.ajax.calldecorator.AjaxPostprocessingCallDecorator;
import org.apache.wicket.behavior.IBehavior;
import org.apache.wicket.behavior.IIgnoreDisabledComponentBehavior;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupElement;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.WicketTag;
import org.apache.wicket.markup.html.IHeaderResponse;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.internal.HtmlHeaderContainer;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.parser.filter.HtmlHeaderSectionHandler;
import org.apache.wicket.markup.resolver.IComponentResolver;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.protocol.http.ClientProperties;
import org.apache.wicket.protocol.http.request.WebClientInfo;
import org.apache.wicket.request.ClientInfo;
import org.apache.wicket.response.StringResponse;
import org.apache.wicket.util.string.AppendingStringBuffer;
import org.apache.wicket.util.string.Strings;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.Undefined;
import com.servoy.base.persistence.constants.IValueListConstants;
import com.servoy.base.scripting.api.IJSEvent.EventType;
import com.servoy.j2db.FormController;
import com.servoy.j2db.FormManager;
import com.servoy.j2db.IApplication;
import com.servoy.j2db.IForm;
import com.servoy.j2db.IMainContainer;
import com.servoy.j2db.IProvideTabSequence;
import com.servoy.j2db.IScriptExecuter;
import com.servoy.j2db.IView;
import com.servoy.j2db.MediaURLStreamHandler;
import com.servoy.j2db.component.ComponentFactory;
import com.servoy.j2db.dataprocessing.DBValueList;
import com.servoy.j2db.dataprocessing.DataAdapterList;
import com.servoy.j2db.dataprocessing.FindState;
import com.servoy.j2db.dataprocessing.FoundSetListWrapper;
import com.servoy.j2db.dataprocessing.FoundSetManager;
import com.servoy.j2db.dataprocessing.IDataAdapter;
import com.servoy.j2db.dataprocessing.IDisplay;
import com.servoy.j2db.dataprocessing.IDisplayData;
import com.servoy.j2db.dataprocessing.IDisplayRelatedData;
import com.servoy.j2db.dataprocessing.IFoundSetInternal;
import com.servoy.j2db.dataprocessing.IRecordInternal;
import com.servoy.j2db.dataprocessing.ISupportsNeedEntireState;
import com.servoy.j2db.dataprocessing.ISwingFoundSet;
import com.servoy.j2db.dataprocessing.IValueList;
import com.servoy.j2db.dataprocessing.Record;
import com.servoy.j2db.dataprocessing.Row;
import com.servoy.j2db.dataprocessing.SortColumn;
import com.servoy.j2db.dataui.IServoyAwareBean;
import com.servoy.j2db.dnd.DRAGNDROP;
import com.servoy.j2db.dnd.JSDNDEvent;
import com.servoy.j2db.persistence.AbstractBase;
import com.servoy.j2db.persistence.BaseComponent;
import com.servoy.j2db.persistence.Bean;
import com.servoy.j2db.persistence.Field;
import com.servoy.j2db.persistence.Form;
import com.servoy.j2db.persistence.GraphicalComponent;
import com.servoy.j2db.persistence.IAnchorConstants;
import com.servoy.j2db.persistence.IDataProviderLookup;
import com.servoy.j2db.persistence.IFormElement;
import com.servoy.j2db.persistence.IPersist;
import com.servoy.j2db.persistence.ISupportAnchors;
import com.servoy.j2db.persistence.ISupportBounds;
import com.servoy.j2db.persistence.ISupportName;
import com.servoy.j2db.persistence.ISupportScrollbars;
import com.servoy.j2db.persistence.ISupportSize;
import com.servoy.j2db.persistence.ISupportTabSeq;
import com.servoy.j2db.persistence.Part;
import com.servoy.j2db.persistence.Portal;
import com.servoy.j2db.persistence.PositionComparator;
import com.servoy.j2db.persistence.Relation;
import com.servoy.j2db.persistence.RepositoryException;
import com.servoy.j2db.persistence.TabSeqComparator;
import com.servoy.j2db.persistence.Table;
import com.servoy.j2db.persistence.ValueList;
import com.servoy.j2db.scripting.IScriptable;
import com.servoy.j2db.scripting.IScriptableProvider;
import com.servoy.j2db.server.headlessclient.MainPage;
import com.servoy.j2db.server.headlessclient.TabIndexHelper;
import com.servoy.j2db.server.headlessclient.WebClientSession;
import com.servoy.j2db.server.headlessclient.WebForm;
import com.servoy.j2db.server.headlessclient.WebOnRenderHelper;
import com.servoy.j2db.server.headlessclient.WrapperContainer;
import com.servoy.j2db.server.headlessclient.dataui.TemplateGenerator.TextualStyle;
import com.servoy.j2db.server.headlessclient.dnd.DraggableBehavior;
import com.servoy.j2db.ui.DataRendererOnRenderWrapper;
import com.servoy.j2db.ui.IComponent;
import com.servoy.j2db.ui.IDataRenderer;
import com.servoy.j2db.ui.IEventExecutor;
import com.servoy.j2db.ui.IFieldComponent;
import com.servoy.j2db.ui.ILabel;
import com.servoy.j2db.ui.IPortalComponent;
import com.servoy.j2db.ui.IProviderStylePropertyChanges;
import com.servoy.j2db.ui.IScriptRenderMethods;
import com.servoy.j2db.ui.IStylePropertyChanges;
import com.servoy.j2db.ui.IStylePropertyChangesRecorder;
import com.servoy.j2db.ui.ISupportOnRender;
import com.servoy.j2db.ui.ISupportOnRenderCallback;
import com.servoy.j2db.ui.ISupportRowStyling;
import com.servoy.j2db.ui.ISupportValueList;
import com.servoy.j2db.ui.ISupportWebBounds;
import com.servoy.j2db.ui.PropertyCopy;
import com.servoy.j2db.ui.RenderableWrapper;
import com.servoy.j2db.ui.runtime.HasRuntimeClientProperty;
import com.servoy.j2db.ui.runtime.HasRuntimeReadOnly;
import com.servoy.j2db.ui.runtime.IRuntimeComponent;
import com.servoy.j2db.ui.scripting.AbstractRuntimeBaseComponent;
import com.servoy.j2db.ui.scripting.RuntimePortal;
import com.servoy.j2db.util.ComponentFactoryHelper;
import com.servoy.j2db.util.Debug;
import com.servoy.j2db.util.IDestroyable;
import com.servoy.j2db.util.IStyleRule;
import com.servoy.j2db.util.IStyleSheet;
import com.servoy.j2db.util.ISupplyFocusChildren;
import com.servoy.j2db.util.OrientationApplier;
import com.servoy.j2db.util.Pair;
import com.servoy.j2db.util.PersistHelper;
import com.servoy.j2db.util.ScopesUtils;
import com.servoy.j2db.util.Settings;
import com.servoy.j2db.util.SortedList;
import com.servoy.j2db.util.Utils;
/**
* This class is normally used to show a portal or tableview
*
* @author jblok
*/
public class WebCellBasedView extends WebMarkupContainer implements IView, IPortalComponent, IDataRenderer, IProviderStylePropertyChanges, TableModelListener,
ListSelectionListener, ISupportWebBounds, ISupportWebTabSeq, ISupportRowStyling, IProvideTabSequence
{
private static final int SCROLLBAR_SIZE = 17;
private static final long serialVersionUID = 1L;
private final double NEW_PAGE_MULITPLIER;
public final ResourceReference R_ARROW_OFF = new ResourceReference(IApplication.class, "images/arrow_off.png"); //$NON-NLS-1$
public final ResourceReference R_ARROW_DOWN = new ResourceReference(IApplication.class, "images/arrow_down.png"); //$NON-NLS-1$
public final ResourceReference R_ARROW_UP = new ResourceReference(IApplication.class, "images/arrow_up.png"); //$NON-NLS-1$
private final LinkedHashMap<IPersist, Component> elementToColumnIdentifierComponent = new LinkedHashMap<>(); // IPersist -> column identifier components - used by JavaScript
private final HashMap<IPersist, Integer> elementTabIndexes = new HashMap<>();
private final LinkedHashMap<Component, IPersist> cellToElement = new LinkedHashMap<>(); // each cell component -> IPersist (on the form)
private final Map<IPersist, Component> elementToColumnHeader = new HashMap<>(); // links each column identifier component
// to a column header component (if such a component exists)
private final Map<IPersist, Map<Object, Object>> elementToClientProperties = new HashMap<>();
private String relationName;
private List<SortColumn> defaultSort = null;
private SortableCellViewHeaders headers;
private final WebMarkupContainer tableContainerBody;
private final WebCellBasedViewListView table;
private final IModel<FoundSetListWrapper> data = new Model<>();
private PagingNavigator pagingNavigator;
boolean showPageNavigator = true;
private DataAdapterList dal;
private final Map<String, Boolean> initialSortColumnNames = new HashMap<>();
private final Map<String, Boolean> initialSortedColumns = new HashMap<>();
private final boolean addHeaders;
private int tabIndex;
private final IApplication application;
private final AbstractBase cellview;
protected final FormController fc;
private final int startY, endY, sizeHint, formDesignHeight;
private int maxHeight;
private int bodyHeightHint = -1;
private int bodyWidthHint = -1;
private final boolean useAJAX, useAnchors;
private Component resizedComponent; // the component that has been resized because of a column resize
private final ISupportOnRenderCallback dataRendererOnRenderWrapper;
private IStyleSheet styleSheet;
private IStyleRule oddStyle, evenStyle, selectedStyle, headerStyle;
private ServoyTableResizeBehavior tableResizeBehavior;
private boolean bodySizeHintSetFromClient;
private Label loadingInfo; // used to show loading info when rendering is postponed waiting for size info response from browser\
private String lastRenderedPath;
private boolean isAnchored;
private final RuntimePortal scriptable;
private boolean isScrollMode;
private ScrollBehavior scrollBehavior;
private TopPlaceholderUpdater topPlaceholderUpdater;
private int maxRowsPerPage;
private boolean isScrollFirstShow = true;
// hide the body of this table/list (in case isKeepLoadedRowsInScrollMode == false) on initial render to avoid a possible flicker effect due to initial show followed by quick scroll and empty space divs being added
boolean displayNoneUntilAfterRender = false;
private boolean isKeepLoadedRowsInScrollMode;
private boolean hasTopBuffer, hasBottomBuffer;
private int currentScrollTop;
private int currentScrollLeft;
private int topPhHeight;
private int scrollableHeaderHeight;
public boolean selectionChanged = false;
private boolean isLeftToRightOrientation;
private Dimension formBodySize;
private boolean isListViewMode;
private StringBuilder javascriptForScrollBehaviorRenderHead;
private boolean isRendering = false;
private final class HeaderHeightCalculationBehavior extends AbstractServoyDefaultAjaxBehavior implements IIgnoreDisabledComponentBehavior
{
@Override
public void renderHead(IHeaderResponse response)
{
StringBuilder jsScrollLeft = new StringBuilder("$('#").append(tableContainerBody.getMarkupId()).append("').scrollLeft(").append( //$NON-NLS-1$//$NON-NLS-2$
currentScrollLeft).append(");");
response.renderOnDomReadyJavascript(jsScrollLeft.toString());
String top;
if (headers != null)
{
top = "$('#" + headers.getMarkupId() + "').height()"; //$NON-NLS-1$ //$NON-NLS-2$
}
else
{
top = "0"; //$NON-NLS-1$
}
StringBuffer tbodyStyle = new StringBuffer("var tbodyTop=").append(top).append(";if(tbodyTop != null) { if(tbodyTop != ").append( //$NON-NLS-1$//$NON-NLS-2$
scrollableHeaderHeight).append(") { $('#").append( //$NON-NLS-1$
tableContainerBody.getMarkupId()).append("').css('top',tbodyTop+'px');};"); //$NON-NLS-1$
tbodyStyle.append("wicketAjaxGet('").append(getCallbackUrl()).append("&h=' + tbodyTop);}"); //$NON-NLS-1$ //$NON-NLS-2$
response.renderOnDomReadyJavascript(tbodyStyle.toString());
StringBuffer onLoadtBodyCheck = new StringBuffer("var tbodyTop=").append(top).append(";if((tbodyTop != null) && ((tbodyTop + 'px') != $('#").append( //$NON-NLS-1$//$NON-NLS-2$
tableContainerBody.getMarkupId()).append("').css('top'))) { if(tbodyTop != ").append(scrollableHeaderHeight).append(") { $('#").append( //$NON-NLS-1$
tableContainerBody.getMarkupId()).append("').css('top',tbodyTop+'px');};"); //$NON-NLS-1$
onLoadtBodyCheck.append("wicketAjaxGet('").append(getCallbackUrl()).append("&h=' + tbodyTop);}"); //$NON-NLS-1$ //$NON-NLS-2$
response.renderOnLoadJavascript(onLoadtBodyCheck.toString());
}
/*
* (non-Javadoc)
*
* @see org.apache.wicket.ajax.AbstractDefaultAjaxBehavior#respond(org.apache.wicket.ajax.AjaxRequestTarget)
*/
@Override
protected void respond(AjaxRequestTarget target)
{
scrollableHeaderHeight = Integer.parseInt(getComponent().getRequest().getParameter("h")); //$NON-NLS-1$
}
}
/**
* @author jcompagner
*
*/
private final class ServoyTableResizeBehavior extends AbstractServoyDefaultAjaxBehavior implements IIgnoreDisabledComponentBehavior
{
private final int resizeStartY;
private final int resizeEndY;
private final AbstractBase resizeCellview;
private boolean responded = false;
/**
* @param startY
* @param endY
* @param cellview
*/
private ServoyTableResizeBehavior(int startY, int endY, AbstractBase cellview)
{
this.resizeStartY = startY;
this.resizeEndY = endY;
this.resizeCellview = cellview;
}
@SuppressWarnings("nls")
@Override
public void renderHead(IHeaderResponse response)
{
super.renderHead(response);
if (!responded)
{
String cellViewId = WebCellBasedView.this.getMarkupId();
StringBuilder sb = new StringBuilder();
if (useAnchors)
{
sb.append("if(typeof(tablesPreferredHeight) != \"undefined\")\n").append("{\n"); //$NON-NLS-1$ //$NON-NLS-2$
sb.append("tablesPreferredHeight['").append(cellViewId).append("'] = new Array();\n"); //$NON-NLS-1$ //$NON-NLS-2$
sb.append("tablesPreferredHeight['").append(cellViewId).append("']['height'] = ").append("-1").append(";\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
sb.append("tablesPreferredHeight['").append(cellViewId).append("']['width'] = ").append("-1").append(";\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
sb.append("tablesPreferredHeight['").append(cellViewId).append("']['callback'] = '").append(getCallbackUrl()).append("';\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
sb.append("updateTablePreferredSize('").append(cellViewId).append("');\n");
sb.append("}\n"); //$NON-NLS-1$
response.renderOnLoadJavascript(sb.toString());
}
else
{
sb.append("var preferredSize = getPreferredTableSize('").append(cellViewId); //$NON-NLS-1$
sb.append("');\n"); //$NON-NLS-1$
sb.append("if(preferredSize && preferredSize[1] != ").append(bodyHeightHint).append(") wicketAjaxGet('").append(getCallbackUrl()).append( //$NON-NLS-1$ //$NON-NLS-2$
"&bodyWidth=' + preferredSize[0] + '&bodyHeight=' + preferredSize[1]);\n"); //$NON-NLS-1$
response.renderOnLoadJavascript(sb.toString());
}
}
responded = false;
}
@Override
protected void respond(AjaxRequestTarget target)
{
int newBodyWidthHint = Integer.parseInt(getComponent().getRequest().getParameter("bodyWidth")); //$NON-NLS-1$
int newBodyHeightHint = Integer.parseInt(getComponent().getRequest().getParameter("bodyHeight")); //$NON-NLS-1$
if (newBodyWidthHint != bodyWidthHint || newBodyHeightHint != bodyHeightHint || !bodySizeHintSetFromClient)
{
boolean needToRenderTable = true;
// if size is changed, reset scroll
if (newBodyWidthHint != bodyWidthHint || newBodyHeightHint != bodyHeightHint)
{
if (isScrollMode() && newBodyWidthHint == bodyWidthHint)
{
needToRenderTable = false;
Pair<Boolean, Pair<Integer, Integer>> rowsCalculation = needsMoreThanOnePage(Math.max(newBodyHeightHint, endY - startY));
maxRowsPerPage = rowsCalculation.getRight().getLeft().intValue();
if (newBodyHeightHint > bodyHeightHint)
{
WebCellBasedView.this.scrollBehavior.appendRows(target, newBodyHeightHint - bodyHeightHint, true);
}
}
else resetScrollParams();
}
bodyWidthHint = newBodyWidthHint;
bodyHeightHint = newBodyHeightHint;
bodySizeHintSetFromClient = true;
if (needToRenderTable)
{
responded = true;
distributeExtraSpace();
IWebFormContainer tabPanel = findParent(IWebFormContainer.class);
if (tabPanel instanceof WebTabPanel)
{
int bodyDesignHeight = resizeEndY - resizeStartY;
int otherPartsHeight = (resizeCellview instanceof Portal) ? 0 : formDesignHeight - bodyDesignHeight;
((WebTabPanel)tabPanel).setTabSize(new Dimension(getDisplayBodyWidthHint(), bodyHeightHint + otherPartsHeight));
}
WebCellBasedView.this.setVisibilityAllowed(true);
WebCellBasedView.this.getStylePropertyChanges().setChanged();
WebEventExecutor.generateResponse(target, getComponent().getPage());
}
}
}
}
public static class CellContainer extends WebMarkupContainer
{
private final Component childComp;
public StyleAppendingModifier backgroundModifier = null;
public CellContainer(Component childComp)
{
super(childComp.getId() + '_');
this.childComp = childComp;
backgroundModifier = new StyleAppendingModifier(new Model<String>(""));
}
public static Component getContentsForCell(Component child)
{
if (child instanceof CellContainer)
{
Iterator< ? extends Component> it = ((CellContainer)child).iterator();
if (it.hasNext())
{
return it.next();
}
Debug.log("Strange - CellContainer with no child..."); //$NON-NLS-1$
}
return child instanceof WrapperContainer ? ((WrapperContainer)child).getDelegate() : child;
}
@Override
protected void onBeforeRender()
{
if (childComp instanceof IComponent && ((IComponent)childComp).isOpaque())
{
Color childCompBG = ((IComponent)childComp).getBackground();
super.onBeforeRender();
Color newChildBG = ((IComponent)childComp).getBackground();
if (!Utils.equalObjects(childCompBG, newChildBG))
{
String sNewChildBG = newChildBG != null ? PersistHelper.createColorString(newChildBG) : ""; //$NON-NLS-1$
if (!"".equals(sNewChildBG)) add(new StyleAppendingModifier(new Model<String>("background-color: " + sNewChildBG))); //$NON-NLS-1$ //$NON-NLS-2$
}
}
else
{
super.onBeforeRender();
}
}
}
interface ItemAdd
{
void add(IPersist element, Component comp);
}
/*
* (Persist, ColumnIdentifierComponent) used to compare based on X position; if ColumnIdentifierComponent X position is not available, the persist position
* is used
*/
private static class PersistColumnIdentifierComponent implements Comparable<PersistColumnIdentifierComponent>
{
private final IPersist persist;
private final IComponent component;
public PersistColumnIdentifierComponent(IPersist persist, IComponent component)
{
this.persist = persist;
this.component = component;
}
public IPersist getPersist()
{
return persist;
}
public IComponent getComponent()
{
return component;
}
public int compareTo(PersistColumnIdentifierComponent pc)
{
if (pc == null) return -1;
IComponent c = pc.getComponent();
Point componentLocation = component.getLocation();
if (componentLocation == null)
{
componentLocation = ((ISupportBounds)persist).getLocation();
}
Point cLocation = c.getLocation();
if (cLocation == null)
{
cLocation = ((ISupportBounds)pc.getPersist()).getLocation();
}
return PositionComparator.comparePoint(true, componentLocation, cLocation);
}
}
private class WebCellBasedViewListView extends ServoyListView<IRecordInternal>
{
private final AbstractBase listCellview;
private final IDataProviderLookup dataProviderLookup;
private final IScriptExecuter el;
private final int listStartY, listEndY;
private final Form form;
private final ArrayList<ListItem<IRecordInternal>> removedListItems = new ArrayList<ListItem<IRecordInternal>>();
public WebCellBasedViewListView(String id, IModel<FoundSetListWrapper> model, int rowsPerPage, AbstractBase cellview,
IDataProviderLookup dataProviderLookup, IScriptExecuter el, int startY, int endY, Form form)
{
super(id, model, rowsPerPage);
this.listCellview = cellview;
this.dataProviderLookup = dataProviderLookup;
this.el = el;
this.listStartY = startY;
this.listEndY = endY;
this.form = form;
}
@Override
protected void onBeforeRender()
{
getViewSize();
final int firstIndex = getStartIndex();
List< ? extends IRecordInternal> modelList = getList();
for (int i = 0; i < getViewSize(); i++)
{
// Get index
final int index = firstIndex + i;
if (index >= modelList.size())
{
break;
}
modelList.get(index);
updateListItem(index);
}
updateHeaders();
super.onBeforeRender();
//set focus on correct (cell) component now; cells should be created at this point
if (focusRequestingColIdentComponent != null)
{
Component cell = getCellToFocus(focusRequestingColIdentComponent);
if (cell != null && RequestCycle.get().getRequestTarget() instanceof AjaxRequestTarget)
{
((AjaxRequestTarget)RequestCycle.get().getRequestTarget()).focusComponent(cell);
}
else
{
Debug.log("couldn't set focus to " + focusRequestingColIdentComponent); //$NON-NLS-1$
}
focusRequestingColIdentComponent = null;
}
}
/**
* Create a new ListItem for list item at index.
*
* @param index
* @return ListItem
*/
@Override
protected ListItem<IRecordInternal> newItem(final int index)
{
if (WebCellBasedView.this.addHeaders)
{
return new ReorderableListItem(this, index, getListItemModel(getModel(), index));
}
else
{
return new WebCellBasedViewListItem(this, index, getListItemModel(getModel(), index));
}
}
private void updateListItem(int index)
{
ListItem<IRecordInternal> item = (ListItem<IRecordInternal>)get(Integer.toString(index));
if (item != null)
{
// update it's model to reflect the correct record (records may have been added/deleted)
// and the model may no longer be correct (points to a record with another index)
item.setModel(getListItemModel(getModel(), index));
// re-apply all changes to the list item and it's child components
setUpItem(item, false);
}
}
@Override
protected void populateItem(ListItem<IRecordInternal> listItem)
{
setUpItem(listItem, true);
}
private boolean isRecordSelected(IRecordInternal rec)
{
if (rec == null)
{
return false;
}
IFoundSetInternal parentFoundSet = rec.getParentFoundSet();
return Arrays.binarySearch(parentFoundSet.getSelectedIndexes(), parentFoundSet.getRecordIndex(rec)) >= 0;
}
private void setUpItem(final ListItem<IRecordInternal> listItem, boolean createComponents)
{
if (!createComponents)
{
// this list item has been set up once before - reset previous behaviors
List<IBehavior> allBehaviors = listItem.getBehaviors();
for (int i = 0; i < allBehaviors.size(); i++)
{
listItem.remove(allBehaviors.get(i));
}
}
final IRecordInternal rec = listItem.getModelObject();
boolean selected = isRecordSelected(rec);
Object color = null, fgColor = null, styleFont = null, styleBorder = null;
if (!isListViewMode())
{
color = WebCellBasedView.this.getListItemBgColor(listItem, selected, false);
if (color instanceof Undefined) color = null;
fgColor = WebCellBasedView.this.getListItemFgColor(listItem, selected, false);
if (fgColor instanceof Undefined) fgColor = null;
styleFont = WebCellBasedView.this.getListItemFont(listItem, selected);
if (styleFont instanceof Undefined) styleFont = null;
styleBorder = WebCellBasedView.this.getListItemBorder(listItem, selected);
}
if (color == null && fgColor == null && styleFont == null && styleBorder == null)
{
listItem.add(new AttributeModifier("class", new Model<String>((listItem.getIndex() % 2) == 0 ? "even" : "odd"))); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
}
final int visibleRowIndex = isScrollMode ? listItem.getIndex() : listItem.getIndex() % getRowsPerPage();
final WebMarkupContainer listItemContainer = listItem instanceof WebCellBasedViewListItem ? ((WebCellBasedViewListItem)listItem).getListContainer()
: listItem;
if (createComponents)
{
final Object compColor = color;
final Object compFgColor = fgColor;
final Object compFont = styleFont;
final Object listItemBorder = styleBorder;
createComponents(application, form, listCellview, dataProviderLookup, el, listStartY, listEndY, new ItemAdd()
{
public void add(IPersist element, final Component comp)
{
Component listItemChild = comp;
if (!isListViewMode())
{
Component component = elementToColumnIdentifierComponent.values().iterator().next();
if (component instanceof IComponent && comp instanceof IScriptableProvider)
{
IScriptable so = ((IScriptableProvider)comp).getScriptObject();
if (so instanceof IRuntimeComponent)
{
IRuntimeComponent ic = (IRuntimeComponent)so;
ic.setSize(ic.getWidth(), ((IComponent)component).getSize().height);
ic.setLocation(ic.getLocationX(), visibleRowIndex * ic.getHeight());
}
}
if (element instanceof ISupportName)
{
String elementName = ((ISupportName)element).getName();
if ((elementName != null) && (elementName.trim().length() > 0) || WebCellBasedView.this.addHeaders)
{
// this column's cells can be made invisible (and <td> tag is the one that has to change)
// so we will link this <td> to a wicket component
listItemChild = new CellContainer(comp);
listItemChild.setOutputMarkupPlaceholderTag(true);
((MarkupContainer)listItemChild).add(comp);
}
}
}
else
{
// if anchoring add wrapper to the listItemChild
if (!(cellview instanceof Portal) && useAnchors &&
(((element instanceof Field) && WebAnchoringHelper.needsWrapperDivForAnchoring((Field)element)) || (element instanceof Bean) ||
((element instanceof GraphicalComponent) && ComponentFactory.isButton((GraphicalComponent)element))))
{
listItemChild = WebAnchoringHelper.getWrapperComponent(comp, (IFormElement)element, listStartY, formBodySize,
isLeftToRightOrientation, isListViewMode());
}
}
cellToElement.put(comp, element);
listItemContainer.add(listItemChild);
setUpComponent(comp, rec, compColor, compFgColor, compFont, listItemBorder, visibleRowIndex);
}
});
}
else
{
// we only need to set up again all components in the list item (refresh them)
Iterator< ? extends Component> children = listItemContainer.iterator();
while (children.hasNext())
{
Component child = CellContainer.getContentsForCell(children.next());
// re-initialize :) it - apply js_ user changes applied on the column identifier component
// and other initializations...
initializeComponent(child, listCellview, cellToElement.get(child));
setUpComponent(child, rec, color, fgColor, styleFont, styleBorder, visibleRowIndex);
}
}
//listItem.add(new SimpleAttributeModifier("onfocus", "Wicket.Log.info('ONFOCUS')"));
enableChildrenInContainer(this, isEnabled());
}
private void setUpComponent(Component comp, IRecordInternal record, Object compColor, Object fgColor, Object compFont, Object listItemBorder,
int visibleRowIndex)
{
// set correct tab index
if (tabIndex < 0)
{
TabIndexHelper.setUpTabIndexAttributeModifier(comp, tabIndex);
}
else
{
if (elementTabIndexes.size() > 0)
{
Integer idx = elementTabIndexes.get(cellToElement.get(comp));
if (idx == null)
{
TabIndexHelper.setUpTabIndexAttributeModifier(comp, ISupportWebTabSeq.SKIP);
}
else
{
TabIndexHelper.setUpTabIndexAttributeModifier(comp, tabIndex + 1 + visibleRowIndex * elementTabIndexes.size() + idx.intValue());
}
}
else
{
TabIndexHelper.setUpTabIndexAttributeModifier(comp, tabIndex + 1);
}
}
if (compColor != null)
{
setParentBGcolor(comp, compColor);
}
if (scriptable.isReadOnly() && validationEnabled && comp instanceof IScriptableProvider &&
((IScriptableProvider)comp).getScriptObject() instanceof HasRuntimeReadOnly) // if in find mode, the field should not be readonly
{
((HasRuntimeReadOnly)((IScriptableProvider)comp).getScriptObject()).setReadOnly(true);
}
if (!isEnabled() && comp instanceof IComponent)
{
((IComponent)comp).setComponentEnabled(false);
}
if (comp instanceof IDisplayRelatedData && record != null)
{
((IDisplayRelatedData)comp).setRecord(record, true);
}
MarkupContainer parent = comp.getParent();
if (parent instanceof CellContainer)
{
// apply properties that need to be applied to <td> tag instead
parent.setVisible(comp.isVisible());
}
}
@Override
protected IModel<IRecordInternal> getListItemModel(final IModel< ? extends List<IRecordInternal>> listViewModel, final int index)
{
List<IRecordInternal> list = listViewModel.getObject();
if (list != null)
{
IRecordInternal r = list.get(index);
if (r instanceof FindState)
{
return new FindStateItemModel(r);
}
if (r != null)
{
return new FoundsetRecordItemModel(this, r, index);
}
}
return null;
}
public ListItem<IRecordInternal> getOrCreateListItem(int index)
{
// if there are missing list items in the top of the view, create them now
if (size() > 1 && index < ((ListItem<IRecordInternal>)get(0)).getIndex())
{
int firstIdx = ((ListItem<IRecordInternal>)get(0)).getIndex();
ArrayList<ListItem<IRecordInternal>> els = new ArrayList<ListItem<IRecordInternal>>();
for (int i = 0; i < size(); i++)
els.add((ListItem<IRecordInternal>)get(i));
removeAll();
ListItem<IRecordInternal> newItem;
for (int i = index; i < firstIdx; i++)
{
newItem = newItem(i);
add(newItem);
onBeginPopulateItem(newItem);
populateItem(newItem);
}
for (ListItem<IRecordInternal> l : els)
add(l);
}
ListItem<IRecordInternal> listItem = (ListItem<IRecordInternal>)get(String.valueOf(index));
if (listItem == null)
{
// Create item for index
listItem = newItem(index);
// Add list item
add(listItem);
// Populate the list item
onBeginPopulateItem(listItem);
populateItem(listItem);
}
else setUpItem(listItem, false);
return listItem;
}
@Override
protected void onDetach()
{
super.onDetach();
for (ListItem<IRecordInternal> listItem : removedListItems)
{
if (listItem.getParent() == null)
{
listItem.visitChildren(Component.class, new IVisitor<Component>()
{
@Override
public Object component(Component component)
{
if (component instanceof IDestroyable)
{
((IDestroyable)component).destroy();
}
if (cellToElement.remove(component) == null)
{
return IVisitor.CONTINUE_TRAVERSAL;
}
return IVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
}
});
}
}
removedListItems.clear();
}
void addRemovedListItem(ListItem<IRecordInternal> listItem)
{
removedListItems.add(listItem);
}
}
public class WebCellBasedViewListViewItem extends WebMarkupContainer implements IProviderStylePropertyChanges
{
public ListItem<IRecordInternal> listItem;
private final IStylePropertyChanges changesRecorder = new ChangesRecorder();
public WebCellBasedViewListViewItem(ListItem<IRecordInternal> listItem)
{
super("listViewItem"); //$NON-NLS-1$
setOutputMarkupId(true);
this.listItem = listItem;
add(new ServoyAjaxEventBehavior("onclick", "listView") //$NON-NLS-1$ //$NON-NLS-2$
{
@Override
protected void onEvent(AjaxRequestTarget target)
{
int webModifier = Utils.getAsInteger(RequestCycle.get().getRequest().getParameter(IEventExecutor.MODIFIERS_PARAMETER));
WebEventExecutor.setSelectedIndex(WebCellBasedViewListViewItem.this, target, WebEventExecutor.convertModifiers(webModifier), true);
currentScrollTop = Utils.getAsInteger(RequestCycle.get().getRequest().getParameter("currentScrollTop")); //$NON-NLS-1$
if (application.getFoundSetManager().getEditRecordList().isEditing())
{
application.getFoundSetManager().getEditRecordList().stopEditing(false);
}
WebEventExecutor.generateResponse(target, getPage());
}
@Override
protected String enhanceFunctionScript(String newEh)
{
return "if(event.target && event.target.id == componentId) {" + newEh + '}'; //$NON-NLS-1$
}
/**
* generateCallbackScript and getAjaxCallDecorator is used decorate the wicket on click event script with the current scroll top
* It may happen that a user rapidly scrolls and clicks on the list items , before the scroll update timer has a chance to fire
* and the scrollIntoView thinks this is a selection outside the viewport
*/
@Override
protected CharSequence generateCallbackScript(final CharSequence partialCall)
{
return super.generateCallbackScript(partialCall + "+Servoy.Utils.getActionParams(event,false)" + "+'¤tScrollTop='+currentScrollTop"); //$NON-NLS-1$ //$NON-NLS-2$
}
@Override
public CharSequence getCallbackUrl(boolean onlyTargetActivePage)
{
return super.getCallbackUrl(true);
}
@Override
protected IAjaxCallDecorator getAjaxCallDecorator()
{
return new AjaxPostprocessingCallDecorator(null)
{
private static final long serialVersionUID = 1L;
@SuppressWarnings("nls")
@Override
public CharSequence postDecorateScript(CharSequence script)
{
StringBuilder scriptBuilder = new StringBuilder();
scriptBuilder.append("var currentScrollTop = $('#").append(WebCellBasedView.this.tableContainerBody.getMarkupId()).append(
"').scrollTop();");
scriptBuilder.append(script);
return scriptBuilder.toString();
}
};
}
});
add(new StyleAppendingModifier(new Model<String>()
{
@Override
public String getObject()
{
boolean isSelectedEl = isSelected();
WebCellBasedView view = WebCellBasedViewListViewItem.this.listItem.findParent(WebCellBasedView.class);
Object color = view.getStyleAttributeForListItem(WebCellBasedViewListViewItem.this.listItem, isSelectedEl,
ISupportRowStyling.ATTRIBUTE.BGCOLOR, false);
boolean isColorSetByOnRender = false;
String onRenderBorder = null;
if (dataRendererOnRenderWrapper.getRenderEventExecutor().hasRenderCallback())
{
dataRendererOnRenderWrapper.getRenderEventExecutor().setRenderState(WebCellBasedViewListViewItem.this.listItem.getModelObject(),
WebCellBasedViewListViewItem.this.listItem.getIndex(), isSelectedEl, false);
dataRendererOnRenderWrapper.getRenderEventExecutor().fireOnRender(false);
if (WebCellBasedView.this.getBackground() != null)
{