001    package com.sptci.rwt.webui;
002    
003    import nextapp.echo2.app.Button;
004    import nextapp.echo2.app.event.ActionEvent;
005    
006    import nextapp.echo2.app.filetransfer.Download;
007    
008    import com.sptci.echo2.Configuration;
009    import com.sptci.echo2.Listener;
010    
011    /**
012     * Action listener for exporting the results of a SQL statement to
013     * Excel.  This uses the
014     * <a href='http://poi.apache.org/hssf/index.html'>Apache POI HSSF</a>
015     * library for creating Excel workbooks.
016     *
017     * @see ExcelDownloadProvider
018     * <p>&copy; Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
019     * @author Rakesh Vidyadharan 2007-10-05
020     * @version $Id: ExportListener.java 4123 2008-05-25 21:49:01Z rakesh $
021     */
022    public class ExportListener extends Listener<MainController>
023    {
024      /**
025       * Create a new instance of the listener using the specified controller.
026       *
027       * @param controller The controller to use to interact with the
028       *   application.
029       */
030      public ExportListener( final MainController controller )
031      {
032        super( controller );
033      }
034    
035      /**
036       * The action listener implementation.  Executes the query and exports
037       * the results.
038       *
039       * @see #checkView
040       * @param event The event that triggers the export process.
041       */
042      public void actionPerformed( ActionEvent event )
043      {
044        Button button = (Button) event.getSource();
045        button.setEnabled( false );
046    
047        try
048        {
049          final ExecutorView view =
050            (ExecutorView) getController().getParentView( button );
051        if ( ! checkView( view ) ) return;
052    
053          final ExcelDownloadProvider provider = new ExcelDownloadProvider(
054              view.getQuery(), getController().getConnectionManager() );
055    
056          final Download download = new Download();
057          download.setProvider( provider );
058          download.setActive( true );
059          getController().getApplication().enqueueCommand( download );
060        }
061        catch ( Throwable t )
062        {
063          getController().processFatalException( Configuration.getString(
064                ExcelDownloadProvider.class, "error" ), t );
065        }
066        finally
067        {
068          button.setEnabled( true );
069        }
070      }
071    
072      /**
073       * Check the {@link QueryExecutorView} to ensure that a valid excel
074       * workbook can be generated.  Ensures that some SQL statement has been
075       * entered into {@link QueryExecutorView#query}.
076       *
077       * @return Returns <code>true</code> if some text has been entered.
078       */
079      private boolean checkView( final ExecutorView view )
080      {
081        return ( view.getQuery().length() > 0 );
082      }
083    }