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)

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);
}


« 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
-- Advertisement --

EASY DEDICATED AND VIRTUAL DEDICATED SERVERS FOR AS LOW AS $67.99 PER MONTH
Customize and configure your own dedicated server. Simply choose one of our popular plans or select your own Linux or Windows server and plan options.

NO LONG WAITS. Server provisioned within hours.

Tap here now and be up and running with your own server tonight.

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