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