8 Using Validation in the ADF Swing User Interface

This chapter describes how to use client-side validation in the ADF Swing application.

This chapter contains the following sections:

8.1 About Validating Events

JDeveloper performs validation using:

  • Rules defined in the ADF Business Components data model project

  • Upon activity in the user interface

Through the ADF Swing application you perform validation on the ADF Swing form to prevent users from entering erroneous data. For example, you may want to prevent alpha characters from being input to a field which requires a telephone number. Similarly you may want to validate the data entered and inform the user.

8.2 How to Use Validation With ADF Control Bindings

Validation in the user interface is handled through source code at the level of the control binding. To do this, you define your validation code as a PlainDocument event and then use the setDocument() method to register the code with the specific control. Then the call to setDocument() on the control you bound to the ADF Business Components attribute item will work with the document which contains the validation code. Example 8-1 illustrate validation that occurs at each key press.

Example 8-1 Validation at Every KeyStroke Example

//Add this code to create a document with the validation code and set it for the control
 
mDeptno.setDocument(new javax.swing.text.PlainDocument()
{
public void insertString(int offs, String str, javax.swing.text.AttributeSet a) throws    javax.swing.text.BadLocationException 
   {
   StringBuffer buf = new StringBuffer(str);
   int size = buf.length();
   char c;
   for (int i = 0; i < size; i++)
      {
      c = buf.charAt(i);
      if (!Character.isDigit(c))
      {
   Toolkit.getDefaultToolkit().beep(); 
   buf.deleteCharAt(i);
}
}
 
super.insertString(offs, buf.toString(), a);
}
 
});  
 
/*Here is the code that is generated for you which will set the control binding for the document. This will work with the document you defined above. */ 
 
mDepartmentId.setDocument((Document)panelBinding.bindUIControl("DepartmentId", mDepartmentId));

8.3 How to Use Validation With ADF Swing Panels

The panelBinding object, which is responsible for managing the link between the UI and the ADF Business Components data model layer, can trigger an event on an action. This type of validation allows you to perform validation when the user inputs data into a field and then navigates to a different field. You may want to perform this type of validation at the panel which should notify the user, without breaking a business rule as defined in the business components.

For example, consider a salary which has a range of 100 up to 1000 defined in the business components. If the salary field is set to anything less that 500, you want to alert the user and highlight the field. In this case, you want to validate when the value is being set. The panelBinding object will trigger an event when beforeSetAttribute() is called.

To set validation on the ADF Swing panel:

  1. Open the panel in the Java visual editor.

  2. In the Structure window, expand Other and then select panelBinding.

  3. In the Properties window, expand the Events section and enter an event name in the beforeSetAttribute field.

  4. In the Java source editor, add the validation code to the generated event handler, similar to the sample shown in Example 8-2.

Example 8-2 Code Added to Generated Event Handler

if (e.getAttributeName().equals("Salary"))
   {
   Object val = e.getNewValue();
   if (val != null)
      {
      Integer n = new Integer(val.toString());
      if (n.intValue() < 500)
         {
         mSalary.setBackground(Color.red);
         throw new oracle.jbo.JboException("That's a bit low!");
         }
      }
   }