[Schmitzm-commits] r2371 - trunk/schmitzm-core/src/main/java/de/schmitzm/swing

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Jul 26 16:08:41 CEST 2013


Author: mojays
Date: 2013-07-26 16:08:41 +0200 (Fri, 26 Jul 2013)
New Revision: 2371

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ApplicationFrame.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
Log:
SwingUtil.setWaitCursor(): improved by consider keyboard input (e.g. TAB key) and possibility to add KeyListener to react on during wait cursor state

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ApplicationFrame.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ApplicationFrame.java	2013-07-24 12:40:16 UTC (rev 2370)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ApplicationFrame.java	2013-07-26 14:08:41 UTC (rev 2371)
@@ -6,6 +6,7 @@
 import java.awt.Cursor;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.KeyListener;
 import java.awt.event.WindowEvent;
 
 import javax.swing.Action;
@@ -187,16 +188,28 @@
    * Sets the cursor of the main frame to {@link Cursor#WAIT_CURSOR}, to lock the frame.
    */
   public void setWaitCursor() {
-    SwingUtil.setWaitCursor(this);
-}
+    setWaitCursor(null);
+  }
 
   /**
-   * Sets the cursor of the main frame to the default cursor.
+   * Sets the cursor of the main frame to {@link Cursor#WAIT_CURSOR}, to lock the frame.
+   * @param keyListener listener to react on during wait process (e.g. ESC to abort)
    */
+  public void setWaitCursor(KeyListener keyListener) {
+    SwingUtil.setWaitCursor(this, keyListener);
+  }
+
+  /**
+   * Sets the cursor of the main frame to the default cursor. A possible
+   * {@link KeyListener} which was added to the glass pane during
+   * {@link #setWaitCursor(KeyListener)} is automatically removed.
+   */
   public void resetCursor() {
     SwingUtil.resetCursor(this);
   }
 
+
+  
   /**
    * Initializes the actions, menu entries and tool bars.
    */

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2013-07-24 12:40:16 UTC (rev 2370)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2013-07-26 14:08:41 UTC (rev 2371)
@@ -50,6 +50,10 @@
 import java.awt.Window;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.FocusAdapter;
+import java.awt.event.FocusEvent;
+import java.awt.event.FocusListener;
+import java.awt.event.KeyListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseWheelListener;
 import java.awt.font.TextAttribute;
@@ -201,7 +205,24 @@
     /** Dummy MouseAdapter which does nothing. Used for {@link #setWaitCursor(Component)}
      *  and {@link #resetCursor(Component)} */
     private static final MouseAdapter DUMMY_MOUSEADAPTER = new MouseAdapter() {};
-
+    /** Dummy FocusListener which always grabs the focus back to the component
+     *  every time the component has loses the focus. Used for {@link #setWaitCursor(Component)}
+     *  and {@link #resetCursor(Component)} */
+    private static final FocusListener DUMMY_FOCUSLISTENER = new FocusAdapter() {
+      @Override
+      public void focusLost(FocusEvent e) {
+        e.getComponent().requestFocusInWindow();
+//        System.out.println("Got focus back! Haha!");
+      }
+    };
+    /** 
+     * Holds the {@link KeyListener} during wait cursor state. Cache is used
+     * to remove the listener during {@link #resetCursor(Component)}.
+     * @see SwingUtil#setWaitCursor(Component, KeyListener) 
+     * @see SwingUtil#resetCursor(Component) 
+     */
+    private static Map<Component,KeyListener> waitCursorKeyListenerCache = new HashMap<Component,KeyListener>();
+    
     /**
 	 * {@link ResourceProvider}, der die Lokalisation fuer GUI-Komponenten des
 	 * Package {@code schmitzm.swing} zur Verfuegung stellt. Diese sind in
@@ -2763,11 +2784,21 @@
       return headerRendererWithColumnTypes;
     }
     
+    /**
+     * Sets the cursor of the parent frame to {@link Cursor#WAIT_CURSOR}, to lock the frame.
+     * @param comp component to set the wait cursor for
+     */
+    public static void setWaitCursor(Component comp) {
+      setWaitCursor(comp, null);
+    }
 
     /**
      * Sets the cursor of the parent frame to {@link Cursor#WAIT_CURSOR}, to lock the frame.
+     * @param comp component to set the wait cursor for
+     * @param keyListener {@link KeyListener} to react on during wait cursor state (will
+     *                    be added to glass pane)
      */
-    public static void setWaitCursor(Component comp) {
+    public static void setWaitCursor(Component comp, KeyListener keyListener) {
 //    // Original code taken from http://www.javaspecialists.eu/archive/Issue065.html
 //    // applicable for JComponent      
 //    RootPaneContainer root = (RootPaneContainer)((JComponent)getContentPane()).getTopLevelAncestor();
@@ -2779,12 +2810,21 @@
       if ( rootPane != null && rootPane.getContentPane() != null ) {
         rootPane.getGlassPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
         rootPane.getGlassPane().addMouseListener(DUMMY_MOUSEADAPTER);
+        rootPane.getGlassPane().addFocusListener(DUMMY_FOCUSLISTENER);
+        if ( keyListener != null ) {
+          rootPane.getGlassPane().addKeyListener(keyListener);
+          waitCursorKeyListenerCache.put(comp,keyListener);
+        }
         rootPane.getGlassPane().setVisible(true);
+        rootPane.getGlassPane().requestFocusInWindow();
       }
     }
 
     /**
-     * Sets the cursor of the parent frame to the default cursor.
+     * Sets the cursor of the parent frame to the default cursor. A possible
+     * {@link KeyListener} which was added to the glass pane during
+     * {@link #setWaitCursor(Component, KeyListener)} is automatically removed.
+     * @param comp component to reset the wait cursor for
      */
     public static void resetCursor(Component comp) {
 //    // Original code taken from http://www.javaspecialists.eu/archive/Issue065.html
@@ -2797,7 +2837,11 @@
       if ( rootPane != null && rootPane.getContentPane() != null ) {
         rootPane.getGlassPane().setCursor(Cursor.getDefaultCursor());
         rootPane.getGlassPane().removeMouseListener(DUMMY_MOUSEADAPTER);
-        rootPane.getGlassPane().setVisible(false);      }
+        rootPane.getGlassPane().removeFocusListener(DUMMY_FOCUSLISTENER);
+        KeyListener keyListener = waitCursorKeyListenerCache.remove(comp);
+        rootPane.getGlassPane().removeKeyListener(keyListener); // no problem if listener is NULL!
+        rootPane.getGlassPane().setVisible(false);
+      }
     }
 
     /**



More information about the Schmitzm-commits mailing list