|
|
|
|
|
|
|
|
Using a reusable code approach to HTML select option lists: part II (continued)
This time we'll add two constructors so that we can add a little variety to its capabilities. The primary constructor takes as an argument a previously loaded Java Properties object for the source of our LabelValueBean ArrayList. The secondary constructor takes a String as an argument, with the assumption that the String will contain a valid path and file name to a text file that contains name/value pairs that can be used as the underlying data source for a new Java Properties object. The secondary constructor simply loads a new Properties object from the file and then invokes the primary constructor with the loaded Properties object.
Both the SimpleOptionListSource and PropertiesOptionListSource share all of the common code in the abstract base class, as will any other variation you might elect to produce using our little option list source framework.
Incremental improvement #3: enhancing the shared base As we add more and more implementing classes, the value of our base components increases, as does the potential value of improvements to the base classes. Our original, simplified concept assumed that our list of options would be used for input fields where the selection of one of the options was mandatory. There was no provision for selecting "no entry" or leaving the field blank. While this might be acceptable for many instances, it certainly isn't applicable to all instances, since there are a number of potential uses for a drop-down list where you would not want to be forced to select something from the list. Lust think of a search criteria entry screen--you won't always be searching on gender, although you might at some point.
To resolve this deficiency, we can add a new method to our interface that takes as an argument a boolean indicator that defines whether or not this field is required. When this indicator is set to true, our framework will behave in its current, default manner. However, when set to false, an additional, "no entry" option will be added to the head of the list of options to support this new requirement. To maintain backward compatibility with all of our potential existing users, we'll retain the original getOptions() method with no argument, but rather than retain or duplicate the code in that method, we will just have it invoke the new method, defaulting the indicator to "true".
Listing 3 contains our revised interface.
package step.two;
import java.util.ArrayList;
/**
* This interface specifies the required methods for an "Option
* List" source, which returns the name/value pairs for the options
* related to an HTML "select" tag.
*/
public interface OptionListSource {
/**
* Returns an <code>ArrayList</code> of <code>LabelValueBean</code>
* objects defining the available options.
*
* This method should return the same results as using the method
* <code>getOptions(true)</code>.
*
* @return an <code>ArrayList</code> of <code>LabelValueBean</code>
* objects defining the available options
*/
public ArrayList getOptions();
/**
* Returns an <code>ArrayList</code> of <code>LabelValueBean</code>
* objects defining the available options.
*
* @param required a boolean indicating whether or not input is
* required for this field. Setting this indicator to false will
* cause the generation of an additional option for no selection.
* @return an <code>ArrayList</code> of <code>LabelValueBean</code>
* objects defining the available options
*/
public ArrayList getOptions(boolean required);
}
|
|
|
|
|
|
|
|
|
|
|
|