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.util.Optional;
22  import org.apache.commons.lang3.StringUtils;
23  import org.apache.wicket.Component;
24  import org.apache.wicket.markup.html.basic.Label;
25  import org.apache.wicket.markup.html.panel.Fragment;
26  import org.apache.wicket.markup.html.panel.Panel;
27  import org.apache.wicket.model.IModel;
28  import org.apache.wicket.model.ResourceModel;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  public abstract class AbstractFieldPanel<T> extends Panel {
33  
34      protected static final Logger LOG = LoggerFactory.getLogger(AbstractFieldPanel.class);
35  
36      private static final long serialVersionUID = 5958017546318855690L;
37  
38      public static final String LABEL = "field-label";
39  
40      protected boolean isRequiredLabelAdded = false;
41  
42      protected String name;
43  
44      public AbstractFieldPanel(final String id, final String name, final IModel<T> model) {
45          super(id, model);
46          this.name = name;
47  
48          add(new Fragment("required", "emptyFragment", AbstractFieldPanel.this));
49          add(new Fragment("externalAction", "emptyFragment", AbstractFieldPanel.this));
50  
51          addLabel();
52          setOutputMarkupId(true);
53      }
54  
55      public final AbstractFieldPanel<T> addLabel() {
56          return addLabel(this.name);
57      }
58  
59      public final AbstractFieldPanel<T> addLabel(final String name) {
60          addOrReplace(new Label(LABEL, new ResourceModel(name, name)));
61          return this;
62      }
63  
64      public AbstractFieldPanel<T> hideLabel() {
65          Optional.ofNullable(get(LABEL)).ifPresent(label -> label.setVisible(false));
66          return this;
67      }
68  
69      public AbstractFieldPanel<T> showExternAction(final Component component) {
70          Fragment fragment = new Fragment("externalAction", "externalActionFragment", AbstractFieldPanel.this);
71          addOrReplace(fragment);
72          fragment.add(component.setRenderBodyOnly(false));
73          return this;
74      }
75  
76      public boolean isRequired() {
77          return false;
78      }
79  
80      public AbstractFieldPanel<T> setRequired(final boolean required) {
81          return this;
82      }
83  
84      public AbstractFieldPanel<T> addRequiredLabel() {
85          if (!isRequired()) {
86              setRequired(true);
87          }
88  
89          Fragment fragment = new Fragment("required", "requiredFragment", this);
90          fragment.add(new Label("requiredLabel", "*"));
91          replace(fragment);
92  
93          this.isRequiredLabelAdded = true;
94  
95          return this;
96      }
97  
98      public AbstractFieldPanel<T> removeRequiredLabel() {
99          if (isRequired()) {
100             setRequired(false);
101         }
102 
103         replace(new Fragment("required", "emptyFragment", this));
104 
105         this.isRequiredLabelAdded = false;
106 
107         return this;
108     }
109 
110     protected String externalActionIcon() {
111         return StringUtils.EMPTY;
112     }
113 
114     public abstract AbstractFieldPanel<T> setModelObject(T object);
115 
116     public String getName() {
117         return this.name;
118     }
119 
120     public abstract AbstractFieldPanel<T> setReadOnly(boolean readOnly);
121 }