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
}
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;
}
}
}
}
Aggregations