001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.db.CmsResourceState;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsProperty;
033import org.opencms.file.CmsPropertyDefinition;
034import org.opencms.file.CmsResource;
035import org.opencms.jsp.util.CmsJspElFunctions;
036import org.opencms.main.CmsLog;
037import org.opencms.site.CmsSite;
038import org.opencms.ui.A_CmsUI;
039import org.opencms.ui.CmsVaadinUtils;
040import org.opencms.ui.components.CmsResourceIcon.IconMode;
041import org.opencms.util.CmsStringUtil;
042import org.opencms.workplace.explorer.CmsResourceUtil;
043
044import java.util.Locale;
045import java.util.Map;
046
047import org.apache.commons.logging.Log;
048
049import com.vaadin.server.ExternalResource;
050import com.vaadin.server.Resource;
051import com.vaadin.ui.Component;
052import com.vaadin.ui.CustomLayout;
053import com.vaadin.ui.TextField;
054import com.vaadin.v7.ui.Label;
055
056/**
057 * Class representing a resource info box.<p>
058 */
059public class CmsResourceInfo extends CustomLayout {
060
061    /** Button container location id. */
062    private static final String BUTTON_CONTAINER = "buttonContainer";
063
064    /** Logger instance for this class. */
065    private static final Log LOG = CmsLog.getLog(CmsResourceInfo.class);
066
067    /** The serial version id. */
068    private static final long serialVersionUID = -1715926038770100307L;
069
070    /** The sub title label. */
071    private Label m_bottomText = new Label();
072
073    /** The button label. */
074    private Label m_buttonLabel = new Label();
075
076    /** The resource icon. */
077    private CmsResourceIcon m_icon = new CmsResourceIcon();
078
079    /** The label on top. */
080    private Label m_topText = new Label();
081
082    /** The input on top. */
083    private TextField m_topInput = new TextField();
084
085    /**
086     * Constructor.<p>
087     */
088    public CmsResourceInfo() {
089
090        super();
091        try {
092            initTemplateContentsFromInputStream(
093                CmsVaadinUtils.readCustomLayout(CmsResourceInfo.class, "resourceinfo.html"));
094            addComponent(m_topText, "topLabel");
095            addComponent(m_bottomText, "bottomLabel");
096            addComponent(m_icon, "icon");
097            addComponent(m_buttonLabel, "buttonContainer");
098            addComponent(m_topInput, "topInput");
099            m_topInput.setVisible(false);
100        } catch (Exception e) {
101            LOG.error(e.getLocalizedMessage(), e);
102        }
103    }
104
105    /**
106     * Constructor.<p>
107     *
108     * @param resource the resource
109     */
110    public CmsResourceInfo(CmsResource resource) {
111
112        this(resource, true);
113    }
114
115    /**
116     * Constructor.<p>
117     *
118     * @param resource the resource
119     * @param useState true if the resource state should be displayed
120     */
121    public CmsResourceInfo(CmsResource resource, boolean useState) {
122
123        this();
124        Locale locale = A_CmsUI.get().getLocale();
125        CmsResourceUtil resourceUtil = new CmsResourceUtil(A_CmsUI.getCmsObject(), resource);
126        resourceUtil.setAbbrevLength(100);
127        String galleryTitle = resourceUtil.getGalleryTitle(locale);
128        m_topText.setValue(galleryTitle);
129        m_topInput.setValue(galleryTitle);
130        m_bottomText.setValue(resourceUtil.getPath());
131        if (!resourceUtil.isReleasedAndNotExpired()) {
132            m_topText.addStyleName("o-expired");
133            m_topInput.addStyleName("o-expired");
134            m_bottomText.addStyleName("o-expired");
135        }
136        m_icon.initContent(resourceUtil, useState ? resource.getState() : null, true, true);
137
138    }
139
140    /**
141     * Constructor.<p>
142     *
143     * @param top the title
144     * @param bottom the sub title
145     * @param iconResource the icon resource path
146     */
147    public CmsResourceInfo(String top, String bottom, Resource iconResource) {
148
149        this(top, bottom, iconResource, null);
150    }
151
152    /**
153     * Constructor.<p>
154     *
155     * @param top the title
156     * @param bottom the sub title
157     * @param iconResource the icon resource path
158     * @param resourceState the resource state
159     */
160    public CmsResourceInfo(String top, String bottom, Resource iconResource, CmsResourceState resourceState) {
161
162        this();
163        m_topText.setValue(top);
164        m_topInput.setValue(top);
165        m_bottomText.setValue(CmsJspElFunctions.stripHtml(bottom));
166        m_icon.initContent(null, iconResource, resourceState, false, true);
167    }
168
169    /**
170     * Constructor.<p>
171     *
172     * @param top the title
173     * @param bottom the sub title
174     * @param iconPath the icon resource path
175     */
176    public CmsResourceInfo(String top, String bottom, String iconPath) {
177
178        this(top, bottom, new ExternalResource(iconPath));
179    }
180
181    /**
182     * Creates a resource info widget for a resource that looks like the sitemap entry for that resource.<p>
183     *
184     * @param resource the resource
185     * @param baseSite the base site
186     *
187     * @return the resource info widget
188     */
189    public static CmsResourceInfo createSitemapResourceInfo(CmsResource resource, CmsSite baseSite) {
190
191        String title = resource.getName();
192        String path = resource.getRootPath();
193
194        CmsResourceInfo info = new CmsResourceInfo();
195        CmsResourceUtil resUtil = new CmsResourceUtil(A_CmsUI.getCmsObject(), resource);
196
197        CmsObject cms = A_CmsUI.getCmsObject();
198        try {
199            Map<String, CmsProperty> props = CmsProperty.toObjectMap(cms.readPropertyObjects(resource, false));
200            CmsProperty navtextProp = props.get(CmsPropertyDefinition.PROPERTY_NAVTEXT);
201            CmsProperty titleProp = props.get(CmsPropertyDefinition.PROPERTY_TITLE);
202
203            if ((navtextProp != null) && (navtextProp.getValue() != null)) {
204                title = navtextProp.getValue();
205            } else if ((titleProp != null) && (titleProp.getValue() != null)) {
206                title = titleProp.getValue();
207            }
208        } catch (Exception e) {
209            LOG.error(e.getLocalizedMessage(), e);
210        }
211        info.getTopLine().setValue(title);
212        if (baseSite != null) {
213            String siteRoot = baseSite.getSiteRoot();
214            if (path.startsWith(siteRoot)) {
215                path = path.substring(siteRoot.length());
216                path = CmsStringUtil.joinPaths("/", path);
217            }
218        }
219        info.getBottomLine().setValue(path);
220        Resource icon = CmsResourceIcon.getSitemapResourceIcon(
221            A_CmsUI.getCmsObject(),
222            resUtil.getResource(),
223            IconMode.localeCompare);
224        info.getResourceIcon().initContent(resUtil, icon, null, true, false);
225        return info;
226    }
227
228    /**
229     *
230     */
231    public void decorateTopInput() {
232
233        m_topText.setVisible(false);
234        m_topInput.setVisible(true);
235    }
236
237    /**
238     *
239     */
240    public void decorateTopLabel() {
241
242        m_topText.setVisible(true);
243        m_topInput.setVisible(false);
244    }
245
246    /**
247     * Gets the bottom label.<p>
248     *
249     * @return the bottom label
250     */
251    public Label getBottomLine() {
252
253        return m_bottomText;
254    }
255
256    /**
257     * Gets the button label.<p>
258     *
259     * @return the button label
260     */
261    public Component getButtonWidget() {
262
263        return getComponent("buttonContainer");
264    }
265
266    /**
267     * Gets the resource icon.<p>
268     *
269     * @return the resource icon
270     */
271    public CmsResourceIcon getResourceIcon() {
272
273        return m_icon;
274    }
275
276    /**
277     * Returns the editable text on top.<p>
278     *
279     * @return the editable text on top
280     */
281    public TextField getTopInput() {
282
283        return m_topInput;
284    }
285
286    /**
287     * Gets the top label.<p>
288     *
289     * @return the top label
290     */
291    public Label getTopLine() {
292
293        return m_topText;
294    }
295
296    /**
297     * Replaces the button component.<p>
298     *
299     * @param button the new button component
300     */
301    public void setButtonWidget(Component button) {
302
303        addComponent(button, BUTTON_CONTAINER);
304    }
305
306    /**
307     * Replaces the text of the top label and top input.
308     *
309     * @param text the text
310     */
311    public void setTopLineText(String text) {
312
313        m_topText.setValue(text);
314        m_topInput.setValue(text);
315    }
316
317}