view flys-artifacts/src/main/java/de/intevation/flys/utils/ThemeUtil.java @ 1747:d2a17e990c70

Improved the Themes: we now support special themes for facets which need to match a given pattern string. flys-artifacts/trunk@3047 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 20 Oct 2011 13:45:45 +0000
parents 6cdc7a77d3d4
children d56b94325bec
line wrap: on
line source
package de.intevation.flys.utils;

import org.apache.log4j.Logger;

import java.awt.Color;
import java.awt.Font;

import org.w3c.dom.Document;

import de.intevation.artifacts.common.utils.XMLUtils;


/**
 * Utility to deal with themes and their representations.
 */
public class ThemeUtil {

    private static Logger logger =
        Logger.getLogger(ThemeUtil.class);

    public final static String XPATH_LINE_COLOR =
        "/theme/field[@name='linecolor']/@default";

    public static final String XPATH_LINE_SIZE =
        "/theme/field[@name='linesize']/@default";

    public final static String XPATH_TEXT_COLOR =
        "/theme/field[@name='textcolor']/@default";

    public final static String XPATH_TEXT_SIZE =
        "/theme/field[@name='textsize']/@default";

    public final static String XPATH_TEXT_FONT =
        "/theme/field[@name='font']/@default";

    /**
     * Parses line width, defaulting to 0.
     * @param theme the theme
     */
    public static int parseLineWidth(Document theme) {
        String size = XMLUtils.xpathString(theme, XPATH_LINE_SIZE, null);
        if (size == null || size.length() == 0) {
            return 0;
        }

        try {
            return Integer.valueOf(size);
        }
        catch (NumberFormatException nfe) {
            //logger.warn("Unable to set line size from string: '" + size + "'");
        }
        return 0;
    }


    /**
     * Parses text size, defaulting to 10.
     * @param theme The theme.
     */
    public static int parseTextSize(Document theme) {
        String size = XMLUtils.xpathString(theme, XPATH_TEXT_SIZE, null);
        if (size == null || size.length() == 0) {
            return 10;
        }

        try {
            return Integer.valueOf(size);
        }
        catch (NumberFormatException nfe) {
        }
        return 10;
    }


    /**
     * Parses text color.
     * @param theme The theme.
     */
    public static Color parseTextColor(Document theme) {
        String color = XMLUtils.xpathString(theme, XPATH_TEXT_COLOR, null);
        return parseRGB(color);
    }


    /**
     * Parses the font.
     * @param theme The theme.
     */
    public static Font parseTextFont(Document theme) {
        String font = XMLUtils.xpathString(theme, XPATH_TEXT_FONT, null);
        if (font == null || font.length() == 0) {
            return null;
        }

        int size = parseTextSize(theme);
        Font f = new Font (font, 0, size);
        return f;
    }


    /**
     * Parse a string like "103, 100, 0" and return a corresping color.
     * @param rgbtext Color as string representation, e.g. "255,0,20".
     * @return Color, null in case of issues.
     */
    public static Color parseRGB(String rgbtext) {
        logger.debug("parseColor: " + rgbtext);
        if (rgbtext == null) {
            return null;
        }
        String rgb[] = rgbtext.split(",");
        Color c = null;
        try {
            c = new Color(
                    Integer.valueOf(rgb[0].trim()),
                    Integer.valueOf(rgb[1].trim()),
                    Integer.valueOf(rgb[2].trim()));
        }
        catch (NumberFormatException nfe) {
            c = null;
        }
        return c;
    }

    /**
     * Gets color from color field.
     * @param theme    the theme document.
     * @return color.
     */
    public static Color parseLineColorField(Document theme) {
        String color = XMLUtils.xpathString(theme, XPATH_LINE_COLOR, null);
        return parseRGB(color);
    }
}

http://dive4elements.wald.intevation.org