[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit FormControl.java,1.39,1.40
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2003-05-06 13:31:09
|
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit
In directory sc8-pr-cvs1:/tmp/cvs-serv1408/src/com/meterware/httpunit
Modified Files:
FormControl.java
Log Message:
Select first valid value for Selection control
Index: FormControl.java
===================================================================
RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/FormControl.java,v
retrieving revision 1.39
retrieving revision 1.40
diff -u -r1.39 -r1.40
--- FormControl.java 4 May 2003 12:45:10 -0000 1.39
+++ FormControl.java 6 May 2003 13:31:04 -0000 1.40
@@ -850,7 +850,7 @@
_multiSelect = NodeUtils.isNodeAttributePresent( node, "multiple" );
_listBox = _multiSelect || NodeUtils.isNodeAttributePresent( node, "size" );
- _selectionOptions = new Options( node );
+ _selectionOptions = _multiSelect ? (Options) new MultiSelectOptions( node ) : (Options) new SingleSelectOptions( node );
}
@@ -1034,11 +1034,10 @@
}
- class Options extends ScriptableDelegate implements SelectionOptions {
+ abstract class Options extends ScriptableDelegate implements SelectionOptions {
private Option[] _options;
-
Options( Node selectionNode ) {
NodeList nl = ((Element) selectionNode).getElementsByTagName( "option" );
@@ -1054,22 +1053,15 @@
boolean claimUniqueValues( List values ) {
- boolean changed = false;
- int numMatches = 0;
- for (int i = 0; i < _options.length; i++) {
- final boolean newValue = values.contains( _options[i].getValue() );
- if (newValue != _options[i].isSelected()) changed = true;
- _options[i].setSelected( newValue );
- if (newValue) {
- values.remove( _options[i].getValue() );
- numMatches++;
- if (!_multiSelect) for (++i; i < _options.length; i++) _options[i].setSelected( false );
- }
- }
- if (!_listBox && numMatches == 0) {
- throw new IllegalParameterValueException( getName(), (String) values.get(0), getOptionValues() );
- }
- return changed;
+ return claimUniqueValues( values, _options );
+ }
+
+
+ protected abstract boolean claimUniqueValues( List values, Option[] options );
+
+
+ final protected void reportNoMatches( List values ) {
+ if (!_listBox) throw new IllegalParameterValueException( getName(), (String) values.get(0), getOptionValues() );
}
@@ -1121,10 +1113,13 @@
for (int i = 0; i < _options.length; i++) {
if (_options[i].isSelected()) return i;
}
- return _multiSelect ? -1 : 0;
+ return noOptionSelectedIndex();
}
+ protected abstract int noOptionSelectedIndex();
+
+
public int getLength() {
return _options.length;
}
@@ -1151,17 +1146,12 @@
}
_options[i] = (Option) option;
_options[i].setIndex( this, i );
- if (option.isSelected()) ensureUniqueOption(i);
+ if (option.isSelected()) ensureUniqueOption( _options, i);
}
}
- private void ensureUniqueOption( int i ) {
- if (_multiSelect) return;
- for (int j = 0; j < _options.length; j++) {
- _options[j]._selected = (i == j);
- }
- }
+ protected abstract void ensureUniqueOption( Option[] options, int i );
private void deleteOptionsEntry( int i ) {
@@ -1186,7 +1176,7 @@
/** Invoked when an option is set true. **/
void optionSet( int i ) {
- ensureUniqueOption(i);
+ ensureUniqueOption( _options, i);
}
@@ -1208,6 +1198,74 @@
return (value == null) ? "" : value;
}
+ }
+
+
+ class SingleSelectOptions extends Options {
+
+ public SingleSelectOptions( Node selectionNode ) {
+ super( selectionNode );
+ }
+
+
+ protected void ensureUniqueOption( Option[] options, int i ) {
+ for (int j = 0; j < options.length; j++) {
+ options[j]._selected = (i == j);
+ }
+ }
+
+
+ protected int noOptionSelectedIndex() {
+ return 0;
+ }
+
+
+ protected boolean claimUniqueValues( List values, Option[] options ) {
+ boolean changed = false;
+ for (int i = 0; i < values.size(); i++) {
+ String value = (String) values.get( i );
+ for (int j = 0; j < options.length; j++) {
+ boolean selected = value.equals( options[j].getValue() );
+ if (selected != options[j].isSelected()) changed = true;
+ options[j].setSelected( selected );
+ if (selected) {
+ values.remove( value );
+ for (++j; j < options.length; j++) options[j].setSelected( false );
+ return changed;
+ }
+ }
+ }
+ reportNoMatches( values );
+ return changed;
+ }
+ }
+
+
+ class MultiSelectOptions extends Options {
+
+ public MultiSelectOptions( Node selectionNode ) {
+ super( selectionNode );
+ }
+
+
+ protected void ensureUniqueOption( Option[] options, int i ) {}
+
+
+ protected int noOptionSelectedIndex() {
+ return -1;
+ }
+
+
+ protected boolean claimUniqueValues( List values, Option[] options ) {
+ boolean changed = false;
+ for (int i = 0; i < options.length; i++) {
+ final boolean newValue = values.contains( options[i].getValue() );
+ if (newValue != options[i].isSelected()) changed = true;
+ options[i].setSelected( newValue );
+ if (newValue) values.remove( options[i].getValue() );
+ }
+ return changed;
+ }
}
|