1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.advanced;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.io.File;
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.util.ArrayList;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.Comparator;
|
---|
15 | import java.util.LinkedHashMap;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Locale;
|
---|
18 | import java.util.Map;
|
---|
19 | import java.util.Map.Entry;
|
---|
20 | import java.util.Objects;
|
---|
21 |
|
---|
22 | import javax.swing.AbstractAction;
|
---|
23 | import javax.swing.Box;
|
---|
24 | import javax.swing.JButton;
|
---|
25 | import javax.swing.JFileChooser;
|
---|
26 | import javax.swing.JLabel;
|
---|
27 | import javax.swing.JMenu;
|
---|
28 | import javax.swing.JOptionPane;
|
---|
29 | import javax.swing.JPanel;
|
---|
30 | import javax.swing.JPopupMenu;
|
---|
31 | import javax.swing.JScrollPane;
|
---|
32 | import javax.swing.event.DocumentEvent;
|
---|
33 | import javax.swing.event.DocumentListener;
|
---|
34 | import javax.swing.event.MenuEvent;
|
---|
35 | import javax.swing.event.MenuListener;
|
---|
36 | import javax.swing.filechooser.FileFilter;
|
---|
37 |
|
---|
38 | import org.openstreetmap.josm.Main;
|
---|
39 | import org.openstreetmap.josm.actions.DiskAccessAction;
|
---|
40 | import org.openstreetmap.josm.data.CustomConfigurator;
|
---|
41 | import org.openstreetmap.josm.data.Preferences;
|
---|
42 | import org.openstreetmap.josm.data.Preferences.Setting;
|
---|
43 | import org.openstreetmap.josm.gui.actionsupport.LogShowDialog;
|
---|
44 | import org.openstreetmap.josm.gui.preferences.DefaultTabPreferenceSetting;
|
---|
45 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
46 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
47 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
48 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
49 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
---|
50 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
51 | import org.openstreetmap.josm.tools.GBC;
|
---|
52 | import org.openstreetmap.josm.tools.Utils;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Advanced preferences, allowing to set preference entries directly.
|
---|
56 | */
|
---|
57 | public final class AdvancedPreference extends DefaultTabPreferenceSetting {
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Factory used to create a new {@code AdvancedPreference}.
|
---|
61 | */
|
---|
62 | public static class Factory implements PreferenceSettingFactory {
|
---|
63 | @Override
|
---|
64 | public PreferenceSetting createPreferenceSetting() {
|
---|
65 | return new AdvancedPreference();
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | private AdvancedPreference() {
|
---|
70 | super(/* ICON(preferences/) */ "advanced", tr("Advanced Preferences"), tr("Setting Preference entries directly. Use with caution!"));
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | public boolean isExpert() {
|
---|
75 | return true;
|
---|
76 | }
|
---|
77 |
|
---|
78 | protected List<PrefEntry> allData;
|
---|
79 | protected List<PrefEntry> displayData = new ArrayList<>();
|
---|
80 | protected JosmTextField txtFilter;
|
---|
81 | protected PreferencesTable table;
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | public void addGui(final PreferenceTabbedPane gui) {
|
---|
85 | JPanel p = gui.createPreferenceTab(this);
|
---|
86 |
|
---|
87 | txtFilter = new JosmTextField();
|
---|
88 | JLabel lbFilter = new JLabel(tr("Search: "));
|
---|
89 | lbFilter.setLabelFor(txtFilter);
|
---|
90 | p.add(lbFilter);
|
---|
91 | p.add(txtFilter, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
92 | txtFilter.getDocument().addDocumentListener(new DocumentListener(){
|
---|
93 | @Override public void changedUpdate(DocumentEvent e) {
|
---|
94 | action();
|
---|
95 | }
|
---|
96 | @Override public void insertUpdate(DocumentEvent e) {
|
---|
97 | action();
|
---|
98 | }
|
---|
99 | @Override public void removeUpdate(DocumentEvent e) {
|
---|
100 | action();
|
---|
101 | }
|
---|
102 | private void action() {
|
---|
103 | applyFilter();
|
---|
104 | }
|
---|
105 | });
|
---|
106 | readPreferences(Main.pref);
|
---|
107 |
|
---|
108 | applyFilter();
|
---|
109 | table = new PreferencesTable(displayData);
|
---|
110 | JScrollPane scroll = new JScrollPane(table);
|
---|
111 | p.add(scroll, GBC.eol().fill(GBC.BOTH));
|
---|
112 | scroll.setPreferredSize(new Dimension(400,200));
|
---|
113 |
|
---|
114 | JButton add = new JButton(tr("Add"));
|
---|
115 | p.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
|
---|
116 | p.add(add, GBC.std().insets(0,5,0,0));
|
---|
117 | add.addActionListener(new ActionListener(){
|
---|
118 | @Override public void actionPerformed(ActionEvent e) {
|
---|
119 | PrefEntry pe = table.addPreference(gui);
|
---|
120 | if (pe!=null) {
|
---|
121 | allData.add(pe);
|
---|
122 | Collections.sort(allData);
|
---|
123 | applyFilter();
|
---|
124 | }
|
---|
125 | }
|
---|
126 | });
|
---|
127 |
|
---|
128 | JButton edit = new JButton(tr("Edit"));
|
---|
129 | p.add(edit, GBC.std().insets(5,5,5,0));
|
---|
130 | edit.addActionListener(new ActionListener(){
|
---|
131 | @Override public void actionPerformed(ActionEvent e) {
|
---|
132 | boolean ok = table.editPreference(gui);
|
---|
133 | if (ok) applyFilter();
|
---|
134 | }
|
---|
135 | });
|
---|
136 |
|
---|
137 | JButton reset = new JButton(tr("Reset"));
|
---|
138 | p.add(reset, GBC.std().insets(0,5,0,0));
|
---|
139 | reset.addActionListener(new ActionListener(){
|
---|
140 | @Override public void actionPerformed(ActionEvent e) {
|
---|
141 | table.resetPreferences(gui);
|
---|
142 | }
|
---|
143 | });
|
---|
144 |
|
---|
145 | JButton read = new JButton(tr("Read from file"));
|
---|
146 | p.add(read, GBC.std().insets(5,5,0,0));
|
---|
147 | read.addActionListener(new ActionListener(){
|
---|
148 | @Override public void actionPerformed(ActionEvent e) {
|
---|
149 | readPreferencesFromXML();
|
---|
150 | }
|
---|
151 | });
|
---|
152 |
|
---|
153 | JButton export = new JButton(tr("Export selected items"));
|
---|
154 | p.add(export, GBC.std().insets(5,5,0,0));
|
---|
155 | export.addActionListener(new ActionListener(){
|
---|
156 | @Override public void actionPerformed(ActionEvent e) {
|
---|
157 | exportSelectedToXML();
|
---|
158 | }
|
---|
159 | });
|
---|
160 |
|
---|
161 | final JButton more = new JButton(tr("More..."));
|
---|
162 | p.add(more, GBC.std().insets(5,5,0,0));
|
---|
163 | more.addActionListener(new ActionListener() {
|
---|
164 | private JPopupMenu menu = buildPopupMenu();
|
---|
165 | @Override public void actionPerformed(ActionEvent ev) {
|
---|
166 | menu.show(more, 0, 0);
|
---|
167 | }
|
---|
168 | });
|
---|
169 | }
|
---|
170 |
|
---|
171 | private void readPreferences(Preferences tmpPrefs) {
|
---|
172 | Map<String, Setting<?>> loaded;
|
---|
173 | Map<String, Setting<?>> orig = Main.pref.getAllSettings();
|
---|
174 | Map<String, Setting<?>> defaults = tmpPrefs.getAllDefaults();
|
---|
175 | orig.remove("osm-server.password");
|
---|
176 | defaults.remove("osm-server.password");
|
---|
177 | if (tmpPrefs != Main.pref) {
|
---|
178 | loaded = tmpPrefs.getAllSettings();
|
---|
179 | // plugins preference keys may be changed directly later, after plugins are downloaded
|
---|
180 | // so we do not want to show it in the table as "changed" now
|
---|
181 | Setting<?> pluginSetting = orig.get("plugins");
|
---|
182 | if (pluginSetting!=null) {
|
---|
183 | loaded.put("plugins", pluginSetting);
|
---|
184 | }
|
---|
185 | } else {
|
---|
186 | loaded = orig;
|
---|
187 | }
|
---|
188 | allData = prepareData(loaded, orig, defaults);
|
---|
189 | }
|
---|
190 |
|
---|
191 | private File[] askUserForCustomSettingsFiles(boolean saveFileFlag, String title) {
|
---|
192 | FileFilter filter = new FileFilter() {
|
---|
193 | @Override
|
---|
194 | public boolean accept(File f) {
|
---|
195 | return f.isDirectory() || Utils.hasExtension(f, "xml");
|
---|
196 | }
|
---|
197 | @Override
|
---|
198 | public String getDescription() {
|
---|
199 | return tr("JOSM custom settings files (*.xml)");
|
---|
200 | }
|
---|
201 | };
|
---|
202 | AbstractFileChooser fc = DiskAccessAction.createAndOpenFileChooser(!saveFileFlag, !saveFileFlag, title, filter,
|
---|
203 | JFileChooser.FILES_ONLY, "customsettings.lastDirectory");
|
---|
204 | if (fc != null) {
|
---|
205 | File[] sel = fc.isMultiSelectionEnabled() ? fc.getSelectedFiles() : (new File[]{fc.getSelectedFile()});
|
---|
206 | if (sel.length==1 && !sel[0].getName().contains(".")) sel[0]=new File(sel[0].getAbsolutePath()+".xml");
|
---|
207 | return sel;
|
---|
208 | }
|
---|
209 | return new File[0];
|
---|
210 | }
|
---|
211 |
|
---|
212 | private void exportSelectedToXML() {
|
---|
213 | List<String> keys = new ArrayList<>();
|
---|
214 | boolean hasLists = false;
|
---|
215 |
|
---|
216 | for (PrefEntry p: table.getSelectedItems()) {
|
---|
217 | // preferences with default values are not saved
|
---|
218 | if (!(p.getValue() instanceof Preferences.StringSetting)) {
|
---|
219 | hasLists = true; // => append and replace differs
|
---|
220 | }
|
---|
221 | if (!p.isDefault()) {
|
---|
222 | keys.add(p.getKey());
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | if (keys.isEmpty()) {
|
---|
227 | JOptionPane.showMessageDialog(Main.parent,
|
---|
228 | tr("Please select some preference keys not marked as default"), tr("Warning"), JOptionPane.WARNING_MESSAGE);
|
---|
229 | return;
|
---|
230 | }
|
---|
231 |
|
---|
232 | File[] files = askUserForCustomSettingsFiles(true, tr("Export preferences keys to JOSM customization file"));
|
---|
233 | if (files.length == 0) {
|
---|
234 | return;
|
---|
235 | }
|
---|
236 |
|
---|
237 | int answer = 0;
|
---|
238 | if (hasLists) {
|
---|
239 | answer = JOptionPane.showOptionDialog(
|
---|
240 | Main.parent, tr("What to do with preference lists when this file is to be imported?"), tr("Question"),
|
---|
241 | JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,
|
---|
242 | new String[]{tr("Append preferences from file to existing values"), tr("Replace existing values")}, 0);
|
---|
243 | }
|
---|
244 | CustomConfigurator.exportPreferencesKeysToFile(files[0].getAbsolutePath(), answer == 0, keys);
|
---|
245 | }
|
---|
246 |
|
---|
247 | private void readPreferencesFromXML() {
|
---|
248 | File[] files = askUserForCustomSettingsFiles(false, tr("Open JOSM customization file"));
|
---|
249 | if (files.length == 0) return;
|
---|
250 |
|
---|
251 | Preferences tmpPrefs = CustomConfigurator.clonePreferences(Main.pref);
|
---|
252 |
|
---|
253 | StringBuilder log = new StringBuilder();
|
---|
254 | log.append("<html>");
|
---|
255 | for (File f : files) {
|
---|
256 | CustomConfigurator.readXML(f, tmpPrefs);
|
---|
257 | log.append(CustomConfigurator.getLog());
|
---|
258 | }
|
---|
259 | log.append("</html>");
|
---|
260 | String msg = log.toString().replace("\n", "<br/>");
|
---|
261 |
|
---|
262 | new LogShowDialog(tr("Import log"), tr("<html>Here is file import summary. <br/>"
|
---|
263 | + "You can reject preferences changes by pressing \"Cancel\" in preferences dialog <br/>"
|
---|
264 | + "To activate some changes JOSM restart may be needed.</html>"), msg).showDialog();
|
---|
265 |
|
---|
266 | readPreferences(tmpPrefs);
|
---|
267 | // sorting after modification - first modified, then non-default, then default entries
|
---|
268 | Collections.sort(allData, customComparator);
|
---|
269 | applyFilter();
|
---|
270 | }
|
---|
271 |
|
---|
272 | private Comparator<PrefEntry> customComparator = new Comparator<PrefEntry>() {
|
---|
273 | @Override
|
---|
274 | public int compare(PrefEntry o1, PrefEntry o2) {
|
---|
275 | if (o1.isChanged() && !o2.isChanged()) return -1;
|
---|
276 | if (o2.isChanged() && !o1.isChanged()) return 1;
|
---|
277 | if (!(o1.isDefault()) && o2.isDefault()) return -1;
|
---|
278 | if (!(o2.isDefault()) && o1.isDefault()) return 1;
|
---|
279 | return o1.compareTo(o2);
|
---|
280 | }
|
---|
281 | };
|
---|
282 |
|
---|
283 | private List<PrefEntry> prepareData(Map<String, Setting<?>> loaded, Map<String, Setting<?>> orig, Map<String, Setting<?>> defaults) {
|
---|
284 | List<PrefEntry> data = new ArrayList<>();
|
---|
285 | for (Entry<String, Setting<?>> e : loaded.entrySet()) {
|
---|
286 | Setting<?> value = e.getValue();
|
---|
287 | Setting<?> old = orig.get(e.getKey());
|
---|
288 | Setting<?> def = defaults.get(e.getKey());
|
---|
289 | if (def == null) {
|
---|
290 | def = value.getNullInstance();
|
---|
291 | }
|
---|
292 | PrefEntry en = new PrefEntry(e.getKey(), value, def, false);
|
---|
293 | // after changes we have nondefault value. Value is changed if is not equal to old value
|
---|
294 | if (!Objects.equals(old, value)) {
|
---|
295 | en.markAsChanged();
|
---|
296 | }
|
---|
297 | data.add(en);
|
---|
298 | }
|
---|
299 | for (Entry<String, Setting<?>> e : defaults.entrySet()) {
|
---|
300 | if (!loaded.containsKey(e.getKey())) {
|
---|
301 | PrefEntry en = new PrefEntry(e.getKey(), e.getValue(), e.getValue(), true);
|
---|
302 | // after changes we have default value. So, value is changed if old value is not default
|
---|
303 | Setting<?> old = orig.get(e.getKey());
|
---|
304 | if (old != null) {
|
---|
305 | en.markAsChanged();
|
---|
306 | }
|
---|
307 | data.add(en);
|
---|
308 | }
|
---|
309 | }
|
---|
310 | Collections.sort(data);
|
---|
311 | displayData.clear();
|
---|
312 | displayData.addAll(data);
|
---|
313 | return data;
|
---|
314 | }
|
---|
315 |
|
---|
316 | private Map<String,String> profileTypes = new LinkedHashMap<>();
|
---|
317 |
|
---|
318 | private JPopupMenu buildPopupMenu() {
|
---|
319 | JPopupMenu menu = new JPopupMenu();
|
---|
320 | profileTypes.put(marktr("shortcut"), "shortcut\\..*");
|
---|
321 | profileTypes.put(marktr("color"), "color\\..*");
|
---|
322 | profileTypes.put(marktr("toolbar"), "toolbar.*");
|
---|
323 | profileTypes.put(marktr("imagery"), "imagery.*");
|
---|
324 |
|
---|
325 | for (Entry<String,String> e: profileTypes.entrySet()) {
|
---|
326 | menu.add(new ExportProfileAction(Main.pref, e.getKey(), e.getValue()));
|
---|
327 | }
|
---|
328 |
|
---|
329 | menu.addSeparator();
|
---|
330 | menu.add(getProfileMenu());
|
---|
331 | menu.addSeparator();
|
---|
332 | menu.add(new AbstractAction(tr("Reset preferences")) {
|
---|
333 | @Override
|
---|
334 | public void actionPerformed(ActionEvent ae) {
|
---|
335 | if (!GuiHelper.warnUser(tr("Reset preferences"),
|
---|
336 | "<html>"+
|
---|
337 | tr("You are about to clear all preferences to their default values<br />"+
|
---|
338 | "All your settings will be deleted: plugins, imagery, filters, toolbar buttons, keyboard, etc. <br />"+
|
---|
339 | "Are you sure you want to continue?")
|
---|
340 | +"</html>", null, "")) {
|
---|
341 | Main.pref.resetToDefault();
|
---|
342 | try {
|
---|
343 | Main.pref.save();
|
---|
344 | } catch (IOException e) {
|
---|
345 | Main.warn("IOException while saving preferences: "+e.getMessage());
|
---|
346 | }
|
---|
347 | readPreferences(Main.pref);
|
---|
348 | applyFilter();
|
---|
349 | }
|
---|
350 | }
|
---|
351 | });
|
---|
352 | return menu;
|
---|
353 | }
|
---|
354 |
|
---|
355 | private JMenu getProfileMenu() {
|
---|
356 | final JMenu p =new JMenu(tr("Load profile"));
|
---|
357 | p.addMenuListener(new MenuListener() {
|
---|
358 | @Override
|
---|
359 | public void menuSelected(MenuEvent me) {
|
---|
360 | p.removeAll();
|
---|
361 | File[] files = new File(".").listFiles();
|
---|
362 | if (files != null) {
|
---|
363 | for (File f: files) {
|
---|
364 | String s = f.getName();
|
---|
365 | int idx = s.indexOf('_');
|
---|
366 | if (idx>=0) {
|
---|
367 | String t=s.substring(0,idx);
|
---|
368 | if (profileTypes.containsKey(t)) {
|
---|
369 | p.add(new ImportProfileAction(s, f, t));
|
---|
370 | }
|
---|
371 | }
|
---|
372 | }
|
---|
373 | }
|
---|
374 | files = Main.pref.getPreferencesDirectory().listFiles();
|
---|
375 | if (files != null) {
|
---|
376 | for (File f: files) {
|
---|
377 | String s = f.getName();
|
---|
378 | int idx = s.indexOf('_');
|
---|
379 | if (idx>=0) {
|
---|
380 | String t=s.substring(0,idx);
|
---|
381 | if (profileTypes.containsKey(t)) {
|
---|
382 | p.add(new ImportProfileAction(s, f, t));
|
---|
383 | }
|
---|
384 | }
|
---|
385 | }
|
---|
386 | }
|
---|
387 | }
|
---|
388 | @Override
|
---|
389 | public void menuDeselected(MenuEvent me) {
|
---|
390 | // Not implemented
|
---|
391 | }
|
---|
392 | @Override
|
---|
393 | public void menuCanceled(MenuEvent me) {
|
---|
394 | // Not implemented
|
---|
395 | }
|
---|
396 | });
|
---|
397 | return p;
|
---|
398 | }
|
---|
399 |
|
---|
400 | private class ImportProfileAction extends AbstractAction {
|
---|
401 | private final File file;
|
---|
402 | private final String type;
|
---|
403 |
|
---|
404 | public ImportProfileAction(String name, File file, String type) {
|
---|
405 | super(name);
|
---|
406 | this.file = file;
|
---|
407 | this.type = type;
|
---|
408 | }
|
---|
409 |
|
---|
410 | @Override
|
---|
411 | public void actionPerformed(ActionEvent ae) {
|
---|
412 | Preferences tmpPrefs = CustomConfigurator.clonePreferences(Main.pref);
|
---|
413 | CustomConfigurator.readXML(file, tmpPrefs);
|
---|
414 | readPreferences(tmpPrefs);
|
---|
415 | String prefRegex = profileTypes.get(type);
|
---|
416 | // clean all the preferences from the chosen group
|
---|
417 | for (PrefEntry p : allData) {
|
---|
418 | if (p.getKey().matches(prefRegex) && !p.isDefault()) {
|
---|
419 | p.reset();
|
---|
420 | }
|
---|
421 | }
|
---|
422 | // allow user to review the changes in table
|
---|
423 | Collections.sort(allData, customComparator);
|
---|
424 | applyFilter();
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 | private void applyFilter() {
|
---|
429 | displayData.clear();
|
---|
430 | for (PrefEntry e : allData) {
|
---|
431 | String prefKey = e.getKey();
|
---|
432 | Setting<?> valueSetting = e.getValue();
|
---|
433 | String prefValue = valueSetting.getValue() == null ? "" : valueSetting.getValue().toString();
|
---|
434 |
|
---|
435 | String[] input = txtFilter.getText().split("\\s+");
|
---|
436 | boolean canHas = true;
|
---|
437 |
|
---|
438 | // Make 'wmsplugin cache' search for e.g. 'cache.wmsplugin'
|
---|
439 | final String prefKeyLower = prefKey.toLowerCase(Locale.ENGLISH);
|
---|
440 | final String prefValueLower = prefValue.toLowerCase(Locale.ENGLISH);
|
---|
441 | for (String bit : input) {
|
---|
442 | bit = bit.toLowerCase(Locale.ENGLISH);
|
---|
443 | if (!prefKeyLower.contains(bit) && !prefValueLower.contains(bit)) {
|
---|
444 | canHas = false;
|
---|
445 | break;
|
---|
446 | }
|
---|
447 | }
|
---|
448 | if (canHas) {
|
---|
449 | displayData.add(e);
|
---|
450 | }
|
---|
451 | }
|
---|
452 | if (table!=null) table.fireDataChanged();
|
---|
453 | }
|
---|
454 |
|
---|
455 | @Override
|
---|
456 | public boolean ok() {
|
---|
457 | for (PrefEntry e : allData) {
|
---|
458 | if (e.isChanged()) {
|
---|
459 | Main.pref.putSetting(e.getKey(), e.getValue().getValue() == null ? null : e.getValue());
|
---|
460 | }
|
---|
461 | }
|
---|
462 | return false;
|
---|
463 | }
|
---|
464 | }
|
---|