package org.jboss.as.console.client.tools;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import org.jboss.as.console.client.shared.help.StaticHelpPanel;
import org.jboss.ballroom.client.widgets.tools.ToolButton;
import org.jboss.ballroom.client.widgets.tools.ToolStrip;
import org.jboss.dmr.client.ModelNode;
import org.jboss.dmr.client.Property;

/**
 * @author Heiko Braun
 * @date 6/15/12
 */
public class RawView {


    private HTML dump;
    private BrowserPresenter presenter;
    private ModelNode currentAddress;

    Widget asWidget() {
        VerticalPanel layout = new VerticalPanel();
        layout.setStyleName("fill-layout-width");
        layout.getElement().setAttribute("style", "padding:10px");

        dump = new HTML("");

        ToolStrip tools = new ToolStrip();
        tools.addToolButtonRight(new ToolButton("Refresh", new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                if(currentAddress!=null)
                {
                    dump.setHTML("");
                    Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
                        @Override
                        public void execute() {
                            presenter.readResource(currentAddress);
                        }
                    });
                }
            }
        }));

        layout.add(tools.asWidget());

        SafeHtmlBuilder helpText = new SafeHtmlBuilder();
        helpText.appendHtmlConstant("<ul>");
        helpText.appendHtmlConstant("<li>");
        helpText.appendEscaped("This is a raw dump of the current configuration values on this node. It only resolves values on the current level, hence some attributes (or children) may show up as UNDEFINED.");
        helpText.appendHtmlConstant("</ul>");
        StaticHelpPanel help = new StaticHelpPanel(helpText.toSafeHtml());
        layout.add(help.asWidget());

        layout.add(dump);

        return layout;
    }

    public void display(ModelNode address, Property model)
    {
        currentAddress = address;
        SafeHtmlBuilder parsed = parse(model.getValue().toString());
        dump.setHTML(parsed.toSafeHtml());
    }

    private static SafeHtmlBuilder parse(String s) {

        SafeHtmlBuilder html = new SafeHtmlBuilder();

        html.appendHtmlConstant("<pre class='model-dump'>");

        String[] lines = s.split("\n");
        for(String line : lines)
        {
            html.appendHtmlConstant("<span class='browser-dump-line'>");
            html.appendEscaped(line).appendHtmlConstant("<br/>");
            html.appendHtmlConstant("</span>");
        }

        html.appendHtmlConstant("</pre>");
        return html;
    }

    public void clearDisplay()
    {
        currentAddress = null;
        dump.setHTML("");
    }

    public void setPresenter(BrowserPresenter presenter) {
        this.presenter = presenter;
    }
}

@@ -267,27 +267,37 @@
 gwt.
-user.client.ui.HTML
+safehtml.shared.SafeHtmlUtils
 ;%0Aim
@@ -451,24 +451,73 @@
 cHelpPanel;%0A
+import org.jboss.as.console.client.widgets.Code;%0A
 import org.j
@@ -784,20 +784,20 @@
 private 
-HTML
+Code
  dump;%0A 
@@ -1087,23 +1087,21 @@
 p = new 
-HTML(%22%22
+Code(
 );%0A%0A    
@@ -1378,34 +1378,30 @@
        dump.
-setHTML(%22%22
+clear(
 );%0A         
@@ -2451,708 +2451,299 @@
     
-SafeHtmlBuilder parsed = parse(model.getValue().toString());%0A        dump.setHTML(parsed.toSafeHtml());%0A    %7D%0A%0A    private static SafeHtmlBuilder parse(String s) %7B%0A%0A        SafeHtmlBuilder html = new SafeHtmlBuilder();%0A%0A        html.appendHtmlConstant(%22%3Cpre class='model-dump'%3E%22);%0A%0A        String%5B%5D lines = s.split(%22%5Cn%22
+dump.setVisible(true);%0A        dump.setValue(SafeHtmlUtils.fromString(model.getValue().toString())
 );%0A    
+%7D%0A%0A
     
-for(String line : lines)%0A        %7B%0A            html.appendHtmlConstant(%22%3Cspan class='browser-dump-line'%3E%22);%0A            html.appendEscaped(line).appendHtmlConstant(%22%3Cbr/%3E%22);%0A            html.appendHtmlConstant(%22%3C/span%3E%22);%0A        %7D%0A%0A        html.appendHtmlConstant(%22%3C/pre%3E%22);%0A        return html;%0A    %7D%0A%0A    public void clearDisplay()%0A    %7B%0A        currentAddress = null
+public void clearDisplay()%0A    %7B%0A        currentAddress = null;%0A        // to prevent an empty code widget which would render as an empty border box%0A        dump.setVisible(false)
 ;%0A  
@@ -2757,18 +2757,14 @@
 ump.
-setHTML(%22%22
+clear(
 );%0A 

Use code widget in management model browser