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.web.skin;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.runtime.model.Enumerator;
029
030/**
031 * Enumerates the available skins.
032 */
033public class SkinEnumerator implements Enumerator<String>, org.ametys.runtime.parameter.Enumerator, Serviceable
034{
035    
036    /** The skin manager. */
037    protected SkinsManager _skinManager;
038    
039    @Override
040    public void service(ServiceManager serviceManager) throws ServiceException
041    {
042        _skinManager = (SkinsManager) serviceManager.lookup(SkinsManager.ROLE);
043    }
044    
045    public Map<String, I18nizableText> getTypedEntries() throws Exception
046    {
047        Map<String, I18nizableText> entries = new HashMap<>();
048        
049        Set<String> skins = _skinManager.getSkins();
050        for (String id : skins)
051        {
052            Skin skin = _skinManager.getSkin(id);
053            
054            entries.put(id, skin.getLabel());
055        }
056        
057        return entries;
058    }
059    
060    public I18nizableText getEntry(String value) throws Exception
061    {
062        Skin skin = _skinManager.getSkin(value);
063        
064        return skin == null ? null : skin.getLabel();
065    }
066    
067    @Override
068    // TODO NEWATTRIBUTEAPI: remove this method when org.ametys.runtime.parameter.Enumerator will be removed
069    public Map<Object, I18nizableText> getEntries() throws Exception
070    {
071        Map<Object, I18nizableText> result = new HashMap<>();
072        for (Map.Entry<String, I18nizableText> entry : getTypedEntries().entrySet())
073        {
074            result.put(entry.getKey(), entry.getValue());
075        }
076        return Collections.unmodifiableMap(result);
077    }
078    
079    @Override
080    // TODO NEWATTRIBUTEAPI: remove this method when org.ametys.runtime.parameter.Enumerator will be removed
081    public Map<String, Object> getConfiguration()
082    {
083        return Collections.EMPTY_MAP;
084    }
085
086}