|
|
|
|
|
|
|
|
Using a reusable code approach to HTML select option lists: part II (continued)
Our original example from Part I and the following new example will now both produce acceptable results:
OptionListSource priorityOptionSource =
new SimpleOptionListSource("Low,Normal,High");
OptionListSource genderOptionSource =
new SimpleOptionListSource("Male:M,Female:F");
|
One other thing you may notice when comparing this version with the previous is that we have cleaned up the way in which the comma-separated values are parsed. The original version used a cumbersome manual method, while the new version uses a slightly cleaner method using a StringTokenizer object. Users of Java 1.4.x could use an even cleaner approach using the new split() method of String. Once again, the value of reusable code is that you only have to make these kinds of changes in one place and every using or extending class inherits the benefits without having to be touched in any way.
Incremental improvement #2: adding a new variation The purpose of creating the interface and the abstract base class was to lay the foundations for multiple variations. Our SimpleOptionListSource was one possible variation, but there are many potential sources of option list data. You're probably familiar with working with databases and could easily construct your own SqlOptionListSource or XmlOptionListSource, so I thought I would tackle something slightly more obscure and look at getting our options from a Java Properties object.
Listing 2 contains our PropertiesOptionListSource class, which again extends the OptionListSourceBase class and implements the loadOptions() method.
package step.two;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;
import org.apache.struts.util.LabelValueBean;
/**
* Returns the name/value pairs for the options related to an HTML
* "select" tag.
*
* Source data for this implementation is obtained from a Java
* <code>Properties</code> object. You can instantiate this class by
* providing the Properties object directly, or by providing the fully
* qualified name of a text file from which to load a new Properties
* object.
*/
public class PropertiesOptionListSource extends OptionListSourceBase {
private Properties props = null;
/**
* Constructs a new "PropertiesOptionListSource" object using the
* parameters provided.
*
* @param fileName a String containing the fully qualified file name
* of the text file from which to load a new Properties object
*/
public PropertiesOptionListSource(String fileName) {
this(getProperties(fileName));
}
/**
* Constructs a new "PropertiesOptionListSource" object using the
* parameters provided.
*
* @param props the Java <code>Properties</code> object from which
* we will obtain our labels and values
*/
public PropertiesOptionListSource(Properties props) {
this.props = props;
}
/**
* Creates a new <code>Properties</code> object and load it from
* the named file.
*
* @param fileName a String containing the fully qualified file name
* of the text file from which to load the Properties object
* @return the loaded <code>Properties</code> object
*/
private static Properties getProperties(String fileName) {
Properties props = new Properties();
InputStream is = null;
try {
is = new FileInputStream(fileName);
if (is != null) {
props.load(is);
is.close();
is = null;
}
} catch (Throwable t) {
// here is where you would invoke your own event management or logging
System.out.println(" load Properties error: " + t.toString());
} finally {
if (is != null) {
try {
is.close();
} catch (Throwable t) {
// here is where you would invoke your own event management or logging
System.out.println(" load Properties error: " + t.toString());
}
}
}
return props;
}
/**
* Loads the list of available options from the <code>Properties</code>
* object.
*/
protected void loadOptions() {
ArrayList list = new ArrayList();
Enumeration e = props.propertyNames();
while (e.hasMoreElements()) {
String value = e.nextElement().toString();
String label = props.getProperty(value);
LabelValueBean thisOption = new LabelValueBean(label, value);
list.add(thisOption);
}
setOptions(list);
}
}
|
|
|
|
|
|
|
|
|
|
|
|