Xcos palettes: perform only sanity check on an headless env. 40/8740/1
Clément DAVID [Fri, 17 Aug 2012 16:17:23 +0000 (18:17 +0200)]
Change-Id: I46779500841b5a03ad563821f61ff8c5634fda5c

scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/Palette.java

index f057e27..a160eed 100644 (file)
@@ -15,6 +15,7 @@ package org.scilab.modules.xcos.palette;
 import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.buildCall;
 import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.synchronousScilabExec;
 
+import java.awt.GraphicsEnvironment;
 import java.awt.image.BufferedImage;
 import java.io.File;
 import java.io.IOException;
@@ -140,6 +141,16 @@ public final class Palette {
     @ScilabExported(module = XCOS, filename = PALETTE_GIWS_XML)
     public static void loadPal(final String name, final String[] category) throws JavasciException {
         /*
+         * If the env. is headless only perform fake loading to assert data
+         * integrity.
+         */
+        if (GraphicsEnvironment.isHeadless()) {
+            LOG.warning("Headless environment detected, only perform sanity check");
+            loadPalHeadless(name);
+            return;
+        }
+
+        /*
          * Import the palette
          */
         final ScilabTList data = (ScilabTList) Scilab.getInCurrentScilabSession(name);
@@ -151,50 +162,54 @@ public final class Palette {
             SwingUtilities.invokeAndWait(new Runnable() {
                 @Override
                 public void run() {
-                    /*
-                     * Decode the style part of the palette
-                     */
-                    final mxStylesheet styleSheet = Xcos.getInstance().getStyleSheet();
                     try {
-                        new StyleElement().decode(data, styleSheet);
-                    } catch (final ScicosFormatException e) {
-                        throw new RuntimeException(e);
-                    }
-
-                    // reload all the opened diagram (clear states)
-                    for (final XcosDiagram d : Xcos.getInstance().openedDiagrams()) {
-                        if (d != null) {
-                            final mxGraphView view = d.getView();
-                            if (view != null) {
-                                view.reload();
-                            }
+                        /*
+                         * Decode the style part of the palette
+                         */
+                        final mxStylesheet styleSheet = Xcos.getInstance().getStyleSheet();
+                        try {
+                            new StyleElement().decode(data, styleSheet);
+                        } catch (final ScicosFormatException e) {
+                            throw new RuntimeException(e);
+                        }
 
-                            final mxGraphComponent comp = d.getAsComponent();
-                            if (comp != null) {
-                                comp.refresh();
+                        // reload all the opened diagram (clear states)
+                        for (final XcosDiagram d : Xcos.getInstance().openedDiagrams()) {
+                            if (d != null) {
+                                final mxGraphView view = d.getView();
+                                if (view != null) {
+                                    view.reload();
+                                }
+
+                                final mxGraphComponent comp = d.getAsComponent();
+                                if (comp != null) {
+                                    comp.refresh();
+                                }
                             }
                         }
-                    }
 
-                    final PaletteNode node = getPathNode(category, true);
-                    if (!(node instanceof Category)) {
-                        throw new RuntimeException(String.format(WRONG_INPUT_ARGUMENT_S_INVALID_TREE_PATH, "category"));
-                    }
-                    final Category cat = (Category) node;
+                        final PaletteNode node = getPathNode(category, true);
+                        if (!(node instanceof Category)) {
+                            throw new RuntimeException(String.format(WRONG_INPUT_ARGUMENT_S_INVALID_TREE_PATH, "category"));
+                        }
+                        final Category cat = (Category) node;
+
+                        /*
+                         * Adding the palette tree part of the palette
+                         */
+                        PreLoaded pal;
+                        try {
+                            pal = new PreLoadedElement().decode(data, new PreLoaded.Dynamic());
+                        } catch (final ScicosFormatException e) {
+                            throw new RuntimeException(e);
+                        }
+                        cat.getNode().add(pal);
+                        pal.setParent(cat);
 
-                    /*
-                     * Adding the palette tree part of the palette
-                     */
-                    PreLoaded pal;
-                    try {
-                        pal = new PreLoadedElement().decode(data, new PreLoaded.Dynamic());
-                    } catch (final ScicosFormatException e) {
-                        throw new RuntimeException(e);
+                        PaletteNode.refreshView(pal);
+                    } catch (Exception e) {
+                        e.printStackTrace();
                     }
-                    cat.getNode().add(pal);
-                    pal.setParent(cat);
-
-                    PaletteNode.refreshView(pal);
                 }
             });
         } catch (final InterruptedException e) {
@@ -211,6 +226,21 @@ public final class Palette {
         }
     }
 
+    private static final void loadPalHeadless(final String name) throws JavasciException {
+        try {
+            final ScilabTList data = (ScilabTList) Scilab.getInCurrentScilabSession(name);
+
+            // style check
+            new StyleElement().decode(data, new mxStylesheet());
+
+            // palette data check
+            new PreLoadedElement().decode(data, new PreLoaded.Dynamic());
+
+        } catch (ScicosFormatException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
     /**
      * Load an xcos palette into the palette manager at the root category.
      *