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