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 java.lang.reflect.Constructor;
22  import java.util.ArrayList;
23  import java.util.List;
24  import org.apache.syncope.client.console.widgets.BaseExtWidget;
25  import org.apache.syncope.client.ui.commons.annotations.ExtWidget;
26  import org.apache.wicket.AttributeModifier;
27  import org.apache.wicket.PageReference;
28  import org.apache.wicket.markup.html.WebMarkupContainer;
29  import org.apache.wicket.markup.html.list.ListItem;
30  import org.apache.wicket.markup.html.list.ListView;
31  import org.apache.wicket.markup.html.panel.Panel;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  import org.springframework.util.ClassUtils;
35  
36  public class DashboardExtensionsPanel extends Panel {
37  
38      private static final long serialVersionUID = 6381578992589664490L;
39  
40      protected static final Logger LOG = LoggerFactory.getLogger(DashboardExtensionsPanel.class);
41  
42      public DashboardExtensionsPanel(
43              final String id, final List<Class<? extends BaseExtWidget>> extWidgetClasses, final PageReference pageRef) {
44  
45          super(id);
46  
47          List<BaseExtWidget> instances = new ArrayList<>();
48  
49          extWidgetClasses.forEach(clazz -> {
50              Constructor<? extends BaseExtWidget> constructor =
51                      ClassUtils.getConstructorIfAvailable(clazz, String.class, PageReference.class);
52              if (constructor == null) {
53                  LOG.error("Could not find required construtor in {}, ignoring", clazz);
54              } else {
55                  try {
56                      instances.add(constructor.newInstance("widget", pageRef));
57                  } catch (Exception e) {
58                      LOG.error("While creating instance of {}", clazz, e);
59                  }
60              }
61          });
62  
63          ListView<BaseExtWidget> widgets = new ListView<>("widgets", instances) {
64  
65              private static final long serialVersionUID = 4949588177564901031L;
66  
67              @Override
68              protected void populateItem(final ListItem<BaseExtWidget> item) {
69                  WebMarkupContainer widgetContainer = new WebMarkupContainer("widgetContainer");
70                  widgetContainer.setOutputMarkupId(true);
71                  ExtWidget ann = item.getModelObject().getClass().getAnnotation(ExtWidget.class);
72                  if (ann != null) {
73                      widgetContainer.add(new AttributeModifier("class", ann.cssClass()));
74                  }
75                  item.add(widgetContainer);
76  
77                  item.getModelObject().setOutputMarkupId(true);
78                  widgetContainer.add(item.getModelObject());
79              }
80          };
81          add(widgets);
82      }
83  }