Search in sources :

Example 1 with PreferencesManager

use of jmri.spi.PreferencesManager in project JMRI by JMRI.

the class JmriConfigurationManager method load.

@Override
public boolean load(URL file, boolean registerDeferred) throws JmriException {
    log.debug("loading {} ...", file);
    try {
        if (file == null || //NOI18N
        (new File(file.toURI())).getName().equals("ProfileConfig.xml") || (new File(file.toURI())).getName().equals(Profile.CONFIG)) {
            Profile profile = ProfileManager.getDefault().getActiveProfile();
            List<PreferencesManager> providers = new ArrayList<>(InstanceManager.getList(PreferencesManager.class));
            providers.stream().forEach((provider) -> {
                this.initializeProvider(provider, profile);
            });
            if (!this.initializationExceptions.isEmpty()) {
                if (!GraphicsEnvironment.isHeadless()) {
                    ArrayList<String> errors = new ArrayList<>();
                    this.initialized.forEach((provider) -> {
                        List<Exception> exceptions = provider.getInitializationExceptions(profile);
                        if (!exceptions.isEmpty()) {
                            exceptions.forEach((exception) -> {
                                errors.add(exception.getLocalizedMessage());
                            });
                        } else if (this.initializationExceptions.get(provider) != null) {
                            errors.add(this.initializationExceptions.get(provider).getLocalizedMessage());
                        }
                    });
                    Object list;
                    if (errors.size() == 1) {
                        list = errors.get(0);
                    } else {
                        list = new JList<>(errors.toArray(new String[errors.size()]));
                    }
                    JOptionPane.showMessageDialog(null, new Object[] { (list instanceof JList) ? Bundle.getMessage("InitExMessageListHeader") : null, list, // Add a visual break between list of errors and notes // NOI18N
                    "<html><br></html>", // NOI18N
                    Bundle.getMessage("InitExMessageLogs"), // NOI18N
                    Bundle.getMessage("InitExMessagePrefs") }, // NOI18N
                    Bundle.getMessage("InitExMessageTitle", Application.getApplicationName()), JOptionPane.ERROR_MESSAGE);
                    (new TabbedPreferencesAction()).actionPerformed();
                }
            }
            if (file != null && (new File(file.toURI())).getName().equals("ProfileConfig.xml")) {
                // NOI18N
                log.debug("Loading legacy configuration...");
                return this.legacy.load(file, registerDeferred);
            }
            return this.initializationExceptions.isEmpty();
        }
    } catch (URISyntaxException ex) {
        log.error("Unable to get File for {}", file);
        throw new JmriException(ex.getMessage(), ex);
    }
    return this.legacy.load(file, registerDeferred);
// return true; // always return true once legacy support is dropped
}
Also used : TabbedPreferencesAction(apps.gui3.TabbedPreferencesAction) ArrayList(java.util.ArrayList) JmriException(jmri.JmriException) URISyntaxException(java.net.URISyntaxException) PreferencesManager(jmri.spi.PreferencesManager) Profile(jmri.profile.Profile) URISyntaxException(java.net.URISyntaxException) InitializationException(jmri.util.prefs.InitializationException) JmriException(jmri.JmriException) File(java.io.File) XmlFile(jmri.jmrit.XmlFile) JList(javax.swing.JList)

Example 2 with PreferencesManager

use of jmri.spi.PreferencesManager in project JMRI by JMRI.

the class AbstractPreferencesManager method requiresNoInitializedWithExceptions.

/**
     * Require that instances of the specified classes have initialized
     * correctly. This method should only be called from within
     * {@link #initialize(jmri.profile.Profile)}, generally immediately after
     * the PreferencesManager verifies that it is not already initialized. If
     * this method is within a try-catch block, the exception it generates
     * should be re-thrown by initialize(profile).
     *
     * @param profile the profile against which the manager is being initialized
     * @param classes the manager classes for which all calling
     *                {@link #isInitialized(jmri.profile.Profile)} must return
     *                true against all instances of
     * @param message the localized message to display if an
     *                InitializationExcpetion is thrown
     * @throws InitializationException  if any instance of any class in classes
     *                                  returns false on isIntialized(profile)
     * @throws IllegalArgumentException if any member of classes is not also in
     *                                  the set of classes returned by
     *                                  {@link #getRequires()}
     */
protected void requiresNoInitializedWithExceptions(@Nonnull Profile profile, @Nonnull Set<Class<? extends PreferencesManager>> classes, @Nonnull String message) throws InitializationException {
    classes.stream().filter((clazz) -> (!this.getRequires().contains(clazz))).forEach((clazz) -> {
        throw new IllegalArgumentException("Class " + clazz.getClass().getName() + " not marked as required by " + this.getClass().getName());
    });
    for (Class<? extends PreferencesManager> clazz : classes) {
        for (PreferencesManager instance : InstanceManager.getList(clazz)) {
            if (instance.isInitializedWithExceptions(profile)) {
                InitializationException exception = new InitializationException("Refusing to initialize", message);
                this.addInitializationException(profile, exception);
                this.setInitialized(profile, true);
                throw exception;
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) InstanceManager(jmri.InstanceManager) List(java.util.List) ConnectionConfigManager(jmri.jmrix.ConnectionConfigManager) PreferencesManager(jmri.spi.PreferencesManager) Bean(jmri.beans.Bean) Profile(jmri.profile.Profile) Set(java.util.Set) HashMap(java.util.HashMap) Nonnull(javax.annotation.Nonnull) ArrayList(java.util.ArrayList) PreferencesManager(jmri.spi.PreferencesManager)

Aggregations

ArrayList (java.util.ArrayList)2 Profile (jmri.profile.Profile)2 PreferencesManager (jmri.spi.PreferencesManager)2 TabbedPreferencesAction (apps.gui3.TabbedPreferencesAction)1 File (java.io.File)1 URISyntaxException (java.net.URISyntaxException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Nonnull (javax.annotation.Nonnull)1 JList (javax.swing.JList)1 InstanceManager (jmri.InstanceManager)1 JmriException (jmri.JmriException)1 Bean (jmri.beans.Bean)1 XmlFile (jmri.jmrit.XmlFile)1 ConnectionConfigManager (jmri.jmrix.ConnectionConfigManager)1 InitializationException (jmri.util.prefs.InitializationException)1