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.layout;
23  
24  import java.io.Serializable;
25  import java.util.HashMap;
26  import java.util.HashSet;
27  import java.util.Map;
28  import java.util.Set;
29  
30  import org.primefaces.extensions.component.layout.GsonLayoutOptions;
31  
32  /**
33   * Model class representing layout options.
34   *
35   * @author Oleg Varaksin / last modified by Melloware
36   * @since 0.6.0
37   */
38  public class LayoutOptions implements Serializable {
39  
40      private static final long serialVersionUID = 1L;
41  
42      private String id;
43  
44      // direct options
45      private Map<String, Object> options = new HashMap<>();
46  
47      // options for all panes
48      private LayoutOptions defaults;
49  
50      // tooltips
51      private LayoutOptions tips;
52  
53      // options for every specific pane (depends on position)
54      private LayoutOptions north;
55      private LayoutOptions south;
56      private LayoutOptions west;
57      private LayoutOptions east;
58      private LayoutOptions center;
59  
60      // options for child layout
61      private LayoutOptions child;
62  
63      public LayoutOptions() {
64      }
65  
66      public LayoutOptions(final String id) {
67          this.id = id;
68      }
69  
70      public String getId() {
71          return id;
72      }
73  
74      public void setId(final String id) {
75          this.id = id;
76      }
77  
78      public Map<String, Object> getOptions() {
79          return options;
80      }
81  
82      public void setOptions(final Map<String, Object> options) {
83          this.options = options;
84      }
85  
86      public void addOption(final String key, final Object value) {
87          options.put(key, value);
88      }
89  
90      public void addOptions(final Map<String, Object> options) {
91          options.putAll(options);
92      }
93  
94      public void setPanesOptions(final LayoutOptions layoutOptions) {
95          defaults = layoutOptions;
96      }
97  
98      public LayoutOptions getPanesOptions() {
99          return defaults;
100     }
101 
102     public final LayoutOptions getTips() {
103         return tips;
104     }
105 
106     public final void setTips(final LayoutOptions tips) {
107         this.tips = tips;
108     }
109 
110     public void setNorthOptions(final LayoutOptions layoutOptions) {
111         north = layoutOptions;
112     }
113 
114     public LayoutOptions getNorthOptions() {
115         return north;
116     }
117 
118     public void setSouthOptions(final LayoutOptions layoutOptions) {
119         south = layoutOptions;
120     }
121 
122     public LayoutOptions getSouthOptions() {
123         return south;
124     }
125 
126     public void setWestOptions(final LayoutOptions layoutOptions) {
127         west = layoutOptions;
128     }
129 
130     public LayoutOptions getWestOptions() {
131         return west;
132     }
133 
134     public void setEastOptions(final LayoutOptions layoutOptions) {
135         east = layoutOptions;
136     }
137 
138     public LayoutOptions getEastOptions() {
139         return east;
140     }
141 
142     public void setCenterOptions(final LayoutOptions layoutOptions) {
143         center = layoutOptions;
144     }
145 
146     public LayoutOptions getCenterOptions() {
147         return center;
148     }
149 
150     public void setChildOptions(final LayoutOptions layoutOptions) {
151         child = layoutOptions;
152     }
153 
154     public LayoutOptions getChildOptions() {
155         return child;
156     }
157 
158     public Set<LayoutOptions> getDirectionPanes() {
159         final Set<LayoutOptions> result = new HashSet<>();
160         result.add(getCenterOptions());
161         result.add(getNorthOptions());
162         result.add(getEastOptions());
163         result.add(getWestOptions());
164         result.add(getSouthOptions());
165         return result;
166     }
167 
168     public LayoutOptions getLayoutOptions(final String id) {
169         if (id == null) {
170             return null;
171         }
172 
173         LayoutOptions loOptions = null;
174 
175         if (child != null) {
176             if (id.equals(child.getId())) {
177                 return child;
178             }
179             else {
180                 loOptions = child.getLayoutOptions(id);
181             }
182         }
183 
184         if (loOptions == null && defaults != null) {
185             if (id.equals(defaults.getId())) {
186                 return defaults;
187             }
188             else {
189                 loOptions = defaults.getLayoutOptions(id);
190             }
191         }
192 
193         if (loOptions == null && tips != null) {
194             if (id.equals(tips.getId())) {
195                 return tips;
196             }
197             else {
198                 loOptions = tips.getLayoutOptions(id);
199             }
200         }
201 
202         if (loOptions == null && center != null) {
203             if (id.equals(center.getId())) {
204                 return center;
205             }
206             else {
207                 loOptions = center.getLayoutOptions(id);
208             }
209         }
210 
211         if (loOptions == null && north != null) {
212             if (id.equals(north.getId())) {
213                 return north;
214             }
215             else {
216                 loOptions = north.getLayoutOptions(id);
217             }
218         }
219 
220         if (loOptions == null && south != null) {
221             if (id.equals(south.getId())) {
222                 return south;
223             }
224             else {
225                 loOptions = south.getLayoutOptions(id);
226             }
227         }
228 
229         if (loOptions == null && east != null) {
230             if (id.equals(east.getId())) {
231                 return east;
232             }
233             else {
234                 loOptions = east.getLayoutOptions(id);
235             }
236         }
237 
238         if (loOptions == null && west != null) {
239             if (id.equals(west.getId())) {
240                 return west;
241             }
242             else {
243                 loOptions = west.getLayoutOptions(id);
244             }
245         }
246 
247         return loOptions;
248     }
249 
250     public boolean replace(final String id, final LayoutOptions layoutOptions) {
251         boolean replaced = false;
252 
253         if (id == null) {
254             return replaced;
255         }
256 
257         if (child != null) {
258             if (id.equals(child.getId())) {
259                 child = layoutOptions;
260 
261                 return true;
262             }
263             else {
264                 replaced = child.replace(id, layoutOptions);
265             }
266         }
267 
268         if (!replaced && defaults != null) {
269             if (id.equals(defaults.getId())) {
270                 defaults = layoutOptions;
271 
272                 return true;
273             }
274             else {
275                 replaced = defaults.replace(id, layoutOptions);
276             }
277         }
278 
279         if (!replaced && tips != null) {
280             if (id.equals(tips.getId())) {
281                 tips = layoutOptions;
282 
283                 return true;
284             }
285             else {
286                 replaced = tips.replace(id, layoutOptions);
287             }
288         }
289 
290         if (!replaced && center != null) {
291             if (id.equals(center.getId())) {
292                 center = layoutOptions;
293 
294                 return true;
295             }
296             else {
297                 replaced = center.replace(id, layoutOptions);
298             }
299         }
300 
301         if (!replaced && north != null) {
302             if (id.equals(north.getId())) {
303                 north = layoutOptions;
304 
305                 return true;
306             }
307             else {
308                 replaced = north.replace(id, layoutOptions);
309             }
310         }
311 
312         if (!replaced && south != null) {
313             if (id.equals(south.getId())) {
314                 south = layoutOptions;
315 
316                 return true;
317             }
318             else {
319                 replaced = south.replace(id, layoutOptions);
320             }
321         }
322 
323         if (!replaced && east != null) {
324             if (id.equals(east.getId())) {
325                 east = layoutOptions;
326 
327                 return true;
328             }
329             else {
330                 replaced = east.replace(id, layoutOptions);
331             }
332         }
333 
334         if (!replaced && west != null) {
335             if (id.equals(west.getId())) {
336                 west = layoutOptions;
337 
338                 return true;
339             }
340             else {
341                 replaced = west.replace(id, layoutOptions);
342             }
343         }
344 
345         return replaced;
346     }
347 
348     /**
349      * Serializes layout options to JSON string.
350      *
351      * @return Layout options as JSON string
352      */
353     public String toJson() {
354         return GsonLayoutOptions.getGson().toJson(this);
355     }
356 
357 }