001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.cms.clientsideelement;
017
018import org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020
021import org.ametys.core.ui.StaticClientSideElement;
022import org.ametys.runtime.i18n.I18nizableText;
023
024/**
025 * This element creates a control button with a menu
026 */
027public class StaticMenu extends StaticClientSideElement
028{
029    private int _galleryIndex;
030    private int _menuIndex;
031    
032    @Override
033    public void configure(Configuration configuration) throws ConfigurationException
034    {
035        super.configure(configuration);
036        
037        _configureGalleries (configuration);
038        _configureItemsMenu(configuration);
039    }
040    
041    /**
042     * Configure the galleries
043     * @param configuration the configuration
044     * @throws ConfigurationException If the configuration has an issue
045     */
046    protected void _configureGalleries(Configuration configuration) throws ConfigurationException
047    {
048        for (Configuration galleryConfiguration : configuration.getChildren("gallery-item"))
049        {
050            _configureGroupsGallery (galleryConfiguration);
051        }
052    }
053    
054    /**
055     * Configure the groups gallery
056     * @param configuration the configuration
057     * @throws ConfigurationException If the configuration has an issue
058     */
059    protected void _configureGroupsGallery(Configuration configuration) throws ConfigurationException
060    {
061        for (Configuration gpConfiguration : configuration.getChildren("gallery-group"))
062        {
063            _configureItemsGallery(gpConfiguration, gpConfiguration.getAttribute("label"));
064        }
065    }
066    
067    /**
068     * Configure the gallery items
069     * @param configuration the configuration
070     * @param group the group gallery
071     * @throws ConfigurationException If the configuration has an issue
072     */
073    protected void _configureItemsGallery(Configuration configuration, String group) throws ConfigurationException
074    {
075        for (Configuration itemConfiguration : configuration.getChildren("button"))
076        {
077            this._script.getParameters().put("gallery-" + _galleryIndex, itemConfiguration.getAttribute("id"));
078            this._script.getParameters().put("gallery-" + _galleryIndex + "-group",  new I18nizableText("plugin." + _pluginName, group));
079            _configureItem(itemConfiguration, "gallery-" + _galleryIndex);
080            _galleryIndex++;
081        }
082        this._script.getParameters().put("gallery-size", _galleryIndex);
083    }
084    
085    /**
086     * Configure the items menu
087     * @param configuration the configuration
088     * @throws ConfigurationException If the configuration has an issue
089     */
090    protected void _configureItemsMenu(Configuration configuration) throws ConfigurationException
091    {
092        for (Configuration itemConfiguration : configuration.getChildren("menu-item"))
093        {
094            this._script.getParameters().put("menu-" + _menuIndex,  itemConfiguration.getAttribute("id"));
095            _configureItem(itemConfiguration, "menu-" + _menuIndex);
096            _menuIndex++;
097        }
098        this._script.getParameters().put("menu-size", _menuIndex);
099    }
100    
101    /**
102     * Configure the item parameters
103     * @param configuration the item configuration
104     * @param prefix the item prefix
105     * @throws ConfigurationException If the configuration has an issue
106     */
107    protected void _configureItem (Configuration configuration, String prefix) throws ConfigurationException
108    {
109        for (Configuration paramConfiguration : configuration.getChildren("param"))
110        {
111            String name = paramConfiguration.getAttribute("name");
112            String value = paramConfiguration.getValue("");
113            
114            if (paramConfiguration.getAttributeAsBoolean("i18n", false))
115            {
116                this._script.getParameters().put(prefix + "-" + name, new I18nizableText("plugin." + _pluginName, value));
117            }
118            else if (paramConfiguration.getAttributeAsBoolean("file", false))
119            {
120                String pluginName = paramConfiguration.getAttribute("plugin", getPluginName());
121                this._script.getParameters().put(prefix + "-" + name, "/plugins/" + pluginName + "/resources/" + value);
122            }
123            else
124            {
125                this._script.getParameters().put(prefix + "-" + name, value);
126            }
127        }
128    }
129}