Options Field

Hi people!

Here is my small contribution to jigsaw community. This class represents
option selection in HTML forms. You can put this class in any package 
you
like. If new version of jigsaw does not have support for selection field
in forms, someone of jigsaw officials can freely put this resource or 
its
modification in w3c.jigsaw.forms package.

Anyway, OptionsField class can be used like one value selection or 
multiple
selection list. In both cases, getValue() returns Vector with selected 
keys.
Function setValue() takes value and put it in selected list. Every call
of setValue inserts one key. You can reset this list with setOptions.

I must say that I dont like the way that setValue and getValue funcions
are designed in my resource, but that's consequence of few admissions in
forms package. My comments on this problem I'll send in saparate mail.

I hope you will find this resource useful. Any comment is welcome. Send 
it on this list or directly to me (mailto:rade@ban.junis.ni.ac.yu).

___START___

// OptionsField.java v1.0
// Author: Radomir Mladenovic <mailto:rade@ban.junis.ni.ac.yu>

package XXX;

import java.util.*;

import w3c.jigsaw.forms.*;
import w3c.jigsaw.html.*;

/**
 * This resource represents options selection it HTML forms.
 * <p>
 * OptionsField takes Hashtable where key can be any Object, but it's
 * expected that value is String. Change code and decode functions
 * if you prefer some other way of key representation. This class 
 * numerates keys.
**/
public class OptionsField extends FormField
{
	protected Hashtable	options;
	protected Vector	selected;
	protected boolean	multi = false;
	protected int		size = -1;

	protected Vector	codes;

	public boolean setValue (String nval)
		throws FormFieldException
	{
		selected.addElement( decode( nval ) );
		return true;
	}

	public Object getValue()
	{
		return selected;
	}

	protected String code( Object k )
	{
		Integer pos = new Integer( codes.size() );
		codes.addElement( k );
		return pos.toString();
	}

	protected Object decode( String val )
	{
		return codes.elementAt( (new Integer( val )).intValue() 
);
	}

	public void dump( HtmlGenerator into )
	{
		dumpTitle( into );

		into.append( "<select name=\"" + getName() + "\" ",
			( multi ? "multiple" : "" ) +
			( ( size != -1 ) ? ( "size=\"" + size + "\"" ) : 
"" ) + ">" );

		for( Enumeration e = options.keys(); 
e.hasMoreElements(); )   {

			Object o = e.nextElement();
			String text = (String) options.get( o );
			into.append( "<option value=\"" + code( o ) + 
"\">" );
			into.appendAndEscape( text );
		}

		into.append( "</select>" );
	}

	public void setMultiple( boolean multi )
	{
		this.multi = multi;
	}

	public void setSize( int s )
	{
		size = s;
	}

	public void setOptions( Hashtable options )
	{
		this.options = options;
		selected.setSize( 0 );
		codes.setSize( 0 );
	}

	public void initialize(String name, String title, String url, 
Object val)
		throws FormFieldException
	{
		super.initialize( name, title, url, null );
		try
			options = (Hashtable) val;
		catch (ClassCastException ex)   {
			String msg = "Invalid type: 
"+val.getClass().getName();
			throw new FormFieldException( msg );
		}
		selected.setSize( 0 );
		codes.setSize( 0 );
	}

	public OptionsField( String name, String title, String url, 
Hashtable options )
	{
		super( name, title, url );
		this.options = options;
		selected = new Vector();
		codes = new Vector();
	}

	public OptionsField()
	{
	}
}

___END___

Received on Friday, 23 August 1996 07:54:44 UTC