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.wicket.markup.html.form;
20  
21  import java.io.Serializable;
22  import java.util.ArrayList;
23  import java.util.List;
24  import org.apache.wicket.markup.html.list.ListItem;
25  import org.apache.wicket.markup.html.list.ListView;
26  import org.apache.wicket.markup.html.panel.Panel;
27  import org.apache.wicket.model.IModel;
28  
29  /**
30   * This empty class must exist because there not seems to be alternative to provide specialized HTML for edit links.
31   *
32   * @param <T> model object type.
33   */
34  public final class ActionsPanel<T extends Serializable> extends Panel {
35  
36      private static final long serialVersionUID = 322966537010107771L;
37  
38      private final List<Action<T>> actions = new ArrayList<>();
39  
40      private IModel<T> model;
41  
42      public ActionsPanel(final String componentId, final IModel<T> model) {
43          super(componentId, model);
44          setOutputMarkupId(true);
45          this.model = model;
46  
47          add(new ListView<>("actionRepeater", actions) {
48  
49              private static final long serialVersionUID = -9180479401817023838L;
50  
51              @Override
52              protected void populateItem(final ListItem<Action<T>> item) {
53                  item.add(new ActionPanel<>(ActionsPanel.this.model, item.getModelObject()));
54              }
55  
56          }.setRenderBodyOnly(true));
57      }
58  
59      public Action<T> add(
60              final ActionLink<T> link,
61              final ActionLink.ActionType type,
62              final String entitlements) {
63  
64          return add(link, type, entitlements, false);
65      }
66  
67      public Action<T> add(
68              final ActionLink<T> link,
69              final ActionLink.ActionType type,
70              final String entitlements,
71              final boolean onConfirm) {
72  
73          Action<T> action = new Action<>(link, type);
74          action.setEntitlements(entitlements);
75          action.setOnConfirm(onConfirm);
76          actions.add(action);
77          return action;
78      }
79  
80      public Action<T> add(final Action<T> action) {
81          actions.add(action);
82          return action;
83      }
84  
85      public Action<T> add(final int index, final Action<T> action) {
86          actions.add(index, action);
87          return action;
88      }
89  
90      public Action<T> set(final int index, final Action<T> action) {
91          actions.set(index, action);
92          return action;
93      }
94  
95      public List<Action<T>> getActions() {
96          return actions;
97      }
98  
99      public ActionsPanel<T> clone(final String componentId, final IModel<T> model) {
100         ActionsPanel<T> panel = new ActionsPanel<>(componentId, model);
101         panel.actions.addAll(actions);
102         return panel;
103     }
104 
105     /**
106      * Use this with toggle panels.
107      *
108      * @param componentId Component Id.
109      * @param model Model.
110      * @return Actions panel.
111      */
112     public ActionsPanel<T> cloneWithLabels(final String componentId, final IModel<T> model) {
113         ActionsPanel<T> panel = new ActionsPanel<>(componentId, model);
114         actions.forEach(action -> panel.actions.add(action.showLabel()));
115         return panel;
116     }
117 
118     public boolean isEmpty() {
119         return this.actions.isEmpty();
120     }
121 }