1 /*
2 * Copyright (c) 2011-2024 PrimeFaces Extensions
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22 package org.primefaces.extensions.model.dynaform;
23
24 import java.io.Serializable;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 /**
29 * Class representing a row inside of <code>DynaForm</code>.
30 *
31 * @author Oleg Varaksin / last modified by $Author$
32 * @version $Revision$
33 * @since 0.5
34 */
35 public class DynaFormRow implements Serializable {
36
37 private static final long serialVersionUID = 20120514L;
38
39 private int row;
40 private boolean extended;
41 private int totalColspan = 0;
42 private DynaFormModel dynaFormModel;
43
44 // all elements (label and controls) in this row
45 private final List<AbstractDynaFormElement> elements = new ArrayList<>();
46
47 /**
48 * This constructor is required for serialization. Please do not remove.
49 */
50 public DynaFormRow() {
51 }
52
53 public DynaFormRow(final int row, final boolean extended, final DynaFormModel dynaFormModel) {
54 this.row = row;
55 this.extended = extended;
56 this.dynaFormModel = dynaFormModel;
57 }
58
59 /**
60 * Adds control with data, default type, colspan = 1 and rowspan = 1.
61 *
62 * @param data data object
63 * @return DynaFormControl added control
64 */
65 public DynaFormControl addControl(final Serializable data) {
66 return addControl(data, DynaFormControl.DEFAULT_TYPE, 1, 1);
67 }
68
69 /**
70 * Adds control with given data, type, colspan = 1 and rowspan = 1.
71 *
72 * @param data data object
73 * @param type type to match the type attribute in pe:dynaFormControl
74 * @return DynaFormControl added control
75 */
76 public DynaFormControl addControl(final Serializable data, final String type) {
77 return addControl(data, type, 1, 1);
78 }
79
80 /**
81 * Adds control with given data, default type, colspan and rowspan.
82 *
83 * @param data data object
84 * @param colspan colspan
85 * @param rowspan rowspan
86 * @return DynaFormControl added control
87 */
88 public DynaFormControl addControl(final Serializable data, final int colspan, final int rowspan) {
89 return addControl(data, DynaFormControl.DEFAULT_TYPE, colspan, rowspan);
90 }
91
92 /**
93 * Adds control with given data, type, colspan and rowspan.
94 *
95 * @param data data object
96 * @param type type to match the type attribute in pe:dynaFormControl
97 * @param colspan colspan
98 * @param rowspan rowspan
99 * @return DynaFormControl added control
100 */
101 public DynaFormControl addControl(final Serializable data, final String type, final int colspan, final int rowspan) {
102 final DynaFormControl dynaFormControl = new DynaFormControl(data,
103 type,
104 colspan,
105 rowspan,
106 row,
107 elements.size() + 1,
108 dynaFormModel.getControls().size() + 1,
109 extended);
110
111 elements.add(dynaFormControl);
112 dynaFormModel.getControls().add(dynaFormControl);
113 totalColspan = totalColspan + colspan;
114
115 return dynaFormControl;
116 }
117
118 /***
119 * Adds nested model with colspan = 1 and rowspan = 1.
120 *
121 * @param model the DynaFormModel
122 * @return DynaFormModelElement added model
123 */
124 public DynaFormModelElement addModel(final DynaFormModel model) {
125 return addModel(model, 1, 1);
126 }
127
128 /***
129 * Adds nested model with given colspan and rowspan.
130 *
131 * @param model
132 * @param colspan
133 * @param rowspan
134 * @return DynaFormModelElement added model
135 */
136 public DynaFormModelElement addModel(final DynaFormModel model, final int colspan, final int rowspan) {
137 final DynaFormModelElement nestedModel = new DynaFormModelElement(model,
138 colspan,
139 rowspan,
140 row,
141 elements.size() + 1,
142 dynaFormModel.getControls().size() + 1,
143 extended);
144
145 elements.add(nestedModel);
146 dynaFormModel.getControls().addAll(model.getControls());
147 totalColspan = totalColspan + colspan;
148
149 return nestedModel;
150 }
151
152 /**
153 * Adds a label with given text, colspan = 1 and rowspan = 1.
154 *
155 * @param value label text
156 * @return DynaFormLabel added label
157 */
158 public DynaFormLabel addLabel(final String value) {
159 return addLabel(value, true, 1, 1);
160 }
161
162 /**
163 * Adds a label with given text, colspan and rowspan.
164 *
165 * @param value label text
166 * @param colspan colspan
167 * @param rowspan rowspan
168 * @return DynaFormLabel added label
169 */
170 public DynaFormLabel addLabel(final String value, final int colspan, final int rowspan) {
171 return addLabel(value, true, colspan, rowspan);
172 }
173
174 /**
175 * Adds a label with given text, escape flag, colspan and rowspan.
176 *
177 * @param value label text
178 * @param escape boolean flag if the label text should escaped or not
179 * @param colspan colspan
180 * @param rowspan rowspan
181 * @return DynaFormLabel added label
182 */
183 public DynaFormLabel addLabel(final String value, final boolean escape, final int colspan, final int rowspan) {
184 final DynaFormLabel dynaFormLabel = new DynaFormLabel(value, escape, colspan, rowspan, row, elements.size() + 1, extended);
185
186 elements.add(dynaFormLabel);
187 dynaFormModel.getLabels().add(dynaFormLabel);
188 totalColspan = totalColspan + colspan;
189
190 return dynaFormLabel;
191 }
192
193 public List<AbstractDynaFormElement> getElements() {
194 return elements;
195 }
196
197 public int getTotalColspan() {
198 return totalColspan;
199 }
200
201 void setRow(final int row) {
202 this.row = row;
203 }
204 }