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.panels;
20  
21  import com.googlecode.wicket.jquery.core.Options;
22  import com.googlecode.wicket.kendo.ui.widget.notification.Notification;
23  import java.util.List;
24  import org.apache.syncope.client.ui.commons.Constants;
25  import org.apache.syncope.client.ui.commons.StyledNotificationBehavior;
26  import org.apache.wicket.IGenericComponent;
27  import org.apache.wicket.core.request.handler.IPartialPageRequestHandler;
28  import org.apache.wicket.feedback.FeedbackMessage;
29  import org.apache.wicket.feedback.FeedbackMessagesModel;
30  import org.apache.wicket.feedback.IFeedback;
31  import org.apache.wicket.markup.html.panel.Panel;
32  import org.apache.wicket.model.IModel;
33  
34  public class NotificationPanel extends Panel
35          implements IFeedback, IGenericComponent<List<FeedbackMessage>, NotificationPanel> {
36  
37      private static final long serialVersionUID = 5895940553202128621L;
38  
39      private final Notification notification;
40  
41      public NotificationPanel(final String id) {
42          super(id);
43  
44          Options options = new Options();
45          options.set("appendTo", "'#appendto'");
46          options.set("stacking", "'up'");
47          options.set("templates",
48                  "[ { type: 'success', template: $('#successTemplate').html() },"
49                  + " { type: 'info', template: $('#infoTemplate').html() },"
50                  + " { type: 'error', template: $('#errorTemplate').html() },"
51                  + " { type: 'warning', template: $('#warningTemplate').html() } ] ");
52  
53          notification = new Notification(Constants.FEEDBACK, options) {
54  
55              private static final long serialVersionUID = 785830249476640904L;
56  
57              @Override
58              public StyledNotificationBehavior newWidgetBehavior(final String selector) {
59                  return new StyledNotificationBehavior(selector, options);
60              }
61          };
62  
63          add(notification);
64      }
65  
66      public final void hide(final IPartialPageRequestHandler handler) {
67          this.notification.hide(handler);
68      }
69  
70      public final void refresh(final IPartialPageRequestHandler handler) {
71          this.getModelObject().forEach(message -> {
72              if (message.isError()) {
73                  this.notification.error(handler, message.getMessage());
74              } else if (message.isWarning()) {
75                  // this is necessary before check for success and info in order to show warnings: isSuccess and isInfo
76                  // return true also in case of warnings ...
77                  this.notification.warn(handler, message.getMessage());
78              } else if (message.isSuccess()) {
79                  this.notification.success(handler, message.getMessage());
80              } else if (message.isInfo()) {
81                  this.notification.info(handler, message.getMessage());
82              } else {
83                  this.notification.warn(handler, message.getMessage());
84              }
85              message.markRendered();
86          });
87      }
88  
89      @Override
90      protected IModel<?> initModel() {
91          return new FeedbackMessagesModel(this);
92      }
93  
94      @Override
95      @SuppressWarnings("unchecked")
96      public IModel<List<FeedbackMessage>> getModel() {
97          return (IModel<List<FeedbackMessage>>) this.getDefaultModel();
98      }
99  
100     @Override
101     public NotificationPanel setModel(final IModel<List<FeedbackMessage>> model) {
102         this.setDefaultModel(model);
103         return this;
104     }
105 
106     @Override
107     public NotificationPanel setModelObject(final List<FeedbackMessage> object) {
108         this.setDefaultModelObject(object);
109         return this;
110     }
111 
112     @Override
113     @SuppressWarnings("unchecked")
114     public List<FeedbackMessage> getModelObject() {
115         return (List<FeedbackMessage>) this.getDefaultModelObject();
116     }
117 
118     public String getNotificationMarkupId() {
119         return this.notification.getMarkupId();
120     }
121     
122 }