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