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 de.agilecoders.wicket.core.markup.html.bootstrap.components.PopoverBehavior;
22  import de.agilecoders.wicket.core.markup.html.bootstrap.components.PopoverConfig;
23  import de.agilecoders.wicket.core.markup.html.bootstrap.components.TooltipConfig;
24  import java.io.Serializable;
25  import java.util.List;
26  import java.util.Optional;
27  import java.util.function.Predicate;
28  import org.apache.commons.lang3.SerializationUtils;
29  import org.apache.commons.lang3.StringUtils;
30  import org.apache.syncope.common.lib.Attr;
31  import org.apache.syncope.common.lib.Attributable;
32  import org.apache.wicket.AttributeModifier;
33  import org.apache.wicket.PageReference;
34  import org.apache.wicket.markup.html.form.FormComponent;
35  import org.apache.wicket.markup.html.list.ListItem;
36  import org.apache.wicket.model.IModel;
37  import org.apache.wicket.model.Model;
38  import org.apache.wicket.model.ResourceModel;
39  
40  public abstract class FieldPanel<T extends Serializable> extends AbstractFieldPanel<T> implements Cloneable {
41  
42      private static final long serialVersionUID = -198988924922541273L;
43  
44      protected FormComponent<T> field;
45  
46      protected String title;
47  
48      private final Model<Integer> index = Model.of(0);
49  
50      public FieldPanel(final String id, final IModel<T> model) {
51          this(id, id, model);
52      }
53  
54      public FieldPanel(final String id, final String name, final IModel<T> model) {
55          super(id, name, model);
56      }
57  
58      public FormComponent<T> getField() {
59          return field;
60      }
61  
62      public FieldPanel<T> setPlaceholder(final String id) {
63          field.add(new AttributeModifier("placeholder", new ResourceModel(id, id)));
64          return this;
65      }
66  
67      public FieldPanel<T> setTitle(final String title) {
68          return setTitle(title, false);
69      }
70  
71      public FieldPanel<T> setTitle(final String title, final boolean html) {
72          this.title = title;
73          field.add(new PopoverBehavior(
74                  Model.<String>of(),
75                  Optional.ofNullable(title).map(Model::of).orElseGet(Model::<String>of),
76                  new PopoverConfig().withHtml(html).withHoverTrigger().withPlacement(
77                          index.getObject() != null && index.getObject() == 0
78                          ? TooltipConfig.Placement.bottom
79                          : this instanceof AjaxCheckBoxPanel
80                                  ? TooltipConfig.Placement.right
81                                  : TooltipConfig.Placement.top)));
82          return this;
83      }
84  
85      public FieldPanel<T> setStyleSheet(final String... classes) {
86          return setStyleSheet(true, classes);
87      }
88  
89      public FieldPanel<T> setStyleSheet(final boolean replace, final String... classes) {
90          if (replace) {
91              field.add(AttributeModifier.replace("class", StringUtils.join(classes, ' ')));
92          } else {
93              field.add(AttributeModifier.append("class", StringUtils.join(classes, ' ')));
94          }
95          return this;
96      }
97  
98      @Override
99      public FieldPanel<T> setRequired(final boolean required) {
100         field.setRequired(required);
101         return this;
102     }
103 
104     @Override
105     public FieldPanel<T> setReadOnly(final boolean readOnly) {
106         field.setEnabled(!readOnly);
107         return this;
108     }
109 
110     @Override
111     public boolean isRequired() {
112         return field.isRequired();
113     }
114 
115     public boolean isReadOnly() {
116         return !field.isEnabled();
117     }
118 
119     @Override
120     public FieldPanel<T> setModelObject(final T object) {
121         field.setModelObject(object);
122         return this;
123     }
124 
125     public T getModelObject() {
126         return this.field.getModelObject();
127     }
128 
129     public FieldPanel<T> setNewModel(final IModel<T> model) {
130         field.setModel(model == null ? new Model<>() : model);
131         return this;
132     }
133 
134     @SuppressWarnings({ "rawtypes", "unchecked" })
135     public FieldPanel<T> setNewModel(final Attributable attributable, final String schema) {
136         field.setModel(new Model() {
137 
138             private static final long serialVersionUID = -4214654722524358000L;
139 
140             @Override
141             public Serializable getObject() {
142                 return attributable.getPlainAttr(schema).map(Attr::getValues).filter(Predicate.not(List::isEmpty)).
143                         map(values -> values.get(0)).
144                         orElse(null);
145             }
146 
147             @Override
148             public void setObject(final Serializable object) {
149                 attributable.getPlainAttr(schema).ifPresent(plainAttr -> {
150                     plainAttr.getValues().clear();
151                     Optional.ofNullable(object).ifPresent(o -> plainAttr.getValues().add(o.toString()));
152                 });
153             }
154         });
155 
156         return this;
157     }
158 
159     /**
160      * Used by MultiFieldPanel to attach items (usually strings).
161      * This method has to be overridden in case of type conversion is required.
162      *
163      * @param item item to attach.
164      * @return updated FieldPanel object.
165      */
166     @SuppressWarnings({ "unchecked", "rawtypes" })
167     public FieldPanel<T> setNewModel(final ListItem item) {
168         return setNewModel(new IModel() {
169 
170             private static final long serialVersionUID = 6799404673615637845L;
171 
172             @Override
173             public Object getObject() {
174                 return item.getModelObject();
175             }
176 
177             @Override
178             public void setObject(final Object object) {
179                 item.setModelObject(object);
180             }
181 
182             @Override
183             public void detach() {
184                 // no detach
185             }
186         });
187     }
188 
189     @SuppressWarnings({ "unchecked", "rawtypes" })
190     public FieldPanel<T> setNewModel(final List<Serializable> list) {
191         return setNewModel(new Model() {
192 
193             private static final long serialVersionUID = 1088212074765051906L;
194 
195             @Override
196             public Serializable getObject() {
197                 return list == null || list.isEmpty() ? null : list.get(0);
198             }
199 
200             @Override
201             public void setObject(final Serializable object) {
202                 list.clear();
203 
204                 if (object != null) {
205                     list.add(object);
206                 }
207             }
208         });
209     }
210 
211     public FieldPanel<T> setIndex(final int index) {
212         this.index.setObject(index);
213         return this;
214     }
215 
216     public int getIndex() {
217         return index.getObject();
218     }
219 
220     /**
221      * Override to add settings depending components.
222      * It has to be used by default to add components depending by index model.
223      *
224      * @return the current field panel.
225      */
226     public FieldPanel<T> settingsDependingComponents() {
227         return this;
228     }
229 
230     @Override
231     @SuppressWarnings("unchecked")
232     public FieldPanel<T> clone() {
233         final FieldPanel<T> panel = SerializationUtils.clone(this);
234         panel.setModelObject(null);
235         panel.addLabel();
236         return panel;
237     }
238 
239     protected PageReference getPageReference() {
240         // SYNCOPE-1213
241         // default implementation does not require to pass page reference, override this method of want otherwise
242         return null;
243     }
244 }