package e4s.tutorial; import java.util.*; import e4s.html.*; import e4s.html.input.extended.*; import e4s.db.*; import e4s.servlet.*; import e4s.translate.E4Label_Intf; import e4s.translate.E4LabelApp; import e4s.translate.E4LabelNone; // import tabledef.T_TUTORIAL_ARTICLES_Mod; // import tabledef.T_TUTORIAL_ARTICLES_Sel; // import tabledef.tutorialArticleNumber; /** * Layout input fields on multiple forms, related together by a cascading menu. This is useful when * larger input forms are required that would result in loss of oversight. You can divide the whole form * into different pages, but treat them as one form without having the neccessarity of taking care of changes * in the subforms. Each subform is represented by a menu bar on the top of the frame/page. * * {@tutorial Example_TabbedInputForm} */ public class Example_TabbedInputForm extends E4ModuleImplementation { public static E4Method tabbedInputForm = null; public static E4Method selectEditingTab = null; public static E4Method saveEditingTab = null; public static E4Method reloadEditingTab = null; public static E4Method editExistingRecord = null; public static E4MethodSilent whitePage = null; private class ValidatorArticleNumber implements AEP_ValidationObject_Intf { public E4Label_Intf validate( E4InputFieldName_Intf fieldname, E4Label_Intf label, String value ) { System.out.println("validate( " + fieldname + "," + label + "," + value + ")"); int maxlen = tutorialArticleNumber.LEN; int minlen = 3; E4Label_Intf res = null; try { if ((value == null) || value.trim().equals("")) res = new E4LabelApp("You must enter a character value for # between # and # characters",label.getDest(getLanguage()),Integer.toString(minlen),Integer.toString(maxlen)); else if (value.length() < minlen) res = new E4LabelApp("An article number must have at least # characters",Integer.toString(minlen)); else if (value.length() > maxlen) res = new E4LabelApp("An article number must have maximum up to # characters",Integer.toString(maxlen)); else if (value.indexOf(' ') > 0) res = new E4LabelApp("An article number may not contain blanks"); else if (isArticleExisting(new tutorialArticleNumber(value))) res = new E4LabelApp("Article # allready exists in the database",value); } catch( E4DBException dbe ) { res = new E4LabelNone(dbe.toString()); } return res; } public void setFieldset(char mode, E4Fieldset fieldset) { } public boolean isMandatory() { return true; } } /* * We recommend, that you use identifiers for the input field parameters by name. * This makes your HTML forms more clear and readable. But as long as you deal * only with the values you also can use the ANY() capability which creates an * unique but anonymous parameter. The name will change on each call, therefore * subsequent attribute assignments at runtime will not work. */ private final static E4InputFieldName ARTNO = new E4InputFieldName(T_TUTORIAL_ARTICLES_Sel.COL_ARTNO); private final static E4InputFieldName COLOR = new E4InputFieldName("COLOR"); private final static E4InputFieldName PRICE = E4InputFieldName.ANY(); private final static E4InputFieldName ONSTOCK = E4InputFieldName.ANY(); private final static E4InputFieldName TRANS_SOURCE = E4InputFieldName.ANY(); private final static E4InputFieldName CREATED = E4InputFieldName.ANY(); private final static E4InputFieldName WAREHOUSE = E4InputFieldName.ANY(); private final static E4InputFieldName NEXT_DELIVERY = E4InputFieldName.ANY(); private final static E4InputFieldName TRANS = E4InputFieldName.ANY(); private final static E4InputFieldName DESIGNATION = E4InputFieldName.ANY(); private final static E4InputFieldName CANORDER = E4InputFieldName.ANY(); private final static int ID_MAIN_DATA = 1; private E4FieldsetTabbed buildInputForm() throws Exception { // ------------------------------------------------------------------------------ // construct the "Tabbed" input fieldset E4FieldsetTabbed res = new E4FieldsetTabbed( E4ID() ); // ------------------------------------------------------------------------------ // construct the first tab - input fields for the main data E4Fieldset mainFields = res.FIELDSET("Main",ID_MAIN_DATA); TEXTFIELD fPartNum = mainFields.TEXTFIELD(ARTNO,"Article Number",tutorialArticleNumber.LEN); fPartNum.setToUppercase(); TEXTFIELD fDesignation = mainFields.TEXTFIELD(DESIGNATION,"Designation",40); FLOATFIELD fPrice = mainFields.FLOATFIELD(PRICE,"Price"); fPartNum.setValidator(new ValidatorArticleNumber()); fDesignation.setValidator(new E4ValidatorNoBlanks()); // add options for your color selection Vector colors = new Vector(); colors.addElement(SELECT.newOption("Red")); colors.addElement(SELECT.newOption("Green","Yellowstone-Green")); colors.addElement(SELECT.newOption("Blue","Sky-Blue")); colors.addElement(SELECT.newOption("Purple")); SELECT fColor = mainFields.SELECT(COLOR,"Color",colors); // ------------------------------------------------------------------------------ // construct the second tab - input fields for the warehouse data E4Fieldset warehouseFields = res.FIELDSET("Warehouse",ID_MAIN_DATA + 1); LONGFIELD fOnStock = warehouseFields.LONGFIELD(ONSTOCK,"Quantity on stock",6); fOnStock.setValidation(0,10000); // add options for your color selection Vector warehouses = new Vector(); warehouses.addElement(SELECT.newOption("A","Chicago")); warehouses.addElement(SELECT.newOption("B","San Francisco")); SELECT fWarehouse = mainFields.SELECT(WAREHOUSE,"Warehouse",warehouses); CHECKBOX fCanOrder = warehouseFields.CHECKBOX(CANORDER,"Can-Order"); DATEFIELD fCreated = warehouseFields.DATEFIELD(CREATED,"Article on stock since"); fCreated.setReadOnly(true); DATEFIELD fNEXT_DELIVERY = warehouseFields.DATEFIELD(NEXT_DELIVERY,"Next delivery scheduled"); // ------------------------------------------------------------------------------ // construct the third tab - input fields for translations E4Fieldset translationFields = res.FIELDSET("Translations",ID_MAIN_DATA + 2); // Add a non-editing TEXTAREA element to our form TEXTAREA tAreaSource = translationFields.TEXTAREA(TRANS_SOURCE,"Translate this",60,3); tAreaSource.setReadOnly(true); // add a paragraph to give some look translationFields.P(); // now, create an input field (in this case it is an text area with 60 columns, // 3 rows outside the form. Then create a input field of type MultiValue referenced // to the form, and give this new created TEXTAREA as parameter to the MultiValue // field. TEXTAREA tArea = new TEXTAREA( E4ID(), TRANS,"Please enter a translation in several languages",60,3); E4InputMultiValue mVal = translationFields.E4InputMultiValue(tArea); // add some multiple fields now. Internally, those fields later will be named // TRANS.DE, TRANS.FR and TRANS.IT. But this is not important at all. mVal.addExtension("DE","German"); mVal.addExtension("FR","French"); mVal.addExtension("IT","Italian"); mVal.addExtension("ES","Spanish"); // ------------------------------------------------------------------------------ // construct the fourth tab - just a link (callback function) res.addElement(whitePage,"Info",ID_MAIN_DATA + 3); // ------------------------------------------------------------------------------ // this function is called, whenever another tab is selected res.setMethodSelectTab( selectEditingTab ); // ------------------------------------------------------------------------------ // this function is called on the Save button // res.setMethodSave( saveEditingTab ); // ------------------------------------------------------------------------------ // this function is called on the Reload button // res.setMethodReload( reloadEditingTab ); return res; } public void tabbedInputForm( HTML html ) throws Exception { E4FieldsetTabbed inputForm = buildInputForm(); // activate the main form.. inputForm.setActive(ID_MAIN_DATA); inputForm.setValue(CREATED,new java.util.Date()); inputForm.setValue(ONSTOCK,1); inputForm.setValue(WAREHOUSE,"A"); html.addElement(inputForm); } public void editExistingRecord( HTML html, E4CgiParams params ) throws Exception { E4FieldsetTabbed inputForm = buildInputForm(); // activate the main form.. inputForm.setActive(ID_MAIN_DATA); tutorialArticleNumber artno = new tutorialArticleNumber(params.get(T_TUTORIAL_ARTICLES_Sel.COL_ARTNO)); if (isok(artno)) { T_TUTORIAL_ARTICLES_Sel T_TUTORIAL_ARTICLES = new T_TUTORIAL_ARTICLES_Sel(); T_TUTORIAL_ARTICLES.where_ARTNO(artno); T_TUTORIAL_ARTICLES.executeQuery(); if (T_TUTORIAL_ARTICLES.next()) { inputForm.setValue(ARTNO,T_TUTORIAL_ARTICLES.get_ARTNO()); inputForm.setValue(CREATED,T_TUTORIAL_ARTICLES.get_CREATED()); inputForm.setValue(CANORDER,T_TUTORIAL_ARTICLES.get_CANORDER()); inputForm.setValue(COLOR,T_TUTORIAL_ARTICLES.get_COLOR()); inputForm.setValue(DESIGNATION,T_TUTORIAL_ARTICLES.get_DESIGNATION()); inputForm.setValue(NEXT_DELIVERY,T_TUTORIAL_ARTICLES.get_NEXT_DELIVERY()); inputForm.setValue(PRICE,T_TUTORIAL_ARTICLES.get_PRICE()); inputForm.setValue(WAREHOUSE,T_TUTORIAL_ARTICLES.get_WAREHOUSE()); inputForm.setValue(ONSTOCK,T_TUTORIAL_ARTICLES.get_ONSTOCK()); inputForm.setReadOnly(ARTNO,true); } T_TUTORIAL_ARTICLES.close(); html.addElement(inputForm); } else { throw new Exception("Article with number " + artno + " not found"); } } public void whitePage( HTML html, E4CgiParams p ) { String partnum = p.get(ARTNO); html.print("Feel free, to do your own stuff here"); html.P(); html.print("The actual editing partnumber is: " + partnum); html.P(); p.toTable(html.TABLE(),"All the input parameters:"); } public void selectEditingTab( HTML html, E4CgiParams params ) throws Exception { E4FieldsetTabbed inputfields = buildInputForm(); inputfields.setValue(params); // maybe this is a little bit tricky: // we need to asign the value of DESIGNATION to TRANS_SOURCE because // the value shall be displayed in two tabs String designation = inputfields.getValue(DESIGNATION); inputfields.setValue(TRANS_SOURCE,designation); // This is the ID of the selected tab int id = params.getInt(E4FieldsetTabbed._PARAM_SELECTED_TAB_ID); if (id > 0) inputfields.setActive(id); // Validate the input fields inputfields.validate(getServlet()); html.addElement(inputfields); } /** * Called, when the user selects the 'Reload' button */ public void reloadEditingTab( HTML html, E4CgiParams params ) throws Exception { // Reloading the input tab is in principle the same as selecting another tab, // instead the same tab get's selected. So we can use the selectEditingTab // function. selectEditingTab( html, params ); } /** * Called, when the user selects the 'Save' button */ public void saveEditingTab( HTML html, E4CgiParams params ) throws Exception { E4FieldsetTabbed inputfields = buildInputForm(); inputfields.setValue(params); // Maybe this is a little bit tricky: // we need to asign the value of DESIGNATION to TRANS_SOURCE because // the value shall be displayed in two different tabs, so we "copy" the // value. Don't worry about that now. String designation = inputfields.getValue(DESIGNATION); inputfields.setValue(TRANS_SOURCE,designation); // Select the active tab inputfields.setActive(params); // Validate the input fields if (inputfields.validate(getServlet())) { tutorialArticleNumber artno = new tutorialArticleNumber(inputfields.getValue(ARTNO)); String color = inputfields.getValue(COLOR); String warehouse = inputfields.getValue(WAREHOUSE); // String designation = inputfields.getValue(DESIGNATION); CHECKBOX cb_canorder = (CHECKBOX)inputfields.getField(CANORDER); boolean canorder = cb_canorder.getValue(); LONGFIELD lf_onstock = (LONGFIELD)inputfields.getField(ONSTOCK); long onstock = lf_onstock.getValue(); FLOATFIELD ff_price = (FLOATFIELD)inputfields.getField(PRICE); float price = ff_price.getValue(); DATEFIELD df_created = (DATEFIELD)inputfields.getField(CREATED); java.util.Date created = df_created.getValue(); DATEFIELD df_next_deliverY = (DATEFIELD)inputfields.getField(NEXT_DELIVERY); java.util.Date next_deliverY = df_next_deliverY.getValue(); // get a record-set for inserting or updating the record T_TUTORIAL_ARTICLES_Mod T_TUTORIAL_ARTICLES = new T_TUTORIAL_ARTICLES_Mod(); // assign field values T_TUTORIAL_ARTICLES.set_DESIGNATION(designation); T_TUTORIAL_ARTICLES.set_COLOR(color); T_TUTORIAL_ARTICLES.set_CREATED(created); T_TUTORIAL_ARTICLES.set_WAREHOUSE(warehouse); T_TUTORIAL_ARTICLES.set_ONSTOCK(onstock); T_TUTORIAL_ARTICLES.set_NEXT_DELIVERY(next_deliverY); T_TUTORIAL_ARTICLES.set_PRICE(price); T_TUTORIAL_ARTICLES.set_CANORDER(canorder); if (isArticleExisting(artno)) { // Article is existing, so we need to define a WHERE condition // for our SQL statement using the index field ARTNO. Then we // can execute an update. T_TUTORIAL_ARTICLES.where_ARTNO(artno); T_TUTORIAL_ARTICLES.executeUpdate(); } else { // Article is not existing, assign a value to primary index field // and execute an insert into the database T_TUTORIAL_ARTICLES.set_ARTNO(artno); T_TUTORIAL_ARTICLES.executeInsert(); } html.A(tabbedInputForm).println("[do it again]"); E4InputMultiValue trans = (E4InputMultiValue)inputfields.getField(TRANS); html.println("German translation: " + trans.getValue("DE")); html.BR(); html.println("French translation: " + trans.getValue("FR")); html.BR(); html.println("Italian translation: " + trans.getValue("IT")); html.BR(); html.println("Spanish translation: " + trans.getValue("ES")); html.BR(); } else { html.Message(E4Message.ERROR,"Please correct the errors first, save not possible now"); html.addElement(inputfields); } } private boolean isArticleExisting( tutorialArticleNumber artno ) throws E4DBException { boolean res = false; T_TUTORIAL_ARTICLES_Sel T_TUTORIAL_ARTICLES = new T_TUTORIAL_ARTICLES_Sel(); T_TUTORIAL_ARTICLES.where_ARTNO(artno); T_TUTORIAL_ARTICLES.executeQuery(); if (T_TUTORIAL_ARTICLES.next()) res = true; T_TUTORIAL_ARTICLES.close(); return res; } }