Search WebSpherePower's 6,567 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.
PROGRAMMING POWER
Using a reusable code approach to HTML select option lists: part II
By Jeff Chilton

"It is best to do things systematically, since we are only human, and disorder is our worst enemy."
-- Hesiod (ancient Greek poet, circa 850 B.C.)

About this series
I've got some serious mea culpa action goin' in this article. As you no doubt noticed, this article is Part II. But where, you might ask, is Part I? Well, here's the story...when it was submitted for editing, I didn't know which ZATZ magazine it was intended for. He's written for DominoPower, so I just assumed that our friend Part I was also intended for DominoPower. Uh, it wasn't. It's all about WebSphere. Fortunately, both Senior Technical Editor Dan Velasco and Contributing Editor Jeff Chilton have a far better grasp on reality than I do, and now, Part II is in its proper home. If you want to read Part I, please visit http://www.dominopower.com/issues/issue200310/00001118001.html. -- David Gewirtz, yer ever-humble Editor-in-Chief

In the first installment of this series, we created a simple framework for the purpose of obtaining an ArrayList of Struts LabelValueBeans to support an html:options tag. To continue with this little microcosm of framework concepts, we're going to go back and tinker with what we've already created and then add another flavor of an implementing class to our portfolio. Finally, we will modify our core components to add some additional functionality that both our original implementing class and our new implementing class will be able to inherit without any further modification to either.

Incremental improvement #1: enhancing what we have
In the final phase of Part I, we created our SimpleOptionListSource class, which took as a data source a String containing a comma-separated list of values. This class extended our abstract OptionListSourceBase class, which implemented our OptionListSource interface. One of the assumptions that we made at the time, in order to keep things as simple as possible, was that both the label and the value would be the same. With just a slight modification and the adoption of a specific syntax, we can enhance this module to accept label/value pairs instead of just values, yet still retain the original functionality for those utilizations that only pass a single value.

Listing 1 contains our new and improved SimpleOptionListSource class, which will now accept both single values and label/value pairs separated by the colon (":") character.

package step.two;

import java.util.ArrayList;
import java.util.StringTokenizer;

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 directly from the
* option list string provided.
*/
public class SimpleOptionListSource extends OptionListSourceBase {
private String optionList = null;

/**
* Constructs a new "SimpleOptionListSource" object using the parameters
* provided.
*
* @param optionList a String containing the comma separated values for
* the option list
*/
public SimpleOptionListSource(String optionList) {
this.optionList = optionList;
}

/**
* Loads the list of available options from the optionList
* String passed to the constructor.
*/
protected void loadOptions() {
ArrayList list = new ArrayList();

StringTokenizer tokens = new StringTokenizer (optionList, ",");
for (int i = 0; i < tokens.countTokens(); i++) {
String thisItem = tokens.nextToken();
LabelValueBean thisOption = new LabelValueBean(thisItem, thisItem);
int x = thisItem.indexOf(":");
if (x > -1) {
String label = thisItem.substring(0, x);
String value = thisItem.substring(x + 1);
thisOption = new LabelValueBean(label, value);
}
list.add(thisOption);
}

setOptions(list);
}
}


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
Submit your Abstracts Today for 2010 Exceptional Web Experience
Building Web Reputation Systems
Accusations Fly in Viacom, YouTube Copyright Fight
Cybercrime's bulletproof hosting exposed
O'Reilly Velocity Conference Opens Registration and Reveals Program
SAN SnapShot Backup for VMWare
What users want from Oracle's Java Community Process
>> Read all the news
More from the ZATZ journals
Computing Unplugged: The iPad defenders have spoken
David Gewirtz Online: CNN commentary and analysis
DominoPower: Application development, William Shatner, and the origin of the universe
OutlookPower: More about disappearing text
-- Advertisement --

Coming soon, new book: How To Save Jobs
This book is about how to create and save jobs. Believe it or not, there's not a single book out there that specifically focuses on job creation and preservation -- until now.

This book, by ZATZ editor-in-chief David Gewirtz, is about helping your business work better. It's about helping you change the things you need to change so your company can perform more effectively.

Plus, through a grant from ZATZ, it's a free download.

Read it and reap.

-- Advertisement --

Influencer. Recommender. Decision Maker.
They all read WebSpherePower Magazine. They all rely on WebSpherePower Magazine.

If you want to reach the inner-circle of IBM IT professionals, you won't find a better resource than WebSpherePower Magazine.

Click for our Media Kit

ZATZ Home  ·  News  ·  Back Issues  ·  Credits/Trademarks ·  Link To Us
Copyright © 2010, ZATZ Publishing. All rights reserved worldwide.
Editor's Login