Index: trunk/src/org/openstreetmap/josm/data/oauth/OAuthAccessTokenHolder.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/oauth/OAuthAccessTokenHolder.java	(revision 12927)
+++ trunk/src/org/openstreetmap/josm/data/oauth/OAuthAccessTokenHolder.java	(revision 12928)
@@ -7,4 +7,5 @@
 import org.openstreetmap.josm.io.auth.CredentialsAgent;
 import org.openstreetmap.josm.io.auth.CredentialsAgentException;
+import org.openstreetmap.josm.spi.preferences.Config;
 import org.openstreetmap.josm.tools.CheckParameterUtil;
 import org.openstreetmap.josm.tools.Logging;
@@ -143,8 +144,34 @@
      * credential manager.
      *
+     * @param cm the credential manager. Must not be null.
+     * @throws IllegalArgumentException if cm is null
+     */
+    public void init(CredentialsAgent cm) {
+        CheckParameterUtil.ensureParameterNotNull(cm, "cm");
+        OAuthToken token = null;
+        try {
+            token = cm.lookupOAuthAccessToken();
+        } catch (CredentialsAgentException e) {
+            Logging.error(e);
+            Logging.warn(tr("Failed to retrieve OAuth Access Token from credential manager"));
+            Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
+        }
+        saveToPreferences = Config.getPref().getBoolean("oauth.access-token.save-to-preferences", true);
+        if (token != null) {
+            accessTokenKey = token.getKey();
+            accessTokenSecret = token.getSecret();
+        }
+    }
+
+    /**
+     * Initializes the content of this holder from the Access Token managed by the
+     * credential manager.
+     *
      * @param pref the preferences. Must not be null.
      * @param cm the credential manager. Must not be null.
      * @throws IllegalArgumentException if cm is null
-     */
+     * @deprecated (since 12928) replaced by {@link #init(org.openstreetmap.josm.io.auth.CredentialsAgent)}
+     */
+    @Deprecated
     public void init(Preferences pref, CredentialsAgent cm) {
         CheckParameterUtil.ensureParameterNotNull(pref, "pref");
@@ -169,13 +196,10 @@
      * by a credential manager.
      *
-     * @param preferences the preferences. Must not be null.
      * @param cm the credentials manager. Must not be null.
-     * @throws IllegalArgumentException if preferences is null
-     * @throws IllegalArgumentException if cm is null
-     */
-    public void save(Preferences preferences, CredentialsAgent cm) {
-        CheckParameterUtil.ensureParameterNotNull(preferences, "preferences");
-        CheckParameterUtil.ensureParameterNotNull(cm, "cm");
-        preferences.putBoolean("oauth.access-token.save-to-preferences", saveToPreferences);
+     * @throws IllegalArgumentException if cm is null
+     */
+    public void save(CredentialsAgent cm) {
+        CheckParameterUtil.ensureParameterNotNull(cm, "cm");
+        Config.getPref().putBoolean("oauth.access-token.save-to-preferences", saveToPreferences);
         try {
             if (!saveToPreferences) {
@@ -192,4 +216,32 @@
 
     /**
+     * Saves the content of this holder to the preferences and a credential store managed
+     * by a credential manager.
+     *
+     * @param preferences the preferences. Must not be null.
+     * @param cm the credentials manager. Must not be null.
+     * @throws IllegalArgumentException if preferences is null
+     * @throws IllegalArgumentException if cm is null
+     * @deprecated (since 12928) replaced by {@link #save(org.openstreetmap.josm.io.auth.CredentialsAgent)}
+     */
+    @Deprecated
+    public void save(Preferences preferences, CredentialsAgent cm) {
+        CheckParameterUtil.ensureParameterNotNull(preferences, "preferences");
+        CheckParameterUtil.ensureParameterNotNull(cm, "cm");
+        preferences.putBoolean("oauth.access-token.save-to-preferences", saveToPreferences);
+        try {
+            if (!saveToPreferences) {
+                cm.storeOAuthAccessToken(null);
+            } else {
+                cm.storeOAuthAccessToken(new OAuthToken(accessTokenKey, accessTokenSecret));
+            }
+        } catch (CredentialsAgentException e) {
+            Logging.error(e);
+            Logging.warn(tr("Failed to store OAuth Access Token to credentials manager"));
+            Logging.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName()));
+        }
+    }
+
+    /**
      * Clears the content of this holder
      */
Index: trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java
===================================================================
--- trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java	(revision 12927)
+++ trunk/src/org/openstreetmap/josm/data/oauth/OAuthParameters.java	(revision 12928)
@@ -7,4 +7,5 @@
 import org.openstreetmap.josm.data.Preferences;
 import org.openstreetmap.josm.io.OsmApi;
+import org.openstreetmap.josm.spi.preferences.Config;
 import org.openstreetmap.josm.tools.CheckParameterUtil;
 import org.openstreetmap.josm.tools.Utils;
@@ -79,7 +80,27 @@
      * Replies a set of parameters as defined in the preferences.
      *
+     * @param apiUrl the API URL. Must not be null.
+     * @return the parameters
+     */
+    public static OAuthParameters createFromApiUrl(String apiUrl) {
+        OAuthParameters parameters = createDefault(apiUrl);
+        return new OAuthParameters(
+                Config.getPref().get("oauth.settings.consumer-key", parameters.getConsumerKey()),
+                Config.getPref().get("oauth.settings.consumer-secret", parameters.getConsumerSecret()),
+                Config.getPref().get("oauth.settings.request-token-url", parameters.getRequestTokenUrl()),
+                Config.getPref().get("oauth.settings.access-token-url", parameters.getAccessTokenUrl()),
+                Config.getPref().get("oauth.settings.authorise-url", parameters.getAuthoriseUrl()),
+                Config.getPref().get("oauth.settings.osm-login-url", parameters.getOsmLoginUrl()),
+                Config.getPref().get("oauth.settings.osm-logout-url", parameters.getOsmLogoutUrl()));
+    }
+
+    /**
+     * Replies a set of parameters as defined in the preferences.
+     *
      * @param pref the preferences
      * @return the parameters
-     */
+     * @deprecated (since 12928) replaced by {@link #createFromApiUrl(java.lang.String)}
+     */
+    @Deprecated
     public static OAuthParameters createFromPreferences(Preferences pref) {
         OAuthParameters parameters = createDefault(pref.get("osm-server.url"));
@@ -95,9 +116,24 @@
 
     /**
+     * Remembers the current values in the preferences.
+     */
+    public void rememberPreferences() {
+        Config.getPref().put("oauth.settings.consumer-key", getConsumerKey());
+        Config.getPref().put("oauth.settings.consumer-secret", getConsumerSecret());
+        Config.getPref().put("oauth.settings.request-token-url", getRequestTokenUrl());
+        Config.getPref().put("oauth.settings.access-token-url", getAccessTokenUrl());
+        Config.getPref().put("oauth.settings.authorise-url", getAuthoriseUrl());
+        Config.getPref().put("oauth.settings.osm-login-url", getOsmLoginUrl());
+        Config.getPref().put("oauth.settings.osm-logout-url", getOsmLogoutUrl());
+    }
+
+    /**
      * Remembers the current values in the preferences <code>pref</code>.
      *
      * @param pref the preferences. Must not be null.
      * @throws IllegalArgumentException if pref is null.
-     */
+     * @deprecated (since 12928) replaced by {@link #rememberPreferences()}
+     */
+    @Deprecated
     public void rememberPreferences(Preferences pref) {
         CheckParameterUtil.ensureParameterNotNull(pref, "pref");
@@ -129,5 +165,5 @@
      * @param osmLogoutUrl the OSM logout URL (for automatic mode)
      * @see #createDefault
-     * @see #createFromPreferences
+     * @see #createFromApiUrl
      * @since 9220
      */
Index: trunk/src/org/openstreetmap/josm/gui/MainApplication.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/MainApplication.java	(revision 12927)
+++ trunk/src/org/openstreetmap/josm/gui/MainApplication.java	(revision 12928)
@@ -981,5 +981,5 @@
         DefaultProxySelector proxySelector = new DefaultProxySelector(ProxySelector.getDefault());
         ProxySelector.setDefault(proxySelector);
-        OAuthAccessTokenHolder.getInstance().init(Main.pref, CredentialsManager.getInstance());
+        OAuthAccessTokenHolder.getInstance().init(CredentialsManager.getInstance());
 
         setupCallbacks();
Index: trunk/src/org/openstreetmap/josm/gui/oauth/AbstractAuthorizationUI.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/oauth/AbstractAuthorizationUI.java	(revision 12927)
+++ trunk/src/org/openstreetmap/josm/gui/oauth/AbstractAuthorizationUI.java	(revision 12928)
@@ -130,9 +130,22 @@
 
     /**
+     * Initializes the authorisation UI.
+     *
+     * @param paramApiUrl the API URL. Must not be null.
+     * @throws IllegalArgumentException if paramApiUrl is null
+     */
+    public void initialize(String paramApiUrl) {
+        CheckParameterUtil.ensureParameterNotNull(paramApiUrl, "paramApiUrl");
+        pnlAdvancedProperties.initialize(paramApiUrl);
+    }
+
+    /**
      * Initializes the authorisation UI with preference values in <code>pref</code>.
      *
      * @param pref the preferences. Must not be null.
      * @throws IllegalArgumentException if pref is null
+     * @deprecated (since 12928) replaced by {@link #initialize(java.lang.String)}
      */
+    @Deprecated
     public void initFromPreferences(Preferences pref) {
         CheckParameterUtil.ensureParameterNotNull(pref, "pref");
Index: trunk/src/org/openstreetmap/josm/gui/oauth/AdvancedOAuthPropertiesPanel.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/oauth/AdvancedOAuthPropertiesPanel.java	(revision 12927)
+++ trunk/src/org/openstreetmap/josm/gui/oauth/AdvancedOAuthPropertiesPanel.java	(revision 12928)
@@ -24,4 +24,5 @@
 import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
 import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
+import org.openstreetmap.josm.spi.preferences.Config;
 import org.openstreetmap.josm.tools.CheckParameterUtil;
 import org.openstreetmap.josm.tools.ImageProvider;
@@ -257,7 +258,28 @@
      * Initializes the panel from the values in the preferences <code>preferences</code>.
      *
+     * @param paramApiUrl the API URL. Must not be null.
+     * @throws IllegalArgumentException if paramApiUrl is null
+     */
+    public void initialize(String paramApiUrl) {
+        CheckParameterUtil.ensureParameterNotNull(paramApiUrl, "paramApiUrl");
+        setApiUrl(paramApiUrl);
+        boolean useDefault = Config.getPref().getBoolean("oauth.settings.use-default", true);
+        ilUseDefault.setEnabled(false);
+        if (useDefault) {
+            resetToDefaultSettings();
+        } else {
+            setAdvancedParameters(OAuthParameters.createFromApiUrl(paramApiUrl));
+        }
+        ilUseDefault.setEnabled(true);
+    }
+
+    /**
+     * Initializes the panel from the values in the preferences <code>preferences</code>.
+     *
      * @param pref the preferences. Must not be null.
      * @throws IllegalArgumentException if pref is null
-     */
+     * @deprecated (since 12928) replaced by {@link #initialize(java.lang.String)}
+     */
+    @Deprecated
     public void initFromPreferences(Preferences pref) {
         CheckParameterUtil.ensureParameterNotNull(pref, "pref");
@@ -275,8 +297,22 @@
     /**
      * Remembers the current values in the preferences <code>pref</code>.
+     */
+    public void rememberPreferences() {
+        Config.getPref().putBoolean("oauth.settings.use-default", cbUseDefaults.isSelected());
+        if (cbUseDefaults.isSelected()) {
+            new OAuthParameters(null, null, null, null, null, null, null).rememberPreferences();
+        } else {
+            getAdvancedParameters().rememberPreferences();
+        }
+    }
+
+    /**
+     * Remembers the current values in the preferences <code>pref</code>.
      *
      * @param pref the preferences. Must not be null.
      * @throws IllegalArgumentException if pref is null.
-     */
+     * @deprecated (since 12928) replaced by {@link #rememberPreferences()}
+     */
+    @Deprecated
     public void rememberPreferences(Preferences pref) {
         CheckParameterUtil.ensureParameterNotNull(pref, "pref");
Index: trunk/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java	(revision 12927)
+++ trunk/src/org/openstreetmap/josm/gui/oauth/FullyAutomaticAuthorizationUI.java	(revision 12928)
@@ -169,6 +169,32 @@
     /**
      * Initializes the panel with values from the preferences
+     * @param paramApiUrl the API URL
+     */
+    @Override
+    public void initialize(String paramApiUrl) {
+        super.initialize(paramApiUrl);
+        CredentialsAgent cm = CredentialsManager.getInstance();
+        try {
+            PasswordAuthentication pa = cm.lookup(RequestorType.SERVER, OsmApi.getOsmApi().getHost());
+            if (pa == null) {
+                tfUserName.setText("");
+                tfPassword.setText("");
+            } else {
+                tfUserName.setText(pa.getUserName() == null ? "" : pa.getUserName());
+                tfPassword.setText(pa.getPassword() == null ? "" : String.valueOf(pa.getPassword()));
+            }
+        } catch (CredentialsAgentException e) {
+            Logging.error(e);
+            tfUserName.setText("");
+            tfPassword.setText("");
+        }
+    }
+
+    /**
+     * Initializes the panel with values from the preferences
      * @param pref Preferences structure
-     */
+     * @deprecated (since 12928) replaced by {@link #initialize(java.lang.String)}
+     */
+    @Deprecated
     @Override
     public void initFromPreferences(Preferences pref) {
Index: trunk/src/org/openstreetmap/josm/gui/oauth/OAuthAuthorizationWizard.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/oauth/OAuthAuthorizationWizard.java	(revision 12927)
+++ trunk/src/org/openstreetmap/josm/gui/oauth/OAuthAuthorizationWizard.java	(revision 12928)
@@ -40,5 +40,4 @@
 
 import org.openstreetmap.josm.Main;
-import org.openstreetmap.josm.data.Preferences;
 import org.openstreetmap.josm.data.oauth.OAuthAccessTokenHolder;
 import org.openstreetmap.josm.data.oauth.OAuthParameters;
@@ -309,10 +308,7 @@
      */
     public void initFromPreferences() {
-        // Copy current JOSM preferences to update API url with the one used in this wizard
-        Preferences copyPref = new Preferences(Main.pref);
-        copyPref.put("osm-server.url", apiUrl);
-        pnlFullyAutomaticAuthorisationUI.initFromPreferences(copyPref);
-        pnlSemiAutomaticAuthorisationUI.initFromPreferences(copyPref);
-        pnlManualAuthorisationUI.initFromPreferences(copyPref);
+        pnlFullyAutomaticAuthorisationUI.initialize(apiUrl);
+        pnlSemiAutomaticAuthorisationUI.initialize(apiUrl);
+        pnlManualAuthorisationUI.initialize(apiUrl);
     }
 
Index: trunk/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java	(revision 12927)
+++ trunk/src/org/openstreetmap/josm/gui/preferences/server/AuthenticationPreferencesPanel.java	(revision 12928)
@@ -134,5 +134,5 @@
 
     /**
-     * Saves the current values to preferences
+     * Saves the current values to the preferences
      */
     public final void saveToPreferences() {
@@ -149,5 +149,5 @@
             pnlBasicAuthPreferences.saveToPreferences();
             OAuthAccessTokenHolder.getInstance().clear();
-            OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance());
+            OAuthAccessTokenHolder.getInstance().save(CredentialsManager.getInstance());
         } else if ("oauth".equals(authMethod)) {
             // clear the password in the preferences
Index: trunk/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java
===================================================================
--- trunk/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java	(revision 12927)
+++ trunk/src/org/openstreetmap/josm/gui/preferences/server/OAuthAuthenticationPreferencesPanel.java	(revision 12928)
@@ -96,5 +96,5 @@
         gc.weighty = 1.0;
         pnl.add(pnlAdvancedProperties, gc);
-        pnlAdvancedProperties.initFromPreferences(Main.pref);
+        pnlAdvancedProperties.initialize(OsmApi.getOsmApi().getServerUrl());
         pnlAdvancedProperties.setBorder(
                 BorderFactory.createCompoundBorder(
@@ -162,6 +162,6 @@
     public void saveToPreferences() {
         OAuthAccessTokenHolder.getInstance().setSaveToPreferences(cbSaveToPreferences.isSelected());
-        OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance());
-        pnlAdvancedProperties.rememberPreferences(Main.pref);
+        OAuthAccessTokenHolder.getInstance().save(CredentialsManager.getInstance());
+        pnlAdvancedProperties.rememberPreferences();
     }
 
@@ -359,5 +359,5 @@
         public void actionPerformed(ActionEvent evt) {
             OAuthToken token = OAuthAccessTokenHolder.getInstance().getAccessToken();
-            OAuthParameters parameters = OAuthParameters.createFromPreferences(Main.pref);
+            OAuthParameters parameters = OAuthParameters.createFromApiUrl(OsmApi.getOsmApi().getServerUrl());
             TestAccessTokenTask task = new TestAccessTokenTask(
                     OAuthAuthenticationPreferencesPanel.this,
Index: trunk/src/org/openstreetmap/josm/io/OsmConnection.java
===================================================================
--- trunk/src/org/openstreetmap/josm/io/OsmConnection.java	(revision 12927)
+++ trunk/src/org/openstreetmap/josm/io/OsmConnection.java	(revision 12928)
@@ -114,5 +114,5 @@
     protected void addOAuthAuthorizationHeader(HttpClient connection) throws OsmTransferException {
         if (oauthParameters == null) {
-            oauthParameters = OAuthParameters.createFromPreferences(Main.pref);
+            oauthParameters = OAuthParameters.createFromApiUrl(OsmApi.getOsmApi().getServerUrl());
         }
         OAuthConsumer consumer = oauthParameters.buildConsumer();
@@ -146,5 +146,5 @@
             fetcher.obtainAccessToken(apiUrl);
             OAuthAccessTokenHolder.getInstance().setSaveToPreferences(true);
-            OAuthAccessTokenHolder.getInstance().save(Main.pref, CredentialsManager.getInstance());
+            OAuthAccessTokenHolder.getInstance().save(CredentialsManager.getInstance());
         } catch (MalformedURLException | InterruptedException | InvocationTargetException e) {
             throw new MissingOAuthAccessTokenException(e);
