001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences.server;
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;
010import java.beans.PropertyChangeListener;
011
012import javax.swing.JPanel;
013import javax.swing.JTabbedPane;
014
015import org.openstreetmap.josm.gui.help.HelpUtil;
016import org.openstreetmap.josm.gui.preferences.DefaultTabPreferenceSetting;
017import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
018import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
019import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
020
021/**
022 * Connection preferences, including authentication and proxy sub-preferences.
023 */
024public final class ServerAccessPreference extends DefaultTabPreferenceSetting {
025
026    /**
027     * Factory used to create a new {@code ServerAccessPreference}.
028     */
029    public static class Factory implements PreferenceSettingFactory {
030        @Override
031        public PreferenceSetting createPreferenceSetting() {
032            return new ServerAccessPreference();
033        }
034    }
035
036    private ServerAccessPreference() {
037        super(/* ICON(preferences/) */ "connection", tr("Connection Settings"),
038                tr("Connection Settings for the OSM server."), false, new JTabbedPane());
039    }
040
041    /** indicates whether to use the default OSM URL or not */
042    private OsmApiUrlInputPanel pnlApiUrlPreferences;
043
044    /**
045     * Builds the tabbed pane with the server preferences
046     *
047     * @return panel with server preferences tabs
048     */
049    protected JPanel buildTabbedServerPreferences() {
050        JPanel pnl = new JPanel(new BorderLayout());
051        pnl.add(getTabPane(), BorderLayout.CENTER);
052        return pnl;
053    }
054
055    /**
056     * Builds the panel for entering the server access preferences
057     *
058     * @return preferences panel for server settings
059     */
060    protected JPanel buildContentPanel() {
061        JPanel pnl = new JPanel(new GridBagLayout());
062        GridBagConstraints gc = new GridBagConstraints();
063
064        // the checkbox for the default UL
065        gc.fill = GridBagConstraints.HORIZONTAL;
066        gc.anchor = GridBagConstraints.NORTHWEST;
067        gc.weightx = 1.0;
068        gc.insets = new Insets(0, 0, 0, 0);
069        pnlApiUrlPreferences = new OsmApiUrlInputPanel();
070        pnl.add(pnlApiUrlPreferences, gc);
071
072        // the remaining access properties
073        gc.gridy = 1;
074        gc.fill = GridBagConstraints.BOTH;
075        gc.weightx = 1.0;
076        gc.weighty = 1.0;
077        gc.insets = new Insets(10, 0, 3, 3);
078        pnl.add(buildTabbedServerPreferences(), gc);
079
080        HelpUtil.setHelpContext(pnl, HelpUtil.ht("/Preferences/Connection"));
081        return pnl;
082    }
083
084    /**
085     * Adds a listener that will be notified of API URL change.
086     * @param listener the listener
087     * @since 6523
088     */
089    public void addApiUrlChangeListener(PropertyChangeListener listener) {
090        pnlApiUrlPreferences.addPropertyChangeListener(listener);
091    }
092
093    @Override
094    public void addGui(PreferenceTabbedPane gui) {
095        GridBagConstraints gc = new GridBagConstraints();
096        gc.fill = GridBagConstraints.BOTH;
097        gc.weightx = 1.0;
098        gc.weighty = 1.0;
099        gc.anchor = GridBagConstraints.NORTHWEST;
100        gui.createPreferenceTab(this).add(buildContentPanel(), gc);
101
102        pnlApiUrlPreferences.initFromPreferences();
103    }
104
105    /**
106     * Saves the values to the preferences
107     */
108    @Override
109    public boolean ok() {
110        pnlApiUrlPreferences.saveToPreferences();
111        return false;
112    }
113}