Avoid an exception on closeXcos() in headless mode 06/8706/3
Calixte DENIZET [Tue, 14 Aug 2012 14:51:25 +0000 (16:51 +0200)]
Change-Id: Ic5a48dcd5169d08308a03d598f817e603cdabb0b

scilab/modules/commons/src/java/org/scilab/modules/commons/gui/ScilabKeyStroke.java
scilab/modules/gui/src/java/org/scilab/modules/gui/utils/ClosingOperationsManager.java

index ae02e7c..db2c1df 100644 (file)
 
 package org.scilab.modules.commons.gui;
 
-import java.awt.event.KeyEvent;
+import java.awt.GraphicsEnvironment;
 import java.awt.Toolkit;
+import java.awt.event.KeyEvent;
 
 import javax.swing.KeyStroke;
 
+import org.scilab.modules.commons.OS;
+
 /**
  * Class to handle the keystroke and the default menu shortcut key.
  */
@@ -26,7 +29,19 @@ public final class ScilabKeyStroke {
 
     static {
         String key = "";
-        int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
+        int mask;
+        if (!GraphicsEnvironment.isHeadless()) {
+            mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
+        } else {
+            switch (OS.get()) {
+            case MAC:
+                mask = KeyEvent.META_MASK;
+                break;
+            default:
+                mask = KeyEvent.CTRL_MASK;
+            }
+        }
+
         if ((mask & KeyEvent.CTRL_MASK) != 0) {
             key += " Ctrl";
         }
index b45cede..9631f62 100644 (file)
@@ -12,6 +12,7 @@
 
 package org.scilab.modules.gui.utils;
 
+import java.awt.GraphicsEnvironment;
 import java.awt.event.ActionEvent;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -45,6 +46,7 @@ import org.scilab.modules.commons.xml.XConfiguration;
  *
  * @author Calixte DENIZET
  */
+
 @SuppressWarnings(value = { "serial" })
 public class ClosingOperationsManager {
 
@@ -76,7 +78,7 @@ public class ClosingOperationsManager {
      *            the closing operation
      */
     public static void registerClosingOperation(SwingScilabTab tab,
-            ClosingOperation op) {
+                                                ClosingOperation op) {
         if (tab != null) {
             closingOps.put(tab, op);
         }
@@ -132,20 +134,24 @@ public class ClosingOperationsManager {
      * @return true if the closing operation succeeded
      */
     public static boolean startClosingOperationOnRoot() {
-        if (root != null) {
-            // STD mode
-            SwingScilabWindow win = getWindow(root);
-            if (win == null) {
+        if (!GraphicsEnvironment.isHeadless()) {
+            if (root != null) {
+                // STD mode
+                SwingScilabWindow win = getWindow(root);
+                if (win == null) {
+                    return true;
+                }
+                return startClosingOperation(win, true, true);
+            } else if (deps.get(null).size() != 0) {
+                // NW mode
+                List<SwingScilabTab> list = new ArrayList<SwingScilabTab>();
+                for (SwingScilabTab tab : deps.get(null)) {
+                    collectTabsToClose(tab, list);
+                }
+                return close(list, null, true, true);
+            } else {
                 return true;
             }
-            return startClosingOperation(win, true, true);
-        } else if (deps.get(null).size() != 0) {
-            // NW mode
-            List<SwingScilabTab> list = new ArrayList<SwingScilabTab>();
-            for (SwingScilabTab tab : deps.get(null)) {
-                collectTabsToClose(tab, list);
-            }
-            return close(list, null, true, true);
         } else {
             return true;
         }
@@ -683,10 +689,10 @@ public class ClosingOperationsManager {
             String question = makeQuestion(list);
             final boolean[] checked = new boolean[1];
             final Action action = new AbstractAction() {
-                public void actionPerformed(ActionEvent e) {
-                    checked[0] = ((JCheckBox) e.getSource()).isSelected();
-                }
-            };
+                    public void actionPerformed(ActionEvent e) {
+                        checked[0] = ((JCheckBox) e.getSource()).isSelected();
+                    }
+                };
 
             if (question != null) {
                 if (ScilabModalDialog.show(window, new String[] { question }, EXIT, IconType.WARNING_ICON, ButtonType.YES_NO, DONT_SHOW, action) == AnswerOption.NO_OPTION) {
@@ -750,10 +756,10 @@ public class ClosingOperationsManager {
             }
         }
         switch (apps.size()) {
-            case 0:
-                return null;
-            case 1:
-                return String.format(EXIT_CONFIRM, apps.get(0));
+        case 0:
+            return null;
+        case 1:
+            return String.format(EXIT_CONFIRM, apps.get(0));
         }
 
         String str = apps.remove(0);
@@ -774,7 +780,7 @@ public class ClosingOperationsManager {
      *            the list
      */
     private static final void collectTabsToClose(SwingScilabTab tab,
-            List<SwingScilabTab> list) {
+                                                 List<SwingScilabTab> list) {
         List<SwingScilabTab> children = deps.get(tab);
         if (children != null) {
             for (SwingScilabTab t : children) {