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.console.panels;
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 org.apache.commons.lang3.StringUtils;
27  import org.apache.syncope.client.console.wicket.markup.html.form.ActionLink;
28  import org.apache.syncope.client.console.wicket.markup.html.form.ActionsPanel;
29  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxCheckBoxPanel;
30  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxDropDownChoicePanel;
31  import org.apache.syncope.client.ui.commons.markup.html.form.AjaxTextFieldPanel;
32  import org.apache.syncope.common.lib.types.SRARoutePredicate;
33  import org.apache.syncope.common.lib.types.SRARoutePredicateCond;
34  import org.apache.syncope.common.lib.types.SRARoutePredicateFactory;
35  import org.apache.wicket.ajax.AjaxRequestTarget;
36  import org.apache.wicket.extensions.ajax.markup.html.IndicatingAjaxButton;
37  import org.apache.wicket.markup.html.WebMarkupContainer;
38  import org.apache.wicket.markup.html.basic.Label;
39  import org.apache.wicket.markup.html.list.ListItem;
40  import org.apache.wicket.markup.html.list.ListView;
41  import org.apache.wicket.markup.html.panel.Panel;
42  import org.apache.wicket.model.IModel;
43  import org.apache.wicket.model.Model;
44  import org.apache.wicket.model.PropertyModel;
45  
46  public class SRARoutePredicatePanel extends Panel {
47  
48      private static final long serialVersionUID = -5321936363511301735L;
49  
50      public SRARoutePredicatePanel(final String id, final IModel<List<SRARoutePredicate>> model) {
51          super(id);
52          setOutputMarkupId(true);
53  
54          WebMarkupContainer predicateContainer = new WebMarkupContainer("predicateContainer");
55          predicateContainer.setOutputMarkupId(true);
56          add(predicateContainer);
57  
58          predicateContainer.add(new Label("factoryInfo", Model.of()).add(new PopoverBehavior(
59                  Model.<String>of(),
60                  Model.of(getString("factoryInfo.help")),
61                  new PopoverConfig().withHtml(true).withPlacement(TooltipConfig.Placement.right)) {
62  
63              private static final long serialVersionUID = -7032694831250368230L;
64  
65              @Override
66              protected String createRelAttribute() {
67                  return "factoryInfo";
68              }
69          }));
70  
71          ListView<SRARoutePredicate> predicates = new ListView<>("predicates", model) {
72  
73              private static final long serialVersionUID = 1814616131938968887L;
74  
75              @Override
76              protected void populateItem(final ListItem<SRARoutePredicate> item) {
77                  SRARoutePredicate predicate = item.getModelObject();
78  
79                  AjaxCheckBoxPanel negate =
80                      new AjaxCheckBoxPanel("negate", "negate", new PropertyModel<>(predicate, "negate"));
81                  item.add(negate.hideLabel());
82  
83                  AjaxDropDownChoicePanel<SRARoutePredicateFactory> factory =
84                      new AjaxDropDownChoicePanel<>("factory", "factory", new PropertyModel<>(predicate, "factory"));
85                  factory.setChoices(List.of(SRARoutePredicateFactory.values()));
86                  item.add(factory.hideLabel());
87  
88                  AjaxTextFieldPanel args =
89                      new AjaxTextFieldPanel("args", "args", new PropertyModel<>(predicate, "args"));
90                  item.add(args.hideLabel());
91  
92                  AjaxDropDownChoicePanel<SRARoutePredicateCond> cond =
93                      new AjaxDropDownChoicePanel<>("cond", "cond", new PropertyModel<>(predicate, "cond"));
94                  cond.setChoices(List.of(SRARoutePredicateCond.values()));
95                  item.add(cond.hideLabel());
96  
97                  ActionsPanel<Serializable> actions = new ActionsPanel<>("actions", null);
98                  actions.add(new ActionLink<>() {
99  
100                     private static final long serialVersionUID = -3722207913631435501L;
101 
102                     @Override
103                     public void onClick(final AjaxRequestTarget target, final Serializable ignore) {
104                         model.getObject().remove(item.getIndex());
105 
106                         item.getParent().removeAll();
107                         target.add(SRARoutePredicatePanel.this);
108                     }
109                 }, ActionLink.ActionType.DELETE, StringUtils.EMPTY, true).hideLabel();
110                 if (model.getObject().size() > 1) {
111                     if (item.getIndex() > 0) {
112                         actions.add(new ActionLink<>() {
113 
114                             private static final long serialVersionUID = -3722207913631435501L;
115 
116                             @Override
117                             public void onClick(final AjaxRequestTarget target, final Serializable ignore) {
118                                 SRARoutePredicate pre = model.getObject().get(item.getIndex() - 1);
119                                 model.getObject().set(item.getIndex(), pre);
120                                 model.getObject().set(item.getIndex() - 1, predicate);
121 
122                                 item.getParent().removeAll();
123                                 target.add(SRARoutePredicatePanel.this);
124                             }
125                         }, ActionLink.ActionType.UP, StringUtils.EMPTY).hideLabel();
126                     }
127                     if (item.getIndex() < model.getObject().size() - 1) {
128                         actions.add(new ActionLink<>() {
129 
130                             private static final long serialVersionUID = -3722207913631435501L;
131 
132                             @Override
133                             public void onClick(final AjaxRequestTarget target, final Serializable ignore) {
134                                 SRARoutePredicate post = model.getObject().get(item.getIndex() + 1);
135                                 model.getObject().set(item.getIndex(), post);
136                                 model.getObject().set(item.getIndex() + 1, predicate);
137 
138                                 item.getParent().removeAll();
139                                 target.add(SRARoutePredicatePanel.this);
140                             }
141                         }, ActionLink.ActionType.DOWN, StringUtils.EMPTY).hideLabel();
142                     }
143                 }
144                 item.add(actions);
145             }
146         };
147         predicates.setReuseItems(true);
148         predicateContainer.add(predicates);
149 
150         IndicatingAjaxButton addPredicateBtn = new IndicatingAjaxButton("addPredicateBtn") {
151 
152             private static final long serialVersionUID = -4804368561204623354L;
153 
154             @Override
155             protected void onSubmit(final AjaxRequestTarget target) {
156                 model.getObject().add(new SRARoutePredicate());
157                 target.add(SRARoutePredicatePanel.this);
158             }
159         };
160         addPredicateBtn.setDefaultFormProcessing(false);
161         predicateContainer.add(addPredicateBtn);
162     }
163 }