- Timestamp:
- 2011-12-04T19:17:56+01:00 (13 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 5 added
- 3 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r4614 r4634 65 65 * accessed using one of the get...() methods. You can use the same preference 66 66 * key in different parts of the code, but the default value must be the same 67 * everywhere. null is a legitimate default value. 67 * everywhere. A default value of null means, the setting has been requested, but 68 * no default value was set. This is used in advanced preferences to present a list 69 * off all possible settings. 68 70 * 69 71 * At the moment, there is no such thing as an empty value. … … 99 101 T getValue(); 100 102 void visit(SettingVisitor visitor); 103 Setting<T> getNullInstance(); 101 104 } 102 105 … … 118 121 visitor.visit(this); 119 122 } 123 public StringSetting getNullInstance() { 124 return new StringSetting(null); 125 } 120 126 } 121 127 … … 127 133 visitor.visit(this); 128 134 } 135 public ListSetting getNullInstance() { 136 return new ListSetting(null); 137 } 129 138 } 130 139 … … 136 145 visitor.visit(this); 137 146 } 147 public ListListSetting getNullInstance() { 148 return new ListListSetting(null); 149 } 138 150 } 139 151 … … 144 156 public void visit(SettingVisitor visitor) { 145 157 visitor.visit(this); 158 } 159 public MapListSetting getNullInstance() { 160 return new MapListSetting(null); 146 161 } 147 162 } … … 825 840 */ 826 841 public Collection<String> getCollection(String key, Collection<String> def) { 827 if (def != null) { 828 putCollectionDefault(key, new ArrayList<String>(def)); 829 } 842 putCollectionDefault(key, def == null ? null : new ArrayList<String>(def)); 830 843 Collection<String> prop = getCollectionInternal(key); 831 844 if (prop != null) … … 904 917 } 905 918 906 p rivatestatic boolean equalCollection(Collection<String> a, Collection<String> b) {919 public static boolean equalCollection(Collection<String> a, Collection<String> b) { 907 920 if (a == null) return b == null; 908 921 if (b == null) return false; … … 947 960 } 948 961 putArrayDefault(key, Collections.unmodifiableList(defCopy)); 962 } else { 963 putArrayDefault(key, null); 949 964 } 950 965 List<List<String>> prop = getArrayInternal(key); … … 955 970 } else 956 971 return def; 972 } 973 974 public Collection<Collection<String>> getArray(String key) { 975 putArrayDefault(key, null); 976 List<List<String>> prop = getArrayInternal(key); 977 if (prop != null) { 978 @SuppressWarnings("unchecked") 979 Collection<Collection<String>> prop_cast = (Collection) prop; 980 return prop_cast; 981 } else 982 return Collections.emptyList(); 957 983 } 958 984 … … 1020 1046 } 1021 1047 1022 p rivatestatic boolean equalArray(Collection<Collection<String>> a, Collection<List<String>> b) {1048 public static boolean equalArray(Collection<Collection<String>> a, Collection<List<String>> b) { 1023 1049 if (a == null) return b == null; 1024 1050 if (b == null) return false; … … 1043 1069 } 1044 1070 putListOfStructsDefault(key, Collections.unmodifiableList(defCopy)); 1071 } else { 1072 putListOfStructsDefault(key, null); 1045 1073 } 1046 1074 Collection<Map<String, String>> prop = getListOfStructsInternal(key); … … 1117 1145 } 1118 1146 1119 p rivatestatic boolean equalListOfStructs(Collection<Map<String, String>> a, Collection<Map<String, String>> b) {1147 public static boolean equalListOfStructs(Collection<Map<String, String>> a, Collection<Map<String, String>> b) { 1120 1148 if (a == null) return b == null; 1121 1149 if (b == null) return false; … … 1298 1326 } 1299 1327 return struct; 1328 } 1329 1330 public boolean putSetting(final String key, Setting value) { 1331 if (value == null) return false; 1332 class PutVisitor implements SettingVisitor { 1333 public boolean changed; 1334 public void visit(StringSetting setting) { 1335 changed = put(key, setting.getValue()); 1336 } 1337 public void visit(ListSetting setting) { 1338 changed = putCollection(key, setting.getValue()); 1339 } 1340 public void visit(ListListSetting setting) { 1341 changed = putArray(key, (Collection) setting.getValue()); 1342 } 1343 public void visit(MapListSetting setting) { 1344 changed = putListOfStructs(key, setting.getValue()); 1345 } 1346 }; 1347 PutVisitor putVisitor = new PutVisitor(); 1348 value.visit(putVisitor); 1349 return putVisitor.changed; 1350 } 1351 1352 public Map<String, Setting> getAllSettings() { 1353 Map<String, Setting> settings = new TreeMap<String, Setting>(); 1354 1355 for (Entry<String, String> e : properties.entrySet()) { 1356 settings.put(e.getKey(), new StringSetting(e.getValue())); 1357 } 1358 for (Entry<String, List<String>> e : collectionProperties.entrySet()) { 1359 settings.put(e.getKey(), new ListSetting(e.getValue())); 1360 } 1361 for (Entry<String, List<List<String>>> e : arrayProperties.entrySet()) { 1362 settings.put(e.getKey(), new ListListSetting(e.getValue())); 1363 } 1364 for (Entry<String, List<Map<String, String>>> e : listOfStructsProperties.entrySet()) { 1365 settings.put(e.getKey(), new MapListSetting(e.getValue())); 1366 } 1367 return settings; 1368 } 1369 1370 public Map<String, Setting> getAllDefaults() { 1371 Map<String, Setting> allDefaults = new TreeMap<String, Setting>(); 1372 1373 for (Entry<String, String> e : defaults.entrySet()) { 1374 allDefaults.put(e.getKey(), new StringSetting(e.getValue())); 1375 } 1376 for (Entry<String, List<String>> e : collectionDefaults.entrySet()) { 1377 allDefaults.put(e.getKey(), new ListSetting(e.getValue())); 1378 } 1379 for (Entry<String, List<List<String>>> e : arrayDefaults.entrySet()) { 1380 allDefaults.put(e.getKey(), new ListListSetting(e.getValue())); 1381 } 1382 for (Entry<String, List<Map<String, String>>> e : listOfStructsDefaults.entrySet()) { 1383 allDefaults.put(e.getKey(), new MapListSetting(e.getValue())); 1384 } 1385 return allDefaults; 1300 1386 } 1301 1387 -
trunk/src/org/openstreetmap/josm/gui/preferences/PreferenceTabbedPane.java
r4586 r4634 28 28 29 29 import org.openstreetmap.josm.Main; 30 import org.openstreetmap.josm.gui.preferences.advanced.AdvancedPreference; 30 31 import org.openstreetmap.josm.plugins.PluginDownloadTask; 31 32 import org.openstreetmap.josm.plugins.PluginHandler; -
trunk/src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java
r4626 r4634 1 1 // License: GPL. Copyright 2007 by Immanuel Scholz and others 2 package org.openstreetmap.josm.gui.preferences ;2 package org.openstreetmap.josm.gui.preferences.advanced; 3 3 4 4 import static org.openstreetmap.josm.tools.I18n.tr; … … 6 6 import java.awt.Component; 7 7 import java.awt.Dimension; 8 import java.awt.Font; 8 9 import java.awt.GridBagLayout; 9 10 import java.awt.event.ActionEvent; … … 11 12 import java.awt.event.MouseAdapter; 12 13 import java.awt.event.MouseEvent; 13 import java.util.Arrays;14 14 import java.util.ArrayList; 15 import java.util.List; 15 16 import java.util.Map; 16 import java.util.TreeMap;17 import java.util.TreeSet;18 17 import java.util.Map.Entry; 18 import java.util.Collection; 19 import java.util.Collections; 19 20 20 21 import javax.swing.Box; 22 import javax.swing.ButtonGroup; 21 23 import javax.swing.JButton; 22 24 import javax.swing.JLabel; 23 25 import javax.swing.JOptionPane; 24 26 import javax.swing.JPanel; 27 import javax.swing.JRadioButton; 25 28 import javax.swing.JScrollPane; 26 29 import javax.swing.JTable; … … 30 33 import javax.swing.table.DefaultTableCellRenderer; 31 34 import javax.swing.table.DefaultTableModel; 35 import javax.swing.DefaultCellEditor; 32 36 33 37 import org.openstreetmap.josm.Main; 34 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; 35 49 import org.openstreetmap.josm.tools.GBC; 50 import org.openstreetmap.josm.tools.Utils; 36 51 37 52 public class AdvancedPreference implements PreferenceSetting { … … 43 58 } 44 59 45 private Map<String,String> orig; 46 private Map<String,String> defaults; 47 private DefaultTableModel model; 48 protected Map<String, String> data; 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; 49 113 protected JTextField txtFilter; 50 114 … … 59 123 p.add(txtFilter, GBC.eol().fill(GBC.HORIZONTAL)); 60 124 txtFilter.getDocument().addDocumentListener(new DocumentListener(){ 61 public void changedUpdate(DocumentEvent e) {125 @Override public void changedUpdate(DocumentEvent e) { 62 126 action(); 63 127 } 64 65 public void insertUpdate(DocumentEvent e) { 128 @Override public void insertUpdate(DocumentEvent e) { 66 129 action(); 67 130 } 68 69 public void removeUpdate(DocumentEvent e) { 131 @Override public void removeUpdate(DocumentEvent e) { 70 132 action(); 71 133 } 72 73 134 private void action() { 74 dataToModel();135 applyFilter(); 75 136 } 76 137 }); 77 138 78 model = new DefaultTableModel(new String[]{tr("Key"), tr("Value")},0) { 79 @Override public boolean isCellEditable(int row, int column) { 80 return column != 0; 81 } 82 @Override public void fireTableCellUpdated(int row, int column) 83 { 84 super.fireTableCellUpdated(row, column); 85 if(column == 1) 86 { 87 data.put((String) model.getValueAt(row, 0), 88 (String) model.getValueAt(row, 1)); 89 } 90 } 91 92 }; 93 DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(){ 94 @Override 95 public Component getTableCellRendererComponent(JTable table, Object value, 96 boolean isSelected, boolean hasFocus, int row, int column) 97 { 98 JLabel label=new JLabel(); 99 String s = defaults.get(value); 100 if(s != null) 101 { 102 if(s.equals(model.getValueAt(row, 1))) { 103 label.setToolTipText(tr("Current value is default.")); 104 } else { 105 label.setToolTipText(tr("Default value is ''{0}''.", s)); 106 } 107 } else { 108 label.setToolTipText(tr("Default value currently unknown (setting has not been used yet).")); 109 } 110 label.setText((String)value); 111 return label; 112 } 113 }; 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 114 147 final JTable list = new JTable(model); 115 148 list.putClientProperty("terminateEditOnFocusLost", true); 116 list.getColumn(tr("Key")).setCellRenderer(renderer); 149 list.getColumnModel().getColumn(1).setCellRenderer(new SettingCellRenderer()); 150 list.getColumnModel().getColumn(1).setCellEditor(new SettingCellEditor()); 151 117 152 JScrollPane scroll = new JScrollPane(list); 118 153 p.add(scroll, GBC.eol().fill(GBC.BOTH)); 119 154 scroll.setPreferredSize(new Dimension(400,200)); 120 121 orig = Main.pref.getAllPrefix("");122 defaults = Main.pref.getDefaults();123 orig.remove("osm-server.password");124 defaults.remove("osm-server.password");125 prepareData();126 dataToModel();127 155 128 156 JButton add = new JButton(tr("Add")); … … 143 171 }); 144 172 145 JButton delete = new JButton(tr("Delete"));146 p.add( delete, GBC.std().insets(0,5,0,0));147 delete.addActionListener(new ActionListener(){173 JButton reset = new JButton(tr("Reset")); 174 p.add(reset, GBC.std().insets(0,5,0,0)); 175 reset.addActionListener(new ActionListener(){ 148 176 public void actionPerformed(ActionEvent e) { 149 re movePreference(gui, list);177 resetPreference(gui, list); 150 178 } 151 179 }); … … 160 188 } 161 189 162 private void prepareData() { 163 TreeSet<String> ts = new TreeSet<String>(orig.keySet()); 164 for (String s : defaults.keySet()) 165 { 166 if(!ts.contains(s)) { 167 ts.add(s); 168 } 169 } 170 data = new TreeMap<String, String>(); 171 for (String s : ts) 172 { 173 String val = Main.pref.get(s); 174 if(val == null) { 175 val = ""; 176 } 177 data.put(s, val); 178 } 179 } 180 181 private void dataToModel() { 182 while (model.getRowCount() > 0) { 183 model.removeRow(0); 184 } 185 for (String prefKey : data.keySet()) { 186 String prefValue = data.get(prefKey); 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 187 287 String input[] = txtFilter.getText().split("\\s+"); 188 288 boolean canHas = true; … … 195 295 if (!prefKeyLower.contains(bit) && !prefValueLower.contains(bit)) { 196 296 canHas = false; 197 }198 }199 297 break; 298 } 299 } 200 300 if (canHas) { 201 model.addRow(new String[] {prefKey, prefValue}); 202 } 203 } 204 } 205 301 displayData.add(e); 302 } 303 } 304 model.fireTableDataChanged(); 305 } 306 307 @Override 206 308 public boolean ok() { 207 for (String key : data.keySet()) { 208 String value = data.get(key); 209 if(value.length() != 0) 210 { 211 String origValue = orig.get(key); 212 if (origValue == null || !origValue.equals(value)) { 213 Main.pref.put(key, value); 214 } 215 orig.remove(key); // processed. 216 } 217 } 218 for (Entry<String, String> e : orig.entrySet()) { 219 Main.pref.put(e.getKey(), null); 309 for (PrefEntry e : data) { 310 if (e.isChanged()) { 311 Main.pref.putSetting(e.getKey(), e.getValue()); 312 } 220 313 } 221 314 return false; 222 315 } 223 316 224 private void re movePreference(final PreferenceTabbedPane gui, final JTable list) {317 private void resetPreference(final PreferenceTabbedPane gui, final JTable list) { 225 318 if (list.getSelectedRowCount() == 0) { 226 319 JOptionPane.showMessageDialog( … … 232 325 return; 233 326 } 234 for(int row: list.getSelectedRows()) { 235 data.put((String) model.getValueAt(row, 0), ""); 236 model.setValueAt("", row, 1); 237 } 327 for (int row : list.getSelectedRows()) { 328 PrefEntry e = displayData.get(row); 329 e.reset(); 330 } 331 model.fireTableDataChanged(); 238 332 } 239 333 240 334 private void addPreference(final PreferenceTabbedPane gui) { 241 String s[] = showEditDialog(gui, tr("Enter a new key/value pair"), 242 "", ""); 243 if(s != null && !s[0].isEmpty() && !s[1].isEmpty()) { 244 data.put(s[0], s[1]); 245 dataToModel(); 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 } 246 422 } 247 423 } … … 257 433 return; 258 434 } 259 String key = (String)model.getValueAt(list.getSelectedRow(), 0); 260 String value = data.get(key); 261 if(value.isEmpty()) 262 value = defaults.get(key); 263 String s[] = showEditDialog(gui, tr("Change a key/value pair"), 264 key, value); 265 if(s != null && !s[0].isEmpty()) { 266 data.put(s[0], s[1]); 267 if(!s[0].equals(key)) 268 data.put(key,""); 269 dataToModel(); 270 } 271 } 272 273 private String[] showEditDialog(final PreferenceTabbedPane gui, String title, 274 String key, String value) { 275 JPanel p = new JPanel(new GridBagLayout()); 276 p.add(new JLabel(tr("Key")), GBC.std().insets(0,0,5,0)); 277 JTextField tkey = new JTextField(key, 50); 278 JTextField tvalue = new JTextField(value, 50); 279 p.add(tkey, GBC.eop().insets(5,0,0,0).fill(GBC.HORIZONTAL)); 280 PrefValueTableModel model = new PrefValueTableModel(value); 281 p.add(new JLabel(tr("Values")), GBC.std().insets(0,0,5,0)); 282 JTable table = new JTable(model); 283 table.putClientProperty("terminateEditOnFocusLost", true); 284 table.getTableHeader().setVisible(false); 285 JScrollPane pane = new JScrollPane(table); 286 Dimension d = pane.getPreferredSize(); 287 d.height = (d.height/20)*(model.getRowCount()+4); 288 pane.setPreferredSize(d); 289 p.add(pane, GBC.eol().insets(5,10,0,0).fill(GBC.HORIZONTAL)); 290 ExtendedDialog ed = new ExtendedDialog(gui, title, 291 new String[] {tr("OK"), tr("Cancel")}); 292 ed.setButtonIcons(new String[] {"ok.png", "cancel.png"}); 293 ed.setContent(p); 294 ed.showDialog(); 295 if(ed.getValue() == 1) { 296 return new String[]{tkey.getText(), model.getText()}; 297 } 298 return null; 299 } 300 class PrefValueTableModel extends DefaultTableModel { 301 private final ArrayList<String> data = new ArrayList<String>(); 302 public PrefValueTableModel(String val) { 303 data.addAll(Arrays.asList(val.split("\u001e"))); 304 setColumnIdentifiers(new String[]{""}); 305 } 306 307 public String getText() { 308 String s = null; 309 for(String a : data) 310 { 311 if(s == null) { 312 s = a; 313 } else { 314 s += "\u001e" + a; 315 } 316 } 317 return s == null ? "" : s; 318 } 319 320 @Override 321 public int getRowCount() { 322 return data == null ? 1 : data.size()+1; 323 } 324 325 @Override 326 public Object getValueAt(int row, int column) { 327 return data.size() == row ? "" : data.get(row); 328 } 329 330 @Override 331 public void setValueAt(Object o, int row, int column) { 332 String s = (String)o; 333 if(row == data.size()) { 334 data.add(s); 335 fireTableRowsInserted(row+1, row+1); 336 } else { 337 data.set(row, s); 338 } 339 fireTableCellUpdated(row, column); 340 } 341 342 @Override 343 public boolean isCellEditable(int row, int column) { 344 return true; 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 } 345 475 } 346 476 } -
trunk/src/org/openstreetmap/josm/tools/WindowGeometry.java
r4571 r4634 264 264 window.setSize(extent); 265 265 } 266 267 public String toString() { 268 return "WindowGeometry{topLeft="+topLeft+",extent="+extent+"}"; 269 } 266 270 }
Note:
See TracChangeset
for help on using the changeset viewer.