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.webconlet.messagebox;
020
021import freemarker.core.ParseException;
022import freemarker.template.MalformedTemplateNameException;
023import freemarker.template.Template;
024import freemarker.template.TemplateNotFoundException;
025import java.io.IOException;
026import java.io.Serializable;
027import java.util.HashSet;
028import java.util.Map;
029import java.util.Set;
030import org.jgrapes.core.Channel;
031import org.jgrapes.core.Event;
032import org.jgrapes.core.Manager;
033import org.jgrapes.core.annotation.Handler;
034import org.jgrapes.webconsole.base.Conlet.RenderMode;
035import org.jgrapes.webconsole.base.ConsoleConnection;
036import org.jgrapes.webconsole.base.events.AddConletType;
037import org.jgrapes.webconsole.base.events.AddPageResources.ScriptResource;
038import org.jgrapes.webconsole.base.events.ConsoleReady;
039import org.jgrapes.webconsole.base.events.RenderConlet;
040import org.jgrapes.webconsole.base.events.RenderConletRequestBase;
041import org.jgrapes.webconsole.base.freemarker.FreeMarkerConlet;
042
043/**
044 * Example of a simple conlet.
045 */
046public class MessageBoxConlet extends FreeMarkerConlet<Serializable> {
047
048    private static final Set<RenderMode> MODES = RenderMode.asSet(
049        RenderMode.Content);
050
051    /**
052     * Creates a new component with its channel set to the given 
053     * channel.
054     * 
055     * @param componentChannel the channel that the component's 
056     * handlers listen on by default and that 
057     * {@link Manager#fire(Event, Channel...)} sends the event to 
058     */
059    public MessageBoxConlet(Channel componentChannel) {
060        super(componentChannel);
061    }
062
063    /**
064     * Trigger loading of resources when the console is ready.
065     *
066     * @param event the event
067     * @param connection the console connection
068     * @throws TemplateNotFoundException the template not found exception
069     * @throws MalformedTemplateNameException the malformed template name exception
070     * @throws ParseException the parse exception
071     * @throws IOException Signals that an I/O exception has occurred.
072     */
073    @Handler
074    public void onConsoleReady(ConsoleReady event, ConsoleConnection connection)
075            throws TemplateNotFoundException, MalformedTemplateNameException,
076            ParseException, IOException {
077        // Add resources to page
078        connection.respond(
079            new AddConletType(type())
080                .addScript(new ScriptResource()
081                    .setScriptUri(event.renderSupport().conletResource(
082                        type(), "MessageBox-functions.js"))
083                    .setScriptType("module"))
084                .addRenderMode(RenderMode.Content)
085                .addPageContent("headerIcons", Map.of("mailbox", "items")));
086    }
087
088    @Override
089    protected Set<RenderMode> doRenderConlet(RenderConletRequestBase<?> event,
090            ConsoleConnection channel, String conletId,
091            Serializable conletState) throws Exception {
092        Set<RenderMode> renderedAs = new HashSet<>();
093        if (event.renderAs().contains(RenderMode.Content)) {
094            Template tpl
095                = freemarkerConfig().getTemplate("MessageBox.ftl.html");
096            channel.respond(new RenderConlet(type(), conletId,
097                processTemplate(event, tpl,
098                    fmModel(event, channel, conletId, conletState)))
099                        .setRenderAs(RenderMode.Content)
100                        .setSupportedModes(MODES));
101            renderedAs.add(RenderMode.Content);
102        }
103        return renderedAs;
104    }
105
106}