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.Set;
024import org.jgrapes.webconsole.base.Conlet.RenderMode;
025
026/**
027 * Request the browser to remove a conlet view from the display.
028 */
029public class DeleteConlet extends ConsoleCommand {
030
031    private final String conletId;
032    private final Set<RenderMode> renderModes;
033
034    /**
035     * Creates a new event.
036     *
037     * @param conletId the web console component (view) that should be deleted
038     * @param renderModes the views to delete. If empty, all views should
039     * be deleted, i.e. the conlet may no longer be used in the browser. 
040     */
041    public DeleteConlet(String conletId, Set<RenderMode> renderModes) {
042        this.conletId = conletId;
043        this.renderModes = renderModes;
044    }
045
046    /**
047     * Returns the web console component id.
048     * 
049     * @return the web console component id
050     */
051    public String conletId() {
052        return conletId;
053    }
054
055    /**
056     * Returns the render modes that should be deleted. An empty
057     * set indicates that all views should be deleted, i.e.
058     * the conlet is no longer used in the browser.
059     *
060     * @return the render modes
061     */
062    public Set<RenderMode> renderModes() {
063        return renderModes;
064    }
065
066    @Override
067    public void toJson(Writer writer) throws IOException {
068        toJson(writer, "deleteConlet", conletId(),
069            renderModes.stream().map(RenderMode::name)
070                .toArray(size -> new String[size]));
071    }
072
073}