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

File josm-testing-proxy-button.patch, 5.7 KB (added by alno, 8 years ago)

patch file generated with NetBeans 8 subversion client

  • org/openstreetmap/josm/gui/preferences/server/ProxyPreferencesPanel.java

    # This patch file was generated by NetBeans IDE
    # Following Index: paths are relative to: C:\Users\alexandre.nouvel\Documents\NetBeansProjects\trunk\src
    # This patch can be applied using context Tools: Patch action on respective folder.
    # It uses platform neutral UTF-8 encoding and \n newlines.
    # Above lines and this line are ignored by the patching process.
     
    99import java.awt.GridBagConstraints;
    1010import java.awt.GridBagLayout;
    1111import java.awt.Insets;
     12import java.awt.event.ActionEvent;
     13import java.awt.event.ActionListener;
    1214import java.awt.event.ItemEvent;
    1315import java.awt.event.ItemListener;
     16import java.io.IOException;
    1417import java.net.Authenticator.RequestorType;
    1518import java.net.PasswordAuthentication;
    1619import java.net.ProxySelector;
     
    2023
    2124import javax.swing.BorderFactory;
    2225import javax.swing.ButtonGroup;
     26import javax.swing.JButton;
    2327import javax.swing.JLabel;
    2428import javax.swing.JPanel;
    2529import javax.swing.JRadioButton;
     
    3539import org.openstreetmap.josm.io.auth.CredentialsAgentException;
    3640import org.openstreetmap.josm.io.auth.CredentialsManager;
    3741import org.openstreetmap.josm.tools.GBC;
     42import org.openstreetmap.josm.tools.WikiReader;
    3843
    3944/**
    4045 * Component allowing input of proxy settings.
     
    5459        /** Use HTTP proxy: JOSM will use the given SOCKS proxy */
    5560        USE_SOCKS_PROXY("use-socks-proxy");
    5661
    57         private String policyName;
     62        private final String policyName;
    5863        ProxyPolicy(String policyName) {
    5964            this.policyName = policyName;
    6065        }
     
    111116    private JPanel pnlHttpProxyConfigurationPanel;
    112117    private JPanel pnlSocksProxyConfigurationPanel;
    113118
     119    private final JButton bSaveAndTest = new JButton(tr("Save and test the settings"));
     120    private final JLabel lTestConnectionResult = new JLabel();
     121
    114122    /**
    115      * Builds the panel for the HTTP proxy configuration
     123     * Builds the panel for the HTTP proxy configuration.
    116124     *
    117125     * @return panel with HTTP proxy configuration
    118126     */
     
    232240        return pnl;
    233241    }
    234242
     243    /**
     244     * Builds the panel for the various proxy configurations and their options.
     245     *
     246     * @return panel with the different proxy configurations and their options
     247     */
    235248    protected final JPanel buildProxySettingsPanel() {
    236249        JPanel pnl = new JPanel(new GridBagLayout());
    237250        GridBagConstraints gc = new GridBagConstraints();
     
    312325        pnlSocksProxyConfigurationPanel = buildSocksProxyConfigurationPanel();
    313326        pnl.add(pnlSocksProxyConfigurationPanel, gc);
    314327
     328        // A button to save the settings and test the connection
     329        gc.gridx = 1;
     330        gc.gridy = 8;
     331        gc.weighty = 0.0;
     332        bSaveAndTest.addActionListener(new ProxySaveAndTestListener());
     333        pnl.add(bSaveAndTest, gc);
     334       
     335        // A label below the test button to report the test result
     336        gc.gridx = 1;
     337        gc.gridy = 9;
     338        gc.weighty = 0.0;
     339        pnl.add(lTestConnectionResult, gc);
     340
    315341        return pnl;
    316342    }
    317343
    318344    /**
    319      * Initializes the panel with the values from the preferences
     345     * Initializes the panel with the values from the preferences.
    320346     */
    321347    public final void initFromPreferences() {
    322348        String policy = Main.pref.get(PROXY_POLICY, null);
     
    371397    }
    372398
    373399    protected final void updateEnabledState() {
     400        lTestConnectionResult.setText("");
    374401        boolean isHttpProxy = rbProxyPolicy.get(ProxyPolicy.USE_HTTP_PROXY).isSelected();
    375402        for (Component c: pnlHttpProxyConfigurationPanel.getComponents()) {
    376403            c.setEnabled(isHttpProxy);
     
    392419    }
    393420
    394421    /**
     422     * Tests the stored proxy configuration by loading data from the wiki
     423     * startup page.
     424     *
     425     * @return {@code true} if the connection was successful, {@code false}
     426     * otherwise
     427     */
     428    protected final boolean testConnection() {
     429        try {
     430            // Let's try by loading something from the wiki
     431            String motd = "";
     432            motd = new WikiReader().readLang("StartupPage");
     433            return !"".equals(motd);
     434        } catch (IOException ex) {
     435            return false;
     436        }
     437    }
     438   
     439    /**
     440     * Saves the current settings and performs a connection test.
     441     */
     442    protected final void saveSettingsAndTestConnection() {
     443        saveToPreferences();
     444        final String report;
     445        if (testConnection()) {
     446            report = tr(
     447                "The settings are OK and JOSM was able to connect to the Internet.");
     448        } else {
     449            report = tr(
     450                "Unable to connect, please check your settings and your internet connection.");
     451        }
     452        lTestConnectionResult.setText(report);
     453    }
     454
     455    class ProxySaveAndTestListener implements ActionListener {
     456        @Override
     457        public void actionPerformed(ActionEvent arg0) {
     458            saveSettingsAndTestConnection();
     459        }
     460    }
     461
     462    /**
    395463     * Constructs a new {@code ProxyPreferencesPanel}.
    396464     */
    397465    public ProxyPreferencesPanel() {
     
    406474    }
    407475
    408476    /**
    409      * Saves the current values to the preferences
     477     * Saves the current values to the preferences.
    410478     */
    411479    public void saveToPreferences() {
    412480        ProxyPolicy policy = null;