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