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<Object, Object> 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,
041            Map<Object, Object> properties) {
042        super();
043        this.component = component;
044        this.properties = properties;
045    }
046
047    /**
048     * Returns the styling information. The method looks for an entry
049     * with key "style" in properties first. If no entry is found,
050     * it searches through the anchestors of this component for
051     * a component of type {@link ConsoleWeblet} and returns 
052     * the result of invoking {@link ConsoleWeblet#styling()}.
053     * Else it returns "standard".
054     *
055     * @return the result
056     */
057    @SuppressWarnings("PMD.DataflowAnomalyAnalysis")
058    public String get() {
059        if (styling != null) {
060            return styling;
061        }
062        if (properties != null) {
063            styling = (String) properties.get("styling");
064        }
065        if (styling == null) {
066            // Try to get the information from the web console (weblet)
067            ComponentType console = component;
068            while (true) {
069                console = Components.manager(console).parent();
070                if (console == null) {
071                    break;
072                }
073                // Component is in tree, cache result.
074                styling = "standard";
075                if (console instanceof ConsoleWeblet) {
076                    styling = ((ConsoleWeblet) console).styling();
077                    break;
078                }
079            }
080        }
081        if (styling == null) {
082            return "standard";
083        }
084        return styling;
085    }
086
087}