package example;
//-*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.awt.event.*;
import java.util.Objects;
import javax.swing.*;

public final class MainPanel extends JPanel {
    private final JButton button = new JButton("Stop 5sec");
    public MainPanel() {
        super(new BorderLayout());
        EventQueue.invokeLater(() -> {
            JComponent gp = new LockingGlassPane();
            gp.setVisible(false);
            getRootPane().setGlassPane(gp);
        });

        button.addActionListener(e -> {
            //System.out.println("actionPerformed: " + EventQueue.isDispatchThread());
            getRootPane().getGlassPane().setVisible(true);
            button.setEnabled(false);
            (new Task() {
                @Override public void done() {
                    if (!isDisplayable()) {
                        System.out.println("done: DISPOSE_ON_CLOSE");
                        cancel(true);
                        return;
                    }
                    getRootPane().getGlassPane().setVisible(false);
                    button.setEnabled(true);
                }
            }).execute();
        });
        Box box = Box.createHorizontalBox();
        box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JButton b = new JButton("Button&Mnemonic");
        b.setMnemonic(KeyEvent.VK_B);

        JTextField t = new JTextField("TextField&ToolTip");
        t.setToolTipText("ToolTip");

        box.add(b);
        box.add(Box.createHorizontalStrut(5));
        box.add(t);
        box.add(Box.createHorizontalStrut(5));

        add(box, BorderLayout.NORTH);
        add(button, BorderLayout.SOUTH);
        add(new JScrollPane(new JTree()));
        setPreferredSize(new Dimension(320, 240));
    }

    public static void main(String... args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException
               | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        JFrame frame = new JFrame("@title@");
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        //frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class Task extends SwingWorker<String, Void> {
    @Override public String doInBackground() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        return "Done";
    }
}

class LockingGlassPane extends JPanel {
    @Override public void updateUI() {
        super.updateUI();
        setOpaque(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    }
    @Override public void setVisible(boolean isVisible) {
        boolean oldVisible = isVisible();
        super.setVisible(isVisible);
        JRootPane rootPane = getRootPane();
        if (Objects.nonNull(rootPane) && isVisible() != oldVisible) {
            rootPane.getLayeredPane().setVisible(!isVisible);
        }
    }
    @Override protected void paintComponent(Graphics g) {
        JRootPane rootPane = getRootPane();
        if (Objects.nonNull(rootPane)) {
            // http://weblogs.java.net/blog/alexfromsun/archive/2008/01/disabling_swing.html
            // it is important to call print() instead of paint() here
            // because print() doesn't affect the frame's double buffer
            rootPane.getLayeredPane().print(g);
        }
        super.paintComponent(g);
    }
}

//     class LockingGlassPane_ extends JComponent {
//         public LockingGlassPane_() {
//             setOpaque(false);
//             setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
//                 @Override public boolean accept(Component c) {
//                     return false;
//                 }
//             });
// //             Set<AWTKeyStroke> s = Collections.emptySet();
// //             setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, s);
// //             setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, s);
//             //addKeyListener(new KeyAdapter() {});
//             addMouseListener(new MouseAdapter() {});
//             requestFocusInWindow();
//             super.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
//         }
//         @Override public void setVisible(boolean flag) {
//             super.setVisible(flag);
//             setFocusTraversalPolicyProvider(flag);
//         }
//     }

@@ -152,22 +152,23 @@
 a.util.O
-bjects
+ptional
 ;%0Aimport
@@ -3304,32 +3304,231 @@
 ble(isVisible);%0A
+        Optional.ofNullable(getRootPane())%0A                .filter(rootPane -%3E isVisible() != oldVisible)%0A                .ifPresent(rootPane -%3E rootPane.getLayeredPane().setVisible(!isVisible));%0A// 
         JRootPan
@@ -3547,32 +3547,35 @@
  getRootPane();%0A
+// 
         if (Obje
@@ -3620,32 +3620,35 @@
 = oldVisible) %7B%0A
+// 
             root
@@ -3685,32 +3685,35 @@
 le(!isVisible);%0A
+// 
         %7D%0A    %7D%0A
@@ -3766,24 +3766,129 @@
 aphics g) %7B%0A
+        Optional.ofNullable(getRootPane()).ifPresent(rootPane -%3E rootPane.getLayeredPane().print(g));%0A// 
         JRoo
@@ -3919,24 +3919,26 @@
 Pane();%0A
+//
         
 if (Obje
@@ -3921,32 +3921,33 @@
 ne();%0A//        
+ 
 if (Objects.nonN
@@ -3959,24 +3959,27 @@
 ootPane)) %7B%0A
+// 
             
@@ -4059,16 +4059,18 @@
 ng.html%0A
+//
         
@@ -4073,16 +4073,17 @@
         
+ 
 // it is
@@ -4133,16 +4133,19 @@
 () here%0A
+// 
         
@@ -4208,16 +4208,19 @@
  buffer%0A
+// 
         
@@ -4255,24 +4255,27 @@
 ).print(g);%0A
+// 
         %7D%0A  

use Optional instead of Objects.nonNull(o)