Search in sources :

Example 1 with DefaultTemplate

use of uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.DefaultTemplate in project GDSC-SMLM by aherbert.

the class ConfigurationTemplate method restoreLoadedTemplates.

/**
 * Restore the templates that were loaded.
 *
 * <p>Given the list of standard templates is manipulated only by this plugin this should be the
 * same set of templates used last time by the user.
 *
 * @param inlineTemplates the inline templates
 * @return the templates
 */
private static Map<String, Template> restoreLoadedTemplates(Map<String, Template> inlineTemplates) {
    Map<String, Template> map = new LinkedHashMap<>();
    // Allow this to fail silently
    final DefaultTemplateSettings settings = SettingsManager.readDefaultTemplateSettings(SettingsManager.FLAG_SILENT);
    if (settings.getDefaultTemplatesCount() == 0) {
        return map;
    }
    HashMap<String, TemplateResource> templateResources = null;
    // Process in order so that the order is preserved, i.e. do not bulk load each type
    for (final DefaultTemplate d : settings.getDefaultTemplatesList()) {
        switch(d.getTemplateType()) {
            case CUSTOM_TEMPLATE:
                loadCustomTemplate(map, d.getName(), d.getFilename(), d.getTifFilename());
                break;
            case INLINE_TEMPLATE:
                final Template t = inlineTemplates.get(d.getName());
                if (t != null) {
                    map.put(d.getName(), t);
                }
                break;
            case RESOURCE_TEMPLATE:
                if (templateResources == null) {
                    final TemplateResource[] list = listTemplateResources();
                    templateResources = new HashMap<>(list.length);
                    for (final TemplateResource r : list) {
                        templateResources.put(r.name, r);
                    }
                }
                loadTemplateResource(map, templateResources.get(d.getName()));
                break;
            default:
                break;
        }
    }
    if (map.size() != settings.getDefaultTemplatesCount()) {
        // This occurs if we cannot reload some of the templates.
        // Prevent this from happening again.
        saveLoadedTemplates(map);
    }
    return map;
}
Also used : DefaultTemplateSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.DefaultTemplateSettings) DefaultTemplate(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.DefaultTemplate) DefaultTemplate(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.DefaultTemplate) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with DefaultTemplate

use of uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.DefaultTemplate in project GDSC-SMLM by aherbert.

the class ConfigurationTemplate method saveLoadedTemplates.

/**
 * Save the provided templates as the default templates to load on start-up.
 *
 * @param map the template map
 */
private static void saveLoadedTemplates(Map<String, Template> map) {
    final DefaultTemplateSettings.Builder settings = DefaultTemplateSettings.newBuilder();
    final DefaultTemplate.Builder defaultTemplate = DefaultTemplate.newBuilder();
    for (final Entry<String, Template> entry : map.entrySet()) {
        defaultTemplate.clear();
        defaultTemplate.setName(entry.getKey());
        final Template t = entry.getValue();
        switch(t.templateType) {
            case CUSTOM:
                defaultTemplate.setTemplateType(GUIProtos.TemplateType.CUSTOM_TEMPLATE);
                break;
            case INLINE:
                defaultTemplate.setTemplateType(GUIProtos.TemplateType.INLINE_TEMPLATE);
                break;
            default:
                ValidationUtils.checkArgument(t.templateType == TemplateType.RESOURCE, t.templateType);
                defaultTemplate.setTemplateType(GUIProtos.TemplateType.RESOURCE_TEMPLATE);
                break;
        }
        if (t.file != null) {
            defaultTemplate.setFilename(t.file.getPath());
        }
        if (t.tifPath != null) {
            defaultTemplate.setTifFilename(t.tifPath);
        }
        settings.addDefaultTemplates(defaultTemplate.build());
    }
    SettingsManager.writeSettings(settings);
}
Also used : DefaultTemplateSettings(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.DefaultTemplateSettings) DefaultTemplate(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.DefaultTemplate) DefaultTemplate(uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.DefaultTemplate)

Aggregations

DefaultTemplate (uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.DefaultTemplate)2 DefaultTemplateSettings (uk.ac.sussex.gdsc.smlm.ij.settings.GUIProtos.DefaultTemplateSettings)2 LinkedHashMap (java.util.LinkedHashMap)1