All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.tapestry5.internal.services.ClientInfrastructureImpl Maven / Gradle / Ivy

Go to download

Central module for Tapestry, containing interfaces to the Java Servlet API and all core services and components.

There is a newer version: 5.8.6
Show newest version
// Copyright 2009, 2010 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry5.internal.services;

import java.util.Collections;
import java.util.List;
import java.util.Locale;

import org.apache.tapestry5.Asset;
import org.apache.tapestry5.SymbolConstants;
import org.apache.tapestry5.ioc.annotations.Symbol;
import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
import org.apache.tapestry5.ioc.services.SymbolSource;
import org.apache.tapestry5.ioc.services.ThreadLocale;
import org.apache.tapestry5.services.AssetSource;
import org.apache.tapestry5.services.ClientInfrastructure;

/**
 * The default JavaScript Stack consists of Prototype, Scriptaculous & the Tapestry-specific library.
 * 
 * @since 5.1.0.2
 */
public class ClientInfrastructureImpl implements ClientInfrastructure
{
    private final SymbolSource symbolSource;

    private final AssetSource assetSource;

    private final ThreadLocale threadLocale;

    private final List javaScriptStack, stylesheetStack;

    private final Asset consoleJavaScript, consoleStylesheet;

    private final boolean isBlackbirdEnabled;

    private static final String[] CORE_JAVASCRIPT = new String[]
    {
    // Core scripts added to any page that uses scripting

            "${tapestry.scriptaculous}/prototype.js",

            "${tapestry.scriptaculous}/scriptaculous.js",

            "${tapestry.scriptaculous}/effects.js",

            // Uses functions defined by the prior three

            "${tapestry.default-javascript}", };

    // Because of changes to the logic of how stylesheets get incorporated, the default stylesheet
    // was removed, the logic for it is now in TapestryModule.contributeMarkupRenderer().

    private static final String[] CORE_STYLESHEET = new String[0];

    public ClientInfrastructureImpl(SymbolSource symbolSource, AssetSource assetSource, ThreadLocale threadLocale,
            @Symbol(SymbolConstants.BLACKBIRD_ENABLED)
            boolean isBlackbirdEnabled)
    {
        this.symbolSource = symbolSource;
        this.assetSource = assetSource;
        this.threadLocale = threadLocale;
        this.isBlackbirdEnabled = isBlackbirdEnabled;

        javaScriptStack = convertToAssets(CORE_JAVASCRIPT);
        stylesheetStack = convertToAssets(CORE_STYLESHEET);

        consoleJavaScript = expand("${tapestry.blackbird}/blackbird.js", "org/apache/tapestry5/tapestry-console.js",
                null);
        consoleStylesheet = expand("${tapestry.blackbird}/blackbird.css", "org/apache/tapestry5/tapestry-console.css",
                null);
    }

    private List convertToAssets(String[] paths)
    {
        List assets = CollectionFactory.newList();

        for (String path : paths)
        {
            assets.add(expand(path, null));
        }

        return Collections.unmodifiableList(assets);
    }

    private Asset expand(String path, Locale locale)
    {
        String expanded = symbolSource.expandSymbols(path);

        return assetSource.getAsset(null, expanded, locale);
    }

    private Asset expand(String blackbirdPath, String consolePath, Locale locale)
    {
        String path = isBlackbirdEnabled ? blackbirdPath : consolePath;

        return expand(path, locale);
    }

    public List getJavascriptStack()
    {
        Asset messages = assetSource.getAsset(null, "org/apache/tapestry5/tapestry-messages.js", threadLocale
                .getLocale());

        return createStack(javaScriptStack, messages, consoleJavaScript);
    }

    public List getStylesheetStack()
    {
        return createStack(stylesheetStack, consoleStylesheet);
    }

    public List createStack(List stack, Asset... assets)
    {
        List result = CollectionFactory.newList(stack);

        for (Asset next : assets)
        {
            result.add(next);
        }

        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy