psmith      2003/08/07 00:56:04

  Modified:    src/java/org/apache/log4j/chainsaw LogUI.java
  Log:
  Added infrastructure for ShutdownListener and the ability
  to register a different Shutdown Action when
  a shutdown is requested.  By default, Chainsaw
  exits the VM, but the developer may choose to override
  this.
  
  Revision  Changes    Path
  1.7       +78 -2     jakarta-log4j/src/java/org/apache/log4j/chainsaw/LogUI.java
  
  Index: LogUI.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/LogUI.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- LogUI.java        22 Jul 2003 03:07:47 -0000      1.6
  +++ LogUI.java        7 Aug 2003 07:56:04 -0000       1.7
  @@ -148,6 +148,7 @@
   import javax.swing.event.ChangeListener;
   import javax.swing.event.DocumentEvent;
   import javax.swing.event.DocumentListener;
  +import javax.swing.event.EventListenerList;
   import javax.swing.event.ListSelectionEvent;
   import javax.swing.event.ListSelectionListener;
   import javax.swing.event.TableColumnModelEvent;
  @@ -226,6 +227,26 @@
      */
     private boolean isGUIFullyInitialized = false;
     private Object initializationLock = new Object();
  +  
  +  /**
  +   * The shutdownAction is called when the user requests to exit
  +   * Chainsaw, and by default this exits the VM, but
  +   * a developer may replace this action with something that better suits
  +   * their needs
  +   */
  +  private Action shutdownAction = new AbstractAction() {
  +
  +    public void actionPerformed(ActionEvent e)
  +    {
  +      System.exit(0);
  +    }
  +  };
  +  
  +  /**
  +   * Clients can register a ShutdownListener to be notified
  +   * when the user has requested Chainsaw to exit.
  +   */
  +  private EventListenerList shutdownListenerList = new EventListenerList();
   
     /**
      * Constructor which builds up all the visual elements of the frame
  @@ -258,6 +279,27 @@
     }
   
     /**
  +   * Registers a ShutdownListener with this calss so that
  +   * it can be notified when the user has requested 
  +   * that Chainsaw exit.
  +   * 
  +   * @param l
  +   */
  +  public void addShutdownListener(ShutdownListener l) {
  +    shutdownListenerList.add(ShutdownListener.class, l);  
  +  }
  +  
  +  /**
  +   * Removes the registered ShutdownListener so 
  +   * that the listener will not be notified on a shutdown.
  +   * 
  +   * @param l
  +   */
  +  public void removeShutdownListener(ShutdownListener l) {
  +    shutdownListenerList.remove(ShutdownListener.class, l);
  +  }
  +  
  +  /**
      * Starts Chainsaw by attaching a new instance to the Log4J
      * main root Logger via a ChainsawAppender, and activates itself
      * @param args
  @@ -760,12 +802,46 @@
             } catch (Exception e) {
               e.printStackTrace();
             }
  -
  -          System.exit(0);
  +          fireShutdownEvent();
  +          performShutdownAction();
           }
  +
         };
   
       new Thread(runnable).start();
  +  }
  +
  +  /**
  +   * Ensures all the registered ShutdownListeners are notified.
  +   */
  +  private void fireShutdownEvent()
  +  {
  +    ShutdownListener[] listeners = (ShutdownListener[]) 
shutdownListenerList.getListeners(ShutdownListener.class);
  +    for (int i = 0; i < listeners.length; i++) {
  +      listeners[i].shuttingDown();
  +    }
  +  }
  +
  +  /**
  +   * Configures LogUI's with an action to execute when the user
  +   * requests to exit the application, the default action
  +   * is to exit the VM.
  +   * This Action is called AFTER all the ShutdownListeners have been notified
  +   * 
  +   * @param shutdownAction
  +   */
  +  public final void setShutdownAction(Action shutdownAction){
  +    this.shutdownAction = shutdownAction;
  +  }
  +
  +  /**
  +   * Using the current thread, calls the registed Shutdown action's
  +   * actionPerformed(...) method.
  +   *
  +   */
  +  private void performShutdownAction(){
  +    LogLog.debug("Calling the shutdown Action. Goodbye!");
  +    shutdownAction.actionPerformed(new ActionEvent(this, 
ActionEvent.ACTION_PERFORMED, "Shutting Down"));          
     }
   
     /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to