/*
 * HeadsUp Agile
 * Copyright 2009-2012 Heads Up Development Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.headsupdev.agile.web.components.configuration;

import org.headsupdev.agile.storage.DatabaseRegistry;

import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;

/**
 * A field used to choose a valid SQL url from the supported database types.
 *
 * @author Andrew Williams
 * @version $Id$
 * @since 1.0
 */
public class SQLURLField
    extends Panel
{
    private String type, url;

    public SQLURLField( String id )
    {
        super( id );

        layout();
    }

    public SQLURLField( String id, IModel<String> model )
    {
        super( id, model );

        layout();
    }

    protected void onBeforeRender() {
        super.onBeforeRender();

        String o = getDefaultModelObjectAsString();
        int colon1 = o.indexOf( ':' );
        int colon2 = o.indexOf( ':', colon1 + 1 );

        type = o.substring( colon1 + 1, colon2 );
        url = o.substring( colon2 + 1 );
    }

    private void layout()
    {
        final SQLURLField self = this;
        final TextField urlField = new TextField<String>( "url", new PropertyModel<String>( this, "url" ) {
            public void setObject( String o )
            {
                super.setObject( o );
                updateModelObject();
            }
        });
        urlField.setOutputMarkupId( true );
        final DropDownChoice types = new DropDownChoice<String>( "type", new PropertyModel<String>( this, "type" ),
            DatabaseRegistry.getTypes() );
        types.add( new AjaxFormComponentUpdatingBehavior( "onchange" )
        {
            protected void onUpdate( AjaxRequestTarget target )
            {
                String newType = (String) types.getModelObject();

                url = DatabaseRegistry.getDefaultUrl( newType );
                target.addComponent( urlField );
                updateModelObject();

                self.onUpdate( target );
            }
        } );
        add( types );

        urlField.add( new AjaxFormComponentUpdatingBehavior( "onchange" )
        {
            protected void onUpdate( AjaxRequestTarget target )
            {
                self.onUpdate( target );
            }
        } );
        add( urlField );
    }

    private void updateModelObject()
    {
        ( (IModel<String>) getDefaultModel() ).setObject( "jdbc:" + type + ":" + url );
    }

    protected void onUpdate( AjaxRequestTarget target )
    {
    }
}

@@ -797,16 +797,73 @@
 ation;%0A%0A
+import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;%0A
 import o
@@ -2373,16 +2373,40 @@
 rlField.
+setMarkupId( %22sqlurl%22 ).
 setOutpu
@@ -2610,54 +2610,29 @@
 new 
-AjaxFormComponentUpdatingBehavior( %22onchange%22 
+OnChangeAjaxBehavior(
 )%0A  
@@ -2667,32 +2667,38 @@
 d void onUpdate(
+ final
  AjaxRequestTarg
@@ -2958,37 +2958,85 @@
         
-self.onUpdate( target
+target.appendJavascript( %22document.getElementById('sqlurl').onblur()%22
  );%0A    
@@ -3149,14 +3149,12 @@
  %22on
-change
+blur
 %22 )%0A

Fix callbacks and timing of the URL validation