1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.uif.layout;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.kuali.rice.kns.uif.UifConstants;
22 import org.kuali.rice.kns.uif.container.CollectionGroup;
23 import org.kuali.rice.kns.uif.container.Container;
24 import org.kuali.rice.kns.uif.container.View;
25 import org.kuali.rice.kns.uif.core.Component;
26 import org.kuali.rice.kns.uif.field.ActionField;
27 import org.kuali.rice.kns.uif.field.AttributeField;
28 import org.kuali.rice.kns.uif.field.Field;
29 import org.kuali.rice.kns.uif.field.GroupField;
30 import org.kuali.rice.kns.uif.field.LabelField;
31 import org.kuali.rice.kns.uif.field.MessageField;
32 import org.kuali.rice.kns.uif.util.ComponentFactory;
33 import org.kuali.rice.kns.uif.util.ComponentUtils;
34 import org.kuali.rice.kns.uif.widget.TableTools;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 public class TableLayoutManager extends GridLayoutManager implements CollectionLayoutManager {
52 private static final long serialVersionUID = 3622267585541524208L;
53
54 private boolean useShortLabels;
55 private boolean repeatHeader;
56 private LabelField headerFieldPrototype;
57
58 private boolean renderSequenceField;
59 private String conditionalRenderSequenceField;
60 private boolean generateAutoSequence;
61 private Field sequenceFieldPrototype;
62
63 private GroupField actionFieldPrototype;
64
65 private GroupField subCollectionGroupFieldPrototype;
66
67
68 private int numberOfDataColumns;
69
70 private List<LabelField> headerFields;
71 private List<Field> dataFields;
72
73 private TableTools tableTools;
74 private boolean headerAdded = false;
75
76 public TableLayoutManager() {
77 useShortLabels = true;
78 repeatHeader = false;
79 renderSequenceField = true;
80 generateAutoSequence = false;
81
82 headerFields = new ArrayList<LabelField>();
83 dataFields = new ArrayList<Field>();
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97 @Override
98 public void performInitialization(View view, Container container) {
99 super.performInitialization(view, container);
100
101 if (generateAutoSequence && !(sequenceFieldPrototype instanceof MessageField)) {
102 sequenceFieldPrototype = ComponentFactory.getMessageField();
103 }
104
105 view.getViewHelperService().performComponentInitialization(view, headerFieldPrototype);
106 view.getViewHelperService().performComponentInitialization(view, sequenceFieldPrototype);
107 view.getViewHelperService().performComponentInitialization(view, actionFieldPrototype);
108 view.getViewHelperService().performComponentInitialization(view, subCollectionGroupFieldPrototype);
109 }
110
111
112
113
114
115
116
117
118 @Override
119 public void performFinalize(View view, Object model, Container container) {
120 super.performFinalize(view, model, container);
121
122 CollectionGroup collectionGroup = (CollectionGroup) container;
123
124 int totalColumns = getNumberOfDataColumns();
125 if (renderSequenceField) {
126 totalColumns++;
127 }
128
129 if (collectionGroup.isRenderLineActions() && !collectionGroup.isReadOnly()) {
130 totalColumns++;
131 }
132
133 setNumberOfColumns(totalColumns);
134
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149 public void buildLine(View view, Object model, CollectionGroup collectionGroup, List<Field> lineFields,
150 List<GroupField> subCollectionFields, String bindingPath, List<ActionField> actions, String idSuffix,
151 Object currentLine, int lineIndex) {
152 boolean isAddLine = lineIndex == -1;
153
154
155 if (isAddLine || ((!collectionGroup.isRenderAddLine() || collectionGroup.isReadOnly()) && (lineIndex == 0))) {
156 if (isSuppressLineWrapping()) {
157 setNumberOfDataColumns(lineFields.size());
158 } else {
159 setNumberOfDataColumns(getNumberOfColumns());
160 }
161 }
162
163
164
165 if (!headerAdded) {
166 headerFields = new ArrayList<LabelField>();
167 dataFields = new ArrayList<Field>();
168
169 buildTableHeaderRows(collectionGroup, lineFields);
170 ComponentUtils.pushObjectToContext(headerFields, UifConstants.ContextVariableNames.LINE, currentLine);
171 ComponentUtils.pushObjectToContext(headerFields, UifConstants.ContextVariableNames.INDEX, new Integer(
172 lineIndex));
173 headerAdded = true;
174 }
175
176
177 for (Field field : lineFields) {
178 field.setLabelFieldRendered(true);
179
180
181
182 ComponentUtils.setComponentPropertyDeep(field, "summaryMessageField.render", new Boolean(false));
183 }
184
185 int rowCount = calculateNumberOfRows(collectionGroup.getItems());
186 int rowSpan = rowCount + subCollectionFields.size();
187
188
189 if (renderSequenceField) {
190 Field sequenceField = null;
191 if (!isAddLine) {
192 sequenceField = ComponentUtils.copy(sequenceFieldPrototype, idSuffix);
193
194 if (generateAutoSequence && (sequenceField instanceof MessageField)) {
195 ((MessageField) sequenceField).setMessageText(Integer.toString(lineIndex + 1));
196 }
197 }
198 else {
199 sequenceField = ComponentUtils.copy(collectionGroup.getAddLineLabelField(), idSuffix);
200 }
201 sequenceField.setRowSpan(rowSpan);
202
203 if (sequenceField instanceof AttributeField) {
204 ((AttributeField) sequenceField).getBindingInfo().setBindByNamePrefix(bindingPath);
205 }
206
207 ComponentUtils.updateContextForLine(sequenceField, currentLine, lineIndex);
208
209 dataFields.add(sequenceField);
210 }
211
212
213 int cellPosition = 0;
214 for (Field lineField : lineFields) {
215 dataFields.add(lineField);
216
217 cellPosition += lineField.getColSpan();
218
219
220 if ((cellPosition == getNumberOfDataColumns()) && collectionGroup.isRenderLineActions()
221 && !collectionGroup.isReadOnly()) {
222 GroupField lineActionsField = ComponentUtils.copy(actionFieldPrototype, idSuffix);
223
224 ComponentUtils.updateContextForLine(lineActionsField, currentLine, lineIndex);
225 lineActionsField.setRowSpan(rowSpan);
226 lineActionsField.setItems(actions);
227
228 dataFields.add(lineActionsField);
229 }
230 }
231
232
233 for (GroupField subCollectionField : subCollectionFields) {
234 subCollectionField.setColSpan(numberOfDataColumns);
235 }
236
237
238 dataFields.addAll(subCollectionFields);
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261 protected void buildTableHeaderRows(CollectionGroup collectionGroup, List<Field> lineFields) {
262
263
264 int rowCount = calculateNumberOfRows(collectionGroup.getItems());
265
266
267 if (renderSequenceField) {
268 sequenceFieldPrototype.setLabelFieldRendered(true);
269 sequenceFieldPrototype.setRowSpan(rowCount);
270 addHeaderField(sequenceFieldPrototype, 1);
271 }
272
273
274 int cellPosition = 0;
275 for (Field field : lineFields) {
276 if (!field.isRender()) {
277 continue;
278 }
279
280 cellPosition += field.getColSpan();
281 addHeaderField(field, cellPosition);
282
283
284 if ((cellPosition == getNumberOfDataColumns()) && collectionGroup.isRenderLineActions()
285 && !collectionGroup.isReadOnly()) {
286 actionFieldPrototype.setLabelFieldRendered(true);
287 actionFieldPrototype.setRowSpan(rowCount);
288 addHeaderField(actionFieldPrototype, cellPosition);
289 }
290 }
291 }
292
293
294
295
296
297
298
299
300
301
302
303
304 protected void addHeaderField(Field field, int column) {
305 LabelField headerField = ComponentUtils.copy(headerFieldPrototype, "_c" + column);
306 if (useShortLabels) {
307 headerField.setLabelText(field.getLabel());
308 }
309 else {
310 headerField.setLabelText(field.getLabel());
311 }
312
313 headerField.setRowSpan(field.getRowSpan());
314 headerField.setColSpan(field.getColSpan());
315
316 if ((field.getRequired() != null) && field.getRequired().booleanValue()) {
317 headerField.getRequiredMessageField().setRender(true);
318 }
319 else {
320 headerField.getRequiredMessageField().setRender(false);
321 }
322
323 headerFields.add(headerField);
324 }
325
326
327
328
329
330
331
332
333
334
335 protected int calculateNumberOfRows(List<? extends Field> items) {
336 int rowCount = 0;
337
338
339 if (isSuppressLineWrapping()) {
340 return 1;
341 }
342
343 int cellCount = 0;
344 for (Field field : items) {
345 cellCount += field.getColSpan() + field.getRowSpan() - 1;
346 }
347
348 if (cellCount != 0) {
349 rowCount = cellCount / getNumberOfDataColumns();
350 }
351
352 return rowCount;
353 }
354
355
356
357
358 @Override
359 public Class<? extends Container> getSupportedContainer() {
360 return CollectionGroup.class;
361 }
362
363
364
365
366 @Override
367 public List<Component> getNestedComponents() {
368 List<Component> components = super.getNestedComponents();
369
370 components.add(tableTools);
371 components.addAll(headerFields);
372 components.addAll(dataFields);
373
374 return components;
375 }
376
377
378
379
380
381
382
383
384 public boolean isUseShortLabels() {
385 return this.useShortLabels;
386 }
387
388
389
390
391
392
393 public void setUseShortLabels(boolean useShortLabels) {
394 this.useShortLabels = useShortLabels;
395 }
396
397
398
399
400
401
402
403
404 public boolean isRepeatHeader() {
405 return this.repeatHeader;
406 }
407
408
409
410
411
412
413 public void setRepeatHeader(boolean repeatHeader) {
414 this.repeatHeader = repeatHeader;
415 }
416
417
418
419
420
421
422
423
424 public LabelField getHeaderFieldPrototype() {
425 return this.headerFieldPrototype;
426 }
427
428
429
430
431
432
433 public void setHeaderFieldPrototype(LabelField headerFieldPrototype) {
434 this.headerFieldPrototype = headerFieldPrototype;
435 }
436
437
438
439
440
441
442
443 public List<LabelField> getHeaderFields() {
444 return this.headerFields;
445 }
446
447
448
449
450
451
452
453 public boolean isRenderSequenceField() {
454 return this.renderSequenceField;
455 }
456
457
458
459
460
461
462 public void setRenderSequenceField(boolean renderSequenceField) {
463 this.renderSequenceField = renderSequenceField;
464 }
465
466
467
468
469
470
471
472 public String getConditionalRenderSequenceField() {
473 return this.conditionalRenderSequenceField;
474 }
475
476
477
478
479
480
481 public void setConditionalRenderSequenceField(String conditionalRenderSequenceField) {
482 this.conditionalRenderSequenceField = conditionalRenderSequenceField;
483 }
484
485
486
487
488
489
490
491
492 public String getSequencePropertyName() {
493 if ((sequenceFieldPrototype != null) && (sequenceFieldPrototype instanceof AttributeField)) {
494 return ((AttributeField) sequenceFieldPrototype).getPropertyName();
495 }
496
497 return null;
498 }
499
500
501
502
503
504
505 public void setSequencePropertyName(String sequencePropertyName) {
506 if ((sequenceFieldPrototype != null) && (sequenceFieldPrototype instanceof AttributeField)) {
507 ((AttributeField) sequenceFieldPrototype).setPropertyName(sequencePropertyName);
508 }
509 }
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524 public boolean isGenerateAutoSequence() {
525 return this.generateAutoSequence;
526 }
527
528
529
530
531
532
533 public void setGenerateAutoSequence(boolean generateAutoSequence) {
534 this.generateAutoSequence = generateAutoSequence;
535 }
536
537
538
539
540
541
542
543
544 public Field getSequenceFieldPrototype() {
545 return this.sequenceFieldPrototype;
546 }
547
548
549
550
551
552
553 public void setSequenceFieldPrototype(Field sequenceFieldPrototype) {
554 this.sequenceFieldPrototype = sequenceFieldPrototype;
555 }
556
557
558
559
560
561
562
563
564
565
566
567
568
569 public GroupField getActionFieldPrototype() {
570 return this.actionFieldPrototype;
571 }
572
573
574
575
576
577
578 public void setActionFieldPrototype(GroupField actionFieldPrototype) {
579 this.actionFieldPrototype = actionFieldPrototype;
580 }
581
582
583
584
585 public GroupField getSubCollectionGroupFieldPrototype() {
586 return this.subCollectionGroupFieldPrototype;
587 }
588
589
590
591
592
593
594 public void setSubCollectionGroupFieldPrototype(GroupField subCollectionGroupFieldPrototype) {
595 this.subCollectionGroupFieldPrototype = subCollectionGroupFieldPrototype;
596 }
597
598
599
600
601
602
603
604 public List<Field> getDataFields() {
605 return this.dataFields;
606 }
607
608
609
610
611
612
613
614 public TableTools getTableTools() {
615 return this.tableTools;
616 }
617
618
619
620
621
622
623 public void setTableTools(TableTools tableTools) {
624 this.tableTools = tableTools;
625 }
626
627
628
629
630 public int getNumberOfDataColumns() {
631 return this.numberOfDataColumns;
632 }
633
634
635
636
637 public void setNumberOfDataColumns(int numberOfDataColumns) {
638 this.numberOfDataColumns = numberOfDataColumns;
639 }
640
641 }