001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 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.base;
020
021import java.util.Map;
022import org.jgrapes.core.ComponentType;
023import org.jgrapes.core.Components;
024
025/**
026 * Evaluates and provides the styling info to a web console component.
027 */
028public class StylingInfo {
029
030    private final ComponentType component;
031    private final Map<?, ?> properties;
032    private String styling;
033
034    /**
035     * Instantiates a new styling info.
036     *
037     * @param component the component
038     * @param properties the properties
039     */
040    public StylingInfo(ComponentType component, Map<?, ?> properties) {
041        super();
042        this.component = component;
043        this.properties = properties;
044    }
045
046    /**
047     * Returns the styling information. The method looks for an entry
048     * with key "style" in properties first. If no entry is found,
049     * it searches through the anchestors of this component for
050     * a component of type {@link ConsoleWeblet} and returns 
051     * the result of invoking {@link ConsoleWeblet#styling()}.
052     * Else it returns "standard".
053     *
054     * @return the result
055     */
056    @SuppressWarnings("PMD.DataflowAnomalyAnalysis")
057    public String get() {
058        if (styling != null) {
059            return styling;
060        }
061        if (properties != null) {
062            styling = (String) properties.get("styling");
063        }
064        if (styling == null) {
065            // Try to get the information from the web console (weblet)
066            ComponentType console = component;
067            while (true) {
068                console = Components.manager(console).parent();
069                if (console == null) {
070                    break;
071                }
072                // Component is in tree, cache result.
073                styling = "standard";
074                if (console instanceof ConsoleWeblet) {
075                    styling = ((ConsoleWeblet) console).styling();
076                    break;
077                }
078            }
079        }
080        if (styling == null) {
081            return "standard";
082        }
083        return styling;
084    }
085
086}