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

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.ArrayList;


/*
 * @author <a href="mailto:oliver.wehrens@aei.mpg.de">Oliver Wehrens</a>
 * @version $Id$
 */

public class MessageBoxBean extends BaseComponentBean implements TagBean {

    public static final String NAME = "mb";

    private List text = new ArrayList();
    private List keys = new ArrayList();
    private String messageType = TextBean.MSG_INFO;
    private String width = null;
    private String height = null;

    public MessageBoxBean() {
        super(NAME);
    }

    public MessageBoxBean(String beanId) {
        super(NAME);
        this.beanId = beanId;
    }

    public MessageBoxBean(HttpServletRequest req, String beanId) {
        super(NAME);
        this.beanId = beanId;
        this.request = req;
    }

    /**
     * Returns the width of the messagebox
     * @return width of the messagebox
     */
    public String getWidth() {
        return width;
    }

    /**
     * Sets the width of the messgebox
     * @param width witdh of the messagebox
     */
    public void setWidth(String width) {
        this.width = width;
    }

    /**
     * Gets the height of the messagebox
     * @return height of the messagebox
     */
    public String getHeight() {
        return height;
    }

    /**
     * Sets the height of the messageboox
     * @param height height of the messagebox
     */
    public void setHeight(String height) {
        this.height = height;
    }

    /**
     * Returns the type of the message.
     * @see TextBean
     * @return type of the message
     */
    public String getMessageType() {
        return messageType;
    }

    /**
     * Sets the type of the message
     * @see TextBean
     * @param messageType
     */
    public void setMessageType(String messageType) {
        this.messageType = messageType;
    }

    /**
     * Appends text to the messagebox.
     * @param text text to append
     */
    public void appendText(String text) {
        this.text.add(text);
    }

    /**
     * Prepends text to the current text.
     * @param text text to prepend
     */
    public void prependText(String text) {
        this.text.add(0, text);
    }

    /**
     * Appends a localisation key to the current list of keys.
     * @param key key to append
     */
    public void appendKey(String key) {
        this.keys.add(key);
    }

    /**
     * Prepends a localisation key to the current list of keys.
     * @param key key to prepend
     */
    public void prependKey(String key) {
        this.keys.add(0, key);
    }

    /**
     * Removes a key from the list.
     * @param key key to remove
     */
    public void removeKey(String key) {
        this.keys.remove(key);
    }

    /**
     * Removes the text from the list.
     * @param text text to be removed
     */
    public void removeText(String text) {
        this.text.remove(text);
    }

    /**
     * Clears the message. This includes the text and the keys for localization.
     */
    public void clearMessage() {
       this.text = new ArrayList();
       this.keys = new ArrayList();
    }


    /**
     * Returns the message text. If localized keys were specified the translated (and only those) are returned.
     * @return
     */
    private String getMessage() {
        String result = null;

        List list = text;
        if (keys.size()!=0) {
            list = keys;
        }
        if (list.size()!=0) {
            StringBuffer sb = new StringBuffer();
            for (int i=0;i<list.size();i++) {

                String message = (String)list.get(i);
                if (keys.size()!=0) {
                    message = getLocalizedText(message);
                }
                sb.append("<div class=\""+messageType+"\">"+message+"</div>");
            }
            result = sb.toString();
        }
        return result;

    }

    public String toStartString() {
        return "";
    }

    public String toEndString() {
        this.cssClass="ui-messagebox";
        StringBuffer sb = new StringBuffer();
        String message = getMessage();
        if (height!=null) this.addCssStyle("height=\""+height+"\"");
        if (width!=null) this.addCssStyle("width=\""+width+"\"");
        // only return something if we have a message
        if (message!=null) {
            sb.append("<div"+getFormattedCss()+">");
            sb.append(message);
            sb.append("</div>");
        }

        return sb.toString();
    }


}

@@ -1033,32 +1033,506 @@
 n width;%0A    %7D%0A%0A
+    /**%0A     * Sets the value of the message to be displayed. All other messages or deleted.%0A     *%0A     * @param text text to be displayed%0A     */%0A    public void setValue(String text) %7B%0A        this.text = new ArrayList();%0A        this.keys = new ArrayList();%0A        this.text.add(text);%0A    %7D%0A%0A    /**%0A     * Returns the text of the messagebox.%0A     *%0A     * @return text of the messagebox%0A     */%0A    public String getValue() %7B%0A        return getMessage(false);%0A    %7D%0A%0A
     /**%0A     * S
@@ -3808,24 +3808,97 @@
 .%0A     *
- @return
+%0A     * @param format format as html output%0A     * @return text of the messagebox
 %0A     */
@@ -3920,32 +3920,46 @@
 ring getMessage(
+boolean format
 ) %7B%0A        Stri
@@ -4369,32 +4369,44 @@
 %0A               
+ if (format)
  sb.append(%22%3Cdiv
@@ -4444,32 +4444,57 @@
 ssage+%22%3C/div%3E%22);
+ else sb.append(message);
 %0A            %7D%0A 
@@ -4786,16 +4786,20 @@
 Message(
+true
 );%0A     

add set/getValue