/** * */ package e4s.tutorial; import e4s.html.*; import e4s.html.ajax.E4AjaxData; import e4s.html.ajax.E4AjaxData_Intf; import e4s.html.input.extended.E4Fieldset; import e4s.html.input.extended.E4InputFieldName; import e4s.html.input.extended.LONGFIELD; import e4s.html.input.extended.SELECT; import e4s.servlet.E4ModuleImplementation; import e4s.servlet.E4ServletImplementation_Intf; import e4s.translate.E4Label_Intf; import e4s.translate.E4TranslationsVec; import e4s.util.E4StringBufferHtml; import e4s.util.E4ScriptsVec; /** * Ajax FORM example. * * {@tutorial Example_Ajax01} * * @see e4s.html.ajax.Ajax_Intf */ public class Example_Ajax_FORM extends E4ModuleImplementation { public static E4Method start = null; private final static E4InputFieldName PARAM_A = new E4InputFieldName("A"); private final static E4InputFieldName PARAM_B = new E4InputFieldName("B"); private final static E4InputFieldName PARAM_COLOR = new E4InputFieldName("COLOR"); private final static E4InputFieldName PARAM_SIZE = new E4InputFieldName("SIZE"); private static class Calculate extends E4AjaxData implements E4AjaxData_Intf { /** * Get an identification for this Ajax data object, for the case that more * instances of this object are used within the same session, then a unique * identifier is required. */ public String getDivId() { return getClass().getName(); } public void toHtml( E4StringBufferHtml buf, E4CgiParams params, E4ServletImplementation_Intf servlet, boolean initial ) throws Exception { int a = params.getInt(PARAM_A); int b = params.getInt(PARAM_B); FONT font = new FONT(FONT.ARIAL_smaller); E4Label_Intf errA = params.checkInt(PARAM_A); E4Label_Intf errB = params.checkInt(PARAM_B); if (errA != null) font.print(errA); else if (errB != null) font.print(errB); else if ((a < 0) || (b < 0)) font.print("You must enter two positive numbers"); else font.print(a + " plus " + b + " results in " + (a + b)); font.toHtml(buf,servlet); } } private static class PetFinder extends E4AjaxData implements E4AjaxData_Intf { /** * Get an identification for this Ajax data object, for the case that more * instances of this object are used within the same session, then a unique * identifier is required. */ public String getDivId() { return getClass().getName(); } public void toHtml( E4StringBufferHtml buf, E4CgiParams params, E4ServletImplementation_Intf servlet, boolean initial ) throws Exception { String color = params.get(PARAM_COLOR); String size = params.get(PARAM_SIZE); String animal = "unknown"; if (isok(color) && isok(size)) { if (color.equalsIgnoreCase("white") && size.equalsIgnoreCase("huge")) animal = "Icebear"; else if (color.equalsIgnoreCase("white") && size.equalsIgnoreCase("medium")) animal = "Cat"; else if (color.equalsIgnoreCase("white") && size.equalsIgnoreCase("small")) animal = "Dove"; else if (color.equalsIgnoreCase("brown") && size.equalsIgnoreCase("huge")) animal = "Deere"; else if (color.equalsIgnoreCase("brown") && size.equalsIgnoreCase("medium")) animal = "Dog"; else if (color.equalsIgnoreCase("brown") && size.equalsIgnoreCase("small")) animal = "Squirrel"; else if (color.equalsIgnoreCase("black") && size.equalsIgnoreCase("huge")) animal = "Bear"; else if (color.equalsIgnoreCase("black") && size.equalsIgnoreCase("medium")) animal = "Panther"; else if (color.equalsIgnoreCase("black") && size.equalsIgnoreCase("small")) animal = "Raven"; } FONT font = new FONT(FONT.ARIAL_smaller); font.print("If it's " + color + " and " + size + " it's a " + animal); font.toHtml(buf,servlet); } public void collectScripts( E4ScriptsVec vec, E4ServletImplementation_Intf servlet ) { } public void collectTranslationsForEditing(E4TranslationsVec vec, E4ServletImplementation_Intf servlet) { } } public void start( HTML html ) { BODY body = html.BODY(); // create a table, this will later contain the Ajax-HTML element TABLE table = body.TABLE(TABLE.E4S_DEFAULT_TABLE()); // this are label texts only TD tdLabel[] = table.TR().TD(2); TD tdForm = table.TR().TD(); tdLabel[0].print("Ajax Example"); tdLabel[0].setBgColor(E4Color.LIGHT_YELLOW); tdLabel[1].I().print("Please note, that only the content in the table cell left is updated dynamically."); tdLabel[1].setRowspan(2); FORM form = tdForm.FORM(); FORM.setFocusBgColorDefault(null); E4Fieldset fieldsetCalc = form.FIELDSET(); fieldsetCalc.setLegend("Calculate A + B"); LONGFIELD fA = fieldsetCalc.LONGFIELD(PARAM_A,"Value #1"); LONGFIELD fB = fieldsetCalc.LONGFIELD(PARAM_B,"Value #2"); fB.layoutToNextRow(false); E4Fieldset fieldsetPets = form.FIELDSET(); fieldsetPets.setLegend("Pets finder"); SELECT fCOLOR = fieldsetPets.SELECT(PARAM_COLOR,"Color",new String[]{"white","brown","black"}); fCOLOR.layoutToNextRow(false); SELECT fSIZE = fieldsetPets.SELECT(PARAM_SIZE,"Size",new String[]{"huge","medium","small"}); // this is the data object, it will be displayed asynchronly whenever // the client's timeout indicates a new display refresh Calculate calculator = new Calculate(); E4JavaScript jsTest = html.createScript(); jsTest.appendln("function " + jsTest.getName() + "(data)"); jsTest.appendln("{"); jsTest.appendln(" alert('Just to demonstrate that JavaScripts can be plugged in and show you the output:\\n' + data);"); jsTest.appendln(" // return false to stop or true to continue;"); jsTest.appendln(" return true;"); jsTest.appendln("}"); // this is the Ajax Element, compatible with the E4S framework and // cab be integrated into e.g. the FORM element. Basically, the ae element // is a