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