001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.gwt.client.ui; 029 030import java.util.ArrayList; 031import java.util.List; 032 033import com.google.gwt.core.client.Scheduler; 034import com.google.gwt.core.client.Scheduler.ScheduledCommand; 035import com.google.gwt.user.client.Timer; 036 037/** 038 * User feedback provider.<p> 039 * 040 * @since 8.0.0 041 */ 042public final class CmsNotification { 043 044 /** 045 * Notification Mode.<p> 046 */ 047 public static enum Mode { 048 049 /** Alert mode. */ 050 BROADCAST, 051 052 /** Blocking mode. */ 053 BUSY, 054 055 /** Normal mode. */ 056 NORMAL, 057 058 /** Sticky mode. */ 059 STICKY; 060 } 061 062 /** 063 * Notification Type.<p> 064 */ 065 public static enum Type { 066 067 /** Error Notification. */ 068 ERROR, 069 070 /** Normal Notification. */ 071 NORMAL, 072 073 /** Warning Notification. */ 074 WARNING; 075 } 076 077 /** The duration of the animations. */ 078 public static final int ANIMATION_DURATION = 200; 079 080 /** The singleton instance. */ 081 private static CmsNotification INSTANCE; 082 083 /** The widget. */ 084 private I_CmsNotificationWidget m_widget; 085 086 /** The current notifications. */ 087 private List<CmsNotificationMessage> m_messages; 088 089 /** 090 * Hide constructor.<p> 091 */ 092 private CmsNotification() { 093 094 m_messages = new ArrayList<CmsNotificationMessage>(); 095 } 096 097 /** 098 * Returns the singleton instance.<p> 099 * 100 * @return the singleton instance 101 */ 102 public static CmsNotification get() { 103 104 if (INSTANCE == null) { 105 INSTANCE = new CmsNotification(); 106 } 107 return INSTANCE; 108 } 109 110 /** 111 * Returns the widget.<p> 112 * 113 * @return the widget 114 */ 115 public I_CmsNotificationWidget getWidget() { 116 117 return m_widget; 118 } 119 120 /** 121 * Returns if the notification widget is set. Only if the widget is set, notifications can be shown.<p> 122 * 123 * @return <code>true</code> if the notification widget is set 124 */ 125 public boolean hasWidget() { 126 127 return m_widget != null; 128 } 129 130 /** 131 * Removes the given notification message.<p> 132 * 133 * @param message the message to remove 134 */ 135 public void removeMessage(CmsNotificationMessage message) { 136 137 m_messages.remove(message); 138 if (hasWidget()) { 139 m_widget.removeMessage(message); 140 } 141 } 142 143 /** 144 * Sends a new notification, that will be removed automatically.<p> 145 * 146 * @param type the notification type 147 * @param message the message 148 */ 149 public void send(Type type, final String message) { 150 151 final CmsNotificationMessage notificationMessage = new CmsNotificationMessage(Mode.NORMAL, type, message); 152 m_messages.add(notificationMessage); 153 if (hasWidget()) { 154 m_widget.addMessage(notificationMessage); 155 } 156 Timer timer = new Timer() { 157 158 @Override 159 public void run() { 160 161 removeMessage(notificationMessage); 162 } 163 }; 164 timer.schedule(4000 * (type == Type.NORMAL ? 1 : 2)); 165 166 } 167 168 /** 169 * Sends a new blocking alert notification that can be closed by the user.<p> 170 * 171 * @param type the notification type 172 * @param message the message 173 */ 174 public void sendAlert(Type type, String message) { 175 176 CmsNotificationMessage notificationMessage = new CmsNotificationMessage(Mode.BROADCAST, type, message); 177 m_messages.add(notificationMessage); 178 if (hasWidget()) { 179 m_widget.addMessage(notificationMessage); 180 } 181 } 182 183 /** 184 * Sends a new blocking notification that can not be removed by the user.<p> 185 * 186 * @param type the notification type 187 * @param message the message 188 * 189 * @return the message, use to hide the message 190 */ 191 public CmsNotificationMessage sendBusy(Type type, final String message) { 192 193 CmsNotificationMessage notificationMessage = new CmsNotificationMessage(Mode.BUSY, type, message); 194 m_messages.add(notificationMessage); 195 if (hasWidget()) { 196 m_widget.addMessage(notificationMessage); 197 } 198 return notificationMessage; 199 } 200 201 /** 202 * Sends a new notification after all other events have been processed.<p> 203 * 204 * @param type the notification type 205 * @param message the message 206 */ 207 public void sendDeferred(final Type type, final String message) { 208 209 Scheduler.get().scheduleDeferred(new ScheduledCommand() { 210 211 /** 212 * @see com.google.gwt.core.client.Scheduler.ScheduledCommand#execute() 213 */ 214 public void execute() { 215 216 send(type, message); 217 } 218 }); 219 220 } 221 222 /** 223 * Sends a new sticky notification that can not be removed by the user.<p> 224 * 225 * @param type the notification type 226 * @param message the message 227 * 228 * @return the message, use to hide the message 229 */ 230 public CmsNotificationMessage sendSticky(Type type, String message) { 231 232 CmsNotificationMessage notificationMessage = new CmsNotificationMessage(Mode.STICKY, type, message); 233 m_messages.add(notificationMessage); 234 if (hasWidget()) { 235 m_widget.addMessage(notificationMessage); 236 } 237 return notificationMessage; 238 } 239 240 /** 241 * Sets the widget.<p> 242 * 243 * @param widget the widget to set 244 */ 245 public void setWidget(I_CmsNotificationWidget widget) { 246 247 if (m_widget != null) { 248 m_widget.clearMessages(); 249 } 250 m_widget = widget; 251 m_widget.clearMessages(); 252 for (CmsNotificationMessage message : m_messages) { 253 m_widget.addMessage(message); 254 } 255 } 256}