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    @Deprecated
058    public DatatablesProvider(Channel componentChannel, Map<?, ?> properties) {
059        super(componentChannel);
060        stylingInfo = new StylingInfo(this, properties);
061    }
062
063    /**
064     * Provides a resource bundle for localization.
065     * The default implementation looks up a bundle using the
066     * package name plus "l10n" as base name.
067     * 
068     * @return the resource bundle
069     */
070    @Deprecated
071    @Override
072    protected ResourceBundle resourceBundle(Locale locale) {
073        return ResourceBundle.getBundle(
074            getClass().getPackage().getName() + ".l10n", locale,
075            getClass().getClassLoader(),
076            ResourceBundle.Control.getNoFallbackControl(
077                ResourceBundle.Control.FORMAT_DEFAULT));
078    }
079
080    /**
081     * On {@link ConsoleReady}, fire the appropriate {@link AddPageResources}.
082     *
083     * @param event the event
084     * @param connection the web console connection
085     * @throws TemplateNotFoundException the template not found exception
086     * @throws MalformedTemplateNameException the malformed template name exception
087     * @throws ParseException the parse exception
088     * @throws IOException Signals that an I/O exception has occurred.
089     */
090    @Deprecated
091    @Handler(priority = 100)
092    @SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
093    public void onConsoleReady(ConsoleReady event, ConsoleConnection connection)
094            throws TemplateNotFoundException, MalformedTemplateNameException,
095            ParseException, IOException {
096        String minExt = event.renderSupport()
097            .useMinifiedResources() ? ".min" : "";
098        ResourceBundle bundle = resourceBundle(connection.locale());
099        String script = "$.fn.dataTable.defaults.oLanguage._hungarianMap"
100            + "[\"lengthAll\"] = \"sLengthAll\";\n"
101            + "$.extend( $.fn.dataTable.defaults.oLanguage, {\n"
102            + " 'sLengthAll': 'all',\n"
103            + "} );\n"
104            + "$.extend( $.fn.dataTable.defaults.oLanguage, "
105            + bundle.getString("DataTablesL10n") + ");\n";
106        String baseDir = "datatables-20180804";
107        String styling = stylingInfo.get();
108        if (!"jqueryui".equals(styling) && !"bootstrap4".equals(styling)) {
109            styling = "standard";
110        }
111        @SuppressWarnings("PMD.UnnecessaryVarargsArrayCreation")
112        AddPageResources addRequest = new AddPageResources()
113            .addCss(event.renderSupport()
114                .pageResource(baseDir + "/" + styling + "/datatables"
115                    + minExt + ".css"))
116            .addScriptResource(new ScriptResource()
117                .setRequires("jquery")
118                .setProvides(new String[] { "datatables.net" })
119                .setScriptUri(event.renderSupport().pageResource(
120                    baseDir + "/" + styling + "/datatables" + minExt + ".js")))
121            .addScriptResource(new ScriptResource()
122                .setRequires(new String[] { "datatables.net" })
123                .setScriptUri(event.renderSupport().pageResource(
124                    baseDir + "/processing().js")))
125            .addScriptResource(new ScriptResource()
126                .setRequires(new String[] { "datatables.net" })
127                .setScriptSource(script));
128        if ("jqueryui".equals(styling)) {
129            addRequest.addCss(event.renderSupport().pageResource(
130                "jqueryui-overrides-1.0.0.css"));
131        }
132        connection.respond(addRequest);
133    }
134
135}