yPanel.addSpace(5);
this.fileURL = fileURL;
// Retrieve guest credentials (if any)
Credentials guestCredentials = fileURL.getGuestCredentials();
// Fetch credentials from the specified FileURL (if any) and use them only if they're different from the guest ones
Credentials urlCredentials = fileURL.getCredentials();
if(urlCredentials!=null && guestCredentials!=null && urlCredentials.equals(guestCredentials))
urlCredentials = null;
// Retrieve a list of credentials matching the URL from CredentialsManager
credentialsMappings = CredentialsManager.getMatchingCredentials(fileURL);
XAlignedComponentPanel compPanel = new XAlignedComponentPanel(10);
// Connect as Guest/User radio buttons, displayed only if the URL has guest credentials
if(guestCredentials!=null) {
guestRadioButton = new JRadioButton(StringUtils.capitalize(guestCredentials.getLogin()));
guestRadioButton.addActionListener(this);
compPanel.addRow(Translator.get("auth_dialog.connect_as"), guestRadioButton, 0);
userRadioButton = new JRadioButton(Translator.get("user"));
userRadioButton.addActionListener(this);
compPanel.addRow("", userRadioButton, 15);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(guestRadioButton);
buttonGroup.add(userRadioButton);
}
// If not, display an introduction label ("please enter a login and password")
else {
yPanel.add(new JLabel(Translator.get("auth_dialog.desc")));
yPanel.addSpace(15);
}
// Server URL for which the user has to authenticate
compPanel.addRow(Translator.get("auth_dialog.server"), new JLabel(fileURL.toString(false)), 10);
// Login field: create either a text field or an editable combo box, depending on whether
// CredentialsManager returned matches (-> combo box) or not (-> text field).
int nbCredentials = credentialsMappings.length;
JComponent loginComponent;
if(nbCredentials>0) {
// Editable combo box
loginComboBox = new EditableComboBox();
this.loginField = loginComboBox.getTextField();
// Add credentials to the combo box's choices
for(int i=0; i<nbCredentials; i++)
loginComboBox.addItem(credentialsMappings[i].getCredentials().getLogin());
loginComboBox.addEditableComboBoxListener(this);
loginComponent = loginComboBox;
}
else {
// Simple text field
loginField = new JTextField();
loginComponent = loginField;
}
compPanel.addRow(Translator.get("login"), loginComponent, 5);
// Create password field
this.passwordField = new JPasswordField();
passwordField.addActionListener(this);
compPanel.addRow(Translator.get("password"), passwordField, 10);
// Contains the credentials to set in the login and password text fields
Credentials selectedCredentials = null;
// Whether the 'save credentials' checkbox should be enabled
boolean saveCredentialsCheckBoxSelected = false;
// If the provided URL contains credentials, use them
if(urlCredentials!=null) {
selectedCredentials = urlCredentials;
}
// Else if CredentialsManager had matching credentials, use the best ones
else if(nbCredentials>0) {
CredentialsMapping bestCredentialsMapping = credentialsMappings[0];
selectedCredentials = bestCredentialsMapping.getCredentials();
saveCredentialsCheckBoxSelected = bestCredentialsMapping.isPersistent();
}
yPanel.add(compPanel);
this.saveCredentialsCheckBox = new JCheckBox(Translator.get("auth_dialog.store_credentials"), saveCredentialsCheckBoxSelected);
yPanel.add(saveCredentialsCheckBox);
yPanel.addSpace(5);
contentPane.add(yPanel, BorderLayout.CENTER);
// If we have some existing credentials for this location...
if(selectedCredentials!=null) {
// Prefill the login and password fields with the selected credentials
loginField.setText(selectedCredentials.getLogin());
passwordField.setText(selectedCredentials.getPassword());
// Select the text fields' so their content can be erased just by typing the replacement string
loginField.selectAll();
passwordField.selectAll();