001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (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.gwt;
029
030import org.opencms.gwt.shared.CmsIconUtil;
031import org.opencms.util.CmsStringUtil;
032
033import java.util.ArrayList;
034import java.util.List;
035
036/**
037 * This is a helper class for creating the text of the CSS rule for a single icon based on resource type and file suffix.<p>
038 *
039 * @since 8.0.0
040 */
041public class CmsIconCssRuleBuilder {
042
043    /** The uri of the icon image. */
044    private String m_imageUri = "INVALID_ICON";
045
046    /** The list of selector strings. */
047    private List<String> m_selectors = new ArrayList<String>();
048
049    /**
050     * Adds a selector for a resource type and a file suffix.<p>
051     *
052     * @param type the resource type name
053     * @param suffix the file suffix
054     * @param small true if the selector should be for the small icon
055     */
056    public void addSelectorForSubType(String type, String suffix, boolean small) {
057
058        String template = " .%1$s.%2$s.%3$s";
059        String selector = String.format(
060            template,
061            CmsIconUtil.TYPE_ICON_CLASS,
062            CmsIconUtil.getResourceTypeIconClass(type, small),
063            CmsIconUtil.getResourceSubTypeIconClass(type, suffix, small));
064        m_selectors.add(selector);
065    }
066
067    /**
068     * Adds a selector for a resource type.<p>
069     *
070     * @param type the name of the resource type
071     * @param small true if the selector should be for the small icon
072     */
073    public void addSelectorForType(String type, boolean small) {
074
075        String template = " div.%1$s.%2$s";
076        String selector = String.format(
077            template,
078            CmsIconUtil.TYPE_ICON_CLASS,
079            CmsIconUtil.getResourceTypeIconClass(type, small));
080        m_selectors.add(selector);
081    }
082
083    /**
084     * Sets the URI of the icon image file.<p>
085     *
086     * @param imageUri the URI of the icon image file
087     */
088    public void setImageUri(String imageUri) {
089
090        m_imageUri = imageUri;
091    }
092
093    /**
094     * Writes the CSS to a string buffer.<p>
095     *
096     * @param buffer the string buffer to which the
097     */
098    public void writeCss(StringBuffer buffer) {
099
100        buffer.append(CmsStringUtil.listAsString(m_selectors, ", "));
101        buffer.append(" { background-image: url(\"");
102        buffer.append(m_imageUri);
103        buffer.append("\");} ");
104    }
105
106}