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.Objects;
26  
27  import org.primefaces.extensions.model.common.KeyData;
28  
29  /**
30   * Class representing a control inside of <code>DynaForm</code>.
31   *
32   * @author Oleg Varaksin / last modified by $Author$
33   * @version $Revision$
34   * @since 0.5
35   */
36  public class DynaFormControl extends AbstractDynaFormElement implements KeyData {
37  
38      public static final String DEFAULT_TYPE = "default";
39      private static final long serialVersionUID = 1L;
40      private static final String KEY_PREFIX_ROW = "r";
41      private static final String KEY_PREFIX_COLUMN = "c";
42      private static final String KEY_SUFFIX_REGULAR = "reg";
43      private static final String KEY_SUFFIX_EXTENDED = "ext";
44      private static final String KEY_SUFFIX_POSITION = "p";
45  
46      private String key;
47      private Serializable data;
48      private final String type;
49      private int position;
50  
51      public DynaFormControl(Serializable data, String type, int colspan, int rowspan, int row, int column, int position,
52                  boolean extended) {
53          super(colspan, rowspan, row, column, extended);
54          this.position = position;
55  
56          this.data = data;
57          if (type != null) {
58              this.type = type;
59          }
60          else {
61              this.type = DEFAULT_TYPE;
62          }
63  
64          generateKey();
65      }
66  
67      @Override
68      public String getKey() {
69          return key;
70      }
71  
72      @Override
73      public void setKey(String key) {
74          this.key = key;
75      }
76  
77      @Override
78      public Serializable getData() {
79          return data;
80      }
81  
82      @Override
83      public void setData(Serializable data) {
84          this.data = data;
85      }
86  
87      public String getType() {
88          return type;
89      }
90  
91      public int getPosition() {
92          return position;
93      }
94  
95      void setPosition(int position) {
96          this.position = position;
97      }
98  
99      void generateKey() {
100         final StringBuilder sb = new StringBuilder();
101         sb.append(KEY_PREFIX_ROW).append(getRow()).append(KEY_PREFIX_COLUMN).append(getColumn()).append(KEY_SUFFIX_POSITION).append(getPosition());
102         if (isExtended()) {
103             sb.append(KEY_SUFFIX_EXTENDED);
104         }
105         else {
106             sb.append(KEY_SUFFIX_REGULAR);
107         }
108         setKey(sb.toString());
109     }
110 
111     @Override
112     public boolean equals(Object o) {
113         if (this == o) {
114             return true;
115         }
116         if (!(o instanceof DynaFormControl)) {
117             return false;
118         }
119         if (!super.equals(o)) {
120             return false;
121         }
122         DynaFormControl that = (DynaFormControl) o;
123         return getPosition() == that.getPosition();
124     }
125 
126     @Override
127     public int hashCode() {
128         return Objects.hash(super.hashCode(), getPosition());
129     }
130 
131     @Override
132     public String toString() {
133         return "DynaFormControl{" + "key=" + key + ", data=" + data + ", type=" + type + ", position=" + position + '}';
134     }
135 }