package app; // Java imports import java.util.*; // Project imports import util.*; import vue.*; // Top-level software state manager public class App { // Instance fields private boolean useNative; // Produce native core contexts private Localizer.Locale[] locales; // Language translations private ArrayList windows; // Application windows // Configuration fields Localizer localizer; // UI localization manager /////////////////////////////////////////////////////////////////////////// // Constructors // /////////////////////////////////////////////////////////////////////////// // Default constructor public App(boolean useNative) { // Instance fields localizer = new Localizer(); windows = new ArrayList(); // Additional processing setUseNative(useNative); initLocales(); addWindow(); } /////////////////////////////////////////////////////////////////////////// // Package Methods // /////////////////////////////////////////////////////////////////////////// // Add a new program window void addWindow() { windows.add(new MainWindow(this)); windowsChanged(); } // Determine whether the native module is in use boolean getUseNative() { return useNative; } // Retrieve a list of registered locales Localizer.Locale[] listLocales() { var ret = new Localizer.Locale[locales.length]; System.arraycopy(locales, 0, ret, 0, locales.length); return ret; } // Determine the number of a window in the collection int numberOf(MainWindow window) { return windows.indexOf(window) + 1; } // Remove a program window void removeWindow(MainWindow window) { windows.remove(window); windowsChanged(); } // Specify whether using the native module boolean setUseNative(boolean useNative) { return this.useNative = useNative && VUE.isNativeLoaded(); } /////////////////////////////////////////////////////////////////////////// // Private Methods // /////////////////////////////////////////////////////////////////////////// // Load and parse all locale translations private void initLocales() { // Process all locale files var locales = new HashMap(); for (String file : Util.listFiles("locale")) { // Process the file try { var locale = Localizer.parse(Util.textRead("locale/" + file)); String key = locale.id.toLowerCase(); // Register the locale if (locales.containsKey(key)) throw new RuntimeException( "Locale with ID '" + locale.id + "' already registered"); locales.put(key, locale); // The locale matches the one in the config if (key.equals("en-us")) localizer.setLocale(locale); } // Could not process the file catch (Exception e) { System.err.println("Error parsing locale " + file + ": " + e.getMessage()); } } // Produce the list of registered locales this.locales = locales.values().toArray( new Localizer.Locale[locales.size()]); Arrays.sort(this.locales); // Select a default locale if (localizer.getLocale() != null || this.locales.length == 0) return; var locale = locales.get("en-us"); localizer.setLocale(locale != null ? locale : this.locales[0]); } // A window has been added to or removed from the program state private void windowsChanged() { int count = windows.size(); for (int x = 0; x < count; x++) windows.get(x).windowsChanged(x + 1, count == 1); } }