View Javadoc
1   /**
2    * This file Copyright (c) 2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.admincentral.banner;
35  
36  import info.magnolia.event.EventBus;
37  import info.magnolia.i18nsystem.SimpleTranslator;
38  import info.magnolia.ui.api.event.AdmincentralEventBus;
39  import info.magnolia.ui.api.location.DefaultLocation;
40  import info.magnolia.ui.api.location.LocationController;
41  import info.magnolia.ui.api.message.Message;
42  import info.magnolia.ui.contentapp.detail.DetailLocation;
43  import info.magnolia.ui.framework.message.MessageEvent;
44  import info.magnolia.ui.framework.message.MessageEventHandler;
45  
46  import java.util.stream.Stream;
47  
48  import javax.inject.Inject;
49  import javax.inject.Named;
50  
51  import com.google.common.collect.Lists;
52  import com.vaadin.ui.Button;
53  import com.vaadin.ui.CssLayout;
54  
55  /**
56   * Banner container is a wrapper layout around {@link Banner}s. It it also subscribed to listen to the {@link MessageEvent}s.
57   * When {@link MessageEvent} occurs, container will create banner based on the message type and adds it to the layout.
58   */
59  public class BannerContainer extends CssLayout implements MessageEventHandler {
60      private final EventBus eventBus;
61  
62      private final LocationController locationController;
63  
64      private final SimpleTranslator i18n;
65  
66      @Inject
67      public BannerContainer(@Named(AdmincentralEventBus.NAME) EventBus eventBus, LocationController locationController, SimpleTranslator i18n) {
68          this.eventBus = eventBus;
69          this.locationController = locationController;
70          this.i18n = i18n;
71  
72          initialize();
73      }
74  
75      private void initialize() {
76          setWidth(100, Unit.PERCENTAGE);
77          addStyleNames("banner-container");
78          eventBus.addHandler(MessageEvent.class, this);
79      }
80  
81      @Override
82      public void messageSent(MessageEvent event) {
83          final Message message = event.getMessage();
84          Banneranner/Banner.html#Banner">Banner banner = new Banner(message);
85          Button readMoreButton = createReadMoreButton(message);
86          banner.addButton(readMoreButton);
87  
88          if (getAllBanners().count() > 0) {
89              Button readAllButton = createReadAllButton();
90              banner.addButton(readAllButton);
91              // we allow to display only one banner at a time
92              closeAllBanners();
93          }
94  
95          setHeight("80px");
96          addComponent(banner);
97      }
98  
99      private Button createReadMoreButton(Message message) {
100         return new Button(i18n.translate("ui.banner.button.readmore"), e -> {
101             closeAllBanners();
102             goToNotificationDetail(message.getId());
103         });
104     }
105 
106     private Button createReadAllButton() {
107         return new Button(i18n.translate("ui.banner.button.readall"), e -> {
108             closeAllBanners();
109             goToNotifications();
110         });
111     }
112 
113     private void goToNotificationDetail(String messageId) {
114         locationController.goTo(new DetailLocation("notifications", "detail", messageId));
115     }
116 
117     private void closeAllBanners() {
118         getAllBanners().forEach(this::removeBanner);
119     }
120 
121     private Stream<Banner> getAllBanners() {
122         return Lists.newArrayList(iterator()).stream().filter(b -> b instanceof Banner./../../info/magnolia/admincentral/banner/Banner.html#Banner">Banner).map(b -> (Banner) b);
123     }
124 
125     private void goToNotifications() {
126         locationController.goTo(new DefaultLocation("app", "notifications"));
127     }
128 
129     @Override
130     public void messageCleared(MessageEvent event) {
131     }
132 
133     @Override
134     public void messageRemoved(MessageEvent messageEvent) {
135     }
136 
137     public void removeBanner(Banner banner) {
138         setHeight("0px");
139         removeComponent(banner);
140     }
141 }