private Element buildSelect()

in jspwiki-main/src/main/java/org/apache/wiki/forms/FormSelect.java [70:168]


    private Element buildSelect(
            final Map< String, String > pluginParams,
            final Map< String, String > ctxValues,
            final ResourceBundle rb )
            throws PluginException {
        final String inputName = pluginParams.get( PARAM_INPUTNAME );
        if ( inputName == null ) {
            throw new PluginException( rb.getString( "formselect.namemissing" ) );
        }
    
        String inputValue = pluginParams.get( PARAM_VALUE );
        String previousValue = ctxValues.get( inputName );
        //
        // We provide several ways to override the separator, in case some input application the default value.
        //
        String optionSeparator = pluginParams.get( "separator" );
        if ( optionSeparator == null ) {
        	optionSeparator = ctxValues.get( "separator." + inputName);
        }
        if ( optionSeparator == null ) {
        	optionSeparator = ctxValues.get( "select.separator" );
        }
        if ( optionSeparator == null ) {
        	optionSeparator = ";";
        }
        
        String optionSelector = pluginParams.get( "selector" );
        if ( optionSelector == null ) {
        	optionSelector = ctxValues.get( "selector." + inputName );
        }
        if ( optionSelector == null ) {
        	optionSelector = ctxValues.get( "select.selector" );
        }
        if ( optionSelector == null ) {
        	optionSelector = "*";
        }
        if ( optionSelector.equals( optionSeparator ) ) {
        	optionSelector = null;
        }
        if ( inputValue == null ) {
        	inputValue = "";
        }

        // If values from the context contain the separator, we assume that the plugin or something else has given us a better
        // list to display.
        boolean contextValueOverride = false;
        if ( previousValue != null ) {
            if ( previousValue.contains( optionSeparator ) ) {
                inputValue = previousValue;
                previousValue = null;
            } else {
                // If a context value exists, but it's not a list, it'll just override any existing selector indications.
                contextValueOverride = true;
            }
        }

        final String[] options = inputValue.split( optionSeparator );
        int previouslySelected = -1;
        
        final Element[] optionElements = new Element[options.length];
        
        //
        //  Figure out which one of the options to select: prefer the one that was previously selected, otherwise try to find the one
        //  with the "select" marker.
        //
        for( int i = 0; i < options.length; i++ ) {
            int indicated = -1;
            options[i] = options[i].trim();
            
            if ( optionSelector != null && options[i].startsWith( optionSelector ) ) {
                options[i] = options[i].substring( optionSelector.length() );
                indicated = i;
            }
            if ( previouslySelected == -1 ) {
                if ( !contextValueOverride && indicated > 0 ) {
                    previouslySelected = indicated;
                } else if ( options[ i ].equals( previousValue ) ) {
                    previouslySelected = i;
                }
            }
            
            // huh?
//          optionElements[i] = new option( options[i] );
//          optionElements[i].addElement( options[i] );
            
            optionElements[i] = XhtmlUtil.element(XHTML.option,options[i]);
        }

        if ( previouslySelected > -1 ) {
        	optionElements[previouslySelected].setAttribute(XHTML.ATTR_selected,"true");
        }

        final Element select = XhtmlUtil.element(XHTML.select);
        select.setAttribute(XHTML.ATTR_name,HANDLERPARAM_PREFIX + inputName);
        for ( final Element option : optionElements ) {
            select.addContent(option);
        }
        return select;
    }