2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2007-2008 - INRIA - Allan CORNET
4 * Copyright (C) 2007-2008 - INRIA - Vincent COUVERT
5 * Copyright (C) 2007-2008 - INRIA - Sylvestre LEDRU
6 * Copyright (C) 2007-2008 - INRIA - Jean-Baptiste SILVY
7 * Copyright (C) 2007-2008 - INRIA - Bruno JOFRET
8 * Copyright (C) 2011 - Calixte DENIZET
10 * Copyright (C) 2012 - 2016 - Scilab Enterprises
12 * This file is hereby licensed under the terms of the GNU GPL v2.0,
13 * pursuant to article 5.3.4 of the CeCILL v.2.1.
14 * This file was originally licensed under the terms of the CeCILL v2.1,
15 * and continues to be available under such terms.
16 * For more information, see the COPYING file which you should have received
17 * along with this program.
21 package org.scilab.modules.core;
23 import java.awt.GraphicsEnvironment;
24 import java.awt.Toolkit;
25 import java.util.ArrayList;
26 import java.util.List;
28 import javax.swing.JPopupMenu;
29 import javax.swing.SwingUtilities;
31 import org.flexdock.docking.DockingConstants;
32 import org.flexdock.docking.DockingManager;
33 import org.scilab.modules.commons.OS;
34 import org.scilab.modules.commons.ScilabCommons;
35 import org.scilab.modules.commons.ScilabCommonsUtils;
36 import org.scilab.modules.commons.ScilabConstants;
37 import org.scilab.modules.commons.xml.XConfiguration;
38 import org.scilab.modules.graphic_objects.graphicController.GraphicController;
39 import org.scilab.modules.graphic_objects.graphicObject.GraphicObject.Type;
40 import org.scilab.modules.graphic_objects.utils.MenuBarBuilder;
41 import org.scilab.modules.gui.SwingView;
42 import org.scilab.modules.gui.bridge.console.SwingScilabConsole;
43 import org.scilab.modules.gui.bridge.tab.SwingScilabDockablePanel;
44 import org.scilab.modules.gui.bridge.window.SwingScilabWindow;
45 import org.scilab.modules.gui.console.ScilabConsole;
46 import org.scilab.modules.gui.tabfactory.ScilabTabFactory;
47 import org.scilab.modules.gui.utils.ClosingOperationsManager;
48 import org.scilab.modules.gui.utils.ConfigManager;
49 import org.scilab.modules.gui.utils.LookAndFeelManager;
50 import org.scilab.modules.gui.utils.WindowsConfigurationManager;
53 * Main Class for Scilab
54 * @author Allan CORNET
55 * @author Jean-Baptiste SILVY
56 * @author Vincent COUVERT
57 * @author Sylvestre Ledru
58 * @author Bruno JOFRET
59 * @author Calixte DENIZET
64 System.setProperty("java.protocol.handler.pkgs", "org.scilab.modules.commons");
65 XConfiguration.getXConfigurationDocument();
68 /** Index of windows vista version */
69 private static final double VISTA_VERSION = 6.0;
71 private static final String ENABLE_JAVA2D_OPENGL_PIPELINE = "sun.java2d.opengl";
72 private static final String ENABLE = "true";
73 private static final String DISABLE = "false";
74 private static final String DISABLE_DDRAW = "sun.java2d.noddraw";
75 private static final String OSNAME = "os.name";
76 private static final String MACOS = "mac";
78 private static boolean success;
79 private static boolean finish;
80 private static boolean exitCalled;
81 private static int mode;
83 private static List<Runnable> finalhooks = new ArrayList<Runnable>();
84 private static List<Runnable> initialhooks = new ArrayList<Runnable>();
87 * WARNING : mainView is directly referenced from a JNI even it's private.
88 * TODO : Better add getter for this variable.
90 private SwingScilabWindow mainView;
93 * Constructor Scilab Class.
94 * @param mode Mode Scilab -NW -NWNI -STD -API
96 public Scilab(int mode) {
98 ScilabConstants.setMode(mode);
100 DockingManager.setDockableFactory(ScilabTabFactory.getInstance());
103 * Set Scilab directory. Note that it is done in the constructor
104 * and not as directly when setting the member because we had some
105 * race condition. See bug #4419
109 * Set Java directories to Scilab ones
112 /* only modify these properties if Scilab is not called by another application */
113 /* In this case, we let the calling application to use its own properties */
114 System.setProperty("java.io.tmpdir", ScilabConstants.TMPDIR.getCanonicalPath());
115 System.setProperty("user.home", ScilabConstants.SCIHOME.getCanonicalPath());
117 /* build a standard User-Agent string */
118 StringBuilder str = new StringBuilder();
119 str.append("Scilab/").append(ScilabCommons.getScilabVersionMajor()).append('.').append(ScilabCommons.getScilabVersionMinor()).append('.').append(ScilabCommons.getScilabVersionMaintenance());
121 str.append('(').append(System.getProperty("os.name")).append(' ').append(System.getProperty("os.arch")).append(')');
122 System.setProperty("http.agent", str.toString());
125 } catch (Exception e) {
126 System.err.println("Cannot retrieve the variable SCI. Please report on http://bugzilla.scilab.org/");
127 System.err.println(e.getLocalizedMessage());
132 * Set options for JOGL
133 * they must be set before creating GUI
138 SwingView.registerSwingView();
143 * bug 3673 by default in API mode we dont modify look n feel
144 * If SCI_JAVA_ENABLE_HEADLESS is set, do not set the look and feel.
145 * (needed when building the documentation under *ux)
147 if (mode != 1 && System.getenv("SCI_JAVA_ENABLE_HEADLESS") == null) {
148 /* http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/plaf.html */
151 String scilabLookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";
153 if (OS.get() == OS.WINDOWS) {
154 scilabLookAndFeel = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
155 } else if (System.getProperty(OSNAME).toLowerCase().indexOf(MACOS) != -1) {
156 /** OPTION ADDED TO ALLOW DOCKING UNDER MACOSX */
157 System.setProperty(DockingConstants.HEAVYWEIGHT_DOCKABLES, ENABLE);
158 scilabLookAndFeel = "apple.laf.AquaLookAndFeel";
160 scilabLookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
163 * Linux specific desktop integration
165 if (!GraphicsEnvironment.isHeadless()) {
167 Toolkit xToolkit = Toolkit.getDefaultToolkit();
168 java.lang.reflect.Field awtAppClassNameField =
169 xToolkit.getClass().getDeclaredField("awtAppClassName");
170 awtAppClassNameField.setAccessible(true);
172 awtAppClassNameField.set(xToolkit, "Scilab");
173 } catch (Exception e) {
174 System.err.println("Unable to set WM_CLASS, please report a bug on http://bugzilla.scilab.org/.");
175 System.err.println("Error: " + e.getLocalizedMessage());
180 /* Init the LookAndFeelManager all the time since we can
181 * create windows in the NW mode */
182 if (!GraphicsEnvironment.isHeadless()) {
183 LookAndFeelManager lookAndFeel = new LookAndFeelManager();
185 if (lookAndFeel.isSupportedLookAndFeel(scilabLookAndFeel)) {
186 lookAndFeel.setLookAndFeel(scilabLookAndFeel);
188 lookAndFeel.setSystemLookAndFeel();
191 } catch (java.lang.NoClassDefFoundError exception) {
192 System.err.println("Could not initialize graphics Environment");
193 System.err.println("Scilab Graphical option may not be working correctly.");
194 System.err.println("An error occurred: " + exception.getLocalizedMessage());
198 if (mode == 2) { /* Mode GUI */
199 // Create a user config file if not already exists
200 ConfigManager.createUserCopy();
202 Integer consoleId = GraphicController.getController().askObject(Type.JAVACONSOLE);
203 MenuBarBuilder.buildConsoleMenuBar(consoleId);
205 SwingScilabConsole sciConsole = ((SwingScilabConsole) ScilabConsole.getConsole().getAsSimpleConsole());
206 SwingScilabDockablePanel consoleTab = (SwingScilabDockablePanel) sciConsole.getParent();
208 WindowsConfigurationManager.restorationFinished(consoleTab);
210 mainView = SwingScilabWindow.allScilabWindows.get(consoleTab.getParentWindowId());
212 GraphicController.getController().askObject(Type.CONSOLE);
217 * @return the current mode
219 public static int getMode() {
224 * Sets the prompt displayed in Scilab console
225 * @param prompt the prompt to be displayed as a String
227 public void setPrompt(String prompt) {
228 ScilabConsole.getConsole().setPrompt(prompt);
232 * Set the command line flags to the JVM
234 public static void setJOGLFlags() {
236 // Must give popUpMenu heavy weight
237 // in case we enable GLCanvas
238 JPopupMenu.setDefaultLightWeightPopupEnabled(false);
239 // Uneash OpenGL power
241 //System.setProperty(ENABLE_JAVA2D_OPENGL_PIPELINE, ENABLE_WITH_DEBUG);
242 System.setProperty(ENABLE_JAVA2D_OPENGL_PIPELINE, DISABLE);
244 if (OS.get() == OS.WINDOWS) {
245 if ((Double) OS.get().getVersion() >= VISTA_VERSION) {
246 // don't enable OpenGL because of aero
247 System.setProperty(ENABLE_JAVA2D_OPENGL_PIPELINE, DISABLE);
249 // desactivate direct3d and direct draw under windows
250 System.setProperty(DISABLE_DDRAW, ENABLE);
255 * Call from canCloseMainScilabObject (call itself from sci_exit)
256 * @return true if the console is closed
258 public static boolean canClose() {
259 final Object lock = new Object();
261 SwingUtilities.invokeLater(new Runnable() {
265 success = ClosingOperationsManager.startClosingOperationOnRoot();
269 synchronized (lock) {
276 synchronized (lock) {
281 } catch (InterruptedException e) {
282 System.err.println(e);
290 * Call from forceCloseMainScilabObject (call itself from sci_exit)
292 public static void forceClose() {
293 final Object lock = new Object();
295 SwingUtilities.invokeLater(new Runnable() {
299 ClosingOperationsManager.forceClosingOperationOnRoot();
303 synchronized (lock) {
310 synchronized (lock) {
315 } catch (InterruptedException e) {
316 System.err.println(e);
322 * @return true if exit has been called from command line
324 public static boolean getExitCalled() {
329 * Register a hook to execute just before the JVM shutdown.
330 * A hook should not contain threads, there is no warranty that they will be fully executed.
332 public static void registerFinalHook(Runnable hook) {
333 finalhooks.add(hook);
339 public static void removeFinalHook(Runnable hook) {
340 finalhooks.remove(hook);
344 * This method should be called from jni (finishMainScilabObject())
346 public static void executeFinalHooks() {
347 for (Runnable hook : finalhooks) {
353 * Register a hook to execute after the Scilab initialization.
354 * A hook should not contain threads, there is no warranty that they will be fully executed.
356 public static void registerInitialHook(Runnable hook) {
357 initialhooks.add(hook);
363 public static void removeInitialHook(Runnable hook) {
364 initialhooks.remove(hook);
368 * This method should be called from C (realmain)
370 public static void executeInitialHooks() {
371 ScilabCommonsUtils.registerScilabThread();
373 for (final Runnable hook : initialhooks) {
378 /*--------------------------------------------------------------------------*/