1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
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.ActionEvent;
|
---|
10 | import java.awt.event.ActionListener;
|
---|
11 | import java.util.LinkedHashMap;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Map.Entry;
|
---|
14 |
|
---|
15 | import javax.swing.BorderFactory;
|
---|
16 | import javax.swing.Box;
|
---|
17 | import javax.swing.JCheckBox;
|
---|
18 | import javax.swing.JLabel;
|
---|
19 | import javax.swing.JPanel;
|
---|
20 | import javax.swing.JSeparator;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
24 | import org.openstreetmap.josm.io.remotecontrol.PermissionPrefWithDefault;
|
---|
25 | import org.openstreetmap.josm.io.remotecontrol.RemoteControl;
|
---|
26 | import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler;
|
---|
27 | import org.openstreetmap.josm.tools.GBC;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Preference settings for the Remote Control plugin
|
---|
31 | *
|
---|
32 | * @author Frederik Ramm
|
---|
33 | */
|
---|
34 | public class RemoteControlPreference extends DefaultTabPreferenceSetting {
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Factory used to build a new instance of this preference setting
|
---|
38 | */
|
---|
39 | public static class Factory implements PreferenceSettingFactory {
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public PreferenceSetting createPreferenceSetting() {
|
---|
43 | return new RemoteControlPreference();
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | private RemoteControlPreference() {
|
---|
48 | super("remotecontrol", tr("Remote Control"), tr("Settings for the remote control feature."));
|
---|
49 | for (PermissionPrefWithDefault p : PermissionPrefWithDefault.getPermissionPrefs()) {
|
---|
50 | JCheckBox cb = new JCheckBox(p.preferenceText);
|
---|
51 | cb.setSelected(p.isAllowed());
|
---|
52 | prefs.put(p, cb);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | private final Map<PermissionPrefWithDefault, JCheckBox> prefs =
|
---|
56 | new LinkedHashMap<PermissionPrefWithDefault, JCheckBox>();
|
---|
57 | private JCheckBox enableRemoteControl;
|
---|
58 | private JCheckBox loadInNewLayer = new JCheckBox(tr("Download objects to new layer"));
|
---|
59 | private JCheckBox alwaysAskUserConfirm = new JCheckBox(tr("Confirm all Remote Control actions manually"));
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public void addGui(final PreferenceTabbedPane gui) {
|
---|
63 |
|
---|
64 | JPanel remote = new JPanel(new GridBagLayout());
|
---|
65 |
|
---|
66 | final JLabel descLabel = new JLabel("<html>"
|
---|
67 | + tr("Allows JOSM to be controlled from other applications, e.g. from a web browser.")
|
---|
68 | + "</html>");
|
---|
69 | descLabel.setFont(descLabel.getFont().deriveFont(Font.PLAIN));
|
---|
70 | remote.add(descLabel, GBC.eol().insets(5, 5, 0, 10).fill(GBC.HORIZONTAL));
|
---|
71 |
|
---|
72 | final JLabel portLabel = new JLabel("<html>" + tr("JOSM will always listen at <b>port 8111</b> on localhost. "
|
---|
73 | + "<br>This port is not configurable because it is referenced by external applications talking to JOSM.") + "</html>");
|
---|
74 | portLabel.setFont(portLabel.getFont().deriveFont(Font.PLAIN));
|
---|
75 | remote.add(portLabel, GBC.eol().insets(5, 5, 0, 10).fill(GBC.HORIZONTAL));
|
---|
76 |
|
---|
77 | remote.add(enableRemoteControl = new JCheckBox(tr("Enable remote control"), RemoteControl.PROP_REMOTECONTROL_ENABLED.get()), GBC.eol());
|
---|
78 |
|
---|
79 | final JPanel wrapper = new JPanel();
|
---|
80 | wrapper.setLayout(new GridBagLayout());
|
---|
81 | wrapper.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.gray)));
|
---|
82 |
|
---|
83 | remote.add(wrapper, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 5, 5, 5));
|
---|
84 |
|
---|
85 | wrapper.add(new JLabel(tr("Permitted actions:")), GBC.eol());
|
---|
86 | int INDENT = 15;
|
---|
87 | for (JCheckBox p : prefs.values()) {
|
---|
88 | wrapper.add(p, GBC.eol().insets(INDENT, 5, 0, 0).fill(GBC.HORIZONTAL));
|
---|
89 | }
|
---|
90 |
|
---|
91 | wrapper.add(new JSeparator(), GBC.eop().fill(GBC.HORIZONTAL).insets(15, 5, 15, 5));
|
---|
92 | wrapper.add(loadInNewLayer, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
93 | wrapper.add(alwaysAskUserConfirm, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
94 |
|
---|
95 | remote.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.VERTICAL));
|
---|
96 |
|
---|
97 | loadInNewLayer.setSelected(Main.pref.getBoolean(RequestHandler.loadInNewLayerKey, RequestHandler.loadInNewLayerDefault));
|
---|
98 | alwaysAskUserConfirm.setSelected(Main.pref.getBoolean(RequestHandler.globalConfirmationKey, RequestHandler.globalConfirmationDefault));
|
---|
99 |
|
---|
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 | }
|
---|