/*
 * @author <a href="mailto:novotny@aei.mpg.de">Jason Novotny</a>
 * @version $Id$
 */

package org.gridlab.gridsphere.provider.portletui.beans;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;

/**
 * The <code>TextBean</code> represents text to be displayed
 */
public class TextBean extends BaseComponentBean implements TagBean {

    public static final String NAME = "tb";

    protected String style = "info";

    /* @deprectaed -- use MessageStyle instead */
    // CSS definitions according to Portlet API spec. PLT.C
    public static final String MSG_STATUS = "portlet-msg-status";
    public static final String MSG_INFO = "portlet-msg-info";
    public static final String MSG_ERROR = "portlet-msg-error";
    public static final String MSG_ALERT = "portlet-msg-alert";
    public static final String MSG_SUCCESS = "portlet-msg-success";
    public static final String MSG_ITALIC = "italic";
    public static final String MSG_BOLD = "bold";
    public static final String MSG_UNDERLINE = "underline";

    /**
     * Constructs a default text bean
     */
    public TextBean() {
        super(NAME);
        this.cssClass = MessageStyle.MSG_INFO;
    }

    /**
     * Constructs a text bean using a supplied bean identifier
     *
     * @param beanId the bean identifier
     */
    public TextBean(String beanId) {
        super(NAME);
        this.beanId = beanId;
        this.cssClass = MessageStyle.MSG_INFO;
    }

    /**
     * Constructs a text bean from a supplied portlet request and bean identifier
     *
     * @param req    the portlet request
     * @param beanId the bean identifier
     */
    public TextBean(HttpServletRequest req, String beanId) {
        super(NAME, req);
        this.beanId = beanId;
        this.cssClass = MessageStyle.MSG_INFO;
    }

    /**
     * Returns the style of the text: Available styles are
     * <ul>
     * <li>nostyle - plain text</li>
     * <li>error - error text</li>
     * <li>info - default info text</li>
     * <li>status - status text</li>
     * <li>alert - alert text</li>
     * <li>success - success text</li>
     *
     * @return the text style
     */
    public String getStyle() {
        return style;
    }

    /**
     * Sets the style of the text: Available styles are
     * <ul>
     * <li>error</li>
     * <li>info</li>
     * <li>status</li>
     * <li>alert</li>
     * <li>success</li>
     *
     * @param style the text style
     */
    public void setStyle(String style) {
        this.style = style;
    }

    public String toStartString() {
        return "";
    }

    public String toEndString() {
        String text = "";
        if (value == null) return "";
        if (style.equalsIgnoreCase("error") || (style.equalsIgnoreCase("err"))) {
            this.cssClass = MessageStyle.MSG_ERROR;
        } else if (style.equalsIgnoreCase("status")) {
            this.cssClass = MessageStyle.MSG_STATUS;
        } else if (style.equalsIgnoreCase("info")) {
            this.cssClass = MessageStyle.MSG_INFO;
        } else if (style.equalsIgnoreCase("alert")) {
            this.cssClass = MessageStyle.MSG_ALERT;
        } else if (style.equalsIgnoreCase("success")) {
            this.cssClass = MessageStyle.MSG_SUCCESS;
        } else if (style.equalsIgnoreCase("nostyle")) {
            return value;
        } else if (style.equalsIgnoreCase(MessageStyle.MSG_BOLD)) {
            return "<b>" + value + "</b>";
        } else if (style.equalsIgnoreCase(MessageStyle.MSG_ITALIC)) {
            return "<i>" + value + "</i>";
        } else if (style.equalsIgnoreCase(MessageStyle.MSG_UNDERLINE)) {
            return "<u>" + value + "</u>";
        }
        if (locale != null) {
            ComponentOrientation orientation = ComponentOrientation.getOrientation(locale);
            if (orientation.isLeftToRight()) {
                text = "<span " + getFormattedCss();
            } else {
                text = "<span dir=\"rtl\"" + getFormattedCss();
            }
        } else {
            text = "<span " + getFormattedCss();
        }
        text += ">" + value + "</span>";
        return text;
    }
}

@@ -2648,24 +2648,275 @@
  text = %22%22;%0A
+        String dir = %22ltr%22;%0A        if (locale != null) %7B%0A            ComponentOrientation orientation = ComponentOrientation.getOrientation(locale);%0A            if (!orientation.isLeftToRight()) %7B%0A                dir = %22rtl%22;%0A            %7D%0A        %7D%0A
         if (
@@ -3675,16 +3675,37 @@
 turn %22%3Cb
+ dir=%5C%22%22 + dir + %22%5C%22 
 %3E%22 + val
@@ -3809,16 +3809,37 @@
 turn %22%3Ci
+ dir=%5C%22%22 + dir + %22%5C%22 
 %3E%22 + val
@@ -3950,402 +3950,102 @@
  %22%3Cu
-%3E%22 + value + %22%3C/u%3E%22;%0A        %7D%0A        if (locale != null) %7B%0A            ComponentOrientation orientation = ComponentOrientation.getOrientation(locale);%0A            if (orientation.isLeftToRight()) %7B%0A                text = %22%3Cspan %22 + getFormattedCss();%0A            %7D else %7B%0A                text = %22%3Cspan dir=%5C%22rtl%5C%22%22 + getFormattedCss();%0A            %7D%0A        %7D else %7B%0A            text = %22%3Cspan
+ dir=%5C%22%22 + dir + %22%5C%22 %3E%22 + value + %22%3C/u%3E%22;%0A        %7D%0A%0A        text = %22%3Cspan dir=%5C%22%22 + dir + %22%5C%22
  %22 +
@@ -4064,25 +4064,16 @@
 dCss();%0A
-        %7D
 %0A       
@@ -4102,24 +4102,33 @@
  %22%3C/span%3E%22;%0A
+        %0A
         retu

make sure ltr is added in case of b, i, u text