Package org.onemind.swingweb.templaterender.helper

Source Code of org.onemind.swingweb.templaterender.helper.ComponentCssGenerator

/*
* Copyright (C) 2004 TiongHiang Lee
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not,  write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Email: thlee@onemindsoft.org
*/

package org.onemind.swingweb.templaterender.helper;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import org.onemind.commons.java.html.css.Css;
import org.onemind.commons.java.text.SimpleTextGenerator;
import org.onemind.commons.java.text.TextGenerator;
/**
* Css generator for awt component
* @author TiongHiang Lee (thlee@onemindsoft.org)
*/
public class ComponentCssGenerator
{

    private AwtCssEncoder _encoder = new AwtCssEncoder();

    private SimpleTextGenerator _generator = new SimpleTextGenerator(",", ":");

    public ComponentCssGenerator()
    {
        addGenerator("background", new TextGenerator()
        {

            public void generateText(String str, Object obj, StringBuffer sb)
            {
                generateBackground(str, obj, sb);
            }
        });
        addGenerator("border", new TextGenerator()
        {

            public void generateText(String str, Object obj, StringBuffer sb)
            {
                generateBorder(str, obj, sb);
            }
        });
        addGenerator("color", new TextGenerator()
        {

            public void generateText(String str, Object obj, StringBuffer sb)
            {
                generateBorder(str, obj, sb);
            }
        });
        addGenerator("font", new TextGenerator()
        {

            public void generateText(String str, Object obj, StringBuffer sb)
            {
                generateFont(str, obj, sb);
            }
        });
        addGenerator("height", new TextGenerator()
        {

            public void generateText(String str, Object obj, StringBuffer sb)
            {
                generateHeight(str, obj, sb);
            }
        });
        addGenerator("width", new TextGenerator()
        {

            public void generateText(String str, Object obj, StringBuffer sb)
            {
                generateWidth(str, obj, sb);
            }
        });
        addGenerator("x", new TextGenerator()
        {

            public void generateText(String str, Object obj, StringBuffer sb)
            {
                generateLeft(str, obj, sb);
            }
        });
        addGenerator("y", new TextGenerator()
        {

            public void generateText(String str, Object obj, StringBuffer sb)
            {
                generateTop(str, obj, sb);
            }
        });
        addGenerator("text-align", new TextGenerator()
        {

            public void generateText(String str, Object obj, StringBuffer sb)
            {
                generateTextAlignment(str, obj, sb);
            }
        });
        addGenerator("scrollbar", new TextGenerator()
        {

            public void generateText(String str, Object obj, StringBuffer sb)
            {
                generateScrollbar(str, obj, sb);
            }
        });
        addGenerator("textarea-scroll", new TextGenerator()
        {

            public void generateText(String str, Object obj, StringBuffer sb)
            {
                generateTextAreaScroll(str, obj, sb);
            }
        });
    }

    /**
     * Add a sub generator
     * @param spec
     * @param subGen
     */
    protected void addGenerator(String spec, TextGenerator subGen)
    {
        _generator.addGenerator(spec, subGen);
    }

    /**
     * Get the encoder
     * @return the encoder
     */
    public AwtCssEncoder getEncoder()
    {
        return _encoder;
    }

    /**
     * Generate css
     * @param spec the spec
     * @param obj the object
     * @return return the css
     */
    public String generateCss(String spec, Object obj)
    {
        return _generator.generateText(spec, obj).toString();
    }

    /**
     * {@inheritDoc}
     */
    public void generateBackground(String str, Object obj, StringBuffer sb)
    {
        Component com = (Component) obj;
        if (com.isBackgroundSet())
        {
            getEncoder().encodeColor(Css.Attr.background_color, com.getBackground(), sb);
        }
    }

    /**
     * Generate border
     * @param obj the object
     * @param sb
     */
    public void generateBorder(String str, Object obj, StringBuffer sb)
    {
        Component com = (Component) obj;
        if (com instanceof JComponent)
        {
            JComponent con = (JComponent) com;
            Border border = con.getBorder();
            getEncoder().encodeCssAttr(Css.Attr.border_width, "1px", sb);
            boolean paintBorder = true;
            if (con instanceof AbstractButton)
            {
                paintBorder = ((AbstractButton) con).isBorderPainted();
            }
            if (border == null || border instanceof EmptyBorder || !paintBorder) //default
            {
                getEncoder().encodeCssAttr(Css.Attr.border_style, "none", sb);
                getEncoder().encodeCssAttr(Css.Attr.border_width, "0", sb);
            } else if (border instanceof LineBorder)
            {
                LineBorder lb = (LineBorder) border;
                getEncoder().encodeCssAttr(Css.Attr.border_style, "solid", sb);
                getEncoder().encodeColor(Css.Attr.border_color, lb.getLineColor(), sb);
                getEncoder().encodeCssAttr(Css.Attr.border, String.valueOf(lb.getThickness()), sb);
            } else if (border instanceof EtchedBorder)
            {
                EtchedBorder eb = (EtchedBorder) border;
                if (eb.getEtchType() == EtchedBorder.LOWERED)
                {
                    getEncoder().encodeCssAttr(Css.Attr.border_style, "groove", sb);
                } else
                {
                    getEncoder().encodeCssAttr(Css.Attr.border_style, "ridge", sb);
                }
                getEncoder().encodeColor(Css.Attr.border_color, eb.getHighlightColor(), sb);
            } else if (border instanceof BevelBorder)
            {
                BevelBorder bb = (BevelBorder) border;
                if (bb.getBevelType() == BevelBorder.LOWERED)
                {
                    getEncoder().encodeCssAttr(Css.Attr.border_style, "inset", sb);
                } else
                {
                    getEncoder().encodeCssAttr(Css.Attr.border_style, "outset", sb);
                }
                getEncoder().encodeColor(Css.Attr.border_color, bb.getHighlightOuterColor(), sb);
            } else if (border instanceof TitledBorder)
            {
                getEncoder().encodeCssAttr(Css.Attr.border_style, "inset", sb);
            } else
            {
                //getEncoder().encodeCssAttr(Css.Attr.border_style, "outset", sb);
            }
        } else
        {
        }
    }

    /**
     * {@inheritDoc}
     */
    public void generateForeground(String str, Object obj, StringBuffer sb)
    {
        Component com = (Component) obj;
        if (com.isForegroundSet())
        {
            getEncoder().encodeColor(Css.Attr.color, com.getForeground(), sb);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void generateFont(String str, Object obj, StringBuffer sb)
    {
        Component com = (Component) obj;
        if (com.isFontSet())
        {
            getEncoder().encodeFont(com.getFont(), sb);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void generateHeight(String str, Object obj, StringBuffer sb)
    {
        Component com = (Component) obj;
        getEncoder().encodeHeight(com.getHeight(), sb);
    }

    /**
     * {@inheritDoc}
     */
    public void generateWidth(String str, Object obj, StringBuffer sb)
    {
        Component com = (Component) obj;
        getEncoder().encodeWidth(com.getWidth(), sb);
    }

    /**
     * {@inheritDoc}
     */
    public void generateLeft(String str, Object obj, StringBuffer sb)
    {
        Component com = (Component) obj;
        getEncoder().encodeCssAttr(Css.Attr.left, String.valueOf(com.getX()), sb);
    }

    /**
     * {@inheritDoc}
     */
    public void generateTop(String str, Object obj, StringBuffer sb)
    {
        Component com = (Component) obj;
        getEncoder().encodeCssAttr(Css.Attr.top, String.valueOf(com.getY()), sb);
    }

    /**
     * {@inheritDoc}
     */
    public void generateTextAlignment(String str, Object obj, StringBuffer sb)
    {
        Label lbl = (Label) obj;
        switch (lbl.getAlignment())
        {
            case Label.LEFT :
                getEncoder().encodeCssAttr(Css.Attr.text_align, Css.AttrValue.left, sb);
                break;
            case Label.RIGHT :
                getEncoder().encodeCssAttr(Css.Attr.text_align, Css.AttrValue.right, sb);
                break;
            default :
                getEncoder().encodeCssAttr(Css.Attr.text_align, Css.AttrValue.center, sb);
        }
    }

    public void generateTextAreaScroll(String str, Object obj, StringBuffer sb)
    {
        if (obj instanceof TextArea)
        {
            TextArea txt = (TextArea) obj;
            if (txt.getScrollbarVisibility() == TextArea.SCROLLBARS_NONE)
            {
                _encoder.encodeCssAttr(Css.Attr.overflow, Css.AttrValue.hidden, sb);
            } else
            {
                _encoder.encodeCssAttr(Css.Attr.overflow, Css.AttrValue.auto, sb);
            }
        } else if (obj instanceof JTextArea)
        {
            //JTextArea has no scrollbars
            _encoder.encodeCssAttr(Css.Attr.overflow, Css.AttrValue.hidden, sb);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void generateScrollbar(String str, Object obj, StringBuffer sb)
    {
        if (obj instanceof ScrollPane)
        {
            ScrollPane com = (ScrollPane) obj;
            switch (com.getScrollbarDisplayPolicy())
            {
                case ScrollPane.SCROLLBARS_ALWAYS :
                case ScrollPane.SCROLLBARS_AS_NEEDED :
                    getEncoder().encodeCssAttr(Css.Attr.overflow, Css.AttrValue.scroll, sb);
                    break;
                case ScrollPane.SCROLLBARS_NEVER :
                    break;
                default :
            //should not happen
            }
        } else if (obj instanceof JScrollPane)
        {
            getEncoder().encodeCssAttr(Css.Attr.overflow, Css.AttrValue.scroll, sb);
        }
    }
}
TOP

Related Classes of org.onemind.swingweb.templaterender.helper.ComponentCssGenerator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.