001    package com.sptci.rwt.webui;
002    
003    import java.io.File;
004    
005    import nextapp.echo2.app.Column;
006    import nextapp.echo2.app.Component;
007    import nextapp.echo2.app.Row;
008    import nextapp.echo2.app.filetransfer.UploadSelect;
009    
010    import echopointng.TabbedPane;
011    import echopointng.tabbedpane.DefaultTabModel;
012    
013    import com.sptci.echo2.Configuration;
014    import com.sptci.echo2.Dimensions;
015    import com.sptci.echo2.FileUploadListener;
016    import com.sptci.echo2.table.Table;
017    import com.sptci.echo2.table.TableNavigation;
018    import com.sptci.util.StringUtilities;
019    
020    /**
021     * Query executor view component used to send multiple SQL statements in
022     * one batch to the database server.  Uses the {@link
023     * com.sptci.rwt.BatchQueryExecutor} class.
024     *
025     * <p>&copy; Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
026     * @author Rakesh Vidyadharan 2007-10-10
027     * @version $Id: BatchQueryExecutorView.java 4123 2008-05-25 21:49:01Z rakesh $
028     */
029    public class BatchQueryExecutorView extends ExecutorView
030    {
031      /** The tabbed pane used to display the results of query execution. */
032      private TabbedPane tabbedPane;
033    
034      /** The SQL statement that is to be displayed in {@link #query} when it
035       * is initialised.
036       */
037      private String statement;
038    
039      /**
040       * Create instance of the pane using the specified controller.
041       *
042       * @param controller The controller to use to interact with the rest of
043       *   the application.
044       */
045      public BatchQueryExecutorView( final MainController controller )
046      {
047        super( controller );
048      }
049    
050      /**
051       * Create instance of the pane using the specified controller.
052       *
053       * @param controller The controller to use to interact with the rest of
054       *   the application.
055       * @param statement The SQL statement that is to be displayed in the
056       *   {@link #query} component.
057       */
058      public BatchQueryExecutorView( final MainController controller,
059          final String statement )
060      {
061        this( controller );
062        this.statement = statement;
063      }
064    
065      /**
066       * Over-ridden to set the default value of {@link #maxResults}.
067       */
068      @Override
069      public void init()
070      {
071        super.init();
072        if ( statement != null ) query.setText( statement );
073        maxResults.setText(
074            String.valueOf( Dimensions.getInt( this, "maxRows" ) ) );
075      }
076    
077      /**
078       * Over-ridden to display controls relevant to this class.
079       *
080       * @see #createExecute
081       * @see #createMaxResults
082       * @see #createMaxColumnLength
083       * @see #createHistory
084       * @see #createSave
085       * @see #createUpload
086       * @return The layout component.
087       */
088      @Override
089      protected Component createControls()
090      {
091        Column column = new Column();
092        Row row = new Row();
093        row.add( createExecute() );
094        createMaxResults( row );
095        createMaxColumnLength( row );
096        row.add( createExport() );
097        row.add( createSave() );
098        row.add( createHistory() );
099        column.add( row );
100    
101        row = new Row();
102        row.add( createUpload() );
103        column.add( row );
104    
105        return column;
106      }
107    
108      /**
109       * Removes the results of a previous query execution.  Removes all the
110       * child components of {@link #results} and reinitialises {@link
111       * #tabbedPane}.
112       */
113      @Override
114      public void reset()
115      {
116        results.removeAll();
117        tabbedPane = new TabbedPane();
118        results.add( tabbedPane );
119      }
120    
121      /**
122       * Create the component used to upload SQL script files.
123       *
124       * @return The file upload component.
125       */
126      protected Component createUpload()
127      {
128        UploadSelect select = new UploadSelect();
129    
130        try
131        {
132          select.addUploadListener( new FileUploadListener( this, "setFile" ) );
133        }
134        catch ( Throwable t )
135        {
136          controller.getLogger().log( java.util.logging.Level.WARNING,
137              "Too many upload select listeners added.", t );
138        }
139    
140        return select;
141      }
142    
143      /**
144       * Set the contents of {@link #query} to the contents of the file
145       * specified.
146       *
147       * @param file The file whose contents are to be displayed in {@link
148       *   #query}.
149       */
150      public void setFile( final File file )
151      {
152        try
153        {
154          setQuery( StringUtilities.fromFile( file.getAbsolutePath() ) );
155          file.delete();
156        }
157        catch ( Throwable t )
158        {
159          String message = Configuration.getString( this, "fileError" );
160          message = message.replaceAll( "\\$file\\$", file.getAbsolutePath() );
161          controller.processFatalException( message, t );
162        }
163      }
164    
165      /**
166       * Create a {@link nextapp.echo2.app.Table} using the specified 
167       * {@link nextapp.echo2.app.table.TableModel} and display in a {@link
168       * echopointng.TabbedPane}.
169       *
170       * @see #createNavigation
171       * @param name The name to assign for the results tab.
172       * @param model The results table model to display.
173       */
174      public void addResults( final String name, final RowsTableModel model )
175      {
176        Column column = new Column();
177        createNavigation( model, column );
178        Table<com.sptci.rwt.Row> table = new Table<com.sptci.rwt.Row>( model );
179        column.add( table );
180        ( (DefaultTabModel) tabbedPane.getModel() ).addTab( name, column );
181      }
182      
183      
184      /**
185       * Create a {@link com.sptci.echo2.table.TableNavigation} for the specified
186       * {@link RowTable} if necessary.
187       *
188       * @since Version 1.2
189       * @param model The table model based on whose size the navigation
190       *   comonent is to be displayed.
191       * @param parent The parent component to which the navigation component
192       *   is to be added.
193       */
194      void createNavigation( final RowsTableModel model, final Component parent )
195      {
196        if ( model.getTotalRows() > TableNavigation.MINIMUM_PAGE_SIZE )
197        {
198          TableNavigation<Row> navigation = new TableNavigation<Row>( model );
199          parent.add( navigation );
200        }
201      }
202    }