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.Color;
007import java.awt.Font;
008import java.awt.GridBagLayout;
009import java.awt.event.ActionEvent;
010import java.awt.event.ActionListener;
011import java.util.LinkedHashMap;
012import java.util.Map;
013import java.util.Map.Entry;
014
015import javax.swing.BorderFactory;
016import javax.swing.Box;
017import javax.swing.JCheckBox;
018import javax.swing.JLabel;
019import javax.swing.JPanel;
020import javax.swing.JSeparator;
021
022import org.openstreetmap.josm.Main;
023import org.openstreetmap.josm.gui.util.GuiHelper;
024import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
025import org.openstreetmap.josm.io.remotecontrol.RemoteControl;
026import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler;
027import org.openstreetmap.josm.tools.GBC;
028
029/**
030 * Preference settings for the Remote Control plugin
031 *
032 * @author Frederik Ramm
033 */
034public final class RemoteControlPreference extends DefaultTabPreferenceSetting {
035
036    /**
037     * Factory used to build a new instance of this preference setting
038     */
039    public static class Factory implements PreferenceSettingFactory {
040
041        @Override
042        public PreferenceSetting createPreferenceSetting() {
043            return new RemoteControlPreference();
044        }
045    }
046
047    private RemoteControlPreference() {
048        super("remotecontrol", tr("Remote Control"), tr("Settings for the remote control feature."));
049        for (PermissionPrefWithDefault p : PermissionPrefWithDefault.getPermissionPrefs()) {
050            JCheckBox cb = new JCheckBox(p.preferenceText);
051            cb.setSelected(p.isAllowed());
052            prefs.put(p, cb);
053        }
054    }
055    private final Map<PermissionPrefWithDefault, JCheckBox> prefs =
056            new LinkedHashMap<PermissionPrefWithDefault, JCheckBox>();
057    private JCheckBox enableRemoteControl;
058    private JCheckBox loadInNewLayer = new JCheckBox(tr("Download objects to new layer"));
059    private JCheckBox alwaysAskUserConfirm = new JCheckBox(tr("Confirm all Remote Control actions manually"));
060
061    @Override
062    public void addGui(final PreferenceTabbedPane gui) {
063
064        JPanel remote = new JPanel(new GridBagLayout());
065
066        final JLabel descLabel = new JLabel("<html>"
067                + tr("Allows JOSM to be controlled from other applications, e.g. from a web browser.")
068                + "</html>");
069        descLabel.setFont(descLabel.getFont().deriveFont(Font.PLAIN));
070        remote.add(descLabel, GBC.eol().insets(5, 5, 0, 10).fill(GBC.HORIZONTAL));
071
072        final JLabel portLabel = new JLabel("<html>" + tr("JOSM will always listen at <b>port 8111</b> on localhost. "
073                + "<br>This port is not configurable because it is referenced by external applications talking to JOSM.") + "</html>");
074        portLabel.setFont(portLabel.getFont().deriveFont(Font.PLAIN));
075        remote.add(portLabel, GBC.eol().insets(5, 5, 0, 10).fill(GBC.HORIZONTAL));
076
077        remote.add(enableRemoteControl = new JCheckBox(tr("Enable remote control"), RemoteControl.PROP_REMOTECONTROL_ENABLED.get()), GBC.eol());
078
079        final JPanel wrapper = new JPanel();
080        wrapper.setLayout(new GridBagLayout());
081        wrapper.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray)));
082
083        remote.add(wrapper, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 5, 5, 5));
084
085        wrapper.add(new JLabel(tr("Permitted actions:")), GBC.eol());
086        int INDENT = 15;
087        for (JCheckBox p : prefs.values()) {
088            wrapper.add(p, GBC.eol().insets(INDENT, 5, 0, 0).fill(GBC.HORIZONTAL));
089        }
090
091        wrapper.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL).insets(15, 5, 15, 5));
092        wrapper.add(loadInNewLayer, GBC.eol().fill(GBC.HORIZONTAL));
093        wrapper.add(alwaysAskUserConfirm, GBC.eol().fill(GBC.HORIZONTAL));
094
095        remote.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
096
097        loadInNewLayer.setSelected(Main.pref.getBoolean(RequestHandler.loadInNewLayerKey, RequestHandler.loadInNewLayerDefault));
098        alwaysAskUserConfirm.setSelected(Main.pref.getBoolean(RequestHandler.globalConfirmationKey, RequestHandler.globalConfirmationDefault));
099
100        ActionListener remoteControlEnabled = new ActionListener() {
101
102            @Override
103            public void actionPerformed(ActionEvent e) {
104                GuiHelper.setEnabledRec(wrapper, enableRemoteControl.isSelected());
105                // 'setEnabled(false)' does not work for JLabel with html text, so do it manually
106                // FIXME: use QuadStateCheckBox to make checkboxes unset when disabled
107            }
108        };
109        enableRemoteControl.addActionListener(remoteControlEnabled);
110        remoteControlEnabled.actionPerformed(null);
111        createPreferenceTabWithScrollPane(gui, remote);
112    }
113
114    @Override
115    public boolean ok() {
116        boolean enabled = enableRemoteControl.isSelected();
117        boolean changed = RemoteControl.PROP_REMOTECONTROL_ENABLED.put(enabled);
118        if (enabled) {
119            for (Entry<PermissionPrefWithDefault, JCheckBox> p : prefs.entrySet()) {
120                Main.pref.put(p.getKey().pref, p.getValue().isSelected());
121            }
122            Main.pref.put(RequestHandler.loadInNewLayerKey, loadInNewLayer.isSelected());
123            Main.pref.put(RequestHandler.globalConfirmationKey, alwaysAskUserConfirm.isSelected());
124        }
125        if (changed) {
126            if (enabled) {
127                RemoteControl.start();
128            } else {
129                RemoteControl.stop();
130            }
131        }
132        return false;
133    }
134}