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    /** indicates whether to use the default OSM URL or not */
037    private final OsmApiUrlInputPanel pnlApiUrlPreferences = new OsmApiUrlInputPanel();
038
039    private ServerAccessPreference() {
040        super(/* ICON(preferences/) */ "connection", tr("Connection Settings"),
041                tr("Connection Settings for the OSM server."), false, new JTabbedPane());
042    }
043
044    /**
045     * Builds the tabbed pane with the server preferences
046     *
047     * @return panel with server preferences tabs
048     */
049    private 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    private 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        pnl.add(pnlApiUrlPreferences, gc);
070
071        // the remaining access properties
072        gc.gridy = 1;
073        gc.fill = GridBagConstraints.BOTH;
074        gc.weightx = 1.0;
075        gc.weighty = 1.0;
076        gc.insets = new Insets(10, 0, 3, 3);
077        pnl.add(buildTabbedServerPreferences(), gc);
078
079        HelpUtil.setHelpContext(pnl, HelpUtil.ht("/Preferences/Connection"));
080        return pnl;
081    }
082
083    /**
084     * Adds a listener that will be notified of API URL change.
085     * @param listener the listener
086     * @since 6523
087     */
088    public void addApiUrlChangeListener(PropertyChangeListener listener) {
089        pnlApiUrlPreferences.addPropertyChangeListener(listener);
090    }
091
092    @Override
093    public void addGui(PreferenceTabbedPane gui) {
094        GridBagConstraints gc = new GridBagConstraints();
095        gc.fill = GridBagConstraints.BOTH;
096        gc.weightx = 1.0;
097        gc.weighty = 1.0;
098        gc.anchor = GridBagConstraints.NORTHWEST;
099        gui.createPreferenceTab(this).add(buildContentPanel(), gc);
100
101        pnlApiUrlPreferences.initFromPreferences();
102    }
103
104    /**
105     * Saves the values to the preferences
106     */
107    @Override
108    public boolean ok() {
109        pnlApiUrlPreferences.saveToPreferences();
110        return false;
111    }
112}