001/* 002 * JGrapes Event Driven Framework 003 * Copyright (C) 2020 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.examples.styletest; 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.ConsoleConnection; 035import org.jgrapes.webconsole.base.WebConsoleUtils; 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.events.SetLocale; 042import org.jgrapes.webconsole.base.freemarker.FreeMarkerConlet; 043 044/** 045 * 046 */ 047public class StyleTestConlet extends FreeMarkerConlet<Serializable> { 048 049 private static final Set<RenderMode> MODES 050 = RenderMode.asSet(RenderMode.View); 051 052 /** 053 * Creates a new component with its channel set to the given 054 * channel. 055 * 056 * @param componentChannel the channel that the component's 057 * handlers listen on by default and that 058 * {@link Manager#fire(Event, Channel...)} sends the event to 059 */ 060 public StyleTestConlet(Channel componentChannel) { 061 super(componentChannel); 062 } 063 064 /** 065 * Trigger loading of resources when the console is ready. 066 * 067 * @param event the event 068 * @param consoleConnection the console connection 069 * @throws TemplateNotFoundException the template not found exception 070 * @throws MalformedTemplateNameException the malformed template name exception 071 * @throws ParseException the parse exception 072 * @throws IOException Signals that an I/O exception has occurred. 073 */ 074 @Handler 075 public void onConsoleReady(ConsoleReady event, 076 ConsoleConnection consoleConnection) 077 throws TemplateNotFoundException, MalformedTemplateNameException, 078 ParseException, IOException { 079 // Add HelloWorldConlet resources to page 080 consoleConnection.respond(new AddConletType(type()) 081 .addRenderMode(RenderMode.View).setDisplayNames( 082 localizations(consoleConnection.supportedLocales(), 083 "conletName")) 084 .addScript(new ScriptResource() 085 .setScriptType("module").setScriptUri( 086 event.renderSupport().conletResource(type(), 087 "StyleTest-functions.ftl.js"))) 088 .addCss(event.renderSupport(), WebConsoleUtils.uriFromPath( 089 "StyleTest-style.css"))); 090 } 091 092 @Override 093 protected Set<RenderMode> doRenderConlet(RenderConletRequestBase<?> event, 094 ConsoleConnection channel, String conletId, 095 Serializable conletState) throws Exception { 096 Set<RenderMode> renderedAs = new HashSet<>(); 097 if (event.renderAs().contains(RenderMode.View)) { 098 Template tpl 099 = freemarkerConfig().getTemplate("StyleTest-view.ftl.html"); 100 channel.respond(new RenderConlet(type(), conletId, 101 processTemplate(event, tpl, 102 fmModel(event, channel, conletId, conletState))) 103 .setRenderAs( 104 RenderMode.View.addModifiers(event.renderAs())) 105 .setSupportedModes(MODES)); 106 renderedAs.add(RenderMode.View); 107 } 108 return renderedAs; 109 } 110 111 @Override 112 protected boolean doSetLocale(SetLocale event, ConsoleConnection channel, 113 String conletId) throws Exception { 114 return true; 115 } 116 117}