/**
* Copyright (c) 2001, Keith Wong
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package mypackage;
import objectivehtml.htmlwidget.*;
import objectivehtml.htmlwidget.exception.*;
/**
* CustomerForm
* This is an example of how to use the objective html toolkit. Please read all the comments
* in the code as they give handy tips in how to be use this toolkit.
* The example was written by Keith Wong, email: keithwong@optushome.com.au
*/
public class CustomerForm extends HtmlForm
{
/**
* These members variables are made public. We leave it up to each developer to decide which
* access level they wish to have these at. The reason I have made them all public is that
* this provides much greater flexibility, which is generally what you need in building
* web pages. By making them public, you give yourself the opportunity to change the
* attributes of any of the HtmlWidgets from the jsp page (i.e. jsp pages are easier to redeploy).
*/
public HtmlParagraph m_parUserMessage;
public HtmlTable m_tblLayout;
public HtmlText m_htmUserMessage;
public HtmlText m_htmMale;
public HtmlText m_htmFemale;
public HtmlTextBox m_txtFirstName;
public HtmlTextBox m_txtLastName;
public HtmlTextBox m_txtBirthDate;
public HtmlRadioButton m_radMale;
public HtmlRadioButton m_radFemale;
public HtmlListBox m_selTitle;
public HtmlPushButton m_btnSave;
public HtmlPushButton m_btnReset;
public HtmlTextArea m_txaStreet;
public HtmlTextBox m_txtSuburb;
public HtmlTextBox m_txtPostCode;
public HtmlListBox m_selState;
public HtmlTextBox m_txtTelephone;
public HtmlTextBox m_txtEmail;
public HtmlCheckBox m_cbxPostalAddress;
/**
* Constructor
*/
public CustomerForm()
throws Exception
{
super();
setMethod("post");
// all html widgets have a parent except the HtmlForm
// in this case the parent of the html paragraph is the form itself
m_parUserMessage = new HtmlParagraph(this);
// we want to initially hide the paragraph
m_parUserMessage.setVisible(false);
// this html text widget is a child of the html paragraph widget
// n.b. the widget will inherit visibility properties of its parent
// i.e. this widget will also be invisible
m_htmUserMessage = new HtmlText(m_parUserMessage);
// this will create a new html table with the form itself as the parent
// the initial size of table is give in the constructor (rows, columns)
m_tblLayout = new HtmlTable(this, 13, 2);
m_tblLayout.setBorder("0");
m_tblLayout.setBgColor("#FCFADA");
m_tblLayout.setClassAttribute("normal");
// table cell 0,0
m_tblLayout.getTableCell(0, 0).setText("Title *");
// table cell 0,1
m_selTitle = new HtmlListBox(m_tblLayout.getTableCell(0, 1), "m_selTitle");
// add options to a select box by calling addOption, the first parameter
// is the value and the second parameter will be the text displayed
m_selTitle.addOption("", "- Choose Title -"); // default option
m_selTitle.addOption("01", "Mr");
m_selTitle.addOption("02", "Miss");
m_selTitle.addOption("03", "Ms");
m_selTitle.addOption("04", "Mrs");
m_selTitle.setSelected(0, true); // initial selection
// table cell 1,0
m_tblLayout.getTableCell(1, 0).setText("First Name *");
// table cell 1,1
// The second parameter passed to this HtmlControlWidget is the name this input
// field will have when the html is generated. This name can be used by the
// client scripting language.
m_txtFirstName = new HtmlTextBox(m_tblLayout.getTableCell(1, 1), "m_txtFirstName");
// you can set html attributes by calling the generic method setAttribute
// the name of the attribute is case-insensitive (all attribute values are
// set as strings regardless of whether html interprets them as other types)
m_txtFirstName.setAttribute("size", "20"); // n.b. the value "20" as a string
// table cell 2,0
m_tblLayout.getTableCell(2, 0).setText("Last Name *");
// table cell 2,1
m_txtLastName = new HtmlTextBox(m_tblLayout.getTableCell(2, 1), "m_txtLastName");
// The alternate way to set a html attribute is to call the explicit method for
// the attribute. This has the same result as setting the attribute by calling
// setAttribute. The only difference is programmically.
m_txtLastName.setSize("20");
// table cell 3,0
m_tblLayout.getTableCell(3, 0).setText("Birth Date");
// table cell 3,1
m_txtBirthDate = new HtmlTextBox(m_tblLayout.getTableCell(3, 1), "m_txtBirthDate");
// table cell 4,0
m_tblLayout.getTableCell(4, 0).setText("Gender");
// table cell 4,1
// lets setup a radio button group
m_htmMale = new HtmlText(m_tblLayout.getTableCell(4, 1));
m_htmMale.setText("M");
m_radMale = new HtmlRadioButton(m_tblLayout.getTableCell(4, 1), "m_radGender");
m_radMale.setValue("M");
// to set a boolean attribute call the setBooleanAttribute and specify whether
// the attribute is on or off
m_radMale.setBooleanAttribute("checked", true); // lets check male as the default!
m_htmFemale = new HtmlText(m_tblLayout.getTableCell(4, 1));
m_htmFemale.setText("F");
// The male and female radio buttons have the same name, this is important as
// this is how radio buttons are grouped together. Radio buttons are the only
// control widgets that may have the same name.
m_radFemale = new HtmlRadioButton(m_tblLayout.getTableCell(4, 1), "m_radGender");
m_radFemale.setValue("F");
// table cell 5,0
m_tblLayout.getTableCell(5, 0).setText("Street");
// table cell 5,1
m_txaStreet = new HtmlTextArea(m_tblLayout.getTableCell(5, 1), "m_txaStreet");
m_txaStreet.setCols("40");
m_txaStreet.setRows("3");
// table cell 6,0
m_tblLayout.getTableCell(6, 0).setText("Suburb");
// table cell 6,1
m_txtSuburb = new HtmlTextBox(m_tblLayout.getTableCell(6, 1), "m_txtSuburb");
// table cell 7,0
m_tblLayout.getTableCell(7, 0).setText("PostCode");
// table cell 7,1
m_txtPostCode = new HtmlTextBox(m_tblLayout.getTableCell(7, 1), "m_txtPostCode");
// table cell 8,0
m_tblLayout.getTableCell(8, 0).setText("State");
// table cell 8,1
// these are Australian states, thats where I'm from!
m_selState = new HtmlListBox(m_tblLayout.getTableCell(8, 1), "m_selState");
m_selState.addOption("", "- Choose State -"); // default option
m_selState.addOption("01", "NSW");
m_selState.addOption("02", "VIC");
m_selState.addOption("03", "QLD");
m_selState.addOption("04", "SA");
m_selState.addOption("05", "WA");
m_selState.addOption("06", "ACT");
m_selState.addOption("07", "NT");
m_selState.setSelected(0, true); // initial selection
// table cell 9,0
m_tblLayout.getTableCell(9, 0).setText("Same as postal address?");
// table cell 9,1
m_cbxPostalAddress = new HtmlCheckBox(m_tblLayout.getTableCell(9, 1), "m_cbxPostalAddress");
m_cbxPostalAddress.setValue("Y");
// table cell 10,0
m_tblLayout.getTableCell(10, 0).setText("Telephone");
// table cell 10,1
m_txtTelephone = new HtmlTextBox(m_tblLayout.getTableCell(10, 1), "m_txtTelephone");
// table cell 11,0
m_tblLayout.getTableCell(11, 0).setText("Email *");
// table cell 11,1
m_txtEmail = new HtmlTextBox(m_tblLayout.getTableCell(11, 1), "m_txtEmail");
m_txtEmail.setSize("50");
// you can access the table row widget via the table
// lets set the color of the last row
m_tblLayout.getTableRow(12).setBgColor("#D7FFFF");
// lets set the col span for this table cell
m_tblLayout.getTableCell(12, 0).setColSpan(2);
m_tblLayout.getTableCell(12, 0).setAlign("center");
// table cell 12,0
// lets add a submit button
m_btnSave = new HtmlPushButton(m_tblLayout.getTableCell(12, 0), "m_btnSave");
m_btnSave.setText("Save");
m_btnSave.setButtonType("submit");
// lets add a submit button
m_btnReset = new HtmlPushButton(m_tblLayout.getTableCell(12, 0), "m_btnReset");
m_btnReset.setText("Reset");
m_btnReset.setButtonType("reset");
} // end constructor
/**
* This method validates the form details first.
* @return true if the form data is ok, false otherwise
*/
private boolean validateForm()
{
boolean bValid = true;
// if title is blank
if (m_selTitle.isSelected(0) == true)
{
bValid = false;
// sets the class attribute of the table cell with "Title *" text
m_tblLayout.getTableCell(0, 0).setClassAttribute("error");
} // end if title is blank
// if first name is blank
if (m_txtFirstName.getText().trim().length() == 0)
{
bValid = false;
m_tblLayout.getTableCell(1, 0).setClassAttribute("error");
} // end if first name is blank
// if last name is blank
if (m_txtLastName.getText().trim().length() == 0)
{
bValid = false;
m_tblLayout.getTableCell(2, 0).setClassAttribute("error");
} // end if last name is blank
// if email is blank
if (m_txtEmail.getText().trim().length() == 0)
{
bValid = false;
m_tblLayout.getTableCell(11, 0).setClassAttribute("error");
} // end if email is blank
// if invalid then lets set a user message
if (bValid == false)
{
m_htmUserMessage.setText("Please fill in the mandatory fields highlighted below.");
m_parUserMessage.setVisible(true);
m_parUserMessage.setClassAttribute("error");
} // end if not valid
return bValid;
} // end validateForm
/**
* This method should be called when the save action is triggered.
*/
public void saveForm()
throws InvalidAttributeException
{
// lets clear out all class styles set for errors
// you can do this by calling the overloaded version of setAttribute()
// provided by all container widgets
// n.b giving the setAttribute a null value will clear out the attribute,
// so that the attribute is not shown at all when printHtml is called
m_tblLayout.setAttribute("class", null, true); // apply to all children
// lets set the attribute back to the normal
m_tblLayout.setAttribute("class", "normal"); // only applies to the table itself
if (validateForm())
{
// ...
// do some database stuff... save to database..
// this part left out... well because this is only an example after all :)
m_htmUserMessage.setText("Your customer details have been saved.");
m_parUserMessage.setVisible(true);
m_parUserMessage.setClassAttribute("info");
} // end if form is valid
} // end saveForm
} // end class