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.webconsole.provider.gridstack;
020
021import freemarker.core.ParseException;
022import freemarker.template.MalformedTemplateNameException;
023import freemarker.template.TemplateNotFoundException;
024import java.io.IOException;
025import java.util.Collections;
026import java.util.Map;
027import org.jgrapes.core.Channel;
028import org.jgrapes.core.Event;
029import org.jgrapes.core.Manager;
030import org.jgrapes.core.annotation.Handler;
031import org.jgrapes.webconsole.base.ConsoleConnection;
032import org.jgrapes.webconsole.base.PageResourceProvider;
033import org.jgrapes.webconsole.base.events.AddPageResources;
034import org.jgrapes.webconsole.base.events.AddPageResources.ScriptResource;
035import org.jgrapes.webconsole.base.events.ConsoleReady;
036
037/**
038 * Provider for the [Gridstack.js](http://gridstackjs.com/) library.
039 */
040@SuppressWarnings({ "PMD.GuardLogStatement", "PMD.DataflowAnomalyAnalysis" })
041public class GridstackProvider extends PageResourceProvider {
042
043    /**
044     * The Configuration.
045     * 
046     * @deprecated No longer needed because gridstack provides its own
047     * touch library now.
048     */
049    @SuppressWarnings("PMD.FieldNamingConventions")
050    @Deprecated
051    public enum Configuration {
052        CoreOnly, CoreWithJQUiPlugin, All
053    }
054
055    /**
056     * Creates a new component with its channel set to the given 
057     * channel.
058     *
059     * @param componentChannel the channel that the component's
060     * handlers listen on by default and that 
061     * {@link Manager#fire(Event, Channel...)} sends the event to
062     */
063    @SuppressWarnings("PMD.UnusedFormalParameter")
064    public GridstackProvider(Channel componentChannel) {
065        this(componentChannel, Collections.emptyMap());
066    }
067
068    /**
069     * Creates a new component with its channel set to the given 
070     * channel.
071     *
072     * @param componentChannel the channel that the component's
073     * handlers listen on by default and that 
074     * {@link Manager#fire(Event, Channel...)} sends the event to
075     * @param properties the properties used to configure the component
076     */
077    @SuppressWarnings("PMD.UnusedFormalParameter")
078    public GridstackProvider(Channel componentChannel, Map<?, ?> properties) {
079        super(componentChannel);
080    }
081
082    /**
083     * On {@link ConsoleReady}, fire the appropriate {@link AddPageResources}.
084     *
085     * @param event the event
086     * @param connection the web console connection
087     * @throws TemplateNotFoundException the template not found exception
088     * @throws MalformedTemplateNameException the malformed template name exception
089     * @throws ParseException the parse exception
090     * @throws IOException Signals that an I/O exception has occurred.
091     */
092    @Handler(priority = 100)
093    @SuppressWarnings("PMD.AvoidDuplicateLiterals")
094    public void onConsoleReady(ConsoleReady event, ConsoleConnection connection)
095            throws TemplateNotFoundException, MalformedTemplateNameException,
096            ParseException, IOException {
097        String minExt = event.renderSupport()
098            .useMinifiedResources() ? ".min" : "";
099        AddPageResources addRequest = new AddPageResources()
100            .addCss(event.renderSupport().pageResource(
101                "gridstack/gridstack" + minExt + ".css"));
102        connection.respond(new AddPageResources()
103            .addScriptResource(new ScriptResource()
104                .setProvides(new String[] { "gridstack" })));
105        connection.respond(addRequest);
106    }
107}