001    package com.sptci.rwt.webui;
002    
003    import nextapp.echo2.app.Component;
004    import nextapp.echo2.app.event.ActionEvent;
005    
006    import com.sptci.echo2.Configuration;
007    import com.sptci.echo2.Confirmation;
008    import com.sptci.echo2.Executor;
009    import com.sptci.echo2.Listener;
010    
011    import com.sptci.rwt.Connections;
012    import com.sptci.rwt.DatabaseType;
013    
014    /**
015     * The component that is used to delete saved connections.
016     *
017     * <p>&copy; Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
018     * @author Rakesh Vidyadharan 2007-10-14
019     * @version $Id: DeleteSavedConnectionListener.java 4123 2008-05-25 21:49:01Z rakesh $
020     */
021    public class DeleteSavedConnectionListener extends Listener<MainController>
022    {
023      /** The database type under which the connection is saved. */
024      private final String databaseType;
025    
026      /** The name of the saved connection. */
027      private final String name;
028    
029      /**
030       * Create a new instance of the listener using the specified controller.
031       *
032       * @param databaseType The name of the database type under which the
033       *   saved connection is organised.
034       * @param name The name of the saved connection.
035       * @param controller The controller to use to interact with the
036       *   application
037       */
038      public DeleteSavedConnectionListener( final String databaseType,
039          final String name, final MainController controller )
040      {
041        super( controller );
042        this.databaseType = databaseType;
043        this.name = name;
044      }
045    
046      /**
047       * The action listener implementation.  Launches a {@link
048       * com.sptci.echo2.Confirmation} dialogue prompting user to confirm
049       * the delete action.
050       *
051       * @param event The action event that was triggered.
052       */
053      public void actionPerformed( final ActionEvent event )
054      {
055        Executor<DeleteSavedConnectionListener> executor =
056          new Executor<DeleteSavedConnectionListener>( this, "delete" );
057        executor.addParameter( Component.class, (Component) event.getSource() );
058    
059        String message = Configuration.getString( this, "delete.message" );
060        message = message.replaceAll( "\\$name\\$", name );
061    
062        Confirmation confirmation = new Confirmation(
063            Configuration.getString( this, "delete.title" ), message, executor );
064        controller.addPane( confirmation );
065      }
066    
067      /**
068       * Delete the saved connection and update the {@link
069       * ManageSavedConnectionsView} view component.
070       *
071       * @see com.sptci.rwt.Connections#delete( String, String )
072       * @see com.sptci.rwt.Connections#delete( String )
073       * @see MainController#resetMenu
074       * @param component The component that is to be removed from the view.
075       */
076      protected void delete( final Component component )
077      {
078        Connections connections = controller.getConnections();
079        connections.delete( databaseType, name );
080    
081        Component parent = component.getParent();
082        int index = parent.indexOf( component );
083        parent.remove( --index );
084        parent.remove( index );
085    
086        DatabaseType type = connections.getDatabaseType( databaseType );
087        if ( type.getConnectionData().isEmpty() )
088        {
089          connections.delete( databaseType );
090          Component p = parent.getParent().getParent();
091          p.remove( parent.getParent() );
092        }
093    
094        controller.resetMenu();
095      }
096    }