Search WebSpherePower's 6,962 WebSphere, Java, and Eclipse article archive 
Home
EasyPrint
News details Click here for the RSS feed's XML code. This is not a browser URL.
Articles-only Click here for the RSS feed's XML code. This is not a browser URL.
Twitter Feed Click here for the Twitter feed.
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);
}
}


« Previous  ·  1  ·  2  ·  3  ·  4  ·  5  ·  Next »
Other articles you might like
Home > Projects > Reusable Code (5 articles)
   Using a reusable code approach to HTML select option lists, part VI
   Using a reusable code approach to HTML select option lists, part V
   Using a reusable code approach to HTML select option lists, part IV
Get Weekly Email Updates
Subscribe to our regular weekly email newsletter. It's packed with tips, reviews, deep analysis, and the latest news.
 
Recent WebSpherePower Articles
A perfect 10: celebrating 10 years online
You can help bring security and safety back to White House email
Introducing the WebSpherePower RSS feeds
From New Jersey to Palm Bay, Florida
A WebSphere pot o' gold
How Elvis entered the building and CES went out the window
WebSphere Application Server 6: what's it all mean?
WebSpherePower News
Excitement brewing for JavaOne 2010, with or without Google
Large companies ignore data centre advice
Onapsis to Release ERP Vulnerability Testing Suite
Botnet Takedown May Yield Valuable Data
VMware app dev platform gazes beyond SpringSource Java
IBM Claims World's Fastest Chip
'Free Java': InfoWorld's guide to the protest goodies
>> Read all the news
More from the ZATZ journals
Computing Unplugged: Smartphone smarts for a mobile world
David Gewirtz Online: CNN commentary and analysis
DominoPower: It's time for Lotus to double-down on Linux and open source
OutlookPower: The strange case of Outlook losing notes and requiring passwords
ZATZ Home  ·  News  ·  Back Issues  ·  Credits/Trademarks ·  Link To Us
Copyright © 2010, ZATZ Publishing. All rights reserved worldwide.
Editor's Login