components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/swing/glass/BlockingGlassPane.java
changeset 827 0944d8c0158b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.swing.glass;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.awt.event.*;
       
    30 
       
    31 @SuppressWarnings({"serial"})
       
    32 public class BlockingGlassPane extends SimpleGlassPane {
       
    33     //
       
    34     // Static data
       
    35     //
       
    36 
       
    37     public static final KeyListener LISTENER_CONSUME_KEY =
       
    38 	new KeyAdapter() {
       
    39 	    @Override
       
    40 	    public void keyPressed(KeyEvent e) {
       
    41 		e.consume();
       
    42 	    }
       
    43 	};
       
    44 
       
    45     public static final MouseListener LISTENER_CONSUME_MOUSE =
       
    46 	new MouseAdapter() {
       
    47 	    @Override
       
    48 	    public void mousePressed(MouseEvent e) {
       
    49 		e.consume();
       
    50 	    }
       
    51 	};
       
    52 
       
    53     //
       
    54     // Instance data
       
    55     //
       
    56 
       
    57     private Component prevFocusComp;
       
    58     private boolean restoreFocus = true;
       
    59 
       
    60     //
       
    61     // Constructors
       
    62     //
       
    63 
       
    64     public BlockingGlassPane() {
       
    65 	prepareBlockingGlassPane(this);
       
    66 	setFocusCycleRoot(true);
       
    67     }
       
    68 
       
    69 
       
    70     /**
       
    71      * Create an instance of this object.
       
    72      *
       
    73      * @param restoreFocus A value of true indicates component focus should be
       
    74      * restored when invoking {@link #componentHidden}.
       
    75      */
       
    76     public BlockingGlassPane(boolean restoreFocus) {
       
    77 	this();
       
    78 	this.restoreFocus = restoreFocus;
       
    79     }
       
    80 
       
    81     //
       
    82     // SimpleGlassPane methods
       
    83     //
       
    84 
       
    85     @Override
       
    86     public void componentHidden() {
       
    87 	super.componentHidden();
       
    88 	Component comp = prevFocusComp;
       
    89 	if (comp != null) {
       
    90 	    comp.requestFocusInWindow();
       
    91 	}
       
    92     }
       
    93 
       
    94     @Override
       
    95     public void componentShown() {
       
    96 	super.componentShown();
       
    97 	if (restoreFocus) {
       
    98 	    prevFocusComp = KeyboardFocusManager.
       
    99 		getCurrentKeyboardFocusManager().getFocusOwner();
       
   100 	}
       
   101 	requestFocusInWindow();
       
   102     }
       
   103 
       
   104     //
       
   105     // Static methods
       
   106     //
       
   107 
       
   108     public static void addConsumeEventListeners(Component c) {
       
   109 	// Make sure listeners are only added once
       
   110 	removeConsumeEventListeners(c);
       
   111 
       
   112 	c.addKeyListener(LISTENER_CONSUME_KEY);
       
   113 	c.addMouseListener(LISTENER_CONSUME_MOUSE);
       
   114     }
       
   115 
       
   116     public static void prepareBlockingGlassPane(Component glassPane) {
       
   117 	addConsumeEventListeners(glassPane);
       
   118 	glassPane.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
       
   119     }
       
   120 
       
   121     public static void removeConsumeEventListeners(Component c) {
       
   122 	c.removeKeyListener(LISTENER_CONSUME_KEY);
       
   123 	c.removeMouseListener(LISTENER_CONSUME_MOUSE);
       
   124     }
       
   125 }