Search in sources :

Example 6 with InitializationException

use of jmri.util.prefs.InitializationException in project JMRI by JMRI.

the class ScriptButtonModelXml method load.

@Override
public boolean load(Element shared, Element perNode) throws JmriException {
    // Should the script engines be pre-loaded here?
    boolean result = false;
    ScriptButtonModel model = new ScriptButtonModel();
    // NOI18N
    model.setName(shared.getAttribute("name").getValue());
    for (Element child : shared.getChildren("property")) {
        // NOI18N
        if (// NOI18N
        child.getAttributeValue("name").equals("script") && child.getAttributeValue("value") != null) {
            // NOI18N
            // NOI18N
            String script = child.getAttributeValue("value");
            try {
                model.setScript(FileUtil.getFile(script));
                result = true;
            } catch (FileNotFoundException ex) {
                model.addException(new InitializationException(Bundle.getMessage(Locale.ENGLISH, "ScriptButtonModel.ScriptNotFound", script), Bundle.getMessage("ScriptButtonModel.ScriptNotFound", script), ex));
                log.error("Unable to create button for script {}", script);
            }
        }
    }
    // store the model
    InstanceManager.getDefault(StartupActionsManager.class).addAction(model);
    return result;
}
Also used : Element(org.jdom2.Element) FileNotFoundException(java.io.FileNotFoundException) ScriptButtonModel(apps.startup.ScriptButtonModel) StartupActionsManager(apps.StartupActionsManager) InitializationException(jmri.util.prefs.InitializationException)

Example 7 with InitializationException

use of jmri.util.prefs.InitializationException in project JMRI by JMRI.

the class ManagerDefaultSelector method initialize.

@Override
public void initialize(Profile profile) throws InitializationException {
    if (!this.isInitialized(profile)) {
        // NOI18N
        Preferences settings = ProfileUtils.getPreferences(profile, this.getClass(), true).node("defaults");
        try {
            for (String name : settings.keys()) {
                String connection = settings.get(name, null);
                Class<?> cls = this.classForName(name);
                log.debug("Loading default {} for {}", connection, name);
                if (cls != null) {
                    this.defaults.put(cls, connection);
                    log.debug("Loaded default {} for {}", connection, cls);
                }
            }
        } catch (BackingStoreException ex) {
            log.info("Unable to read preferences for Default Selector.");
        }
        InitializationException ex = this.configure();
        ConfigureManager manager = InstanceManager.getNullableDefault(ConfigureManager.class);
        if (manager != null) {
            // allow ProfileConfig.xml to be written correctly
            manager.registerPref(this);
        }
        this.setInitialized(profile, true);
        if (ex != null) {
            throw ex;
        }
    }
}
Also used : ConfigureManager(jmri.ConfigureManager) BackingStoreException(java.util.prefs.BackingStoreException) Preferences(java.util.prefs.Preferences) InitializationException(jmri.util.prefs.InitializationException)

Example 8 with InitializationException

use of jmri.util.prefs.InitializationException in project JMRI by JMRI.

the class ManagerDefaultSelector method configure.

/**
     * Load into InstanceManager
     *
     * @return an exception that can be passed to the user or null if no errors
     *         occur
     */
@CheckForNull
public InitializationException configure() {
    InitializationException error = null;
    List<SystemConnectionMemo> connList = InstanceManager.getList(SystemConnectionMemo.class);
    log.debug("configure defaults into InstanceManager from {} memos, {} defaults", connList.size(), defaults.keySet().size());
    for (Class<?> c : defaults.keySet()) {
        // 'c' is the class to load
        String connectionName = this.defaults.get(c);
        // have to find object of that type from proper connection
        boolean found = false;
        for (SystemConnectionMemo memo : connList) {
            String testName = memo.getUserName();
            if (testName.equals(connectionName)) {
                found = true;
                // match, store
                try {
                    if (memo.provides(c)) {
                        log.debug("   setting default for \"{}\" to \"{}\" in configure", c, memo.get(c));
                        InstanceManager.setDefault(c, memo.get(c));
                    }
                } catch (NullPointerException ex) {
                    // NOI18N
                    String englishMsg = Bundle.getMessage(Locale.ENGLISH, "ErrorNullDefault", memo.getUserName(), c);
                    // NOI18N
                    String localizedMsg = Bundle.getMessage("ErrorNullDefault", memo.getUserName(), c);
                    error = new InitializationException(englishMsg, localizedMsg);
                    log.warn("SystemConnectionMemo for {} ({}) provides a null {} instance", memo.getUserName(), memo.getClass(), c);
                }
                break;
            } else {
                log.debug("   memo name didn't match: {} vs {}", testName, connectionName);
            }
        }
        /*
             * If the set connection can not be found then we shall set the manager default to use what
             * has currently been set.
             */
        if (!found) {
            log.debug("!found, so resetting");
            String currentName = null;
            if (c == ThrottleManager.class && InstanceManager.getNullableDefault(ThrottleManager.class) != null) {
                currentName = InstanceManager.throttleManagerInstance().getUserName();
            } else if (c == PowerManager.class && InstanceManager.getNullableDefault(PowerManager.class) != null) {
                currentName = InstanceManager.getDefault(PowerManager.class).getUserName();
            } else if (c == ProgrammerManager.class && InstanceManager.getNullableDefault(ProgrammerManager.class) != null) {
                currentName = InstanceManager.getDefault(ProgrammerManager.class).getUserName();
            }
            if (currentName != null) {
                log.warn("The configured " + connectionName + " for " + c + " can not be found so will use the default " + currentName);
                this.defaults.put(c, currentName);
            }
        }
    }
    return error;
}
Also used : PowerManager(jmri.PowerManager) InternalSystemConnectionMemo(jmri.jmrix.internal.InternalSystemConnectionMemo) SystemConnectionMemo(jmri.jmrix.SystemConnectionMemo) ThrottleManager(jmri.ThrottleManager) ProgrammerManager(jmri.ProgrammerManager) AddressedProgrammerManager(jmri.AddressedProgrammerManager) GlobalProgrammerManager(jmri.GlobalProgrammerManager) InitializationException(jmri.util.prefs.InitializationException) CheckForNull(javax.annotation.CheckForNull)

Aggregations

InitializationException (jmri.util.prefs.InitializationException)8 Preferences (java.util.prefs.Preferences)3 Element (org.jdom2.Element)3 FileNotFoundException (java.io.FileNotFoundException)2 XmlAdapter (jmri.configurexml.XmlAdapter)2 FileLocationsPreferences (jmri.implementation.FileLocationsPreferences)2 Profile (jmri.profile.Profile)2 JDOMException (org.jdom2.JDOMException)2 StartupActionsManager (apps.StartupActionsManager)1 GuiLafPreferencesManager (apps.gui.GuiLafPreferencesManager)1 ScriptButtonModel (apps.startup.ScriptButtonModel)1 StartupActionModelUtil (apps.startup.StartupActionModelUtil)1 StartupModel (apps.startup.StartupModel)1 StartupModelFactory (apps.startup.StartupModelFactory)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1