001/*
002 *  Copyright 2016 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.core.ui.ribbonconfiguration;
017
018import org.apache.avalon.framework.configuration.Configuration;
019import org.apache.avalon.framework.configuration.ConfigurationException;
020import org.slf4j.Logger;
021
022/**
023 * The class represent an exclusion declared in a ribbon file
024 */
025public class RibbonExclude
026{
027    /**
028     * Type of exclusion in the ribbon configuration
029     */
030    public enum EXCLUDETYPE
031    {
032        /** Excluding an import */
033        IMPORT("import"),
034        /** Excluding an extension */
035        APPMENU("app-menu"),
036        /** Excluding a file */
037        USERMENU("user-menu"),
038        /** Excluding a tab */
039        TAB("tab"),
040        /** Excluding a control*/
041        CONTROL("control");
042        
043        private String _value;
044        
045        private EXCLUDETYPE(String value)
046        {
047            this._value = value;   
048        }
049           
050        @Override
051        public String toString() 
052        {
053            return _value;
054        }   
055        
056        /**
057         * Converts a string to a EXCLUDETYPE
058         * @param type The type to convert
059         * @return The exclude type corresponding to the string or null if unknown
060         */
061        public static EXCLUDETYPE createsFromString(String type)
062        {
063            for (EXCLUDETYPE v : EXCLUDETYPE.values())
064            {
065                if (v.toString().equals(type))
066                {
067                    return v;
068                }
069            }
070            return null;
071        }
072    }
073    
074    /**
075     * Target of exclusion in the ribbon configuration
076     */
077    public enum EXCLUDETARGET
078    {
079        /** Excluding by plugin */
080        PLUGIN("plugin"),
081        /** Excluding by extension */
082        EXTENSION("extension"),
083        /** Excluding by file */
084        FILE("file"),
085        /** Excluding by label */
086        LABEL("label"),
087        /** Excluding by id */
088        ID("id");
089        
090        private String _value;
091        
092        private EXCLUDETARGET(String value)
093        {
094            this._value = value;   
095        }
096           
097        @Override
098        public String toString() 
099        {
100            return _value;
101        }   
102        
103        /**
104         * Converts a string to a EXCLUDETARGET
105         * @param target The type to convert
106         * @return The exclude type corresponding to the string or null if unknown
107         */
108        public static EXCLUDETARGET createsFromString(String target)
109        {
110            for (EXCLUDETARGET v : EXCLUDETARGET.values())
111            {
112                if (v.toString().equals(target))
113                {
114                    return v;
115                }
116            }
117            return null;
118        }
119    }
120    
121    private EXCLUDETYPE _type;
122    private EXCLUDETARGET _target;
123    private String _value;
124    private Logger _logger;
125    
126    /**
127     * Configure a new exclusion
128     * @param configuration The ribbon configuration for the exclude tag
129     * @param logger The logger
130     * @throws ConfigurationException If an error occurs
131     */
132    public RibbonExclude(Configuration configuration, Logger logger) throws ConfigurationException
133    {
134        _logger = logger;
135        
136        String type = configuration.getName();
137        String target = configuration.getAttribute("type", null);
138        _value = configuration.getValue();
139
140        _type = EXCLUDETYPE.createsFromString(type);
141        
142        if (EXCLUDETYPE.CONTROL.equals(_type) && target == null)
143        {
144            target = "id";
145        }
146        
147        _target = EXCLUDETARGET.createsFromString(target);
148        
149        if (_type == null || _target == null || _value == null)
150        {
151            throw new ConfigurationException("Invalid exclude tag '" + type + "' in the ribbon configuration", configuration);
152        }
153        
154        if (_logger.isDebugEnabled())
155        {
156            _logger.debug("RibbonConfigurationManager : Exclusion of " +  type + " with target '" + target + "' and value '" + _value + "'");
157        }
158    }
159    
160    /**
161     * Get the type of exclusion
162     * @return The type
163     */
164    public EXCLUDETYPE getType()
165    {
166        return _type;
167    }
168    
169    /**
170     * Get the target of exclusion
171     * @return The target
172     */
173    public EXCLUDETARGET getTarget()
174    {
175        return _target;
176    }
177    
178    /**
179     * Get the exclusion value
180     * @return The value
181     */
182    public String getValue()
183    {
184        return _value;
185    }
186}