1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.preferences.advanced;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.Font;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.awt.event.ActionListener;
|
---|
12 | import java.awt.event.MouseAdapter;
|
---|
13 | import java.awt.event.MouseEvent;
|
---|
14 | import java.util.ArrayList;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Map;
|
---|
17 | import java.util.Map.Entry;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.Collections;
|
---|
20 |
|
---|
21 | import javax.swing.Box;
|
---|
22 | import javax.swing.ButtonGroup;
|
---|
23 | import javax.swing.JButton;
|
---|
24 | import javax.swing.JLabel;
|
---|
25 | import javax.swing.JOptionPane;
|
---|
26 | import javax.swing.JPanel;
|
---|
27 | import javax.swing.JRadioButton;
|
---|
28 | import javax.swing.JScrollPane;
|
---|
29 | import javax.swing.JTable;
|
---|
30 | import javax.swing.JTextField;
|
---|
31 | import javax.swing.event.DocumentEvent;
|
---|
32 | import javax.swing.event.DocumentListener;
|
---|
33 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
34 | import javax.swing.table.DefaultTableModel;
|
---|
35 | import javax.swing.DefaultCellEditor;
|
---|
36 |
|
---|
37 | import org.openstreetmap.josm.Main;
|
---|
38 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
39 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
40 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
41 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
42 | import org.openstreetmap.josm.data.Preferences;
|
---|
43 | import org.openstreetmap.josm.data.Preferences.ListListSetting;
|
---|
44 | import org.openstreetmap.josm.data.Preferences.ListSetting;
|
---|
45 | import org.openstreetmap.josm.data.Preferences.MapListSetting;
|
---|
46 | import org.openstreetmap.josm.data.Preferences.Setting;
|
---|
47 | import org.openstreetmap.josm.data.Preferences.StringSetting;
|
---|
48 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
49 | import org.openstreetmap.josm.tools.GBC;
|
---|
50 | import org.openstreetmap.josm.tools.Utils;
|
---|
51 |
|
---|
52 | public class AdvancedPreference implements PreferenceSetting {
|
---|
53 |
|
---|
54 | public static class Factory implements PreferenceSettingFactory {
|
---|
55 | public PreferenceSetting createPreferenceSetting() {
|
---|
56 | return new AdvancedPreference();
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | public static class PrefEntry implements Comparable<PrefEntry> {
|
---|
61 | private String key;
|
---|
62 | private Setting value;
|
---|
63 | private Setting defaultValue;
|
---|
64 | private boolean isDefault;
|
---|
65 | private boolean changed;
|
---|
66 |
|
---|
67 | public PrefEntry(String key, Setting value, Setting defaultValue, boolean isDefault) {
|
---|
68 | CheckParameterUtil.ensureParameterNotNull(key);
|
---|
69 | CheckParameterUtil.ensureParameterNotNull(value);
|
---|
70 | CheckParameterUtil.ensureParameterNotNull(defaultValue);
|
---|
71 | this.key = key;
|
---|
72 | this.value = value;
|
---|
73 | this.defaultValue = defaultValue;
|
---|
74 | this.isDefault = isDefault;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public String getKey() {
|
---|
78 | return key;
|
---|
79 | }
|
---|
80 |
|
---|
81 | public Setting getValue() {
|
---|
82 | return value;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public void setValue(Setting value) {
|
---|
86 | this.value = value;
|
---|
87 | changed = true;
|
---|
88 | isDefault = false;
|
---|
89 | }
|
---|
90 |
|
---|
91 | public boolean isDefault() {
|
---|
92 | return isDefault;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public boolean isChanged() {
|
---|
96 | return changed;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public void reset() {
|
---|
100 | value = defaultValue;
|
---|
101 | changed = true;
|
---|
102 | isDefault = true;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public int compareTo(PrefEntry other) {
|
---|
106 | return key.compareTo(other.key);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | private AllSettingsTableModel model;
|
---|
111 | protected List<PrefEntry> data;
|
---|
112 | protected List<PrefEntry> displayData;
|
---|
113 | protected JTextField txtFilter;
|
---|
114 |
|
---|
115 | public void addGui(final PreferenceTabbedPane gui) {
|
---|
116 | JPanel p = gui.createPreferenceTab("advanced", tr("Advanced Preferences"),
|
---|
117 | tr("Setting Preference entries directly. Use with caution!"), false);
|
---|
118 |
|
---|
119 | txtFilter = new JTextField();
|
---|
120 | JLabel lbFilter = new JLabel(tr("Search: "));
|
---|
121 | lbFilter.setLabelFor(txtFilter);
|
---|
122 | p.add(lbFilter);
|
---|
123 | p.add(txtFilter, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
124 | txtFilter.getDocument().addDocumentListener(new DocumentListener(){
|
---|
125 | @Override public void changedUpdate(DocumentEvent e) {
|
---|
126 | action();
|
---|
127 | }
|
---|
128 | @Override public void insertUpdate(DocumentEvent e) {
|
---|
129 | action();
|
---|
130 | }
|
---|
131 | @Override public void removeUpdate(DocumentEvent e) {
|
---|
132 | action();
|
---|
133 | }
|
---|
134 | private void action() {
|
---|
135 | applyFilter();
|
---|
136 | }
|
---|
137 | });
|
---|
138 |
|
---|
139 | Map<String, Setting> orig = Main.pref.getAllSettings();
|
---|
140 | Map<String, Setting> defaults = Main.pref.getAllDefaults();
|
---|
141 | orig.remove("osm-server.password");
|
---|
142 | defaults.remove("osm-server.password");
|
---|
143 | prepareData(orig, defaults);
|
---|
144 | model = new AllSettingsTableModel();
|
---|
145 | applyFilter();
|
---|
146 |
|
---|
147 | final JTable list = new JTable(model);
|
---|
148 | list.putClientProperty("terminateEditOnFocusLost", true);
|
---|
149 | list.getColumnModel().getColumn(1).setCellRenderer(new SettingCellRenderer());
|
---|
150 | list.getColumnModel().getColumn(1).setCellEditor(new SettingCellEditor());
|
---|
151 |
|
---|
152 | JScrollPane scroll = new JScrollPane(list);
|
---|
153 | p.add(scroll, GBC.eol().fill(GBC.BOTH));
|
---|
154 | scroll.setPreferredSize(new Dimension(400,200));
|
---|
155 |
|
---|
156 | JButton add = new JButton(tr("Add"));
|
---|
157 | p.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
|
---|
158 | p.add(add, GBC.std().insets(0,5,0,0));
|
---|
159 | add.addActionListener(new ActionListener(){
|
---|
160 | public void actionPerformed(ActionEvent e) {
|
---|
161 | addPreference(gui);
|
---|
162 | }
|
---|
163 | });
|
---|
164 |
|
---|
165 | JButton edit = new JButton(tr("Edit"));
|
---|
166 | p.add(edit, GBC.std().insets(5,5,5,0));
|
---|
167 | edit.addActionListener(new ActionListener(){
|
---|
168 | public void actionPerformed(ActionEvent e) {
|
---|
169 | editPreference(gui, list);
|
---|
170 | }
|
---|
171 | });
|
---|
172 |
|
---|
173 | JButton reset = new JButton(tr("Reset"));
|
---|
174 | p.add(reset, GBC.std().insets(0,5,0,0));
|
---|
175 | reset.addActionListener(new ActionListener(){
|
---|
176 | public void actionPerformed(ActionEvent e) {
|
---|
177 | resetPreference(gui, list);
|
---|
178 | }
|
---|
179 | });
|
---|
180 |
|
---|
181 | list.addMouseListener(new MouseAdapter(){
|
---|
182 | @Override public void mouseClicked(MouseEvent e) {
|
---|
183 | if (e.getClickCount() == 2) {
|
---|
184 | editPreference(gui, list);
|
---|
185 | }
|
---|
186 | }
|
---|
187 | });
|
---|
188 | }
|
---|
189 |
|
---|
190 | private void prepareData(Map<String, Setting> orig, Map<String, Setting> defaults) {
|
---|
191 | data = new ArrayList<PrefEntry>();
|
---|
192 | for (Entry<String, Setting> e : orig.entrySet()) {
|
---|
193 | Setting value = e.getValue();
|
---|
194 | Setting def = defaults.get(e.getKey());
|
---|
195 | if (def == null) {
|
---|
196 | def = value.getNullInstance();
|
---|
197 | }
|
---|
198 | PrefEntry en = new PrefEntry(e.getKey(), value, def, false);
|
---|
199 | data.add(en);
|
---|
200 | }
|
---|
201 | for (Entry<String, Setting> e : defaults.entrySet()) {
|
---|
202 | if (!orig.containsKey(e.getKey())) {
|
---|
203 | PrefEntry en = new PrefEntry(e.getKey(), e.getValue(), e.getValue(), true);
|
---|
204 | data.add(en);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | Collections.sort(data);
|
---|
208 | displayData = new ArrayList<PrefEntry>(data);
|
---|
209 | }
|
---|
210 |
|
---|
211 | class AllSettingsTableModel extends DefaultTableModel {
|
---|
212 |
|
---|
213 | public AllSettingsTableModel() {
|
---|
214 | setColumnIdentifiers(new String[]{tr("Key"), tr("Value")});
|
---|
215 | }
|
---|
216 |
|
---|
217 | @Override
|
---|
218 | public boolean isCellEditable(int row, int column) {
|
---|
219 | return column == 1 && (displayData.get(row).getValue() instanceof StringSetting);
|
---|
220 | }
|
---|
221 |
|
---|
222 | @Override
|
---|
223 | public int getRowCount() {
|
---|
224 | return displayData.size();
|
---|
225 | }
|
---|
226 |
|
---|
227 | @Override
|
---|
228 | public Object getValueAt(int row, int column) {
|
---|
229 | if (column == 0)
|
---|
230 | return displayData.get(row).getKey();
|
---|
231 | else
|
---|
232 | return displayData.get(row);
|
---|
233 | }
|
---|
234 |
|
---|
235 | @Override
|
---|
236 | public void setValueAt(Object o, int row, int column) {
|
---|
237 | PrefEntry pe = displayData.get(row);
|
---|
238 | String s = (String) o;
|
---|
239 | if (!s.equals(pe.getValue().getValue())) {
|
---|
240 | pe.setValue(new StringSetting(s));
|
---|
241 | fireTableCellUpdated(row, column);
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | private class SettingCellRenderer extends DefaultTableCellRenderer {
|
---|
247 | @Override
|
---|
248 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
249 | if (value == null)
|
---|
250 | return this;
|
---|
251 | PrefEntry pe = (PrefEntry) value;
|
---|
252 | Setting setting = pe.getValue();
|
---|
253 | Object val = setting.getValue();
|
---|
254 | String display = val != null ? val.toString() : "<html><i><"+tr("unset")+"></i></html>";
|
---|
255 |
|
---|
256 | JLabel label = (JLabel)super.getTableCellRendererComponent(table,
|
---|
257 | display, isSelected, hasFocus, row, column);
|
---|
258 | if (!pe.isDefault()) {
|
---|
259 | label.setFont(label.getFont().deriveFont(Font.BOLD));
|
---|
260 | }
|
---|
261 | //label.setToolTipText("..."); TODO
|
---|
262 | return label;
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | private class SettingCellEditor extends DefaultCellEditor {
|
---|
267 | public SettingCellEditor() {
|
---|
268 | super(new JTextField());
|
---|
269 | }
|
---|
270 |
|
---|
271 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
---|
272 | PrefEntry pe = (PrefEntry) value;
|
---|
273 | StringSetting stg = (StringSetting) pe.getValue();
|
---|
274 | String s = stg.getValue() == null ? "" : stg.getValue();
|
---|
275 | return super.getTableCellEditorComponent(table, s, isSelected, row, column);
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | private void applyFilter() {
|
---|
280 | displayData.clear();
|
---|
281 | for (PrefEntry e : data) {
|
---|
282 |
|
---|
283 | String prefKey = e.getKey();
|
---|
284 | Setting valueSetting = e.getValue();
|
---|
285 | String prefValue = valueSetting.getValue() == null ? "" : valueSetting.getValue().toString();
|
---|
286 |
|
---|
287 | String input[] = txtFilter.getText().split("\\s+");
|
---|
288 | boolean canHas = true;
|
---|
289 |
|
---|
290 | // Make 'wmsplugin cache' search for e.g. 'cache.wmsplugin'
|
---|
291 | final String prefKeyLower = prefKey.toLowerCase();
|
---|
292 | final String prefValueLower = prefValue.toLowerCase();
|
---|
293 | for (String bit : input) {
|
---|
294 | bit = bit.toLowerCase();
|
---|
295 | if (!prefKeyLower.contains(bit) && !prefValueLower.contains(bit)) {
|
---|
296 | canHas = false;
|
---|
297 | break;
|
---|
298 | }
|
---|
299 | }
|
---|
300 | if (canHas) {
|
---|
301 | displayData.add(e);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | model.fireTableDataChanged();
|
---|
305 | }
|
---|
306 |
|
---|
307 | @Override
|
---|
308 | public boolean ok() {
|
---|
309 | for (PrefEntry e : data) {
|
---|
310 | if (e.isChanged()) {
|
---|
311 | Main.pref.putSetting(e.getKey(), e.getValue());
|
---|
312 | }
|
---|
313 | }
|
---|
314 | return false;
|
---|
315 | }
|
---|
316 |
|
---|
317 | private void resetPreference(final PreferenceTabbedPane gui, final JTable list) {
|
---|
318 | if (list.getSelectedRowCount() == 0) {
|
---|
319 | JOptionPane.showMessageDialog(
|
---|
320 | gui,
|
---|
321 | tr("Please select the row to delete."),
|
---|
322 | tr("Warning"),
|
---|
323 | JOptionPane.WARNING_MESSAGE
|
---|
324 | );
|
---|
325 | return;
|
---|
326 | }
|
---|
327 | for (int row : list.getSelectedRows()) {
|
---|
328 | PrefEntry e = displayData.get(row);
|
---|
329 | e.reset();
|
---|
330 | }
|
---|
331 | model.fireTableDataChanged();
|
---|
332 | }
|
---|
333 |
|
---|
334 | private void addPreference(final PreferenceTabbedPane gui) {
|
---|
335 | JPanel p = new JPanel(new GridBagLayout());
|
---|
336 | p.add(new JLabel(tr("Key")), GBC.std().insets(0,0,5,0));
|
---|
337 | JTextField tkey = new JTextField("", 50);
|
---|
338 | p.add(tkey, GBC.eop().insets(5,0,0,0).fill(GBC.HORIZONTAL));
|
---|
339 |
|
---|
340 | p.add(new JLabel(tr("Select Setting Type:")), GBC.eol().insets(5,15,5,0));
|
---|
341 |
|
---|
342 | JRadioButton rbString = new JRadioButton(tr("Simple"));
|
---|
343 | JRadioButton rbList = new JRadioButton(tr("List"));
|
---|
344 | JRadioButton rbListList = new JRadioButton(tr("List of lists"));
|
---|
345 | JRadioButton rbMapList = new JRadioButton(tr("List of maps"));
|
---|
346 |
|
---|
347 | ButtonGroup group = new ButtonGroup();
|
---|
348 | group.add(rbString);
|
---|
349 | group.add(rbList);
|
---|
350 | group.add(rbListList);
|
---|
351 | group.add(rbMapList);
|
---|
352 |
|
---|
353 | p.add(rbString, GBC.eol());
|
---|
354 | p.add(rbList, GBC.eol());
|
---|
355 | p.add(rbListList, GBC.eol());
|
---|
356 | p.add(rbMapList, GBC.eol());
|
---|
357 |
|
---|
358 | rbString.setSelected(true);
|
---|
359 |
|
---|
360 | ExtendedDialog dlg = new ExtendedDialog(gui, tr("Add setting"), new String[] {tr("OK"), tr("Cancel")});
|
---|
361 | dlg.setButtonIcons(new String[] {"ok.png", "cancel.png"});
|
---|
362 | dlg.setContent(p);
|
---|
363 | dlg.showDialog();
|
---|
364 |
|
---|
365 | PrefEntry pe = null;
|
---|
366 | boolean ok = false;
|
---|
367 | if (dlg.getValue() == 1) {
|
---|
368 | if (rbString.isSelected()) {
|
---|
369 | StringSetting sSetting = new StringSetting(null);
|
---|
370 | pe = new PrefEntry(tkey.getText(), sSetting, sSetting, false);
|
---|
371 | StringEditor sEditor = new StringEditor(gui, pe, sSetting);
|
---|
372 | sEditor.showDialog();
|
---|
373 | if (sEditor.getValue() == 1) {
|
---|
374 | String data = sEditor.getData();
|
---|
375 | if (!Utils.equal(sSetting.getValue(), data)) {
|
---|
376 | pe.setValue(new StringSetting(data));
|
---|
377 | ok = true;
|
---|
378 | }
|
---|
379 | }
|
---|
380 | } else if (rbList.isSelected()) {
|
---|
381 | ListSetting lSetting = new ListSetting(null);
|
---|
382 | pe = new PrefEntry(tkey.getText(), lSetting, lSetting, false);
|
---|
383 | ListEditor lEditor = new ListEditor(gui, pe, lSetting);
|
---|
384 | lEditor.showDialog();
|
---|
385 | if (lEditor.getValue() == 1) {
|
---|
386 | List<String> data = lEditor.getData();
|
---|
387 | if (!Preferences.equalCollection(lSetting.getValue(), data)) {
|
---|
388 | pe.setValue(new ListSetting(data));
|
---|
389 | ok = true;
|
---|
390 | }
|
---|
391 | }
|
---|
392 | } else if (rbListList.isSelected()) {
|
---|
393 | ListListSetting llSetting = new ListListSetting(null);
|
---|
394 | pe = new PrefEntry(tkey.getText(), llSetting, llSetting, false);
|
---|
395 | ListListEditor llEditor = new ListListEditor(gui, pe, llSetting);
|
---|
396 | llEditor.showDialog();
|
---|
397 | if (llEditor.getValue() == 1) {
|
---|
398 | List<List<String>> data = llEditor.getData();
|
---|
399 | if (!Preferences.equalArray((Collection) llSetting.getValue(), data)) {
|
---|
400 | pe.setValue(new ListListSetting(data));
|
---|
401 | ok = true;
|
---|
402 | }
|
---|
403 | }
|
---|
404 | } else if (rbMapList.isSelected()) {
|
---|
405 | MapListSetting mlSetting = new MapListSetting(null);
|
---|
406 | pe = new PrefEntry(tkey.getText(), mlSetting, mlSetting, false);
|
---|
407 | MapListEditor mlEditor = new MapListEditor(gui, pe, mlSetting);
|
---|
408 | mlEditor.showDialog();
|
---|
409 | if (mlEditor.getValue() == 1) {
|
---|
410 | List<Map<String, String>> data = mlEditor.getData();
|
---|
411 | if (!Preferences.equalListOfStructs(mlSetting.getValue(), data)) {
|
---|
412 | pe.setValue(new MapListSetting(data));
|
---|
413 | ok = true;
|
---|
414 | }
|
---|
415 | }
|
---|
416 | }
|
---|
417 | if (ok) {
|
---|
418 | data.add(pe);
|
---|
419 | Collections.sort(data);
|
---|
420 | applyFilter();
|
---|
421 | }
|
---|
422 | }
|
---|
423 | }
|
---|
424 |
|
---|
425 | private void editPreference(final PreferenceTabbedPane gui, final JTable list) {
|
---|
426 | if (list.getSelectedRowCount() != 1) {
|
---|
427 | JOptionPane.showMessageDialog(
|
---|
428 | gui,
|
---|
429 | tr("Please select the row to edit."),
|
---|
430 | tr("Warning"),
|
---|
431 | JOptionPane.WARNING_MESSAGE
|
---|
432 | );
|
---|
433 | return;
|
---|
434 | }
|
---|
435 | final PrefEntry e = (PrefEntry) model.getValueAt(list.getSelectedRow(), 1);
|
---|
436 | Setting stg = e.getValue();
|
---|
437 | if (stg instanceof StringSetting) {
|
---|
438 | list.editCellAt(list.getSelectedRow(), 1);
|
---|
439 | Component editor = list.getEditorComponent();
|
---|
440 | if (editor != null) {
|
---|
441 | editor.requestFocus();
|
---|
442 | }
|
---|
443 | } else if (stg instanceof ListSetting) {
|
---|
444 | ListSetting lSetting = (ListSetting) stg;
|
---|
445 | ListEditor lEditor = new ListEditor(gui, e, lSetting);
|
---|
446 | lEditor.showDialog();
|
---|
447 | if (lEditor.getValue() == 1) {
|
---|
448 | List<String> data = lEditor.getData();
|
---|
449 | if (!Preferences.equalCollection(lSetting.getValue(), data)) {
|
---|
450 | e.setValue(new ListSetting(data));
|
---|
451 | applyFilter();
|
---|
452 | }
|
---|
453 | }
|
---|
454 | } else if (stg instanceof ListListSetting) {
|
---|
455 | ListListEditor llEditor = new ListListEditor(gui, e, (ListListSetting) stg);
|
---|
456 | llEditor.showDialog();
|
---|
457 | if (llEditor.getValue() == 1) {
|
---|
458 | List<List<String>> data = llEditor.getData();
|
---|
459 | if (!Preferences.equalArray((Collection) stg.getValue(), data)) {
|
---|
460 | e.setValue(new ListListSetting(data));
|
---|
461 | applyFilter();
|
---|
462 | }
|
---|
463 | }
|
---|
464 | } else if (stg instanceof MapListSetting) {
|
---|
465 | MapListSetting mlSetting = (MapListSetting) stg;
|
---|
466 | MapListEditor mlEditor = new MapListEditor(gui, e, mlSetting);
|
---|
467 | mlEditor.showDialog();
|
---|
468 | if (mlEditor.getValue() == 1) {
|
---|
469 | List<Map<String, String>> data = mlEditor.getData();
|
---|
470 | if (!Preferences.equalListOfStructs(mlSetting.getValue(), data)) {
|
---|
471 | e.setValue(new MapListSetting(data));
|
---|
472 | applyFilter();
|
---|
473 | }
|
---|
474 | }
|
---|
475 | }
|
---|
476 | }
|
---|
477 | }
|
---|