001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences;
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;
010
011import javax.swing.JPanel;
012import javax.swing.JScrollPane;
013import javax.swing.JTabbedPane;
014
015import org.openstreetmap.josm.gui.help.HelpUtil;
016import org.openstreetmap.josm.gui.preferences.server.AuthenticationPreferencesPanel;
017import org.openstreetmap.josm.gui.preferences.server.OsmApiUrlInputPanel;
018import org.openstreetmap.josm.gui.preferences.server.ProxyPreferencesPanel;
019import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
020
021public final class ServerAccessPreference extends DefaultTabPreferenceSetting {
022
023    public static class Factory implements PreferenceSettingFactory {
024        @Override
025        public PreferenceSetting createPreferenceSetting() {
026            return new ServerAccessPreference();
027        }
028    }
029
030    private ServerAccessPreference() {
031        super("connection", tr("Connection Settings"), tr("Connection Settings for the OSM server."), false, new JTabbedPane());
032    }
033
034    private OsmApiUrlInputPanel pnlApiUrlPreferences;
035
036    /** indicates whether to use the default OSM URL or not */
037    /** panel for configuring authentication preferences */
038    private AuthenticationPreferencesPanel pnlAuthPreferences;
039    /** panel for configuring proxy preferences */
040    private ProxyPreferencesPanel pnlProxyPreferences;
041
042    /**
043     * Embeds a vertically scrollable panel in a {@link JScrollPane}
044     * @param panel the panel
045     * @return the scroll pane
046     */
047    protected JScrollPane wrapVerticallyScrollablePanel(VerticallyScrollablePanel panel) {
048        JScrollPane sp = new JScrollPane(panel);
049        sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
050        sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
051        return sp;
052    }
053
054    /**
055     * Builds the tabbed pane with the server preferences
056     *
057     * @return panel with server preferences tabs
058     */
059    protected JPanel buildTabbedServerPreferences() {
060        JPanel pnl = new JPanel(new BorderLayout());
061
062        JTabbedPane tpServerPreferences = getTabPane();
063        pnlAuthPreferences = new AuthenticationPreferencesPanel();
064        tpServerPreferences.add(wrapVerticallyScrollablePanel(pnlAuthPreferences));
065        pnlProxyPreferences = new ProxyPreferencesPanel();
066        tpServerPreferences.add(wrapVerticallyScrollablePanel(pnlProxyPreferences));
067
068        tpServerPreferences.setTitleAt(0, tr("Authentication"));
069        tpServerPreferences.setTitleAt(1, tr("Proxy settings"));
070        tpServerPreferences.setToolTipTextAt(0, tr("Configure your identity and how to authenticate at the OSM server"));
071        tpServerPreferences.setToolTipTextAt(1, tr("Configure whether to use a proxy server"));
072
073        pnl.add(tpServerPreferences, BorderLayout.CENTER);
074        return pnl;
075    }
076
077    /**
078     * Builds the panel for entering the server access preferences
079     *
080     * @return preferences panel for server settings
081     */
082    protected JPanel buildContentPanel() {
083        JPanel pnl = new JPanel(new GridBagLayout());
084        GridBagConstraints gc = new GridBagConstraints();
085
086        // the checkbox for the default UL
087        gc.fill = GridBagConstraints.HORIZONTAL;
088        gc.anchor = GridBagConstraints.NORTHWEST;
089        gc.weightx = 1.0;
090        gc.insets = new Insets(0,0,0,0);
091        pnl.add(pnlApiUrlPreferences = new OsmApiUrlInputPanel(), gc);
092
093        // the remaining access properties
094        gc.gridy = 1;
095        gc.fill = GridBagConstraints.BOTH;
096        gc.weightx = 1.0;
097        gc.weighty = 1.0;
098        gc.insets = new Insets(10,0,3,3);
099        pnl.add(buildTabbedServerPreferences(), gc);
100
101        // let the AuthPreferencesPanel know when the API URL changes
102        //
103        pnlApiUrlPreferences.addPropertyChangeListener(pnlAuthPreferences);
104
105        HelpUtil.setHelpContext(pnl, HelpUtil.ht("/Preferences/Connection"));
106        return pnl;
107    }
108
109    @Override
110    public void addGui(PreferenceTabbedPane gui) {
111        GridBagConstraints gc = new GridBagConstraints();
112        gc.fill = GridBagConstraints.BOTH;
113        gc.weightx = 1.0;
114        gc.weighty = 1.0;
115        gc.anchor = GridBagConstraints.NORTHWEST;
116        gui.createPreferenceTab(this).add(buildContentPanel(), gc);
117
118        initFromPreferences();
119    }
120
121    /**
122     * Initializes the configuration panel with values from the preferences
123     */
124    public void initFromPreferences() {
125        pnlApiUrlPreferences.initFromPreferences();
126        pnlAuthPreferences.initFromPreferences();
127        pnlProxyPreferences.initFromPreferences();
128    }
129
130    /**
131     * Saves the values to the preferences
132     */
133    @Override
134    public boolean ok() {
135        pnlApiUrlPreferences.saveToPreferences();
136        pnlAuthPreferences.saveToPreferences();
137        pnlProxyPreferences.saveToPreferences();
138        return false;
139    }
140}