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.components;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProperty;
032import org.opencms.file.CmsPropertyDefinition;
033import org.opencms.file.CmsResource;
034import org.opencms.main.CmsLog;
035import org.opencms.site.CmsSite;
036import org.opencms.ui.A_CmsUI;
037import org.opencms.ui.CmsVaadinUtils;
038import org.opencms.ui.components.CmsResourceIcon.IconMode;
039import org.opencms.util.CmsStringUtil;
040import org.opencms.workplace.explorer.CmsResourceUtil;
041
042import java.util.Locale;
043import java.util.Map;
044
045import org.apache.commons.logging.Log;
046
047import com.vaadin.server.ExternalResource;
048import com.vaadin.server.Resource;
049import com.vaadin.ui.Component;
050import com.vaadin.ui.CustomLayout;
051import com.vaadin.v7.ui.Label;
052
053/**
054 * Resource info box.<p>
055 */
056public class CmsResourceInfo extends CustomLayout {
057
058    /** Button container location id. */
059    private static final String BUTTON_CONTAINER = "buttonContainer";
060
061    /** Logger instance for this class. */
062    private static final Log LOG = CmsLog.getLog(CmsResourceInfo.class);
063
064    /** The serial version id. */
065    private static final long serialVersionUID = -1715926038770100307L;
066
067    /** The sub title label. */
068    private Label m_bottomText = new Label();
069
070    /** The button label. */
071    private Label m_buttonLabel = new Label();
072
073    /** The resource icon. */
074    private CmsResourceIcon m_icon = new CmsResourceIcon();
075
076    /** The title label. */
077    private Label m_topText = new Label();
078
079    /**
080     * Constructor.<p>
081     */
082    public CmsResourceInfo() {
083
084        super();
085        try {
086            initTemplateContentsFromInputStream(
087                CmsVaadinUtils.readCustomLayout(CmsResourceInfo.class, "resourceinfo.html"));
088            addComponent(m_topText, "topLabel");
089            addComponent(m_bottomText, "bottomLabel");
090            addComponent(m_icon, "icon");
091            addComponent(m_buttonLabel, "buttonContainer");
092        } catch (Exception e) {
093            LOG.error(e.getLocalizedMessage(), e);
094        }
095    }
096
097    /**
098     * Constructor.<p>
099     *
100     * @param resource the resource
101     */
102    public CmsResourceInfo(CmsResource resource) {
103
104        this(resource, true);
105    }
106
107    /**
108     * Constructor.<p>
109     *
110     * @param resource the resource
111     * @param useState true if the resource state should be displayed
112     */
113    public CmsResourceInfo(CmsResource resource, boolean useState) {
114
115        this();
116        Locale locale = A_CmsUI.get().getLocale();
117        CmsResourceUtil resUtil = new CmsResourceUtil(A_CmsUI.getCmsObject(), resource);
118        resUtil.setAbbrevLength(100);
119        m_topText.setValue(resUtil.getGalleryTitle(locale));
120        m_bottomText.setValue(resUtil.getPath());
121
122        m_icon.initContent(resUtil, useState ? resource.getState() : null, true, true);
123
124    }
125
126    /**
127     * Constructor.<p>
128     *
129     * @param top the title
130     * @param bottom the sub title
131     * @param iconResource the icon resource path
132     */
133    public CmsResourceInfo(String top, String bottom, Resource iconResource) {
134
135        this();
136        m_topText.setValue(top);
137        m_bottomText.setValue(bottom);
138        m_icon.initContent(null, iconResource, null, false, true);
139    }
140
141    /**
142     * Constructor.<p>
143     *
144     * @param top the title
145     * @param bottom the sub title
146     * @param iconPath the icon resource path
147     */
148    public CmsResourceInfo(String top, String bottom, String iconPath) {
149
150        this();
151        m_topText.setValue(top);
152        m_bottomText.setValue(bottom);
153        m_icon.initContent(null, new ExternalResource(iconPath), null, false, true);
154    }
155
156    /**
157     * Creates a resource info widget for a resource that looks like the sitemap entry for that resource.<p>
158     *
159     * @param resource the resource
160     * @param baseSite the base site
161     *
162     * @return the resource info widget
163     */
164    public static CmsResourceInfo createSitemapResourceInfo(CmsResource resource, CmsSite baseSite) {
165
166        String title = resource.getName();
167        String path = resource.getRootPath();
168
169        CmsResourceInfo info = new CmsResourceInfo();
170        CmsResourceUtil resUtil = new CmsResourceUtil(A_CmsUI.getCmsObject(), resource);
171
172        CmsObject cms = A_CmsUI.getCmsObject();
173        try {
174            Map<String, CmsProperty> props = CmsProperty.toObjectMap(cms.readPropertyObjects(resource, false));
175            CmsProperty navtextProp = props.get(CmsPropertyDefinition.PROPERTY_NAVTEXT);
176            CmsProperty titleProp = props.get(CmsPropertyDefinition.PROPERTY_TITLE);
177
178            if ((navtextProp != null) && (navtextProp.getValue() != null)) {
179                title = navtextProp.getValue();
180            } else if ((titleProp != null) && (titleProp.getValue() != null)) {
181                title = titleProp.getValue();
182            }
183        } catch (Exception e) {
184            LOG.error(e.getLocalizedMessage(), e);
185        }
186        info.getTopLine().setValue(title);
187        if (baseSite != null) {
188            String siteRoot = baseSite.getSiteRoot();
189            if (path.startsWith(siteRoot)) {
190                path = path.substring(siteRoot.length());
191                path = CmsStringUtil.joinPaths("/", path);
192            }
193        }
194        info.getBottomLine().setValue(path);
195        Resource icon = CmsResourceIcon.getSitemapResourceIcon(
196            A_CmsUI.getCmsObject(),
197            resUtil.getResource(),
198            IconMode.localeCompare);
199        info.getResourceIcon().initContent(resUtil, icon, null, true, false);
200        return info;
201    }
202
203    /**
204     * Gets the bottom label.<p>
205     *
206     * @return the bottom label
207     */
208    public Label getBottomLine() {
209
210        return m_bottomText;
211    }
212
213    /**
214     * Gets the button label.<p>
215     *
216     * @return the button label
217     */
218    public Component getButtonWidget() {
219
220        return getComponent("buttonContainer");
221    }
222
223    /**
224     * Gets the resource icon.<p>
225     *
226     * @return the resource icon
227     */
228    public CmsResourceIcon getResourceIcon() {
229
230        return m_icon;
231    }
232
233    /**
234     * Gets the top label.<p>
235     *
236     * @return the top label
237     */
238    public Label getTopLine() {
239
240        return m_topText;
241    }
242
243    /**
244     * Replaces the button component.<p>
245     *
246     * @param button the new button component
247     */
248    public void setButtonWidget(Component button) {
249
250        addComponent(button, BUTTON_CONTAINER);
251    }
252
253}