| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.preferences.remotecontrol;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.Color;
|
|---|
| 7 | import java.awt.Font;
|
|---|
| 8 | import java.awt.GridBagLayout;
|
|---|
| 9 | import java.awt.event.ActionListener;
|
|---|
| 10 | import java.util.LinkedHashMap;
|
|---|
| 11 | import java.util.Map;
|
|---|
| 12 | import java.util.Map.Entry;
|
|---|
| 13 |
|
|---|
| 14 | import javax.swing.BorderFactory;
|
|---|
| 15 | import javax.swing.Box;
|
|---|
| 16 | import javax.swing.JCheckBox;
|
|---|
| 17 | import javax.swing.JLabel;
|
|---|
| 18 | import javax.swing.JPanel;
|
|---|
| 19 | import javax.swing.JSeparator;
|
|---|
| 20 |
|
|---|
| 21 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
|---|
| 22 | import org.openstreetmap.josm.gui.preferences.DefaultTabPreferenceSetting;
|
|---|
| 23 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
|---|
| 24 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
|---|
| 25 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
|---|
| 26 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
|---|
| 27 | import org.openstreetmap.josm.gui.widgets.VerticallyScrollablePanel;
|
|---|
| 28 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
|---|
| 29 | import org.openstreetmap.josm.io.remotecontrol.RemoteControl;
|
|---|
| 30 | import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler;
|
|---|
| 31 | import org.openstreetmap.josm.spi.preferences.Config;
|
|---|
| 32 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Preference settings for Remote Control.
|
|---|
| 36 | *
|
|---|
| 37 | * @author Frederik Ramm
|
|---|
| 38 | */
|
|---|
| 39 | public final class RemoteControlPreference extends DefaultTabPreferenceSetting {
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Factory used to build a new instance of this preference setting
|
|---|
| 43 | */
|
|---|
| 44 | public static class Factory implements PreferenceSettingFactory {
|
|---|
| 45 |
|
|---|
| 46 | @Override
|
|---|
| 47 | public PreferenceSetting createPreferenceSetting() {
|
|---|
| 48 | return new RemoteControlPreference();
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | private RemoteControlPreference() {
|
|---|
| 53 | super(/* ICON(preferences/) */ "remotecontrol", tr("Remote Control"), tr("Settings for the remote control feature."));
|
|---|
| 54 | for (PermissionPrefWithDefault p : PermissionPrefWithDefault.getPermissionPrefs()) {
|
|---|
| 55 | JCheckBox cb = new JCheckBox(p.preferenceText);
|
|---|
| 56 | cb.setSelected(p.isAllowed());
|
|---|
| 57 | prefs.put(p, cb);
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | private final Map<PermissionPrefWithDefault, JCheckBox> prefs = new LinkedHashMap<>();
|
|---|
| 62 | private JCheckBox enableRemoteControl;
|
|---|
| 63 |
|
|---|
| 64 | private final JCheckBox loadInNewLayer = new JCheckBox(tr("Download as new layer"));
|
|---|
| 65 | private final JCheckBox alwaysAskUserConfirm = new JCheckBox(tr("Confirm all Remote Control actions manually"));
|
|---|
| 66 |
|
|---|
| 67 | @Override
|
|---|
| 68 | public void addGui(final PreferenceTabbedPane gui) {
|
|---|
| 69 |
|
|---|
| 70 | JPanel remote = new VerticallyScrollablePanel(new GridBagLayout());
|
|---|
| 71 |
|
|---|
| 72 | final JLabel descLabel = new JLabel("<html>"
|
|---|
| 73 | + tr("Allows JOSM to be controlled from other applications, e.g. from a web browser.")
|
|---|
| 74 | + "</html>");
|
|---|
| 75 | descLabel.setFont(descLabel.getFont().deriveFont(Font.PLAIN));
|
|---|
| 76 | remote.add(descLabel, GBC.eol().insets(5, 5, 0, 10).fill(GBC.HORIZONTAL));
|
|---|
| 77 |
|
|---|
| 78 | final JLabel portLabel = new JLabel("<html>"
|
|---|
| 79 | + tr("JOSM will always listen at <b>port {0}</b> (http) on localhost."
|
|---|
| 80 | + "<br>This port is not configurable because it is referenced by external applications talking to JOSM.",
|
|---|
| 81 | Config.getPref().get("remote.control.port", "8111")) + "</html>");
|
|---|
| 82 | portLabel.setFont(portLabel.getFont().deriveFont(Font.PLAIN));
|
|---|
| 83 | remote.add(portLabel, GBC.eol().insets(5, 5, 0, 10).fill(GBC.HORIZONTAL));
|
|---|
| 84 |
|
|---|
| 85 | enableRemoteControl = new JCheckBox(tr("Enable remote control"), RemoteControl.PROP_REMOTECONTROL_ENABLED.get());
|
|---|
| 86 | remote.add(enableRemoteControl, GBC.eol());
|
|---|
| 87 |
|
|---|
| 88 | final JPanel wrapper = new JPanel(new GridBagLayout());
|
|---|
| 89 | wrapper.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray)));
|
|---|
| 90 |
|
|---|
| 91 | remote.add(wrapper, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 5, 5, 5));
|
|---|
| 92 |
|
|---|
| 93 | wrapper.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL).insets(15, 5, 15, 5));
|
|---|
| 94 |
|
|---|
| 95 | wrapper.add(new JLabel(tr("Permitted actions:")), GBC.eol().insets(5, 0, 0, 0));
|
|---|
| 96 | for (JCheckBox p : prefs.values()) {
|
|---|
| 97 | wrapper.add(p, GBC.eol().insets(15, 5, 0, 0).fill(GBC.HORIZONTAL));
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | wrapper.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL).insets(15, 5, 15, 5));
|
|---|
| 101 | wrapper.add(loadInNewLayer, GBC.eol().fill(GBC.HORIZONTAL));
|
|---|
| 102 | wrapper.add(alwaysAskUserConfirm, GBC.eol().fill(GBC.HORIZONTAL));
|
|---|
| 103 |
|
|---|
| 104 | remote.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
|
|---|
| 105 |
|
|---|
| 106 | loadInNewLayer.setSelected(RequestHandler.LOAD_IN_NEW_LAYER.get());
|
|---|
| 107 | alwaysAskUserConfirm.setSelected(RequestHandler.GLOBAL_CONFIRMATION.get());
|
|---|
| 108 |
|
|---|
| 109 | ActionListener remoteControlEnabled = e -> GuiHelper.setEnabledRec(wrapper, enableRemoteControl.isSelected());
|
|---|
| 110 | enableRemoteControl.addActionListener(remoteControlEnabled);
|
|---|
| 111 | remoteControlEnabled.actionPerformed(null);
|
|---|
| 112 | createPreferenceTabWithScrollPane(gui, remote);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | @Override
|
|---|
| 116 | public boolean ok() {
|
|---|
| 117 | boolean enabled = enableRemoteControl.isSelected();
|
|---|
| 118 | boolean changed = RemoteControl.PROP_REMOTECONTROL_ENABLED.put(enabled);
|
|---|
| 119 | if (enabled) {
|
|---|
| 120 | for (Entry<PermissionPrefWithDefault, JCheckBox> p : prefs.entrySet()) {
|
|---|
| 121 | Config.getPref().putBoolean(p.getKey().pref, p.getValue().isSelected());
|
|---|
| 122 | }
|
|---|
| 123 | RequestHandler.LOAD_IN_NEW_LAYER.put(loadInNewLayer.isSelected());
|
|---|
| 124 | RequestHandler.GLOBAL_CONFIRMATION.put(alwaysAskUserConfirm.isSelected());
|
|---|
| 125 | }
|
|---|
| 126 | if (changed) {
|
|---|
| 127 | if (enabled) {
|
|---|
| 128 | RemoteControl.start();
|
|---|
| 129 | } else {
|
|---|
| 130 | RemoteControl.stop();
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 | return false;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | @Override
|
|---|
| 137 | public String getHelpContext() {
|
|---|
| 138 | return HelpUtil.ht("/Preferences/RemoteControl");
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|