View Javadoc
1   /**
2    * This file Copyright (c) 2011-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.ui.vaadin.gwt.client.magnoliashell.shellmessage;
35  
36  import info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryCallback;
37  import info.magnolia.ui.vaadin.gwt.client.jquerywrapper.JQueryWrapper;
38  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.shell.MagnoliaShellView;
39  
40  import com.google.gwt.dom.client.Style.Display;
41  import com.google.gwt.event.shared.HandlerRegistration;
42  import com.google.gwt.user.client.DOM;
43  import com.google.gwt.user.client.Element;
44  import com.google.gwt.user.client.Event;
45  import com.google.gwt.user.client.Event.NativePreviewEvent;
46  import com.google.gwt.user.client.Event.NativePreviewHandler;
47  import com.google.gwt.user.client.ui.HTML;
48  import info.magnolia.ui.vaadin.gwt.client.magnoliashell.viewport.animation.FadeAnimation;
49  
50  /**
51   * Simple notification object that pops up when warnings/errors occur.
52   */
53  public abstract class ShellMessageWidget extends HTML {
54  
55      /**
56       * Enumeration of possible message types.
57       */
58      public enum MessageType {
59          WARNING, ERROR, INFO
60      }
61  
62      private static final String STYLE_NAME = "v-shell-notification";
63  
64      private HandlerRegistration eventPreviewReg = null;
65  
66      private final MagnoliaShellView shell;
67  
68      private Element messageTypeEl = DOM.createElement("b");
69  
70      private Element header = DOM.createDiv();
71  
72      private Element topicEl = DOM.createSpan();
73  
74      private Element closeEl = DOM.createDiv();
75  
76      private final String id;
77  
78      private final String message;
79  
80      private final String topic;
81  
82      public ShellMessageWidget(MagnoliaShellView shell, String topic, String message, String id) {
83          super();
84          this.topic = topic;
85          this.shell = shell;
86          this.message = message;
87          this.id = id;
88          eventPreviewReg = Event.addNativePreviewHandler(new NativePreviewHandler() {
89              @Override
90              public void onPreviewNativeEvent(NativePreviewEvent event) {
91                  if (event.getTypeInt() == Event.ONCLICK) {
92                      final Element targetEl = event.getNativeEvent().getEventTarget().cast();
93                      if (getElement().isOrHasChild(targetEl)) {
94                          onMessageClicked(targetEl);
95                      }
96                  }
97              }
98          });
99      }
100 
101     @Override
102     protected void onLoad() {
103         super.onLoad();
104         sinkEvents(Event.MOUSEEVENTS);
105         construct();
106         show();
107     }
108 
109     protected void onMessageClicked(Element targetEl) {
110         close();
111     }
112 
113     protected void close() {
114         hide();
115         getShell().closeMessageEager(getId());
116     }
117 
118     protected void construct() {
119         topicEl.setInnerText(topic);
120         header.appendChild(messageTypeEl);
121         header.appendChild(topicEl);
122 
123         addStyleName(STYLE_NAME);
124 
125         header.setClassName("header");
126         getElement().appendChild(header);
127 
128         applyCloseIconStyles(closeEl);
129         header.appendChild(closeEl);
130 
131         messageTypeEl.setInnerHTML(getMessageTypeCaption());
132     }
133 
134     protected void applyCloseIconStyles(Element element) {
135         element.setClassName("close");
136     }
137 
138     protected abstract String getMessageTypeCaption();
139 
140     protected int getHeaderHeight() {
141         return JQueryWrapper.select(header).cssInt("height");
142     }
143 
144     public void show() {
145         getElement().getStyle().setDisplay(Display.NONE);
146         JQueryWrapper.select(this).slideDown(300, null);
147     }
148 
149     public void hide() {
150         /*JQueryWrapper.select(this).slideUp(300, Callbacks.create(new JQueryCallback() {
151             @Override
152             public void execute(JQueryWrapper query) {
153                 removeFromParent();
154             }
155         }));     */
156         FadeAnimationient/magnoliashell/viewport/animation/FadeAnimation.html#FadeAnimation">FadeAnimation fadeAnimation = new FadeAnimation(0,true);
157         fadeAnimation.addCallback(new JQueryCallback() {
158             @Override
159             public void execute(JQueryWrapper query) {
160                 removeFromParent();
161             }
162         });
163         fadeAnimation.run(500,this.getElement());
164     }
165 
166     public void hideWithoutTransition() {
167         removeFromParent();
168     }
169 
170     @Override
171     public void onBrowserEvent(Event event) {
172         super.onBrowserEvent(event);
173         int eventCode = event.getTypeInt();
174         if (eventCode == Event.ONTOUCHEND) {
175             final Element target = event.getEventTarget().cast();
176             if (target == closeEl) {
177                 close();
178             }
179         }
180     }
181 
182     @Override
183     protected void onUnload() {
184         super.onUnload();
185         if (eventPreviewReg != null) {
186             eventPreviewReg.removeHandler();
187         }
188     }
189 
190     public Element getHeader() {
191         return header;
192     }
193 
194     protected final MagnoliaShellView getShell() {
195         return shell;
196     }
197 
198     public String getMessage() {
199         return message;
200     }
201 
202     public String getId() {
203         return id;
204     }
205 
206     public String getTopic() {
207         return topic;
208     }
209 
210 }