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 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
6 |
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Dimension;
|
---|
10 | import java.awt.Font;
|
---|
11 | import java.awt.GridBagLayout;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.awt.event.ActionListener;
|
---|
14 | import java.awt.event.MouseAdapter;
|
---|
15 | import java.awt.event.MouseEvent;
|
---|
16 |
|
---|
17 | import java.io.File;
|
---|
18 | import java.util.ArrayList;
|
---|
19 | import java.util.Collection;
|
---|
20 | import java.util.Collections;
|
---|
21 | import java.util.Comparator;
|
---|
22 | import java.util.List;
|
---|
23 | import java.util.Map;
|
---|
24 | import java.util.Map.Entry;
|
---|
25 |
|
---|
26 | import javax.swing.Box;
|
---|
27 | import javax.swing.ButtonGroup;
|
---|
28 | import javax.swing.DefaultCellEditor;
|
---|
29 | import javax.swing.JButton;
|
---|
30 | import javax.swing.JFileChooser;
|
---|
31 | import javax.swing.JLabel;
|
---|
32 | import javax.swing.JOptionPane;
|
---|
33 | import javax.swing.JPanel;
|
---|
34 | import javax.swing.JRadioButton;
|
---|
35 | import javax.swing.JScrollPane;
|
---|
36 | import javax.swing.JTable;
|
---|
37 | import javax.swing.JTextField;
|
---|
38 | import javax.swing.event.DocumentEvent;
|
---|
39 | import javax.swing.event.DocumentListener;
|
---|
40 | import javax.swing.filechooser.FileFilter;
|
---|
41 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
42 | import javax.swing.table.DefaultTableModel;
|
---|
43 |
|
---|
44 | import org.openstreetmap.josm.Main;
|
---|
45 | import org.openstreetmap.josm.data.CustomConfigurator;
|
---|
46 | import org.openstreetmap.josm.data.Preferences;
|
---|
47 | import org.openstreetmap.josm.data.Preferences.ListListSetting;
|
---|
48 | import org.openstreetmap.josm.data.Preferences.ListSetting;
|
---|
49 | import org.openstreetmap.josm.data.Preferences.MapListSetting;
|
---|
50 | import org.openstreetmap.josm.data.Preferences.Setting;
|
---|
51 | import org.openstreetmap.josm.data.Preferences.StringSetting;
|
---|
52 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
53 | import org.openstreetmap.josm.gui.actionsupport.LogShowDialog;
|
---|
54 | import org.openstreetmap.josm.gui.preferences.DefaultTabPreferenceSetting;
|
---|
55 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
56 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
57 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
58 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
59 | import org.openstreetmap.josm.tools.GBC;
|
---|
60 | import org.openstreetmap.josm.tools.Utils;
|
---|
61 |
|
---|
62 | public class AdvancedPreference extends DefaultTabPreferenceSetting {
|
---|
63 |
|
---|
64 | public static class Factory implements PreferenceSettingFactory {
|
---|
65 | public PreferenceSetting createPreferenceSetting() {
|
---|
66 | return new AdvancedPreference();
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | private AdvancedPreference() {
|
---|
71 | super("advanced", tr("Advanced Preferences"), tr("Setting Preference entries directly. Use with caution!"));
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public boolean isExpert() {
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public static class PrefEntry implements Comparable<PrefEntry> {
|
---|
80 | private String key;
|
---|
81 | private Setting value;
|
---|
82 | private Setting defaultValue;
|
---|
83 | private boolean isDefault;
|
---|
84 | private boolean changed;
|
---|
85 |
|
---|
86 | public PrefEntry(String key, Setting value, Setting defaultValue, boolean isDefault) {
|
---|
87 | CheckParameterUtil.ensureParameterNotNull(key);
|
---|
88 | CheckParameterUtil.ensureParameterNotNull(value);
|
---|
89 | CheckParameterUtil.ensureParameterNotNull(defaultValue);
|
---|
90 | this.key = key;
|
---|
91 | this.value = value;
|
---|
92 | this.defaultValue = defaultValue;
|
---|
93 | this.isDefault = isDefault;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public String getKey() {
|
---|
97 | return key;
|
---|
98 | }
|
---|
99 |
|
---|
100 | public Setting getValue() {
|
---|
101 | return value;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public Setting getDefaultValue() {
|
---|
105 | return defaultValue;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public void setValue(Setting value) {
|
---|
109 | this.value = value;
|
---|
110 | changed = true;
|
---|
111 | isDefault = false;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public boolean isDefault() {
|
---|
115 | return isDefault;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public boolean isChanged() {
|
---|
119 | return changed;
|
---|
120 | }
|
---|
121 |
|
---|
122 | private void markAsChanged() {
|
---|
123 | changed = true;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void reset() {
|
---|
127 | value = defaultValue;
|
---|
128 | changed = true;
|
---|
129 | isDefault = true;
|
---|
130 | }
|
---|
131 |
|
---|
132 | @Override
|
---|
133 | public int compareTo(PrefEntry other) {
|
---|
134 | return key.compareTo(other.key);
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Override
|
---|
138 | public String toString() {
|
---|
139 | return value.toString();
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | private AllSettingsTableModel model;
|
---|
144 | protected List<PrefEntry> data;
|
---|
145 | protected List<PrefEntry> displayData;
|
---|
146 | protected JTextField txtFilter;
|
---|
147 |
|
---|
148 | public void addGui(final PreferenceTabbedPane gui) {
|
---|
149 | JPanel p = gui.createPreferenceTab(this);
|
---|
150 |
|
---|
151 | txtFilter = new JTextField();
|
---|
152 | JLabel lbFilter = new JLabel(tr("Search: "));
|
---|
153 | lbFilter.setLabelFor(txtFilter);
|
---|
154 | p.add(lbFilter);
|
---|
155 | p.add(txtFilter, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
156 | txtFilter.getDocument().addDocumentListener(new DocumentListener(){
|
---|
157 | @Override public void changedUpdate(DocumentEvent e) {
|
---|
158 | action();
|
---|
159 | }
|
---|
160 | @Override public void insertUpdate(DocumentEvent e) {
|
---|
161 | action();
|
---|
162 | }
|
---|
163 | @Override public void removeUpdate(DocumentEvent e) {
|
---|
164 | action();
|
---|
165 | }
|
---|
166 | private void action() {
|
---|
167 | applyFilter();
|
---|
168 | }
|
---|
169 | });
|
---|
170 | readPreferences(Main.pref);
|
---|
171 | model = new AllSettingsTableModel();
|
---|
172 | applyFilter();
|
---|
173 |
|
---|
174 | final JTable list = new JTable(model);
|
---|
175 | list.putClientProperty("terminateEditOnFocusLost", true);
|
---|
176 | list.getColumnModel().getColumn(1).setCellRenderer(new SettingCellRenderer());
|
---|
177 | list.getColumnModel().getColumn(1).setCellEditor(new SettingCellEditor());
|
---|
178 |
|
---|
179 | JScrollPane scroll = new JScrollPane(list);
|
---|
180 | p.add(scroll, GBC.eol().fill(GBC.BOTH));
|
---|
181 | scroll.setPreferredSize(new Dimension(400,200));
|
---|
182 |
|
---|
183 | JButton add = new JButton(tr("Add"));
|
---|
184 | p.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
|
---|
185 | p.add(add, GBC.std().insets(0,5,0,0));
|
---|
186 | add.addActionListener(new ActionListener(){
|
---|
187 | public void actionPerformed(ActionEvent e) {
|
---|
188 | addPreference(gui);
|
---|
189 | }
|
---|
190 | });
|
---|
191 |
|
---|
192 | JButton edit = new JButton(tr("Edit"));
|
---|
193 | p.add(edit, GBC.std().insets(5,5,5,0));
|
---|
194 | edit.addActionListener(new ActionListener(){
|
---|
195 | public void actionPerformed(ActionEvent e) {
|
---|
196 | editPreference(gui, list);
|
---|
197 | }
|
---|
198 | });
|
---|
199 |
|
---|
200 | JButton reset = new JButton(tr("Reset"));
|
---|
201 | p.add(reset, GBC.std().insets(0,5,0,0));
|
---|
202 | reset.addActionListener(new ActionListener(){
|
---|
203 | public void actionPerformed(ActionEvent e) {
|
---|
204 | resetPreference(gui, list);
|
---|
205 | }
|
---|
206 | });
|
---|
207 |
|
---|
208 | JButton read = new JButton(tr("Read from file"));
|
---|
209 | p.add(read, GBC.std().insets(5,5,0,0));
|
---|
210 | read.addActionListener(new ActionListener(){
|
---|
211 | public void actionPerformed(ActionEvent e) {
|
---|
212 | File[] files = askUserForCustomSettingsFiles(false, tr("Open JOSM customization file"));
|
---|
213 | if (files.length==0) return;
|
---|
214 |
|
---|
215 | Preferences tmpPrefs = CustomConfigurator.clonePreferences(Main.pref);
|
---|
216 |
|
---|
217 | StringBuilder log = new StringBuilder();
|
---|
218 | log.append("<html>");
|
---|
219 | for (File f: files) {
|
---|
220 | CustomConfigurator.readXML(f, tmpPrefs);
|
---|
221 | log.append(CustomConfigurator.getLog());
|
---|
222 | }
|
---|
223 | //try { Main.pref.save(); } catch (IOException ex) { }
|
---|
224 | log.append("</html>");
|
---|
225 | String msg = log.toString().replace("\n", "<br/>");
|
---|
226 |
|
---|
227 | new LogShowDialog(tr("Import log"), tr("<html>Here is file import summary. <br/>"
|
---|
228 | + "You can reject preferences changes by pressing \"Cancel\" in preferences dialog <br/>"
|
---|
229 | + "To activate some changes JOSM restart may be needed.</html>"), msg).showDialog();
|
---|
230 |
|
---|
231 | //JOptionPane.showMessageDialog(Main.parent,
|
---|
232 | // tr("Installed plugins and some changes in preferences will start to work after JOSM restart"), tr("Warning"), JOptionPane.WARNING_MESSAGE);
|
---|
233 |
|
---|
234 | readPreferences(tmpPrefs);
|
---|
235 | // sorting after modification - first modified, then non-default, then default entries
|
---|
236 | Collections.sort(data, new Comparator<PrefEntry>() {
|
---|
237 | @Override
|
---|
238 | public int compare(PrefEntry o1, PrefEntry o2) {
|
---|
239 | if (o1.changed && !o2.changed) return -1;
|
---|
240 | if (o2.changed && !o1.changed) return 1;
|
---|
241 | if (!(o1.isDefault) && o2.isDefault) return -1;
|
---|
242 | if (!(o2.isDefault) && o1.isDefault) return 1;
|
---|
243 | return o1.key.compareTo(o2.key);
|
---|
244 | }
|
---|
245 | });
|
---|
246 |
|
---|
247 | applyFilter();
|
---|
248 | ((AllSettingsTableModel) list.getModel()).fireTableDataChanged();
|
---|
249 | }
|
---|
250 |
|
---|
251 | });
|
---|
252 |
|
---|
253 | JButton export = new JButton(tr("Export selected items"));
|
---|
254 | p.add(export, GBC.std().insets(5,5,0,0));
|
---|
255 | export.addActionListener(new ActionListener(){
|
---|
256 | public void actionPerformed(ActionEvent e) {
|
---|
257 | ArrayList<String> keys = new ArrayList<String>();
|
---|
258 | boolean hasLists = false;
|
---|
259 | for (int row : list.getSelectedRows()) {
|
---|
260 | PrefEntry p = (PrefEntry) model.getValueAt(row, -1);
|
---|
261 | if (!p.isDefault()) {
|
---|
262 | // preferences with default values are not saved
|
---|
263 | if (!(p.getValue() instanceof StringSetting)) hasLists=true; // => append and replace differs
|
---|
264 | keys.add(p.getKey());
|
---|
265 | }
|
---|
266 | }
|
---|
267 | if (keys.size()==0) {
|
---|
268 | JOptionPane.showMessageDialog(Main.parent,
|
---|
269 | tr("Please select some preference keys not marked as default"), tr("Warning"), JOptionPane.WARNING_MESSAGE);
|
---|
270 | return;
|
---|
271 | }
|
---|
272 |
|
---|
273 | File[] files = askUserForCustomSettingsFiles(true, tr("Export preferences keys to JOSM customization file"));
|
---|
274 | if (files.length==0) return;
|
---|
275 |
|
---|
276 | int answer = 0;
|
---|
277 | if (hasLists) {
|
---|
278 | answer = JOptionPane.showOptionDialog(Main.parent, tr("What to with preference lists when this file is to be imported?"), tr("Question"),
|
---|
279 | JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE,
|
---|
280 | null, new String[]{"Append preferences from file to existing values","Replace existing values"},0);
|
---|
281 | }
|
---|
282 | CustomConfigurator.exportPreferencesKeysToFile(files[0].getAbsolutePath(), answer==0, keys);
|
---|
283 | }
|
---|
284 | });
|
---|
285 |
|
---|
286 |
|
---|
287 | list.addMouseListener(new MouseAdapter(){
|
---|
288 | @Override public void mouseClicked(MouseEvent e) {
|
---|
289 | if (e.getClickCount() == 2) {
|
---|
290 | editPreference(gui, list);
|
---|
291 | }
|
---|
292 | }
|
---|
293 | });
|
---|
294 | }
|
---|
295 |
|
---|
296 | private void readPreferences(Preferences tmpPrefs) {
|
---|
297 | Map<String, Setting> loaded;
|
---|
298 | Map<String, Setting> orig = Main.pref.getAllSettings();
|
---|
299 | Map<String, Setting> defaults = tmpPrefs.getAllDefaults();
|
---|
300 | orig.remove("osm-server.password");
|
---|
301 | defaults.remove("osm-server.password");
|
---|
302 | if (tmpPrefs != Main.pref) {
|
---|
303 | loaded = tmpPrefs.getAllSettings();
|
---|
304 | // plugins preference keys may be changed directly later, after plugins are downloaded
|
---|
305 | // so we do not want to show it in the table as "changed" now
|
---|
306 | loaded.put("plugins", orig.get("plugins"));
|
---|
307 | } else {
|
---|
308 | loaded = orig;
|
---|
309 | }
|
---|
310 | prepareData(loaded, orig, defaults);
|
---|
311 | }
|
---|
312 |
|
---|
313 | private File[] askUserForCustomSettingsFiles(boolean saveFileFlag, String title) {
|
---|
314 | String dir = Main.pref.get("customsettings.lastDirectory");
|
---|
315 | if (dir.length()==0) dir =".";
|
---|
316 |
|
---|
317 | JFileChooser fc = new JFileChooser(dir);
|
---|
318 | fc.setDialogTitle(title);
|
---|
319 | fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
---|
320 | fc.setAcceptAllFileFilterUsed(false);
|
---|
321 | fc.setFileFilter(new FileFilter() {
|
---|
322 | @Override
|
---|
323 | public boolean accept(File f) {
|
---|
324 | return f.isDirectory() || f.getName().toLowerCase().endsWith(".xml");
|
---|
325 | }
|
---|
326 | @Override
|
---|
327 | public String getDescription() {
|
---|
328 | return tr("JOSM custom settings files (*.xml)");
|
---|
329 | }
|
---|
330 | });
|
---|
331 |
|
---|
332 | fc.setMultiSelectionEnabled(!saveFileFlag);
|
---|
333 | int result = saveFileFlag? fc.showSaveDialog(Main.parent) : fc.showOpenDialog(Main.parent);
|
---|
334 | if (result == JFileChooser.APPROVE_OPTION) {
|
---|
335 | if (!fc.getCurrentDirectory().getAbsolutePath().equals(dir)) {
|
---|
336 | Main.pref.put("customsettings.lastDirectory", fc.getCurrentDirectory().getAbsolutePath());
|
---|
337 | }
|
---|
338 | File sel[] = fc.isMultiSelectionEnabled() ? fc.getSelectedFiles() : (new File[]{fc.getSelectedFile()});
|
---|
339 | if (sel.length==1 && !sel[0].getName().contains(".")) sel[0]=new File(sel[0].getAbsolutePath()+".xml");
|
---|
340 | return sel;
|
---|
341 | }
|
---|
342 | return new File[0];
|
---|
343 | }
|
---|
344 |
|
---|
345 | private void prepareData(Map<String, Setting> loaded, Map<String, Setting> orig, Map<String, Setting> defaults) {
|
---|
346 | data = new ArrayList<PrefEntry>();
|
---|
347 | for (Entry<String, Setting> e : loaded.entrySet()) {
|
---|
348 | Setting value = e.getValue();
|
---|
349 | Setting old = orig.get(e.getKey());
|
---|
350 | Setting def = defaults.get(e.getKey());
|
---|
351 | if (def == null) {
|
---|
352 | def = value.getNullInstance();
|
---|
353 | }
|
---|
354 | PrefEntry en = new PrefEntry(e.getKey(), value, def, false);
|
---|
355 | // after changes we have nondefault value. Value is changed if is not equal to old value
|
---|
356 | if ( !Preferences.isEqual(old, value) ) {
|
---|
357 | en.markAsChanged();
|
---|
358 | }
|
---|
359 | data.add(en);
|
---|
360 | }
|
---|
361 | for (Entry<String, Setting> e : defaults.entrySet()) {
|
---|
362 | if (!loaded.containsKey(e.getKey())) {
|
---|
363 | PrefEntry en = new PrefEntry(e.getKey(), e.getValue(), e.getValue(), true);
|
---|
364 | // after changes we have default value. So, value is changed if old value is not default
|
---|
365 | Setting old = orig.get(e.getKey());
|
---|
366 | if ( old!=null ) {
|
---|
367 | en.markAsChanged();
|
---|
368 | }
|
---|
369 | data.add(en);
|
---|
370 | }
|
---|
371 | }
|
---|
372 | Collections.sort(data);
|
---|
373 | displayData = new ArrayList<PrefEntry>(data);
|
---|
374 | }
|
---|
375 |
|
---|
376 | class AllSettingsTableModel extends DefaultTableModel {
|
---|
377 |
|
---|
378 | public AllSettingsTableModel() {
|
---|
379 | setColumnIdentifiers(new String[]{tr("Key"), tr("Value")});
|
---|
380 | }
|
---|
381 |
|
---|
382 | @Override
|
---|
383 | public boolean isCellEditable(int row, int column) {
|
---|
384 | return column == 1 && (displayData.get(row).getValue() instanceof StringSetting);
|
---|
385 | }
|
---|
386 |
|
---|
387 | @Override
|
---|
388 | public int getRowCount() {
|
---|
389 | return displayData.size();
|
---|
390 | }
|
---|
391 |
|
---|
392 | @Override
|
---|
393 | public Object getValueAt(int row, int column) {
|
---|
394 | if (column == 0)
|
---|
395 | return displayData.get(row).getKey();
|
---|
396 | else
|
---|
397 | return displayData.get(row);
|
---|
398 | }
|
---|
399 |
|
---|
400 | @Override
|
---|
401 | public void setValueAt(Object o, int row, int column) {
|
---|
402 | PrefEntry pe = displayData.get(row);
|
---|
403 | String s = (String) o;
|
---|
404 | if (!s.equals(pe.getValue().getValue())) {
|
---|
405 | pe.setValue(new StringSetting(s));
|
---|
406 | fireTableCellUpdated(row, column);
|
---|
407 | }
|
---|
408 | }
|
---|
409 | }
|
---|
410 |
|
---|
411 | private static class SettingCellRenderer extends DefaultTableCellRenderer {
|
---|
412 | private Color backgroundColor = Main.pref.getUIColor("Table.background");
|
---|
413 | private Color changedColor = Main.pref.getColor(
|
---|
414 | marktr("Advanced Background: Changed"),
|
---|
415 | new Color(200,255,200));
|
---|
416 | private Color foregroundColor = Main.pref.getUIColor("Table.foreground");
|
---|
417 | private Color nonDefaultColor = Main.pref.getColor(
|
---|
418 | marktr("Advanced Background: NonDefalut"),
|
---|
419 | new Color(255,255,200));
|
---|
420 |
|
---|
421 | @Override
|
---|
422 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
|
---|
423 | if (value == null)
|
---|
424 | return this;
|
---|
425 | PrefEntry pe = (PrefEntry) value;
|
---|
426 | Setting setting = pe.getValue();
|
---|
427 | Object val = setting.getValue();
|
---|
428 | String display = val != null ? val.toString() : "<html><i><"+tr("unset")+"></i></html>";
|
---|
429 |
|
---|
430 | JLabel label = (JLabel)super.getTableCellRendererComponent(table,
|
---|
431 | display, isSelected, hasFocus, row, column);
|
---|
432 |
|
---|
433 | label.setBackground(backgroundColor);
|
---|
434 | if (isSelected) {
|
---|
435 | label.setForeground(foregroundColor);
|
---|
436 | }
|
---|
437 | if(pe.isChanged()) {
|
---|
438 | label.setBackground(changedColor);
|
---|
439 | } else if(!pe.isDefault()) {
|
---|
440 | label.setBackground(nonDefaultColor);
|
---|
441 | }
|
---|
442 |
|
---|
443 | if (!pe.isDefault()) {
|
---|
444 | label.setFont(label.getFont().deriveFont(Font.BOLD));
|
---|
445 | }
|
---|
446 | val = pe.getDefaultValue().getValue();
|
---|
447 | if(val != null)
|
---|
448 | {
|
---|
449 | if(pe.isDefault()) {
|
---|
450 | label.setToolTipText(tr("Current value is default."));
|
---|
451 | } else {
|
---|
452 | label.setToolTipText(tr("Default value is ''{0}''.", val));
|
---|
453 | }
|
---|
454 | } else {
|
---|
455 | label.setToolTipText(tr("Default value currently unknown (setting has not been used yet)."));
|
---|
456 | }
|
---|
457 | return label;
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | private static class SettingCellEditor extends DefaultCellEditor {
|
---|
462 | public SettingCellEditor() {
|
---|
463 | super(new JTextField());
|
---|
464 | }
|
---|
465 |
|
---|
466 | @Override
|
---|
467 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
---|
468 | PrefEntry pe = (PrefEntry) value;
|
---|
469 | StringSetting stg = (StringSetting) pe.getValue();
|
---|
470 | String s = stg.getValue() == null ? "" : stg.getValue();
|
---|
471 | return super.getTableCellEditorComponent(table, s, isSelected, row, column);
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | private void applyFilter() {
|
---|
476 | displayData.clear();
|
---|
477 | for (PrefEntry e : data) {
|
---|
478 |
|
---|
479 | String prefKey = e.getKey();
|
---|
480 | Setting valueSetting = e.getValue();
|
---|
481 | String prefValue = valueSetting.getValue() == null ? "" : valueSetting.getValue().toString();
|
---|
482 |
|
---|
483 | String input[] = txtFilter.getText().split("\\s+");
|
---|
484 | boolean canHas = true;
|
---|
485 |
|
---|
486 | // Make 'wmsplugin cache' search for e.g. 'cache.wmsplugin'
|
---|
487 | final String prefKeyLower = prefKey.toLowerCase();
|
---|
488 | final String prefValueLower = prefValue.toLowerCase();
|
---|
489 | for (String bit : input) {
|
---|
490 | bit = bit.toLowerCase();
|
---|
491 | if (!prefKeyLower.contains(bit) && !prefValueLower.contains(bit)) {
|
---|
492 | canHas = false;
|
---|
493 | break;
|
---|
494 | }
|
---|
495 | }
|
---|
496 | if (canHas) {
|
---|
497 | displayData.add(e);
|
---|
498 | }
|
---|
499 | }
|
---|
500 | model.fireTableDataChanged();
|
---|
501 | }
|
---|
502 |
|
---|
503 | @Override
|
---|
504 | public boolean ok() {
|
---|
505 | for (PrefEntry e : data) {
|
---|
506 | if (e.isChanged()) {
|
---|
507 | Main.pref.putSetting(e.getKey(), e.getValue());
|
---|
508 | }
|
---|
509 | }
|
---|
510 | return false;
|
---|
511 | }
|
---|
512 |
|
---|
513 | private void resetPreference(final PreferenceTabbedPane gui, final JTable list) {
|
---|
514 | if (list.getSelectedRowCount() == 0) {
|
---|
515 | JOptionPane.showMessageDialog(
|
---|
516 | gui,
|
---|
517 | tr("Please select the row to delete."),
|
---|
518 | tr("Warning"),
|
---|
519 | JOptionPane.WARNING_MESSAGE
|
---|
520 | );
|
---|
521 | return;
|
---|
522 | }
|
---|
523 | for (int row : list.getSelectedRows()) {
|
---|
524 | PrefEntry e = displayData.get(row);
|
---|
525 | e.reset();
|
---|
526 | }
|
---|
527 | model.fireTableDataChanged();
|
---|
528 | }
|
---|
529 |
|
---|
530 | private void addPreference(final PreferenceTabbedPane gui) {
|
---|
531 | JPanel p = new JPanel(new GridBagLayout());
|
---|
532 | p.add(new JLabel(tr("Key")), GBC.std().insets(0,0,5,0));
|
---|
533 | JTextField tkey = new JTextField("", 50);
|
---|
534 | p.add(tkey, GBC.eop().insets(5,0,0,0).fill(GBC.HORIZONTAL));
|
---|
535 |
|
---|
536 | p.add(new JLabel(tr("Select Setting Type:")), GBC.eol().insets(5,15,5,0));
|
---|
537 |
|
---|
538 | JRadioButton rbString = new JRadioButton(tr("Simple"));
|
---|
539 | JRadioButton rbList = new JRadioButton(tr("List"));
|
---|
540 | JRadioButton rbListList = new JRadioButton(tr("List of lists"));
|
---|
541 | JRadioButton rbMapList = new JRadioButton(tr("List of maps"));
|
---|
542 |
|
---|
543 | ButtonGroup group = new ButtonGroup();
|
---|
544 | group.add(rbString);
|
---|
545 | group.add(rbList);
|
---|
546 | group.add(rbListList);
|
---|
547 | group.add(rbMapList);
|
---|
548 |
|
---|
549 | p.add(rbString, GBC.eol());
|
---|
550 | p.add(rbList, GBC.eol());
|
---|
551 | p.add(rbListList, GBC.eol());
|
---|
552 | p.add(rbMapList, GBC.eol());
|
---|
553 |
|
---|
554 | rbString.setSelected(true);
|
---|
555 |
|
---|
556 | ExtendedDialog dlg = new ExtendedDialog(gui, tr("Add setting"), new String[] {tr("OK"), tr("Cancel")});
|
---|
557 | dlg.setButtonIcons(new String[] {"ok.png", "cancel.png"});
|
---|
558 | dlg.setContent(p);
|
---|
559 | dlg.showDialog();
|
---|
560 |
|
---|
561 | PrefEntry pe = null;
|
---|
562 | boolean ok = false;
|
---|
563 | if (dlg.getValue() == 1) {
|
---|
564 | if (rbString.isSelected()) {
|
---|
565 | StringSetting sSetting = new StringSetting(null);
|
---|
566 | pe = new PrefEntry(tkey.getText(), sSetting, sSetting, false);
|
---|
567 | StringEditor sEditor = new StringEditor(gui, pe, sSetting);
|
---|
568 | sEditor.showDialog();
|
---|
569 | if (sEditor.getValue() == 1) {
|
---|
570 | String data = sEditor.getData();
|
---|
571 | if (!Utils.equal(sSetting.getValue(), data)) {
|
---|
572 | pe.setValue(new StringSetting(data));
|
---|
573 | ok = true;
|
---|
574 | }
|
---|
575 | }
|
---|
576 | } else if (rbList.isSelected()) {
|
---|
577 | ListSetting lSetting = new ListSetting(null);
|
---|
578 | pe = new PrefEntry(tkey.getText(), lSetting, lSetting, false);
|
---|
579 | ListEditor lEditor = new ListEditor(gui, pe, lSetting);
|
---|
580 | lEditor.showDialog();
|
---|
581 | if (lEditor.getValue() == 1) {
|
---|
582 | List<String> data = lEditor.getData();
|
---|
583 | if (!Preferences.equalCollection(lSetting.getValue(), data)) {
|
---|
584 | pe.setValue(new ListSetting(data));
|
---|
585 | ok = true;
|
---|
586 | }
|
---|
587 | }
|
---|
588 | } else if (rbListList.isSelected()) {
|
---|
589 | ListListSetting llSetting = new ListListSetting(null);
|
---|
590 | pe = new PrefEntry(tkey.getText(), llSetting, llSetting, false);
|
---|
591 | ListListEditor llEditor = new ListListEditor(gui, pe, llSetting);
|
---|
592 | llEditor.showDialog();
|
---|
593 | if (llEditor.getValue() == 1) {
|
---|
594 | List<List<String>> data = llEditor.getData();
|
---|
595 | if (!Preferences.equalArray((Collection) llSetting.getValue(), data)) {
|
---|
596 | pe.setValue(new ListListSetting(data));
|
---|
597 | ok = true;
|
---|
598 | }
|
---|
599 | }
|
---|
600 | } else if (rbMapList.isSelected()) {
|
---|
601 | MapListSetting mlSetting = new MapListSetting(null);
|
---|
602 | pe = new PrefEntry(tkey.getText(), mlSetting, mlSetting, false);
|
---|
603 | MapListEditor mlEditor = new MapListEditor(gui, pe, mlSetting);
|
---|
604 | mlEditor.showDialog();
|
---|
605 | if (mlEditor.getValue() == 1) {
|
---|
606 | List<Map<String, String>> data = mlEditor.getData();
|
---|
607 | if (!Preferences.equalListOfStructs(mlSetting.getValue(), data)) {
|
---|
608 | pe.setValue(new MapListSetting(data));
|
---|
609 | ok = true;
|
---|
610 | }
|
---|
611 | }
|
---|
612 | }
|
---|
613 | if (ok) {
|
---|
614 | data.add(pe);
|
---|
615 | Collections.sort(data);
|
---|
616 | applyFilter();
|
---|
617 | }
|
---|
618 | }
|
---|
619 | }
|
---|
620 |
|
---|
621 | private void editPreference(final PreferenceTabbedPane gui, final JTable list) {
|
---|
622 | if (list.getSelectedRowCount() != 1) {
|
---|
623 | JOptionPane.showMessageDialog(
|
---|
624 | gui,
|
---|
625 | tr("Please select the row to edit."),
|
---|
626 | tr("Warning"),
|
---|
627 | JOptionPane.WARNING_MESSAGE
|
---|
628 | );
|
---|
629 | return;
|
---|
630 | }
|
---|
631 | final PrefEntry e = (PrefEntry) model.getValueAt(list.getSelectedRow(), 1);
|
---|
632 | Setting stg = e.getValue();
|
---|
633 | if (stg instanceof StringSetting) {
|
---|
634 | list.editCellAt(list.getSelectedRow(), 1);
|
---|
635 | Component editor = list.getEditorComponent();
|
---|
636 | if (editor != null) {
|
---|
637 | editor.requestFocus();
|
---|
638 | }
|
---|
639 | } else if (stg instanceof ListSetting) {
|
---|
640 | ListSetting lSetting = (ListSetting) stg;
|
---|
641 | ListEditor lEditor = new ListEditor(gui, e, lSetting);
|
---|
642 | lEditor.showDialog();
|
---|
643 | if (lEditor.getValue() == 1) {
|
---|
644 | List<String> data = lEditor.getData();
|
---|
645 | if (!Preferences.equalCollection(lSetting.getValue(), data)) {
|
---|
646 | e.setValue(new ListSetting(data));
|
---|
647 | applyFilter();
|
---|
648 | }
|
---|
649 | }
|
---|
650 | } else if (stg instanceof ListListSetting) {
|
---|
651 | ListListEditor llEditor = new ListListEditor(gui, e, (ListListSetting) stg);
|
---|
652 | llEditor.showDialog();
|
---|
653 | if (llEditor.getValue() == 1) {
|
---|
654 | List<List<String>> data = llEditor.getData();
|
---|
655 | if (!Preferences.equalArray((Collection) stg.getValue(), data)) {
|
---|
656 | e.setValue(new ListListSetting(data));
|
---|
657 | applyFilter();
|
---|
658 | }
|
---|
659 | }
|
---|
660 | } else if (stg instanceof MapListSetting) {
|
---|
661 | MapListSetting mlSetting = (MapListSetting) stg;
|
---|
662 | MapListEditor mlEditor = new MapListEditor(gui, e, mlSetting);
|
---|
663 | mlEditor.showDialog();
|
---|
664 | if (mlEditor.getValue() == 1) {
|
---|
665 | List<Map<String, String>> data = mlEditor.getData();
|
---|
666 | if (!Preferences.equalListOfStructs(mlSetting.getValue(), data)) {
|
---|
667 | e.setValue(new MapListSetting(data));
|
---|
668 | applyFilter();
|
---|
669 | }
|
---|
670 | }
|
---|
671 | }
|
---|
672 | }
|
---|
673 | }
|
---|