We were not processing AWT WindowEvents at all, because
eventTypeEnabled() wasn't implemented for java.awt.Window. This caused
Swing windows not beeing able to be closed, because the event handling
methods in Swing's JFrame never received a message that a window is
about to beeing closed.

2006-10-18  Roman Kennke  <[EMAIL PROTECTED]>

        PR 29448
        * java/awt/Window.java
        (eventTypeEnabled): Overridden to handle WindowEvents.
        (processEvent): Switch between processWindowEvent(),
        processWindowFocusEvent() and processWindowStateEvent() here,
        rather than simply calling processWindowEvent().
        (processWindowEvent): Only dispatch event to listener, do not
        switch to processWindowFocusEvent() or processWindowStateEvent()
        here.
        * javax/swing/JFrame.java
        (frameInit): Explicitly enable window and key events here.
        (processWindowEvent): Throw out some unnecessary code.
        * javax/swing/JWindow.java
        (windowInit): Explicitly enable key events here.
        * javax/swing/JDialog.java
        (close_action): Renamed to closeAction.
        (dialogInit): Explicitly enable window events here.
        (getDefaultCloseOperation): Renamed close_action to closeAction.
        (processWindowEvent): Throw out some unnecessary code.
        Renamed close_action to closeAction.
        (setDefaultCloseOperation): Renamed close_action to closeAction.


/Roman

Index: java/awt/Window.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/awt/Window.java,v
retrieving revision 1.75
diff -u -1 -5 -r1.75 Window.java
--- java/awt/Window.java	20 Sep 2006 12:35:41 -0000	1.75
+++ java/awt/Window.java	18 Oct 2006 09:17:44 -0000
@@ -606,93 +606,95 @@
       }
     super.dispatchEventImpl(e);
   }
 
   /**
    * Processes the specified event for this window.  If the event is an
    * instance of <code>WindowEvent</code>, then
    * <code>processWindowEvent()</code> is called to process the event,
    * otherwise the superclass version of this method is invoked.
    *
    * @param evt The event to process.
    */
   protected void processEvent(AWTEvent evt)
   {
     if (evt instanceof WindowEvent)
-      processWindowEvent((WindowEvent) evt);
+      {
+        WindowEvent we = (WindowEvent) evt;
+        switch (evt.getID())
+          {
+          case WindowEvent.WINDOW_OPENED:
+          case WindowEvent.WINDOW_CLOSED:
+          case WindowEvent.WINDOW_CLOSING:
+          case WindowEvent.WINDOW_ICONIFIED:
+          case WindowEvent.WINDOW_DEICONIFIED:
+          case WindowEvent.WINDOW_ACTIVATED:
+          case WindowEvent.WINDOW_DEACTIVATED:
+            processWindowEvent(we);
+            break;
+          case WindowEvent.WINDOW_GAINED_FOCUS:
+          case WindowEvent.WINDOW_LOST_FOCUS:
+            processWindowFocusEvent(we);
+            break;
+          case WindowEvent.WINDOW_STATE_CHANGED:
+            processWindowStateEvent(we);
+            break;
+          }
+      }
     else
       super.processEvent(evt);
   }
 
   /**
    * Dispatches this event to any listeners that are listening for
    * <code>WindowEvents</code> on this window.  This method only gets
    * invoked if it is enabled via <code>enableEvents()</code> or if
    * a listener has been added.
    *
    * @param evt The event to process.
    */
   protected void processWindowEvent(WindowEvent evt)
   {
-    int id = evt.getID();
-
-    if (id == WindowEvent.WINDOW_GAINED_FOCUS
-	|| id == WindowEvent.WINDOW_LOST_FOCUS)
-      processWindowFocusEvent (evt);
-    else if (id == WindowEvent.WINDOW_STATE_CHANGED)
-      processWindowStateEvent (evt);
-    else
+    if (windowListener != null)
       {
-	if (windowListener != null)
-	  {
-	    switch (evt.getID())
-	      {
-	      case WindowEvent.WINDOW_ACTIVATED:
-		windowListener.windowActivated(evt);
-		break;
-
-	      case WindowEvent.WINDOW_CLOSED:
-		windowListener.windowClosed(evt);
-		break;
-
-	      case WindowEvent.WINDOW_CLOSING:
-		windowListener.windowClosing(evt);
-		break;
-
-	      case WindowEvent.WINDOW_DEACTIVATED:
-		windowListener.windowDeactivated(evt);
-		break;
-
-	      case WindowEvent.WINDOW_DEICONIFIED:
-		windowListener.windowDeiconified(evt);
-		break;
-
-	      case WindowEvent.WINDOW_ICONIFIED:
-		windowListener.windowIconified(evt);
-		break;
-
-	      case WindowEvent.WINDOW_OPENED:
-		windowListener.windowOpened(evt);
-		break;
-
-	      default:
-		break;
-	      }
-	  }
+        switch (evt.getID())
+          {
+          case WindowEvent.WINDOW_ACTIVATED:
+            windowListener.windowActivated(evt);
+            break;
+          case WindowEvent.WINDOW_CLOSED:
+            windowListener.windowClosed(evt);
+            break;
+          case WindowEvent.WINDOW_CLOSING:
+            windowListener.windowClosing(evt);
+            break;
+          case WindowEvent.WINDOW_DEACTIVATED:
+            windowListener.windowDeactivated(evt);
+            break;
+          case WindowEvent.WINDOW_DEICONIFIED:
+            windowListener.windowDeiconified(evt);
+            break;
+          case WindowEvent.WINDOW_ICONIFIED:
+            windowListener.windowIconified(evt);
+            break;
+          case WindowEvent.WINDOW_OPENED:
+            windowListener.windowOpened(evt);
+            break;
+          }
       }
   }
-  
+
   /**
    * Identifies if this window is active.  The active window is a Frame or
    * Dialog that has focus or owns the active window.
    *  
    * @return true if active, else false.
    * @since 1.4
    */
   public boolean isActive()
   {
     KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager ();
     return manager.getActiveWindow() == this;
   }
 
   /**
    * Identifies if this window is focused.  A window is focused if it is the
@@ -1213,20 +1215,56 @@
       ( (WindowPeer) peer).updateAlwaysOnTop();
     else
       System.out.println("Null peer?!");
   }
 
   /**
    * Generate a unique name for this window.
    *
    * @return A unique name for this window.
    */
   String generateName()
   {
     return "win" + getUniqueLong();
   }
 
+  /**
+   * Overridden to handle WindowEvents.
+   *
+   * @return <code>true</code> when the specified event type is enabled,
+   *         <code>false</code> otherwise
+   */
+  boolean eventTypeEnabled(int type)
+  {
+    boolean enabled = false;
+    switch (type)
+      {
+        case WindowEvent.WINDOW_OPENED:
+        case WindowEvent.WINDOW_CLOSED:
+        case WindowEvent.WINDOW_CLOSING:
+        case WindowEvent.WINDOW_ICONIFIED:
+        case WindowEvent.WINDOW_DEICONIFIED:
+        case WindowEvent.WINDOW_ACTIVATED:
+        case WindowEvent.WINDOW_DEACTIVATED:
+          enabled = ((eventMask & AWTEvent.WINDOW_EVENT_MASK) != 0)
+                    || windowListener != null;
+          break;
+        case WindowEvent.WINDOW_GAINED_FOCUS:
+        case WindowEvent.WINDOW_LOST_FOCUS:
+          enabled = ((eventMask & AWTEvent.WINDOW_FOCUS_EVENT_MASK) != 0)
+                    || windowFocusListener != null;
+          break;
+        case WindowEvent.WINDOW_STATE_CHANGED:
+          enabled = ((eventMask & AWTEvent.WINDOW_STATE_EVENT_MASK) != 0)
+                    || windowStateListener != null;
+          break;
+        default:
+          enabled = super.eventTypeEnabled(type);
+      }
+    return enabled;
+  }
+
   private static synchronized long getUniqueLong()
   {
     return next_window_number++;
   }
 }
Index: javax/swing/JFrame.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JFrame.java,v
retrieving revision 1.36
diff -u -1 -5 -r1.36 JFrame.java
--- javax/swing/JFrame.java	5 Jun 2006 20:36:22 -0000	1.36
+++ javax/swing/JFrame.java	18 Oct 2006 09:17:44 -0000
@@ -145,30 +145,34 @@
    *
    * @param title the title for the new <code>JFrame</code>
    * @param gc the <code>GraphicsConfiguration</code> that is used for
    *     the new <code>JFrame</code>
    *
    * @see Frame#Frame(String, GraphicsConfiguration)
    */
   public JFrame(String title, GraphicsConfiguration gc)
   {
     super(title, gc);
     frameInit();
   }
 
   protected void frameInit()
   {
+    // We need to explicitly enable events here so that our processKeyEvent()
+    // and processWindowEvent() gets called.
+    enableEvents(AWTEvent.WINDOW_EVENT_MASK | AWTEvent.KEY_EVENT_MASK);
+
     super.setLayout(new BorderLayout());
     setBackground(UIManager.getDefaults().getColor("control"));
     enableEvents(AWTEvent.WINDOW_EVENT_MASK);
     getRootPane(); // will do set/create
 
     // Setup the defaultLookAndFeelDecoration if requested.
     if (isDefaultLookAndFeelDecorated()
         && UIManager.getLookAndFeel().getSupportsWindowDecorations())
       {
         setUndecorated(true);
         getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
       }
 
     // We're now done the init stage.
     setRootPaneCheckingEnabled(true);
@@ -339,63 +343,46 @@
   {
     StringBuffer sb = new StringBuffer(super.paramString());
     sb.append(",defaultCloseOperation=");
     sb.append(SwingUtilities.convertWindowConstantToString(
         getDefaultCloseOperation()));
     sb.append(",rootPane=");
     if (rootPane != null)
       sb.append(rootPane);
     sb.append(",rootPaneCheckingEnabled=").append(rootPaneCheckingEnabled);
     return sb.toString();
   }
 
   protected void processWindowEvent(WindowEvent e)
   {
     super.processWindowEvent(e);
-    switch (e.getID())
+    if (e.getID() == WindowEvent.WINDOW_CLOSING)
       {
-      case WindowEvent.WINDOW_CLOSING:
-        {
-	  switch (closeAction)
-	    {
-	    case EXIT_ON_CLOSE:
-	      {
-		System.exit(0);
-		break;
-	      }
-	    case DISPOSE_ON_CLOSE:
-	      {
-		dispose();
-		break;
-	      }
-	    case HIDE_ON_CLOSE:
-	      {
-		setVisible(false);
-		break;
-	      }
-	    case DO_NOTHING_ON_CLOSE:
-	      break;
-	    }
-	  break;
-        }
-      case WindowEvent.WINDOW_CLOSED:
-      case WindowEvent.WINDOW_OPENED:
-      case WindowEvent.WINDOW_ICONIFIED:
-      case WindowEvent.WINDOW_DEICONIFIED:
-      case WindowEvent.WINDOW_ACTIVATED:
-      case WindowEvent.WINDOW_DEACTIVATED:
-	break;
+        switch (closeAction)
+	  {
+	  case EXIT_ON_CLOSE:
+	    System.exit(0);
+	    break;
+	  case DISPOSE_ON_CLOSE:
+	    dispose();
+	    break;
+	  case HIDE_ON_CLOSE:
+	    setVisible(false);
+	    break;
+	  case DO_NOTHING_ON_CLOSE:
+	    break;
+	  }
       }
   }
 
   /**
    * Sets the default operation that is performed when this frame is closed.
    * The default is <code>HIDE_ON_CLOSE</code>.  When 
    * <code>EXIT_ON_CLOSE</code> is specified this method calls
    * <code>SecurityManager.checkExit(0)</code> which might throw a
    * <code>SecurityException</code>.
    * 
    * @param operation  a code for the operation (one of: 
    *     [EMAIL PROTECTED] WindowConstants#DO_NOTHING_ON_CLOSE}, 
    *     [EMAIL PROTECTED] WindowConstants#HIDE_ON_CLOSE}, 
    *     [EMAIL PROTECTED] WindowConstants#DISPOSE_ON_CLOSE} and 
    *     [EMAIL PROTECTED] WindowConstants#EXIT_ON_CLOSE}).
Index: javax/swing/JDialog.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JDialog.java,v
retrieving revision 1.22
diff -u -1 -5 -r1.22 JDialog.java
--- javax/swing/JDialog.java	27 Jan 2006 10:10:00 -0000	1.22
+++ javax/swing/JDialog.java	18 Oct 2006 09:17:44 -0000
@@ -26,30 +26,31 @@
 As a special exception, the copyright holders of this library give you
 permission to link this library with independent modules to produce an
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
 
 package javax.swing;
 
+import java.awt.AWTEvent;
 import java.awt.Component;
 import java.awt.Container;
 import java.awt.Dialog;
 import java.awt.Dimension;
 import java.awt.Frame;
 import java.awt.Graphics;
 import java.awt.GraphicsConfiguration;
 import java.awt.IllegalComponentStateException;
 import java.awt.LayoutManager;
 import java.awt.event.WindowEvent;
 
 import javax.accessibility.Accessible;
 import javax.accessibility.AccessibleContext;
 
 /**
@@ -85,31 +86,31 @@
 
   /** DOCUMENT ME! */
   protected AccessibleContext accessibleContext;
 
   /** The single RootPane in the Dialog. */
   protected JRootPane rootPane;
 
   /**
    * Whether checking is enabled on the RootPane.
    *
    * @specnote Should be false to comply with J2SE 5.0
    */ 
   protected boolean rootPaneCheckingEnabled = false;
 
   /** The default action taken when closed. */
-  private int close_action = HIDE_ON_CLOSE;
+  private int closeAction = HIDE_ON_CLOSE;
   
   /** Whether JDialogs are decorated by the Look and Feel. */
   private static boolean decorated;
 
   /* Creates a new non-modal JDialog with no title 
    * using a shared Frame as the owner.
    */
   public JDialog()
   {
     this((Frame) SwingUtilities.getOwnerFrame(null), "", false, null);
   }
 
   /**
    * Creates a new non-modal JDialog with no title
    * using the given owner.
@@ -233,30 +234,34 @@
    */
   public JDialog(Frame owner, String title, boolean modal,
                  GraphicsConfiguration gc)
   {
     super((Frame) SwingUtilities.getOwnerFrame(owner), title, modal, gc);
     dialogInit();
   }
 
   /**
    * This method is called to initialize the 
    * JDialog. It sets the layout used, the locale, 
    * and creates the RootPane.
    */
   protected void dialogInit()
   {
+    // We need to explicitly enable events here so that our processKeyEvent()
+    // and processWindowEvent() gets called.
+    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
+
     // FIXME: Do a check on GraphicsEnvironment.isHeadless()
     setLocale(JComponent.getDefaultLocale());
     getRootPane(); // Will do set/create.
     invalidate();
     // Now that initStageDone is true, adds and layouts apply to contentPane,
     // not top-level.
     setRootPaneCheckingEnabled(true);
   }
 
   /**
    * This method returns whether JDialogs will have their
    * window decorations provided by the Look and Feel.
    *
    * @return Whether the window decorations are Look and Feel provided.
    */
@@ -495,89 +500,75 @@
    */
   public void update(Graphics g)
   {
     paint(g);
   }
   
   
   /**
    * This method handles window events. This allows the JDialog
    * to honour its default close operation.
    *
    * @param e The WindowEvent.
    */
   protected void processWindowEvent(WindowEvent e)
   {
-    //	System.out.println("PROCESS_WIN_EV-1: " + e);
     super.processWindowEvent(e);
-    //	System.out.println("PROCESS_WIN_EV-2: " + e);
-    switch (e.getID())
+    if (e.getID() == WindowEvent.WINDOW_CLOSING)
       {
-      case WindowEvent.WINDOW_CLOSING:
-        {
-	  switch (getDefaultCloseOperation())
-	    {
-	    case DISPOSE_ON_CLOSE:
-	      {
-		dispose();
-		break;
-	      }
-	    case HIDE_ON_CLOSE:
-	      {
-		setVisible(false);
-		break;
-	      }
-	    case DO_NOTHING_ON_CLOSE:
-	      break;
-	    }
-	  break;
-        }
-      case WindowEvent.WINDOW_CLOSED:
-      case WindowEvent.WINDOW_OPENED:
-      case WindowEvent.WINDOW_ICONIFIED:
-      case WindowEvent.WINDOW_DEICONIFIED:
-      case WindowEvent.WINDOW_ACTIVATED:
-      case WindowEvent.WINDOW_DEACTIVATED:
-	break;
+        switch (closeAction)
+          {
+          case EXIT_ON_CLOSE:
+            System.exit(0);
+            break;
+          case DISPOSE_ON_CLOSE:
+            dispose();
+            break;
+          case HIDE_ON_CLOSE:
+            setVisible(false);
+            break;
+          case DO_NOTHING_ON_CLOSE:
+            break;
+          }
       }
   }
 
   /**
    * This method sets the action to take
    * when the JDialog is closed.
    *
    * @param operation The action to take.
    */
   public void setDefaultCloseOperation(int operation)
   {
     /* Reference implementation allows invalid operations
        to be specified.  If so, getDefaultCloseOperation
        must return the invalid code, and the behaviour
        defaults to DO_NOTHING_ON_CLOSE.  processWindowEvent
        above handles this */
-    close_action = operation;
+    closeAction = operation;
   }
 
   /**
    * This method returns the action taken when
    * the JDialog is closed.
    *
    * @return The action to take.
    */
   public int getDefaultCloseOperation()
   {
-    return close_action;
+    return closeAction;
   }
 
   /**
    * This method returns a String describing the JDialog.
    *
    * @return A String describing the JDialog.
    */
   protected String paramString()
   {
     return "JDialog";
   }
 
   /**
    * DOCUMENT ME!
    *
Index: javax/swing/JWindow.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JWindow.java,v
retrieving revision 1.26
diff -u -1 -5 -r1.26 JWindow.java
--- javax/swing/JWindow.java	27 Jan 2006 10:10:00 -0000	1.26
+++ javax/swing/JWindow.java	18 Oct 2006 09:17:44 -0000
@@ -26,30 +26,31 @@
 As a special exception, the copyright holders of this library give you
 permission to link this library with independent modules to produce an
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
 
 package javax.swing;
 
+import java.awt.AWTEvent;
 import java.awt.BorderLayout;
 import java.awt.Component;
 import java.awt.Container;
 import java.awt.Dimension;
 import java.awt.Frame;
 import java.awt.Graphics;
 import java.awt.GraphicsConfiguration;
 import java.awt.LayoutManager;
 import java.awt.Window;
 import java.awt.event.KeyEvent;
 
 import javax.accessibility.Accessible;
 import javax.accessibility.AccessibleContext;
 
 /**
@@ -146,30 +147,34 @@
    * The <code>gc</code> parameter can be used to open the window on a
    * different screen for example.
    *
    * @param owner the owner window of this window; if <code>null</code> a
    *        shared invisible owner frame is used
    * @param gc the graphics configuration to use
    */
   public JWindow(Window owner, GraphicsConfiguration gc)
   {
     super(SwingUtilities.getOwnerFrame(owner), gc);
     windowInit();
   }
 
   protected void windowInit()
   {
+    // We need to explicitly enable events here so that our processKeyEvent()
+    // and processWindowEvent() gets called.
+    enableEvents(AWTEvent.KEY_EVENT_MASK);
+
     super.setLayout(new BorderLayout(1, 1));
     getRootPane(); // will do set/create
     // Now we're done init stage, adds and layouts go to content pane.
     setRootPaneCheckingEnabled(true);
   }
 
   public Dimension getPreferredSize()
   {
     return super.getPreferredSize();
   }
 
   public void setLayout(LayoutManager manager)
   {
     // Check if we're in initialization stage.  If so, call super.setLayout
     // otherwise, valid calls go to the content pane.

Reply via email to