001    package com.sptci.rwt.webui;
002    
003    import java.sql.SQLException;
004    
005    import nextapp.echo2.app.Button;
006    import nextapp.echo2.app.event.ActionEvent;
007    
008    import com.sptci.echo2.Listener;
009    
010    /**
011     * The listener for the {@link QueryExecutorView} used to interact with
012     * the {@link com.sptci.rwt.QueryExecutor} class.
013     *
014     * <p>&copy; Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
015     * @author Rakesh Vidyadharan 2007-10-04
016     * @version $Id: ExecuteQueryListener.java 4123 2008-05-25 21:49:01Z rakesh $
017     */
018    public class ExecuteQueryListener extends Listener<MainController>
019    {
020      /**
021       * Create a new instance of the listener using the specified {@link
022       * com.sptci.echo2.Controller} to interact with the application.
023       *
024       * @param controller The controller to use.
025       */
026      public ExecuteQueryListener( MainController controller )
027      {
028        super( controller );
029      }
030    
031      /**
032       * The {@link nextapp.echo2.app.event.ActionListener} implementation.
033       * Submit the entered query to the database server for execution.  Handle
034       * query execution in the background to enable cancellation of the
035       * query.
036       *
037       * @param event The action event that was triggered by the user.
038       */
039      public void actionPerformed( ActionEvent event )
040      {
041        final Button button = (Button) event.getSource();
042        button.setEnabled( false );
043    
044        try
045        {
046          final QueryExecutorView view =
047            (QueryExecutorView) controller.getParentView( button );
048          view.reset();
049          processQuery( view );
050        }
051        finally
052        {
053          button.setEnabled( true );
054        }
055      }
056    
057      /**
058       * Process the query entered in {@link QueryExecutorView#query} field.
059       *
060       * @param view The view from which this action was triggered.
061       * @throws SQLException If errors are encountered while executing
062       *   the query.
063       */
064      private void processQuery( final QueryExecutorView view )
065      {
066        final String query = view.getQuery();
067        final int maxResults = view.getMaxResults();
068        final int maxColumnLength = view.getMaxColumnLength();
069    
070        if ( query.length() > 0 )
071        {
072          view.addToHistory( query );
073          final RowTableModel model = new RowTableModel(
074              query, maxResults, maxColumnLength, view.getConnectionManager() );
075          view.setResults( new RowTable( model ) );
076        }
077      }
078    }