View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.client.ui.commons.markup.html.form;
20  
21  import java.io.Serializable;
22  import java.util.List;
23  import java.util.Optional;
24  import java.util.function.Predicate;
25  import org.apache.syncope.client.ui.commons.Constants;
26  import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior;
27  import org.apache.syncope.common.lib.Attr;
28  import org.apache.syncope.common.lib.Attributable;
29  import org.apache.wicket.ajax.AjaxRequestTarget;
30  import org.apache.wicket.markup.html.form.CheckBox;
31  import org.apache.wicket.markup.html.list.ListItem;
32  import org.apache.wicket.model.IModel;
33  import org.apache.wicket.model.Model;
34  import org.apache.wicket.model.ResourceModel;
35  
36  public class AjaxCheckBoxPanel extends FieldPanel<Boolean> {
37  
38      private static final long serialVersionUID = 5664138233103884310L;
39  
40      public AjaxCheckBoxPanel(final String id, final String name, final IModel<Boolean> model) {
41          this(id, name, model, true);
42      }
43  
44      public AjaxCheckBoxPanel(
45              final String id, final String name, final IModel<Boolean> model, final boolean enableOnChange) {
46  
47          super(id, name, model);
48  
49          field = new CheckBox("checkboxField", model);
50          add(field.setLabel(new ResourceModel(name, name)).setOutputMarkupId(true));
51  
52          if (enableOnChange && !isReadOnly()) {
53              field.add(new IndicatorAjaxFormComponentUpdatingBehavior(Constants.ON_CHANGE) {
54  
55                  private static final long serialVersionUID = -1107858522700306810L;
56  
57                  @Override
58                  protected void onUpdate(final AjaxRequestTarget target) {
59                      // nothing to do
60                  }
61              });
62          }
63      }
64  
65      @Override
66      public FieldPanel<Boolean> setNewModel(final List<Serializable> list) {
67          setNewModel(new Model<>() {
68  
69              private static final long serialVersionUID = 527651414610325237L;
70  
71              @Override
72              public Boolean getObject() {
73                  Boolean value = null;
74  
75                  if (list != null && !list.isEmpty()) {
76                      value = Boolean.TRUE.toString().equalsIgnoreCase(list.get(0).toString());
77                  }
78  
79                  return value;
80              }
81  
82              @Override
83              public void setObject(final Boolean object) {
84                  list.clear();
85                  if (object != null) {
86                      list.add(object.toString());
87                  }
88              }
89          });
90  
91          return this;
92      }
93  
94      @SuppressWarnings("rawtypes")
95      @Override
96      public FieldPanel<Boolean> setNewModel(final ListItem item) {
97          IModel<Boolean> model = new Model<>() {
98  
99              private static final long serialVersionUID = 6799404673615637845L;
100 
101             @Override
102             public Boolean getObject() {
103                 Boolean bool = null;
104 
105                 final Object obj = item.getModelObject();
106 
107                 if (obj != null && !obj.toString().isEmpty()) {
108                     if (obj instanceof String) {
109                         bool = Boolean.TRUE.toString().equalsIgnoreCase(obj.toString());
110                     } else if (obj instanceof Boolean) {
111                         // Don't parse anything
112                         bool = (Boolean) obj;
113                     }
114                 }
115 
116                 return bool;
117             }
118 
119             @Override
120             @SuppressWarnings("unchecked")
121             public void setObject(final Boolean object) {
122                 item.setModelObject(Optional.ofNullable(object)
123                         .map(Object::toString).orElseGet(Boolean.FALSE::toString));
124             }
125         };
126 
127         field.setModel(model);
128         return this;
129     }
130 
131     @SuppressWarnings({ "rawtypes", "unchecked" })
132     @Override
133     public FieldPanel<Boolean> setNewModel(final Attributable attributable, final String schema) {
134         field.setModel(new Model() {
135 
136             private static final long serialVersionUID = -4214654722524358000L;
137 
138             @Override
139             public Serializable getObject() {
140                 return attributable.getPlainAttr(schema).map(Attr::getValues).filter(Predicate.not(List::isEmpty)).
141                         map(values -> Boolean.TRUE.toString().equalsIgnoreCase(values.get(0))).
142                         orElse(null);
143             }
144 
145             @Override
146             public void setObject(final Serializable object) {
147                 attributable.getPlainAttr(schema).ifPresent(plainAttr -> {
148                     plainAttr.getValues().clear();
149                     plainAttr.getValues().add(object == null ? Boolean.FALSE.toString() : object.toString());
150                 });
151             }
152         });
153 
154         return this;
155     }
156 }