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.datatables;
020
021import freemarker.core.ParseException;
022import freemarker.template.MalformedTemplateNameException;
023import freemarker.template.TemplateNotFoundException;
024import java.io.IOException;
025import java.util.Locale;
026import java.util.Map;
027import java.util.ResourceBundle;
028import org.jgrapes.core.Channel;
029import org.jgrapes.core.Event;
030import org.jgrapes.core.Manager;
031import org.jgrapes.core.annotation.Handler;
032import org.jgrapes.webconsole.base.ConsoleConnection;
033import org.jgrapes.webconsole.base.PageResourceProvider;
034import org.jgrapes.webconsole.base.StylingInfo;
035import org.jgrapes.webconsole.base.events.AddPageResources;
036import org.jgrapes.webconsole.base.events.AddPageResources.ScriptResource;
037import org.jgrapes.webconsole.base.events.ConsoleReady;
038
039/**
040 * Provider for the [Datatables](https://datatables.net/) library.
041 * 
042 * @deprected Because of its dependency on JQuery.
043 */
044@Deprecated
045public class DatatablesProvider extends PageResourceProvider {
046
047    private final StylingInfo stylingInfo;
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 DatatablesProvider(Channel componentChannel, Map<?, ?> properties) {
058        super(componentChannel);
059        stylingInfo = new StylingInfo(this, properties);
060    }
061
062    /**
063     * Provides a resource bundle for localization.
064     * The default implementation looks up a bundle using the
065     * package name plus "l10n" as base name.
066     * 
067     * @return the resource bundle
068     */
069    protected ResourceBundle resourceBundle(Locale locale) {
070        return ResourceBundle.getBundle(
071            getClass().getPackage().getName() + ".l10n", locale,
072            getClass().getClassLoader(),
073            ResourceBundle.Control.getNoFallbackControl(
074                ResourceBundle.Control.FORMAT_DEFAULT));
075    }
076
077    /**
078     * On {@link ConsoleReady}, fire the appropriate {@link AddPageResources}.
079     *
080     * @param event the event
081     * @param connection the web console connection
082     * @throws TemplateNotFoundException the template not found exception
083     * @throws MalformedTemplateNameException the malformed template name exception
084     * @throws ParseException the parse exception
085     * @throws IOException Signals that an I/O exception has occurred.
086     */
087    @Handler(priority = 100)
088    @SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
089    public void onConsoleReady(ConsoleReady event, ConsoleConnection connection)
090            throws TemplateNotFoundException, MalformedTemplateNameException,
091            ParseException, IOException {
092        String minExt = event.renderSupport()
093            .useMinifiedResources() ? ".min" : "";
094        ResourceBundle bundle = resourceBundle(connection.locale());
095        String script = "$.fn.dataTable.defaults.oLanguage._hungarianMap"
096            + "[\"lengthAll\"] = \"sLengthAll\";\n"
097            + "$.extend( $.fn.dataTable.defaults.oLanguage, {\n"
098            + " 'sLengthAll': 'all',\n"
099            + "} );\n"
100            + "$.extend( $.fn.dataTable.defaults.oLanguage, "
101            + bundle.getString("DataTablesL10n") + ");\n";
102        String baseDir = "datatables-20180804";
103        String styling = stylingInfo.get();
104        if (!"jqueryui".equals(styling) && !"bootstrap4".equals(styling)) {
105            styling = "standard";
106        }
107        AddPageResources addRequest = new AddPageResources()
108            .addCss(event.renderSupport()
109                .pageResource(baseDir + "/" + styling + "/datatables"
110                    + minExt + ".css"))
111            .addScriptResource(new ScriptResource()
112                .setRequires("jquery")
113                .setProvides(new String[] { "datatables.net" })
114                .setScriptUri(event.renderSupport().pageResource(
115                    baseDir + "/" + styling + "/datatables" + minExt + ".js")))
116            .addScriptResource(new ScriptResource()
117                .setRequires(new String[] { "datatables.net" })
118                .setScriptUri(event.renderSupport().pageResource(
119                    baseDir + "/processing().js")))
120            .addScriptResource(new ScriptResource()
121                .setRequires(new String[] { "datatables.net" })
122                .setScriptSource(script));
123        if ("jqueryui".equals(styling)) {
124            addRequest.addCss(event.renderSupport().pageResource(
125                "jqueryui-overrides-1.0.0.css"));
126        }
127        connection.respond(addRequest);
128    }
129
130}