Ticket #13765: josm-testing-proxy-button-v2.patch

File josm-testing-proxy-button-v2.patch, 5.1 KB (added by simon04, 8 years ago)
  • src/org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java

    diff --git a/src/org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java b/src/org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java
    index dad6e01..08a8b77 100644
    a b  
    1111import java.awt.Insets;
    1212import java.awt.event.ItemEvent;
    1313import java.awt.event.ItemListener;
     14import java.io.IOException;
    1415import java.net.Authenticator.RequestorType;
    1516import java.net.PasswordAuthentication;
    1617import java.net.ProxySelector;
     
    2021
    2122import javax.swing.BorderFactory;
    2223import javax.swing.ButtonGroup;
     24import javax.swing.JButton;
    2325import javax.swing.JLabel;
    2426import javax.swing.JPanel;
    2527import javax.swing.JRadioButton;
     
    3537import org.openstreetmap.josm.io.auth.CredentialsAgentException;
    3638import org.openstreetmap.josm.io.auth.CredentialsManager;
    3739import org.openstreetmap.josm.tools.GBC;
     40import org.openstreetmap.josm.tools.WikiReader;
    3841
    3942/**
    4043 * Component allowing input of proxy settings.
     
    5457        /** Use HTTP proxy: JOSM will use the given SOCKS proxy */
    5558        USE_SOCKS_PROXY("use-socks-proxy");
    5659
    57         private String policyName;
     60        private final String policyName;
    5861        ProxyPolicy(String policyName) {
    5962            this.policyName = policyName;
    6063        }
    public static ProxyPolicy fromName(String policyName) {  
    111114    private JPanel pnlHttpProxyConfigurationPanel;
    112115    private JPanel pnlSocksProxyConfigurationPanel;
    113116
     117    private final JButton bSaveAndTest = new JButton(tr("Save and test the settings"));
     118    private final JLabel lTestConnectionResult = new JLabel();
     119
    114120    /**
    115      * Builds the panel for the HTTP proxy configuration
     121     * Builds the panel for the HTTP proxy configuration.
    116122     *
    117123     * @return panel with HTTP proxy configuration
    118124     */
    public Dimension getMinimumSize() {  
    232238        return pnl;
    233239    }
    234240
     241    /**
     242     * Builds the panel for the various proxy configurations and their options.
     243     *
     244     * @return panel with the different proxy configurations and their options
     245     */
    235246    protected final JPanel buildProxySettingsPanel() {
    236247        JPanel pnl = new JPanel(new GridBagLayout());
    237248        GridBagConstraints gc = new GridBagConstraints();
    protected final JPanel buildProxySettingsPanel() {  
    312323        pnlSocksProxyConfigurationPanel = buildSocksProxyConfigurationPanel();
    313324        pnl.add(pnlSocksProxyConfigurationPanel, gc);
    314325
     326        // A button to save the settings and test the connection
     327        gc.gridx = 1;
     328        gc.gridy = 8;
     329        gc.weighty = 0.0;
     330        bSaveAndTest.addActionListener(action -> saveSettingsAndTestConnection());
     331        pnl.add(bSaveAndTest, gc);
     332       
     333        // A label below the test button to report the test result
     334        gc.gridx = 1;
     335        gc.gridy = 9;
     336        gc.weighty = 0.0;
     337        pnl.add(lTestConnectionResult, gc);
     338
    315339        return pnl;
    316340    }
    317341
    318342    /**
    319      * Initializes the panel with the values from the preferences
     343     * Initializes the panel with the values from the preferences.
    320344     */
    321345    public final void initFromPreferences() {
    322346        String policy = Main.pref.get(PROXY_POLICY, null);
    public final void initFromPreferences() {  
    371395    }
    372396
    373397    protected final void updateEnabledState() {
     398        lTestConnectionResult.setText("");
    374399        boolean isHttpProxy = rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY).isSelected();
    375400        for (Component c: pnlHttpProxyConfigurationPanel.getComponents()) {
    376401            c.setEnabled(isHttpProxy);
    public void itemStateChanged(ItemEvent arg0) {  
    392417    }
    393418
    394419    /**
     420     * Tests the stored proxy configuration by loading data from the wiki startup page.
     421     *
     422     * @return {@code true} if the connection was successful, {@code false} otherwise
     423     */
     424    protected final boolean testConnection() {
     425        try {
     426            // Let's try by loading something from the wiki
     427            return !new WikiReader().readLang("StartupPage").isEmpty();
     428        } catch (IOException ex) {
     429            return false;
     430        }
     431    }
     432   
     433    /**
     434     * Saves the current settings and performs a connection test.
     435     */
     436    protected final void saveSettingsAndTestConnection() {
     437        saveToPreferences();
     438        final String report;
     439        if (testConnection()) {
     440            report = tr("The settings are OK and JOSM was able to connect to the Internet.");
     441        } else {
     442            report = tr("Unable to connect, please check your settings and your internet connection.");
     443        }
     444        lTestConnectionResult.setText(report);
     445    }
     446
     447    /**
    395448     * Constructs a new {@code ProxyPreferencesPanel}.
    396449     */
    397450    public ProxyPreferencesPanel() {
    public ProxyPreferencesPanel() {  
    406459    }
    407460
    408461    /**
    409      * Saves the current values to the preferences
     462     * Saves the current values to the preferences.
    410463     */
    411464    public void saveToPreferences() {
    412465        ProxyPolicy policy = null;