package creamtec.webcream.examples;

import java.awt.*;

import creamtec.core.TraceMgr;
import creamtec.webcream.rendering.*;
import creamtec.webcream.rendering.html.*;
import creamtec.webcream.core.HttpRequestData;
import creamtec.webcream.core.ActionData;
import creamtec.webcream.ui.*;
import creamtec.webcream.gui.*;

/**
 * Example of implementing a custom renderer, custom updater and action emulator
 * See WebCream documentation and javadoc for more information.
 */
public class CustomControlProcessor
    implements ComponentRenderer, ComponentUpdater, ActionEmulator
{

    public CustomControlProcessor()
    {
        TraceMgr.trace(this, "Loaded");
    }


    public void renderComponent (Document document, Object component) throws Exception
    {
        TraceMgr.trace(this, "Rendering custom control");
        CustomControl control = (CustomControl)component;
        HTMLPage page = (HTMLPage)document;

        // Because custom control contains other elements, it will be rendered as a div for proper children positioning
        page.append("<div");
        page.getPageRenderer().appendComponentStyle(page, control, false);
        page.appendln(">");

        // Name the checkbox to be the same as the control so that the events generated
        // by the checkbox appear as if they come from the control
//        control.getCheckBox().setName(page.getComponentName(control));
//        boolean oldUseComponentNames = page.getRenderContext().useComponentNames;
        // Make sure that the names of components are used to identify them in HTML
//        page.getRenderContext().useComponentNames = true;
        // Delegate rendering of checkbox to WebCream engine for simplicity
        page.getPageRenderer().renderComponent(page, control.getCheckBox());
//        page.getRenderContext().useComponentNames = oldUseComponentNames;

        // Render text ourselves because we want to support 3D effect
        if (control.is3D()) {
            // Only draw the shadow if we need 3D look
            page.append("<span");
            page.append(" style=\"position:absolute;left:");
            page.append(control.getTextOffset() + 1);
            page.append(";top:4;color:white\">");
            page.append(control.getText());
            page.appendln("</span>");
        }

        // Always display text using main color
        page.append("<span");
        page.append(" style=\"position:absolute;left:");
        page.append(control.getTextOffset());
        page.append(";top:3;color:black\">");
        page.append(control.getText());
        page.appendln("</span>");

        page.appendln("</div>");
    }


    /**
     * Called by WebCream to apply the data from HTML page to the GUI component
     */
    public boolean updateComponent (Component comp, HttpRequestData data, ActionData actionData) throws Exception
    {
        CustomControl control = (CustomControl)comp;
        String checkBoxName = WindowUpdater.getComponentName(control.getCheckBox());
        Object value = data.getParams().get(checkBoxName);
        boolean isSelected = value != null;
        TraceMgr.trace(this, "updateComponent for custom control, new state = " + (isSelected? "selected":"not selected"));

        // Only change the checkbox state when needed
        if (control.getCheckBox().isSelected() != isSelected) {
            control.getCheckBox().doClick(0);
        }

        // If the page submit was triggered by an event from our checkbox, mark
        // the action as processed because otherwise WebCream would try to find
        // the event component, update it with data and then process the action.
        // Since we did the rendering of the checkbox ourselves, we have to take care of
        // its events and actions
        if (actionData.getActionString().equals("/event/" + checkBoxName))
            actionData.setProcessed(true);

        return false;
    }


    /**
     * Called by WebCream to emulate a standard or custom action
     */
    public boolean emulateAction(HTMLPage page, ActionData actionData) throws Exception {
        TraceMgr.trace(this, "emulateAction called for page " + page.getTitle());
        if (actionData.getActionString().equals("close")) {
            TraceMgr.trace("Intercepted Close window attempt");
            Window window = page.getClientAgent().getUIManager().getWindowInFocus();
            if (window instanceof FixedDialog) {
                FixedDialog dlg = (FixedDialog)window;
                dlg.setTitle("Intercepted Close action");

                // Mark action as processed and stop emulation
                actionData.setProcessed(true);
                return true;
            }
        }

        return false;       // Keep going
    }


    public void postUpdateComponent(Component comp, HttpRequestData data, ActionData actionData) {
    }

}
