001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.preferences.server; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.BorderLayout; 007import java.awt.GridBagConstraints; 008import java.awt.GridBagLayout; 009import java.awt.Insets; 010import java.net.Authenticator.RequestorType; 011import java.net.PasswordAuthentication; 012 013import javax.swing.BorderFactory; 014import javax.swing.JLabel; 015import javax.swing.JPanel; 016 017import org.openstreetmap.josm.Main; 018import org.openstreetmap.josm.gui.widgets.JosmPasswordField; 019import org.openstreetmap.josm.gui.widgets.JosmTextField; 020import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator; 021import org.openstreetmap.josm.io.OsmApi; 022import org.openstreetmap.josm.io.auth.CredentialsAgent; 023import org.openstreetmap.josm.io.auth.CredentialsAgentException; 024import org.openstreetmap.josm.io.auth.CredentialsManager; 025 026/** 027 * The preferences panel for parameters necessary for the Basic Authentication 028 * Scheme. 029 * 030 */ 031public class BasicAuthenticationPreferencesPanel extends JPanel { 032 033 /** the OSM user name */ 034 private JosmTextField tfOsmUserName; 035 /** the OSM password */ 036 private JosmPasswordField tfOsmPassword; 037 /** a panel with further information, e.g. some warnings */ 038 private JPanel decorationPanel; 039 040 /** 041 * builds the UI 042 */ 043 protected final void build() { 044 setLayout(new GridBagLayout()); 045 setBorder(BorderFactory.createEmptyBorder(3,3,3,3)); 046 GridBagConstraints gc = new GridBagConstraints(); 047 048 // -- OSM user name 049 gc.fill = GridBagConstraints.HORIZONTAL; 050 gc.anchor = GridBagConstraints.NORTHWEST; 051 gc.weightx = 0.0; 052 gc.insets = new Insets(0,0,3,3); 053 add(new JLabel(tr("OSM username:")), gc); 054 055 gc.gridx = 1; 056 gc.weightx = 1.0; 057 add(tfOsmUserName = new JosmTextField(), gc); 058 SelectAllOnFocusGainedDecorator.decorate(tfOsmUserName); 059 UserNameValidator valUserName = new UserNameValidator(tfOsmUserName); 060 valUserName.validate(); 061 062 // -- OSM password 063 gc.gridx = 0; 064 gc.gridy = 1; 065 gc.weightx = 0.0; 066 add(new JLabel(tr("OSM password:")), gc); 067 068 gc.gridx = 1; 069 gc.weightx = 1.0; 070 add(tfOsmPassword = new JosmPasswordField(), gc); 071 SelectAllOnFocusGainedDecorator.decorate(tfOsmPassword); 072 tfOsmPassword.setToolTipText(tr("Please enter your OSM password")); 073 074 // -- an info panel with a warning message 075 gc.gridx = 0; 076 gc.gridy = 2; 077 gc.gridwidth = 2; 078 gc.weightx = 1.0; 079 gc.weighty = 1.0; 080 gc.insets = new Insets(5,0,0,0); 081 gc.fill = GridBagConstraints.BOTH; 082 decorationPanel = new JPanel(new BorderLayout()); 083 add(decorationPanel, gc); 084 } 085 086 /** 087 * Constructs a new {@code BasicAuthenticationPreferencesPanel}. 088 */ 089 public BasicAuthenticationPreferencesPanel() { 090 build(); 091 } 092 093 /** 094 * Inits contents from preferences. 095 */ 096 public void initFromPreferences() { 097 CredentialsAgent cm = CredentialsManager.getInstance(); 098 try { 099 decorationPanel.removeAll(); 100 decorationPanel.add(cm.getPreferencesDecorationPanel(), BorderLayout.CENTER); 101 PasswordAuthentication pa = cm.lookup(RequestorType.SERVER, OsmApi.getOsmApi().getHost()); 102 if (pa == null) { 103 tfOsmUserName.setText(""); 104 tfOsmPassword.setText(""); 105 } else { 106 tfOsmUserName.setText(pa.getUserName() == null? "" : pa.getUserName()); 107 tfOsmPassword.setText(pa.getPassword() == null ? "" : String.valueOf(pa.getPassword())); 108 } 109 } catch(CredentialsAgentException e) { 110 Main.error(e); 111 Main.warn(tr("Failed to retrieve OSM credentials from credential manager.")); 112 Main.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName())); 113 tfOsmUserName.setText(""); 114 tfOsmPassword.setText(""); 115 } 116 } 117 118 /** 119 * Saves contents to preferences. 120 */ 121 public void saveToPreferences() { 122 CredentialsAgent cm = CredentialsManager.getInstance(); 123 try { 124 PasswordAuthentication pa = new PasswordAuthentication( 125 tfOsmUserName.getText().trim(), 126 tfOsmPassword.getPassword() 127 ); 128 cm.store(RequestorType.SERVER, OsmApi.getOsmApi().getHost(), pa); 129 } catch (CredentialsAgentException e) { 130 Main.error(e); 131 Main.warn(tr("Failed to save OSM credentials to credential manager.")); 132 Main.warn(tr("Current credential manager is of type ''{0}''", cm.getClass().getName())); 133 } 134 } 135 136 /** 137 * Clears the password field. 138 */ 139 public void clearPassword() { 140 tfOsmPassword.setText(""); 141 } 142}