Give focus to previous component after a confirmation dialog raised up 89/13789/2
Calixte DENIZET [Wed, 19 Feb 2014 12:44:11 +0000 (13:44 +0100)]
To test: clc() in the console (press Yes or No, the focus must returned to its previous owner)

Change-Id: I4632c9f06f4e00397eb4de43ccf6aa2aa22a6b01

scilab/modules/gui/src/java/org/scilab/modules/gui/bridge/console/SwingScilabConsole.java
scilab/modules/gui/src/java/org/scilab/modules/gui/utils/ClosingOperationsManager.java

index 742ac42..7a4c73d 100644 (file)
@@ -12,6 +12,7 @@
 
 package org.scilab.modules.gui.bridge.console;
 
+import java.awt.Component;
 import java.awt.Dimension;
 import java.awt.Toolkit;
 import java.awt.datatransfer.Clipboard;
@@ -21,6 +22,7 @@ import java.awt.datatransfer.UnsupportedFlavorException;
 import java.awt.event.ActionEvent;
 import java.awt.event.FocusEvent;
 import java.awt.event.FocusListener;
+import java.awt.KeyboardFocusManager;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.io.IOException;
@@ -370,6 +372,7 @@ public class SwingScilabConsole extends SciConsole implements SimpleConsole {
     public void clear() {
         CheckClearConfirmation ccc = XConfiguration.get(CheckClearConfirmation.class, XConfiguration.getXConfigurationDocument(), CONFIRMATION_PATH)[0];
         if (ccc.checked) {
+            final Component focused = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
             final boolean[] checked = new boolean[1];
             final Action action = new AbstractAction() {
                 public void actionPerformed(ActionEvent e) {
@@ -377,7 +380,16 @@ public class SwingScilabConsole extends SciConsole implements SimpleConsole {
                 }
             };
 
-            if (ScilabModalDialog.show(this, new String[] { CLEAR_CONFIRM }, CLEAR, IconType.WARNING_ICON, ButtonType.YES_NO, DONT_SHOW, action) == AnswerOption.NO_OPTION) {
+            boolean isNo = ScilabModalDialog.show(this, new String[] { CLEAR_CONFIRM }, CLEAR, IconType.WARNING_ICON, ButtonType.YES_NO, DONT_SHOW, action) == AnswerOption.NO_OPTION;
+            if (focused != null) {
+                SwingUtilities.invokeLater(new Runnable() {
+                    public void run() {
+                        focused.requestFocus();
+                    }
+                });
+            }
+
+            if (isNo) {
                 if (checked[0]) {
                     XConfiguration.set(XConfiguration.getXConfigurationDocument(), CONFIRMATION_PATH + "/@state", "unchecked");
                 }
index 6da2f3e..bc5dfd7 100644 (file)
@@ -12,7 +12,9 @@
 
 package org.scilab.modules.gui.utils;
 
+import java.awt.Component;
 import java.awt.GraphicsEnvironment;
+import java.awt.KeyboardFocusManager;
 import java.awt.event.ActionEvent;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -696,10 +698,18 @@ public class ClosingOperationsManager {
             };
 
             if (question != null) {
+                final Component focused = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
                 if (ScilabModalDialog.show(window, new String[] { question }, EXIT, IconType.WARNING_ICON, ButtonType.YES_NO, DONT_SHOW, action) == AnswerOption.NO_OPTION) {
                     if (checked[0]) {
                         XConfiguration.set(XConfiguration.getXConfigurationDocument(), CONFIRMATION_PATH + "/@state", "unchecked");
                     }
+                    if (focused != null) {
+                        SwingUtilities.invokeLater(new Runnable() {
+                            public void run() {
+                                focused.requestFocus();
+                            }
+                        });
+                    }
                     return false;
                 }
             }