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.Queries;
012    import com.sptci.rwt.Category;
013    
014    /**
015     * The component that is used to delete saved queries.
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: DeleteSavedQueryListener.java 4123 2008-05-25 21:49:01Z rakesh $
020     */
021    public class DeleteSavedQueryListener extends Listener<MainController>
022    {
023      /** The category under which the query is saved. */
024      private final String category;
025    
026      /** The name of the saved query. */
027      private final String name;
028    
029      /**
030       * Create a new instance of the listener using the specified controller.
031       *
032       * @param category The name of the database type under which the
033       *   saved query is organised.
034       * @param name The name of the saved query.
035       * @param controller The controller to use to interact with the
036       *   application
037       */
038      public DeleteSavedQueryListener( final String category,
039          final String name, final MainController controller )
040      {
041        super( controller );
042        this.category = category;
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<DeleteSavedQueryListener> executor =
056          new Executor<DeleteSavedQueryListener>( 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 query and update the {@link
069       * ManageSavedQueriesView} view component.
070       *
071       * @see com.sptci.rwt.Queries#delete( String, String )
072       * @see com.sptci.rwt.Queries#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        Queries queries = controller.getQueries();
079        queries.delete( category, name );
080    
081        Component parent = component.getParent();
082        int index = parent.indexOf( component );
083        parent.remove( --index );
084        parent.remove( index );
085    
086        Category cat = queries.getCategory( category );
087        if ( cat.getQueries().isEmpty() )
088        {
089          queries.delete( category );
090          Component p = parent.getParent().getParent();
091          p.remove( parent.getParent() );
092        }
093    
094        controller.resetMenu();
095      }
096    }