View Javadoc
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  import java.util.UUID;
28  
29  /**
30   * Model class for <code>DynaForm</code> component.
31   *
32   * @author Oleg Varaksin / last modified by $Author$
33   * @version $Revision$
34   * @since 0.5
35   */
36  public class DynaFormModel implements Serializable {
37  
38      private static final long serialVersionUID = 20120514L;
39  
40      private final String uuid;
41      private final List<DynaFormRow> regularRows = new ArrayList<>();
42      private List<DynaFormRow> extendedRows = null;
43      private final List<DynaFormLabel> labels = new ArrayList<>();
44      private final List<DynaFormControl> controls = new ArrayList<>();
45  
46      public DynaFormModel() {
47          uuid = UUID.randomUUID().toString();
48      }
49  
50      public String getUuid() {
51          return uuid;
52      }
53  
54      public List<DynaFormRow> getRegularRows() {
55          return regularRows;
56      }
57  
58      public List<DynaFormRow> getExtendedRows() {
59          return extendedRows;
60      }
61  
62      public List<DynaFormControl> getControls() {
63          return controls;
64      }
65  
66      public List<DynaFormLabel> getLabels() {
67          return labels;
68      }
69  
70      /**
71       * Creates a new regular row.
72       *
73       * @return {@link DynaFormRow}
74       */
75      public DynaFormRow createRegularRow() {
76          final DynaFormRow dynaFormRow = new DynaFormRow(regularRows.size() + 1, false, this);
77          regularRows.add(dynaFormRow);
78  
79          return dynaFormRow;
80      }
81  
82      /**
83       * Creates a new extended row.
84       *
85       * @return {@link DynaFormRow}
86       */
87      public DynaFormRow createExtendedRow() {
88          if (extendedRows == null) {
89              extendedRows = new ArrayList<>();
90          }
91  
92          final DynaFormRow dynaFormRow = new DynaFormRow(extendedRows.size() + 1, true, this);
93          extendedRows.add(dynaFormRow);
94  
95          return dynaFormRow;
96      }
97  
98      /**
99       * Removes the passed regular row.
100      *
101      * @param rowToBeRemoved {@link DynaFormRow} to be removed
102      */
103     public void removeRegularRow(final DynaFormRow rowToBeRemoved) {
104         final int idx = rowToBeRemoved != null ? regularRows.indexOf(rowToBeRemoved) : -1;
105         if (idx >= 0) {
106             removeRow(regularRows, rowToBeRemoved, idx);
107         }
108     }
109 
110     /**
111      * Removes the regular row by its index (position in the list).
112      *
113      * @param idx index of the row to be removed
114      */
115     public void removeRegularRow(final int idx) {
116         DynaFormRow rowToBeRemoved = null;
117         if (0 <= idx && idx < regularRows.size()) {
118             rowToBeRemoved = regularRows.get(idx);
119         }
120 
121         if (rowToBeRemoved != null) {
122             removeRow(regularRows, rowToBeRemoved, idx);
123         }
124     }
125 
126     /**
127      * Removes the passed extended row.
128      *
129      * @param rowToBeRemoved {@link DynaFormRow} to be removed
130      */
131     public void removeExtendedRow(final DynaFormRow rowToBeRemoved) {
132         final int idx = rowToBeRemoved != null ? extendedRows.indexOf(rowToBeRemoved) : -1;
133         if (idx >= 0) {
134             removeRow(extendedRows, rowToBeRemoved, idx);
135         }
136     }
137 
138     /**
139      * Removes the extended row by its index (position in the list).
140      *
141      * @param idx index of the row to be removed
142      */
143     public void removeExtendedRow(final int idx) {
144         DynaFormRow rowToBeRemoved = null;
145         if (0 <= idx && idx < extendedRows.size()) {
146             rowToBeRemoved = extendedRows.get(idx);
147         }
148 
149         if (rowToBeRemoved != null) {
150             removeRow(extendedRows, rowToBeRemoved, idx);
151         }
152     }
153 
154     private void removeRow(final List<DynaFormRow> rows, final DynaFormRow rowToBeRemoved, final int idx) {
155         final List<DynaFormControl> controlsToBeRemoved = new ArrayList<>();
156         final List<DynaFormLabel> labelsToBeRemoved = new ArrayList<>();
157         for (final AbstractDynaFormElement element : rowToBeRemoved.getElements()) {
158             if (element instanceof DynaFormControl) {
159                 controlsToBeRemoved.add((DynaFormControl) element);
160             }
161             else if (element instanceof DynaFormLabel) {
162                 labelsToBeRemoved.add((DynaFormLabel) element);
163             }
164         }
165 
166         controls.removeAll(controlsToBeRemoved);
167         labels.removeAll(labelsToBeRemoved);
168         for (final DynaFormLabel label : labels) {
169             if (label.getForControl() != null && controlsToBeRemoved.contains(label.getForControl())) {
170                 // control was removed ==> label should not reference this control anymore
171                 label.setForControl(null);
172             }
173         }
174 
175         rows.remove(rowToBeRemoved);
176 
177         // re-index rows, so that the new row's IDs will be generated correct
178         int row = idx;
179         final List<DynaFormRow> rowsToBeAdjusted = rows.subList(idx, rows.size());
180         for (final DynaFormRow dynaFormRow : rowsToBeAdjusted) {
181             ++row;
182             dynaFormRow.setRow(row);
183             for (final AbstractDynaFormElement element : dynaFormRow.getElements()) {
184                 element.setRow(row);
185                 if (element instanceof DynaFormControl) {
186                     final DynaFormControl control = (DynaFormControl) element;
187                     final int delta = rowToBeRemoved.getElements().size();
188                     control.setPosition(control.getPosition() - delta);
189                     control.generateKey();
190                 }
191             }
192         }
193     }
194 
195     public boolean isExistExtendedGrid() {
196         return getExtendedRows() != null && !getExtendedRows().isEmpty();
197     }
198 }