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.portal.providers.fontawesome;
020
021import freemarker.core.ParseException;
022import freemarker.template.MalformedTemplateNameException;
023import freemarker.template.TemplateNotFoundException;
024
025import java.io.IOException;
026
027import org.jgrapes.core.Channel;
028import org.jgrapes.core.Event;
029import org.jgrapes.core.Manager;
030import org.jgrapes.core.annotation.Handler;
031import org.jgrapes.portal.base.PageResourceProvider;
032import org.jgrapes.portal.base.PortalSession;
033import org.jgrapes.portal.base.events.AddPageResources;
034import org.jgrapes.portal.base.events.PortalReady;
035
036/**
037 * Provider for the [Font Awesome](http://fontawesome.com/).
038 */
039public class FontAwesomeProvider extends PageResourceProvider {
040
041    /**
042     * Creates a new component with its channel set to the given 
043     * channel.
044     * 
045     * @param componentChannel the channel that the component's 
046     * handlers listen on by default and that 
047     * {@link Manager#fire(Event, Channel...)} sends the event to 
048     */
049    public FontAwesomeProvider(Channel componentChannel) {
050        super(componentChannel);
051    }
052
053    /**
054     * On {@link PortalReady}, fire the appropriate {@link AddPageResources}.
055     *
056     * @param event the event
057     * @param portalSession the portal session
058     * @throws TemplateNotFoundException the template not found exception
059     * @throws MalformedTemplateNameException the malformed template name exception
060     * @throws ParseException the parse exception
061     * @throws IOException Signals that an I/O exception has occurred.
062     */
063    @Handler(priority = 100)
064    public void onPortalReady(PortalReady event, PortalSession portalSession)
065            throws TemplateNotFoundException, MalformedTemplateNameException,
066            ParseException, IOException {
067        String minExt = event.renderSupport()
068            .useMinifiedResources() ? ".min" : "";
069        portalSession.respond(new AddPageResources()
070            .addCss(event.renderSupport().pageResource(
071                "fontawesome-free-5.2.0-web/css/all" + minExt + ".css")));
072    }
073}