1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.preferences;
|
---|
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.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.ActionListener;
|
---|
11 | import java.awt.event.MouseAdapter;
|
---|
12 | import java.awt.event.MouseEvent;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.TreeSet;
|
---|
15 | import java.util.Map.Entry;
|
---|
16 |
|
---|
17 | import javax.swing.Box;
|
---|
18 | import javax.swing.JButton;
|
---|
19 | import javax.swing.JLabel;
|
---|
20 | import javax.swing.JOptionPane;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.JScrollPane;
|
---|
23 | import javax.swing.JTable;
|
---|
24 | import javax.swing.JTextField;
|
---|
25 | import javax.swing.table.DefaultTableCellRenderer;
|
---|
26 | import javax.swing.table.DefaultTableModel;
|
---|
27 |
|
---|
28 | import org.openstreetmap.josm.Main;
|
---|
29 | import org.openstreetmap.josm.tools.GBC;
|
---|
30 |
|
---|
31 | public class AdvancedPreference implements PreferenceSetting {
|
---|
32 |
|
---|
33 | private Map<String,String> orig;
|
---|
34 | private Map<String,String> defaults;
|
---|
35 | private DefaultTableModel model;
|
---|
36 |
|
---|
37 | public void addGui(final PreferenceDialog gui) {
|
---|
38 | JPanel p = gui.createPreferenceTab("advanced", tr("Advanced Preferences"), tr("Setting Preference entries directly. Use with caution!"));
|
---|
39 |
|
---|
40 | model = new DefaultTableModel(new String[]{tr("Key"), tr("Value")},0) {
|
---|
41 | @Override public boolean isCellEditable(int row, int column) {
|
---|
42 | return column != 0;
|
---|
43 | }
|
---|
44 | };
|
---|
45 | DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(){
|
---|
46 | public Component getTableCellRendererComponent(JTable table, Object value,
|
---|
47 | boolean isSelected, boolean hasFocus, int row, int column)
|
---|
48 | {
|
---|
49 | JLabel label=new JLabel();
|
---|
50 | String s = defaults.get(value);
|
---|
51 | if(s != null)
|
---|
52 | {
|
---|
53 | if(s.equals(model.getValueAt(row, 1)))
|
---|
54 | label.setToolTipText(tr("Current value is default."));
|
---|
55 | else
|
---|
56 | label.setToolTipText(tr("Default value is ''{0}''.", s));
|
---|
57 | }
|
---|
58 | else
|
---|
59 | label.setToolTipText(tr("Default value currently unknown (setting has not been used yet)."));
|
---|
60 | label.setText((String)value);
|
---|
61 | return label;
|
---|
62 | }
|
---|
63 | };
|
---|
64 | final JTable list = new JTable(model);
|
---|
65 | list.getColumn(tr("Key")).setCellRenderer(renderer);
|
---|
66 | JScrollPane scroll = new JScrollPane(list);
|
---|
67 | p.add(scroll, GBC.eol().fill(GBC.BOTH));
|
---|
68 | scroll.setPreferredSize(new Dimension(400,200));
|
---|
69 |
|
---|
70 | orig = Main.pref.getAllPrefix("");
|
---|
71 | defaults = Main.pref.getDefaults();
|
---|
72 | orig.remove("osm-server.password");
|
---|
73 | defaults.remove("osm-server.password");
|
---|
74 | TreeSet<String> ts = new TreeSet<String>(orig.keySet());
|
---|
75 | for (String s : defaults.keySet())
|
---|
76 | {
|
---|
77 | if(!ts.contains(s))
|
---|
78 | ts.add(s);
|
---|
79 | }
|
---|
80 |
|
---|
81 | for (String s : ts)
|
---|
82 | {
|
---|
83 | String val = Main.pref.get(s);
|
---|
84 | if(val == null) val = "";
|
---|
85 | model.addRow(new String[]{s, val});
|
---|
86 | }
|
---|
87 |
|
---|
88 | JButton add = new JButton(tr("Add"));
|
---|
89 | p.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
|
---|
90 | p.add(add, GBC.std().insets(0,5,0,0));
|
---|
91 | add.addActionListener(new ActionListener(){
|
---|
92 | public void actionPerformed(ActionEvent e) {
|
---|
93 | JPanel p = new JPanel(new GridBagLayout());
|
---|
94 | p.add(new JLabel(tr("Key")), GBC.std().insets(0,0,5,0));
|
---|
95 | JTextField key = new JTextField(10);
|
---|
96 | JTextField value = new JTextField(10);
|
---|
97 | p.add(key, GBC.eop().insets(5,0,0,0).fill(GBC.HORIZONTAL));
|
---|
98 | p.add(new JLabel(tr("Value")), GBC.std().insets(0,0,5,0));
|
---|
99 | p.add(value, GBC.eol().insets(5,0,0,0).fill(GBC.HORIZONTAL));
|
---|
100 | int answer = JOptionPane.showConfirmDialog(gui, p, tr("Enter a new key/value pair"), JOptionPane.OK_CANCEL_OPTION);
|
---|
101 | if (answer == JOptionPane.OK_OPTION)
|
---|
102 | model.addRow(new String[]{key.getText(), value.getText()});
|
---|
103 | }
|
---|
104 | });
|
---|
105 |
|
---|
106 | JButton edit = new JButton(tr("Edit"));
|
---|
107 | p.add(edit, GBC.std().insets(5,5,5,0));
|
---|
108 | edit.addActionListener(new ActionListener(){
|
---|
109 | public void actionPerformed(ActionEvent e) {
|
---|
110 | edit(gui, list);
|
---|
111 | }
|
---|
112 | });
|
---|
113 |
|
---|
114 | JButton delete = new JButton(tr("Delete"));
|
---|
115 | p.add(delete, GBC.std().insets(0,5,0,0));
|
---|
116 | delete.addActionListener(new ActionListener(){
|
---|
117 | public void actionPerformed(ActionEvent e) {
|
---|
118 | if (list.getSelectedRowCount() == 0) {
|
---|
119 | JOptionPane.showMessageDialog(gui, tr("Please select the row to delete."));
|
---|
120 | return;
|
---|
121 | }
|
---|
122 | for(int row: list.getSelectedRows())
|
---|
123 | model.setValueAt("", row, 1);
|
---|
124 | }
|
---|
125 | });
|
---|
126 |
|
---|
127 | list.addMouseListener(new MouseAdapter(){
|
---|
128 | @Override public void mouseClicked(MouseEvent e) {
|
---|
129 | if (e.getClickCount() == 2)
|
---|
130 | edit(gui, list);
|
---|
131 | }
|
---|
132 | });
|
---|
133 | }
|
---|
134 |
|
---|
135 | public void ok() {
|
---|
136 | for (int i = 0; i < model.getRowCount(); ++i) {
|
---|
137 | String value = model.getValueAt(i,1).toString();
|
---|
138 | if(value.length() != 0)
|
---|
139 | {
|
---|
140 | String key = model.getValueAt(i,0).toString();
|
---|
141 | String origValue = orig.get(key);
|
---|
142 | if (origValue == null || !origValue.equals(value))
|
---|
143 | Main.pref.put(key, value);
|
---|
144 | orig.remove(key); // processed.
|
---|
145 | }
|
---|
146 | }
|
---|
147 | for (Entry<String, String> e : orig.entrySet())
|
---|
148 | Main.pref.put(e.getKey(), null);
|
---|
149 | }
|
---|
150 |
|
---|
151 |
|
---|
152 | private void edit(final PreferenceDialog gui, final JTable list) {
|
---|
153 | if (list.getSelectedRowCount() != 1) {
|
---|
154 | JOptionPane.showMessageDialog(gui, tr("Please select the row to edit."));
|
---|
155 | return;
|
---|
156 | }
|
---|
157 | String v = JOptionPane.showInputDialog(tr("New value for {0}", model.getValueAt(list.getSelectedRow(), 0)), model.getValueAt(list.getSelectedRow(), 1));
|
---|
158 | if (v != null)
|
---|
159 | model.setValueAt(v, list.getSelectedRow(), 1);
|
---|
160 | }
|
---|
161 | }
|
---|