
package org.jamesgt.rtasample.client;

import com.google.gwt.user.client.ui.RichTextArea;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Label;

/**
 *
 * @author James
 */
public class HelloWorldPanel extends Panel {

    protected RichTextArea textArea;
    protected Label        textLabel;

    public HelloWorldPanel() {
        this.textArea = new RichTextArea();
        this.textLabel = new Label();

        this.add( textArea );
        this.add( new Button( "Set example content", new ButtonListenerAdapter() {

            /** @see com.gwtext.client.widgets.event.ButtonListenerAdapter#onClick(com.gwtext.client.widgets.Button, com.gwtext.client.core.EventObject) */
            @Override
            public void onClick( Button button, EventObject e ) {
                setExapmleContent();
                textLabel.setText( textArea.getText() );
            }
        } ) );
        this.add( textLabel );
    }

    protected void setExapmleContent() {
        textArea.setHTML( bold( "Hello" ) + " " + monospace( "World" + "!" ) );
    }

    private String span( String style, String content ) {
        return "<span style=\"" + style + "\">" + content + "</span>";
    }

    private String bold( String s ) {
        return span( "font-weight: bold;", s );
    }

    private String monospace( String s ) {
        return span( "font-family: monospace;", s );
    }

    /** @see com.gwtext.client.widgets.Component#onDestroy() */
    @Override
    protected void onDestroy() {
        removeAll();
        super.onDestroy();
    }

}
