001package jmri.util.prefs; 002 003import java.io.File; 004import java.util.HashMap; 005import javax.xml.parsers.DocumentBuilderFactory; 006import javax.xml.parsers.ParserConfigurationException; 007import jmri.profile.AuxiliaryConfiguration; 008import jmri.profile.Profile; 009 010/** 011 * Provides a general purpose XML element storage mechanism for the storage of 012 * user interface configuration. 013 * <p> 014 * There are two configuration files per {@link jmri.profile.Profile} and 015 * {@link jmri.util.node.NodeIdentity}, both stored in the directory 016 * <code>profile:profile</code>: 017 * <ul> 018 * <li><code>{@value jmri.profile.Profile#UI_CONFIG}</code> preferences that are 019 * shared across multiple nodes for a single profile. An example of such a 020 * preference would be the Railroad Name preference.</li> 021 * <li><code><node-identity>/{@value jmri.profile.Profile#UI_CONFIG}</code> 022 * preferences that are specific to the profile running on a specific host 023 * (<node-identity> is the identity returned by 024 * {@link jmri.util.node.NodeIdentity#networkIdentity()}). An example of such a 025 * preference would be a file location.</li> 026 * </ul> 027 * <p> 028 * Non-profile specific configuration that applies to all profiles is stored in 029 * the file 030 * <code>settings:preferences/{@value jmri.profile.Profile#UI_CONFIG}</code>. 031 * 032 * @author Randall Wood 2015, 2016 033 */ 034public final class JmriUserInterfaceConfigurationProvider extends AbstractConfigurationProvider { 035 036 private final Configuration configuration; 037 038 public static final String NAMESPACE = "http://www.netbeans.org/ns/auxiliary-configuration/1"; // NOI18N 039 040 static { 041 try { 042 DocumentBuilderFactory.newInstance().newDocumentBuilder(); 043 } catch (ParserConfigurationException e) { 044 throw new AssertionError(e); 045 } 046 } 047 048 private static final HashMap<Profile, JmriUserInterfaceConfigurationProvider> PROVIDERS = new HashMap<>(); 049 050 /** 051 * Get the JmriPrefererncesProvider for the specified profile. 052 * 053 * @param project The profile. This is most often the profile returned by 054 * the {@link jmri.profile.ProfileManager#getActiveProfile()} 055 * method of the ProfileManager returned by 056 * {@link jmri.profile.ProfileManager#getDefault()} 057 * @return The shared or private JmriPreferencesProvider for the project. 058 */ 059 static synchronized JmriUserInterfaceConfigurationProvider findProvider(Profile project) { 060 if (PROVIDERS.get(project) == null) { 061 PROVIDERS.put(project, new JmriUserInterfaceConfigurationProvider(project)); 062 } 063 return PROVIDERS.get(project); 064 } 065 066 /** 067 * Get the {@link java.util.prefs.Preferences} for the specified class in 068 * the specified profile. 069 * 070 * @param project The profile. This is most often the profile returned by 071 * the {@link jmri.profile.ProfileManager#getActiveProfile()} 072 * method of the ProfileManager returned by 073 * {@link jmri.profile.ProfileManager#getDefault()} 074 * @return The shared or private AuxiliaryConfiguration for project. 075 */ 076 public static AuxiliaryConfiguration getConfiguration(final Profile project) { 077 return findProvider(project).getConfiguration(); 078 } 079 080 /** 081 * Get the {@link jmri.profile.AuxiliaryConfiguration}. 082 * 083 * @return The AuxiliaryConfiguration. 084 */ 085 @Override 086 protected AuxiliaryConfiguration getConfiguration() { 087 return this.configuration; 088 } 089 090 @Override 091 protected File getConfigurationFile(boolean shared) { 092 if (this.project == null) { 093 return new File(this.getConfigurationDirectory(shared), Profile.UI_CONFIG); // NOI18N 094 } else { 095 return new File(this.getConfigurationDirectory(shared), Profile.UI_CONFIG); // NOI18N 096 } 097 } 098 099 JmriUserInterfaceConfigurationProvider(Profile project) { 100 super(project); 101 this.configuration = new Configuration(); 102 } 103 104 private class Configuration extends JmriConfiguration { 105 106 private Configuration() { 107 super(); 108 } 109 110 @Override 111 protected File getConfigurationFile(boolean shared) { 112 return JmriUserInterfaceConfigurationProvider.this.getConfigurationFile(shared); 113 } 114 115 @Override 116 protected boolean isSharedBackedUp() { 117 return JmriUserInterfaceConfigurationProvider.this.isSharedBackedUp(); 118 } 119 120 @Override 121 protected void setSharedBackedUp(boolean backedUp) { 122 JmriUserInterfaceConfigurationProvider.this.setSharedBackedUp(backedUp); 123 } 124 125 @Override 126 protected boolean isPrivateBackedUp() { 127 return JmriUserInterfaceConfigurationProvider.this.isPrivateBackedUp(); 128 } 129 130 @Override 131 protected void setPrivateBackedUp(boolean backedUp) { 132 JmriUserInterfaceConfigurationProvider.this.setPrivateBackedUp(backedUp); 133 } 134 135 } 136}