⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formcontrol.java

📁 这是远程web服务调用的一个包,可以将JSP直接作为服务
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    String[] getDisplayedOptions() {        return _displayedValue;    }    void addValues( ParameterProcessor processor, String characterSet ) throws IOException {        if (isChecked() && !isDisabled()) processor.addParameter( getName(), getQueryValue(), characterSet );    }    /**     * Remove any required values for this control from the list, throwing an exception if they are missing.     **/    void claimRequiredValues( List values ) {        if (isValueRequired()) claimValueIsRequired( values, getQueryValue() );    }    protected boolean isValueRequired() {        return isReadOnly() && isChecked();    }    abstract String getQueryValue();    private String[] toArray( String value ) {        _value[0] = value;        return _value;    }}class RadioButtonFormControl extends BooleanFormControl {    public String getType() {        return RADIO_BUTTON_TYPE;    }    public RadioButtonFormControl( WebForm form, Node node ) {        super( form, node );    }    /**     * Returns true if only one control of this kind can have a value.     **/    public boolean isExclusive() {        return true;    }    String getQueryValue() {        return getValueAttribute();    }}class RadioGroupFormControl extends FormControl {    private List _buttonList = new ArrayList();    private RadioButtonFormControl[] _buttons;    private String[] _allowedValues;    public String getType() {        return UNDEFINED_TYPE;    }    public RadioGroupFormControl( WebForm form ) {        super( form );    }    void addRadioButton( RadioButtonFormControl control ) {        _buttonList.add( control );        _buttons = null;        _allowedValues = null;    }    public String[] getValues() {        for (int i = 0; i < getButtons().length; i++) {            if (getButtons()[i].isChecked()) return getButtons()[i].getValues();        }        return NO_VALUE;    }    /**     * Returns the option values defined for this radio button group.     **/    public String[] getOptionValues() {        ArrayList valueList = new ArrayList();        FormControl[] buttons = getButtons();        for (int i = 0; i < buttons.length; i++) {            valueList.addAll( Arrays.asList( buttons[i].getOptionValues() ) );        }        return (String[]) valueList.toArray( new String[ valueList.size() ] );    }    /**     * Returns the options displayed for this radio button group.     */    String[] getDisplayedOptions() {        ArrayList valueList = new ArrayList();        FormControl[] buttons = getButtons();        for (int i = 0; i < buttons.length; i++) {            valueList.addAll( Arrays.asList( buttons[i].getDisplayedOptions() ) );        }        return (String[]) valueList.toArray( new String[ valueList.size() ] );    }    Object getDelegate() {        ScriptableDelegate[] delegates = new ScriptableDelegate[ getButtons().length ];        for (int i = 0; i < delegates.length; i++) {            delegates[i] = getButtons()[i].getScriptableDelegate();        }        return delegates;    }    void addValues( ParameterProcessor processor, String characterSet ) throws IOException {        for (int i = 0; i < getButtons().length; i++) getButtons()[i].addValues( processor, characterSet );    }    /**     * Remove any required values for this control from the list, throwing an exception if they are missing.     **/    void claimRequiredValues( List values ) {        for (int i = 0; i < getButtons().length; i++) {            getButtons()[i].claimRequiredValues( values );        }    }    void claimUniqueValue( List values ) {        int matchingButtonIndex = -1;        for (int i = 0; i < getButtons().length && matchingButtonIndex < 0; i++) {            if (!getButtons()[i].isReadOnly() && values.contains( getButtons()[i].getQueryValue() )) matchingButtonIndex = i;        }        if (matchingButtonIndex <0) throw new IllegalParameterValueException( getButtons()[0].getName(), (String) values.get(0), getAllowedValues() );        boolean wasChecked = getButtons()[ matchingButtonIndex ].isChecked();        for (int i = 0; i < getButtons().length; i++) {            if (!getButtons()[i].isReadOnly()) getButtons()[i].setChecked( i == matchingButtonIndex );        }        values.remove( getButtons()[ matchingButtonIndex ].getQueryValue() );        if (!wasChecked) getButtons()[ matchingButtonIndex ].sendOnClickEvent();    }    void reset() {        for (int i = 0; i < getButtons().length; i++) getButtons()[i].reset();    }    private String[] getAllowedValues() {        if (_allowedValues == null) {            _allowedValues = new String[ getButtons().length ];            for (int i = 0; i < _allowedValues.length; i++) {                _allowedValues[i] = getButtons()[i].getQueryValue();            }        }        return _allowedValues;    }    private RadioButtonFormControl[] getButtons() {        if (_buttons == null) _buttons = (RadioButtonFormControl[]) _buttonList.toArray( new RadioButtonFormControl[ _buttonList.size() ] );        return _buttons;    }}class CheckboxFormControl extends BooleanFormControl {    public String getType() {        return CHECKBOX_TYPE;    }    public CheckboxFormControl( WebForm form, Node node ) {        super( form, node );    }    void claimUniqueValue( List values ) {        if (isValueRequired()) return;        setState( values.contains( getQueryValue() ) );        if (isChecked()) values.remove( getQueryValue() );    }    String getQueryValue() {        final String value = getValueAttribute();        return value.length() == 0 ? "on" : value;    }    /**     * Toggles the value of this control.     */    public void toggle() {        setState( !isChecked() );    }    /**     * Sets the state of this boolean control. Triggers the 'onclick' event if the state has changed.     */    public void setState( boolean state ) {        boolean wasChecked = isChecked();        setChecked( state );        if (isChecked() != wasChecked) sendOnClickEvent();    } }abstract class TextFormControl extends FormControl {    private String[]   _value = new String[1];    private String[]   _defaultValue;    public TextFormControl( WebForm form, Node node, String defaultValue ) {        super( form, node );        _defaultValue = new String[] { defaultValue };    }    /**     * Returns the current value(s) associated with this control. These values will be transmitted to the server     * if the control is 'successful'.     **/    public String[] getValues() {        return (_value[0] != null) ? _value : _defaultValue;   }    /**     * Returns true to indicate that this control accepts free-form text.     **/    public boolean isTextControl() {        return true;    }    protected ScriptableDelegate newScriptable() {        return new Scriptable();    }    void addValues( ParameterProcessor processor, String characterSet ) throws IOException {        if (!isDisabled() && getName().length() > 0) processor.addParameter( getName(), getValues()[0], characterSet );    }    void claimValue( List values ) {        if (isReadOnly()) return;        String oldValue = getValues()[0];        if (values.isEmpty()) {            _value[0] = "";        } else {            _value[0] = (String) values.get(0);            values.remove(0);        }        if (!(oldValue.equals( _value[0] ))) sendOnChangeEvent();    }    void reset() {        _value[0] = null;    }    void claimRequiredValues( List values ) {        if (isReadOnly()) claimValueIsRequired( values );    }    protected void claimValueIsRequired( List values ) {        claimValueIsRequired( values, _defaultValue[0] );    }    class Scriptable extends FormControl.Scriptable {        public Object get( String propertyName ) {            if (propertyName.equalsIgnoreCase( "value" )) {                return getValues()[0];            } else if (propertyName.equalsIgnoreCase( "defaultValue" )) {                return _defaultValue[0];            } else {                return super.get( propertyName );            }        }        public void set( String propertyName, Object value ) {            if (!propertyName.equalsIgnoreCase( "value" )) {                super.set( propertyName, value );            } else if (value instanceof Number) {                _value[0] = HttpUnitUtils.trimmedValue( (Number) value );            } else {                _value[0] = (value == null) ? null : value.toString();            }        }    }}class TextFieldFormControl extends TextFormControl {    public String getType() {        return TEXT_TYPE;    }    public TextFieldFormControl( WebForm form, Node node ) {        super( form, node, NodeUtils.getNodeAttribute( node, "value" ) );        supportAttribute( "maxlength" );    }}class PasswordFieldFormControl extends TextFieldFormControl {    public String getType() {        return PASSWORD_TYPE;    }    public PasswordFieldFormControl(WebForm form, Node node) {        super(form, node);    }}class HiddenFieldFormControl extends TextFieldFormControl {    public String getType() {        return HIDDEN_TYPE;    }    public HiddenFieldFormControl( WebForm form, Node node ) {        super( form, node );    }    void claimRequiredValues( List values ) {        claimValueIsRequired( values );    }    void claimValue( List values ) {    }    boolean isHidden() {        return true;    }}class TextAreaFormControl extends TextFormControl {    public TextAreaFormControl( WebForm form, Node node ) {        super( form, node, getDefaultValue( node ) );        if (!node.getNodeName().equalsIgnoreCase( "textarea" )) {            throw new RuntimeException( "Not a textarea element" );        }    }    private static String getDefaultValue( Node node ) {        return NodeUtils.asText( node.getChildNodes() );    }    public String getType() {        return TEXTAREA_TYPE;    }}class FileSubmitFormControl extends FormControl {    public String getType() {        return FILE_TYPE;    }    private UploadFileSpec _fileToUpload;    protected ScriptableDelegate newScriptable() {        return new Scriptable();    }    class Scriptable extends FormControl.Scriptable {        public Object get( String propertyName ) {            if (propertyName.equalsIgnoreCase( "value" )) {                return getSelectedName();           } else {                return super.get( propertyName );            }        }    }    public FileSubmitFormControl( WebForm form, Node node ) {        super( form, node );    }    /**     * Returns true if this control accepts a file for upload.     **/    public boolean isFileParameter() {        return true;    }    /**     * Returns the name of the selected file, if any.     */    public String[] getValues() {        return new String[] { getSelectedName() };    }    private String getSelectedName() {        return _fileToUpload == null ? "" : _fileToUpload.getFileName();    }    /**     * Specifies a number of file upload specifications for this control.     **/    void claimUploadSpecification( List files ) {        if (files.isEmpty()) {            _fileToUpload = null;        } else {            _fileToUpload = (UploadFileSpec) files.get(0);            files.remove(0);        }    }    void addValues( ParameterProcessor processor, String characterSet ) throws IOException {        if (!isDisabled() && _fileToUpload != null) {            processor.addFile( getName(), _fileToUpload );        }    }}class SelectionFormControl extends FormControl {    private final boolean _multiSelect;    private final boolean _listBox;    private Options _selectionOptions;    public String getType() {        return (isMultiValued()?MULTIPLE_TYPE:SINGLE_TYPE);    }    SelectionFormControl( WebForm form, Node node ) {        super( form, node );        if (!node.getNodeName().equalsIgnoreCase( "select" )) throw new RuntimeException( "Not a select element" );        int size     = NodeUtils.getAttributeValue( node, "size", 0);        _multiSelect = NodeUtils.isNodeAttributePresent( node, "multiple" );        _listBox     = size > 1 || (_multiSelect && size != 1);        _selectionOptions = _listBox ? (Options) new MultiSelectOptions( node ) : (Options) new SingleSelectOptions( node );    }    public String[] getValues() {        return _selectionOptions.getSelectedValues();    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -