001/* 002 * JGrapes Event Driven Framework 003 * Copyright (C) 2017-2018 Michael N. Lipp 004 * 005 * This program is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU Affero General Public License as published by 007 * the Free Software Foundation; either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, but 011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 013 * for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License along 016 * with this program; if not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jgrapes.webconsole.base.events; 020 021import java.io.IOException; 022import java.io.Writer; 023import java.util.HashMap; 024import java.util.Map; 025 026/** 027 * Causes a notification to be display on the top of the web console page. 028 * 029 * The event triggers the creation of a notification widget in the 030 * web console page. 031 */ 032public class DisplayNotification extends ConsoleCommand { 033 034 private String content; 035 private Map<String, Object> options; 036 037 /** 038 * Creates a new event. The content must be valid HTML, i.e. it 039 * must start with a tag (usually a "`<span>`"). See the console's 040 * <a href="../jsdoc/classes/Console.html#notification"> 041 * notification</a> method and the JavaScript documentation of the 042 * <a href="../jsdoc/interfaces/NotificationOptions.html"> 043 * notification options</a> for details. 044 * 045 * @param content the content (valid HTML) 046 * @param options the options (must be serializable as JSON) 047 */ 048 public DisplayNotification(String content, Map<String, Object> options) { 049 this.content = content; 050 this.options = options; 051 } 052 053 /** 054 * Creates a new event without any options. 055 * The content must be valid HTML, i.e. it 056 * must start with a tag (usually a "`<span>`"). 057 * 058 * @param content the content (valid HTML) 059 */ 060 public DisplayNotification(String content) { 061 this(content, new HashMap<>()); 062 } 063 064 /** 065 * Adds an option to the event. 066 * 067 * @param name the option name 068 * @param value the option value (must be serializable as JSON) 069 * @return the event for easy chaining 070 */ 071 public DisplayNotification addOption(String name, Object value) { 072 options.put(name, value); 073 return this; 074 } 075 076 /** 077 * Returns the content. 078 * 079 * @return the content 080 */ 081 public String content() { 082 return content; 083 } 084 085 /** 086 * Return the options. 087 * 088 * @return the options 089 */ 090 public Map<String, Object> options() { 091 return options; 092 } 093 094 @Override 095 public void emitJson(Writer writer) throws IOException { 096 options.put("destroyOnClose", true); 097 emitJson(writer, "displayNotification", content(), options); 098 } 099 100}