package com.creamtec.ajaxswing.examples;
import java.awt.Component;
import java.awt.Window;
import com.creamtec.ajaxswing.core.ActionData;
import com.creamtec.ajaxswing.core.HttpRequestData;
import com.creamtec.ajaxswing.gui.ActionEmulator;
import com.creamtec.ajaxswing.gui.ComponentUpdater;
import com.creamtec.ajaxswing.gui.WindowUpdater;
import com.creamtec.ajaxswing.rendering.ComponentRenderer;
import com.creamtec.ajaxswing.rendering.Document;
import com.creamtec.ajaxswing.rendering.html.HTMLPage;
import com.creamtec.ajaxswing.ui.CustomControl;
import com.creamtec.ajaxswing.ui.FixedDialog;
import com.creamtec.core.TraceMgr;
/**
* Example of implementing a custom renderer, custom updater and action emulator
* See AjaxSwing 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 is rendered as a div for proper children positioning and AJAX updates
page.append("
");
// Delegate rendering of checkbox to AjaxSwing engine for simplicity
page.getPageRenderer().renderComponent(page, control.getCheckBox());
// Render text ourselves because we want to support 3D effect
if (control.is3D()) {
// Only draw the shadow if we need 3D look
page.append("");
page.append(control.getText());
page.appendln("");
}
// Always display text using main color
page.append("");
page.append(control.getText());
page.appendln("");
// Use JavaScript for things we can't do via HTML
StringBuffer script = new StringBuffer();
script.append("document.title = 'Custom Control is 3D ");
script.append(control.is3D()? "enabled": "disabled");
script.append("';\n");
page.appendScript(script.toString());
page.appendln("
");
}
/**
* Called by AjaxSwing 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 AjaxSwing 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 AjaxSwing to emulate a standard or custom action
*/
public boolean emulateAction(Document document, ActionData actionData) throws Exception {
HTMLPage page = (HTMLPage) document;
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) {
}
public void initializaDocument(Document document) {
}
}