001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2022 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.HashSet;
024import java.util.Set;
025import org.jgrapes.webconsole.base.Conlet;
026import org.jgrapes.webconsole.base.Conlet.RenderMode;
027
028/**
029 * Inform the front-end about changes of a conlet type.
030 * 
031 * The only supported change is a modification of the render
032 * modes offered to the user (see {@link AddConletType#addRenderMode}.
033 */
034public class UpdateConletType extends ConsoleCommand {
035
036    private final String conletType;
037    private final Set<Conlet.RenderMode> renderModes = new HashSet<>();
038
039    /**
040     * Create a new event for the given web console component type.
041     * 
042     * @param conletType a unique id for the web console component type 
043     * (usually the class name)
044     */
045    public UpdateConletType(String conletType) {
046        this.conletType = conletType;
047    }
048
049    /**
050     * Return the web console component type.
051     * 
052     * @return the web console component type
053     */
054    public String conletType() {
055        return conletType;
056    }
057
058    /**
059     * Add a render mode.
060     *
061     * @param mode the mode
062     * @return the event for easy chaining
063     */
064    public UpdateConletType addRenderMode(RenderMode mode) {
065        renderModes.add(mode);
066        return this;
067    }
068
069    /**
070     * Return the render modes.
071     * 
072     * @return the result
073     */
074    public Set<RenderMode> renderModes() {
075        return renderModes;
076    }
077
078    @Override
079    public void toJson(Writer writer) throws IOException {
080        toJson(writer, "updateConletType", conletType(),
081            renderModes().stream().map(RenderMode::name)
082                .toArray(size -> new String[size]));
083    }
084}