/* * Moonlight|3D Copyright (C) 2005 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Jan 5, 2005 */ package ml.core; /** * \package ml.core * * This is a container for the core program framework and assorted utilities. * * This package is part of the core program. */ import java.util.logging.Logger; import ml.backend.document.DocumentFactory; import ml.core.tasks.TaskManager; import ml.ui.core.ApplicationWindow; import ml.ui.core.UIManager; /** * This class is the main singleton for Moonlight|3D. All other important * manager and factory classes should be accessed through this singleton * unless they are provided explicitely as arguments. * * @author gregor */ public final class State { /** * Version information */ public static class Version { public static int major=0; public static int minor=1; public static int patch=4; public static String asString() { return major + "." + minor + "." + patch; } }; /** * True if Moonlight|3D is running without a user interface in batch mode. */ private boolean batchMode; /** * Moonlight|3D command line arguments */ private String[] commandLineArguments; /** * Name of directory where Moonlight|3D is installed */ private String installationDirectory; /** * Reference to single global instance of document manager */ private ml.core.document.DocumentManager documentManager; /** * Reference to single global instance of plugin manager */ private ml.core.plugins.Manager pluginManager; /** * Reference to single global instance of preferences manager */ private ml.core.preferences.Manager preferencesManager; /** * Reference to single global instance of image format manager */ private ml.image.FormatManager formatManager; /** * Reference to single global instance of file format manager */ private ml.file.Manager fileFormatManager; /** * Reference to single instance of main window */ private ml.ui.core.ApplicationWindow mainWindow; /** * Reference to single instance of user interface manager */ private ml.ui.core.UIManager uiManager; /** * Reference to single global instance of script manager */ private ml.core.scripts.Manager scriptManager; /** * Reference to single global background task manager */ private TaskManager taskManager; /** * Marker for singleton instance creation */ private static boolean instanceCreated=false; /** * Reference to singleton instance */ private static State instance=null; private DocumentFactory documentFactory; /** * Default constructor for this class. The real initalisation work is * not done in this constructor, but in initialise(), because only then * getInstance() will work reliably. This constructor is only here * to prevent uncontrolled instantiation of this class. * */ private State() { } /** * Main initialisation for this class. This function is allocating * all classes that are queryable from this singleton. It is called * immediately after constructing this class by getInstance() * */ private void initialise() { documentManager=new ml.core.document.DocumentManager(); preferencesManager=new ml.core.preferences.Manager(); pluginManager=new ml.core.plugins.Manager(); formatManager=new ml.image.FormatManager(); documentFactory=new DocumentFactory(); // TODO move this to a plugin: documentManager.registerDocumentFactory(documentFactory); fileFormatManager=new ml.file.Manager(); scriptManager=new ml.core.scripts.Manager(); taskManager=new TaskManager(); uiManager=new UIManager(); } /** * Return whether Moonlight|3D runs in batch mode. If Moonlight|3D runs in * batch mode there must be no attempt to use any user interface functions * and no actions must happen that require user input or user intervention, * because the user interface is not initialised and no user may be present * to react. If Moonlight|3D does not run in batch mode, the full user * interface is available and can be relied upon to interact with the user. * * @return true if Moonlight|3D is in batch mode, false otherwise */ public boolean getBatchMode() { return batchMode; } /** * Internal: set batch mode state upon startup. * * @param batchMode true if Moonlight|3D is running in batch mode, false otherwise */ public void setBatchMode(boolean batchMode) { this.batchMode = batchMode; } /** * Internal: set the command line arguments upon startup. * * @param commandLineArguments the command line arguments as passed to main() */ public void setCommandLineArguments(String[] commandLineArguments) { this.commandLineArguments=commandLineArguments.clone(); } /** * Return the command line arguments that Moonlight|3D has been called with. * This returns the full command line as passed to main() by the Java virtual * machine. * * @return the command line arguments as passed to main() */ public String[] getCommandLineArguments() { return commandLineArguments.clone(); } /** * Internal: set the path where Moonlight|3D is installed. * * @param installationDirectory the Moonlight|3D installation directory */ public void setInstallationDirectory(String installationDirectory) { this.installationDirectory=installationDirectory; } /** * Return the directory into which Moonlight|3D is installed. This * is an absolute path name. * * @return the Moonlight|3D installation directory */ public String getInstallationDirectory() { return installationDirectory; } /** * Return the lifecycle manager singleton * * @return lifecycle manager */ public ml.core.document.DocumentManager getDocumentManager() { return documentManager; } /** * Return the plugin manager singleton * * @return plugin manager */ public ml.core.plugins.Manager getPluginManager() { return pluginManager; } /** * Return the preferences manager * * @return preferences manager */ public ml.core.preferences.Manager getPreferencesManager() { return preferencesManager; } /** * Return the image format manager * * @return image format manager */ public ml.image.FormatManager getFormatManager() { return formatManager; } /** * Return the file format manager * * @return file format manager */ public ml.file.Manager getFileFormatManager() { return fileFormatManager; } public ml.ui.core.UIManager getUIManager() { return uiManager; } /** * create the main window for Moonlight|3D */ public void createMainWindow() throws ml.core.exceptions.Exception { mainWindow=new ApplicationWindow(uiManager.getLayout("mainwindow"),true); } /** * Return the main window for Moonlight|3D * * @return main application window */ public ApplicationWindow getMainWindow() { return mainWindow; } /** * Return the scripts manager * * @return scripts manager */ public ml.core.scripts.Manager getScriptManager() { return scriptManager; } /** * Returns the background task manager * * @return background task manager */ public TaskManager getTaskManager() { return taskManager; } /** * Static accessor method returning the current instance of this class. * This function is reentrant even when the singleton class is constructed * for the first time. * * @return singleton instance */ public static State getInstance() { if(!instanceCreated) { instanceCreated=true; instance=new State(); instance.initialise(); } return instance; } /** * This method is intended for accessing the logger used throughout the application. * @return java.util.Logger instance for the Moonlight|3D application */ public Logger logger() { return Logger.getLogger("Moonlight|3D"); } /** * Wrapper around System.loadLibrary that must be used by plugins that want to load * native libraries. The problem is that classes loaded by our own class loader * cannot currently load native libraries using it. They will get an * UnsatisfiedLinkError stating that Native library ... already loaded in another * classloader. To prevent this, we need to defer the library loading to a class * that has been loaded by the actual system classloader. * * @param library name of native library to load */ public void loadLibrary(String library) { System.loadLibrary(library); } }