001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.apps;
029
030import java.util.Collections;
031import java.util.Map;
032
033import com.google.common.collect.Maps;
034import com.vaadin.ui.AbstractComponent;
035import com.vaadin.ui.Component;
036import com.vaadin.v7.ui.Label;
037
038/**
039 * An abstract subclass of A_CmsWorkplaceApp which provides an additional way for the main component of an app
040 * (the widget returned by getComponentForState) to influence the app layout of the app itself (i.e. the parts outside the main component).
041 *
042 *
043 * This is done using two methods:
044 *
045 * getAttributesForComponent is used to extract additiona
046 *
047 */
048public abstract class A_CmsAttributeAwareApp extends A_CmsWorkplaceApp {
049
050    /**Attribute for info. */
051    public static final String ATTR_INFO_COMPONENT = "ATTR_INFO_COMPONENT";
052
053    /**Attribute for full height. */
054    public static final String ATTR_MAIN_HEIGHT_FULL = "ATTR_MAIN_HEIGHT_FULL";
055
056    /**vaadin component. */
057    private Component m_infoComponent;
058
059    /**
060     * Opens the requested sub view.<p>
061     *
062     * @param state the state
063     * @param updateState <code>true</code> to update the state URL token
064     */
065    @Override
066    public void openSubView(String state, boolean updateState) {
067
068        if (updateState) {
069            CmsAppWorkplaceUi.get().changeCurrentAppState(state);
070        }
071        Component comp = getComponentForState(state);
072        if (comp != null) {
073            updateMainComponent(comp);
074        } else {
075            m_rootLayout.setMainContent(new Label("Malformed path, tool not available for path: " + state));
076            updateAppAttributes(Collections.<String, Object> emptyMap());
077        }
078        updateSubNav(getSubNavEntries(state));
079        updateBreadCrumb(getBreadCrumbForState(state));
080
081    }
082
083    /**
084     * Replaces the app's  main component with the given component.<p>
085     *
086     * This also handles the attributes for the component, just as if the given component was returned by an app's
087     * getComponentForState method.
088     *
089     * @param comp the component to set as the main component
090     */
091    public void updateMainComponent(Component comp) {
092
093        comp.setSizeFull();
094        m_rootLayout.setMainContent(comp);
095        Map<String, Object> attributes = getAttributesForComponent(comp);
096        updateAppAttributes(attributes);
097    }
098
099    /**
100     * Gets the attributes from a given component.<p>
101     *
102     * @param component to read attributes from
103     * @return map of attributes
104     */
105    protected Map<String, Object> getAttributesForComponent(Component component) {
106
107        Map<String, Object> result = Maps.newHashMap();
108        if (component instanceof AbstractComponent) {
109            AbstractComponent abstractComp = (AbstractComponent)component;
110            if (abstractComp.getData() instanceof Map) {
111                Map<?, ?> map = (Map<?, ?>)abstractComp.getData();
112                for (Map.Entry<?, ?> entry : map.entrySet()) {
113                    if (entry.getKey() instanceof String) {
114                        result.put((String)(entry.getKey()), entry.getValue());
115                    }
116                }
117            }
118        }
119        return result;
120    }
121
122    /**
123     * Handles the attributes.<p>
124     *
125     * @param attributes to set
126     */
127    protected void updateAppAttributes(Map<String, Object> attributes) {
128
129        m_rootLayout.setMainHeightFull(Boolean.TRUE.equals(attributes.get(ATTR_MAIN_HEIGHT_FULL)));
130
131        Object infoComponentObj = attributes.get(ATTR_INFO_COMPONENT);
132        if (m_infoComponent != null) {
133            m_infoLayout.removeComponent(m_infoComponent);
134        }
135        if (infoComponentObj instanceof Component) {
136            m_infoComponent = (Component)infoComponentObj;
137            m_infoLayout.addComponent(m_infoComponent);
138        }
139    }
140}