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.Collections; 024import java.util.HashMap; 025import java.util.Map; 026import java.util.concurrent.ExecutionException; 027import java.util.concurrent.Future; 028 029/** 030 * Causes a modal dialog to be display on behalf of the conlet. 031 */ 032public class OpenModalDialog extends ConsoleCommand { 033 034 private final String conletType; 035 private final String conletId; 036 private final Future<String> content; 037 private Map<String, Object> options; 038 039 /** 040 * Creates a new event. The content must be valid HTML, i.e. it 041 * must start with a tag. See the JavaScript documentation of the 042 * <a href="../jsdoc/interfaces/ModalDialogOptions.html"> 043 * modal dialog options</a> for details. 044 * 045 * @param conletType the conlet type 046 * @param conletId the conlet id 047 * @param content the content (valid HTML) 048 * @param options the options (must be serializable as JSON), see 049 * the JavaScript documentation of the 050 * <a href="../jsdoc/interfaces/ModalDialogOptions.html"> 051 * modal dialog options</a> for details. 052 */ 053 public OpenModalDialog(String conletType, String conletId, 054 Future<String> content, Map<String, Object> options) { 055 this.conletType = conletType; 056 this.conletId = conletId; 057 this.content = content; 058 this.options = options; 059 } 060 061 /** 062 * Creates a new event without any options. 063 * The content must be valid HTML, i.e. it 064 * must start with a tag. 065 * 066 * @param conletType the conlet type 067 * @param conletId the conlet id 068 * @param content the content (valid HTML) 069 */ 070 public OpenModalDialog(String conletType, String conletId, 071 Future<String> content) { 072 this(conletType, conletId, content, null); 073 } 074 075 /** 076 * Adds an option to the event. 077 * 078 * @param name the option name 079 * @param value the option value (must be serializable as JSON) 080 * @return the event for easy chaining 081 */ 082 public OpenModalDialog addOption(String name, Object value) { 083 if (options == null) { 084 options = new HashMap<>(); 085 } 086 options.put(name, value); 087 return this; 088 } 089 090 /** 091 * Returns the content. 092 * 093 * @return the content 094 */ 095 public Future<String> content() { 096 return content; 097 } 098 099 /** 100 * Return the options. 101 * 102 * @return the options 103 */ 104 public Map<String, Object> options() { 105 return options == null ? Collections.emptyMap() : options; 106 } 107 108 @Override 109 public void emitJson(Writer writer) throws IOException { 110 Map<String, Object> options = options(); 111 try { 112 emitJson(writer, "openModalDialog", conletType, conletId, 113 content().get(), options); 114 } catch (ExecutionException | InterruptedException e) { 115 throw new IOException(e); 116 } 117 } 118 119}