Changeset 5114 in josm
- Timestamp:
- 2012-03-22T08:37:48+01:00 (13 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/Preferences.java
r5069 r5114 1749 1749 } 1750 1750 } 1751 1752 public static boolean isEqual(Setting a, Setting b) { 1753 if (a==null && b==null) return true; 1754 if (a==null) return false; 1755 if (b==null) return false; 1756 if (a==b) return true; 1757 1758 if (a instanceof StringSetting) 1759 return (a.getValue().equals(b.getValue())); 1760 if (a instanceof ListSetting) 1761 return equalCollection((Collection<String>) a.getValue(), (Collection<String>) b.getValue()); 1762 if (a instanceof ListListSetting) 1763 return equalArray((Collection<Collection<String>>) a.getValue(), (Collection<List<String>>) b.getValue()); 1764 if (a instanceof MapListSetting) 1765 return equalListOfStructs((Collection<Map<String, String>>) a.getValue(), (Collection<Map<String, String>>) b.getValue()); 1766 return a.equals(b); 1767 } 1768 1751 1769 } -
trunk/src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java
r5069 r5114 3 3 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 import static org.openstreetmap.josm.tools.I18n.marktr; 6 7 import java.awt.Color; 6 8 import java.awt.Component; 7 9 import java.awt.Dimension; … … 12 14 import java.awt.event.MouseAdapter; 13 15 import java.awt.event.MouseEvent; 16 17 import java.io.File; 14 18 import java.util.ArrayList; 15 19 import java.util.Collection; 16 20 import java.util.Collections; 21 import java.util.Comparator; 17 22 import java.util.List; 18 23 import java.util.Map; … … 23 28 import javax.swing.DefaultCellEditor; 24 29 import javax.swing.JButton; 30 import javax.swing.JFileChooser; 25 31 import javax.swing.JLabel; 26 32 import javax.swing.JOptionPane; … … 32 38 import javax.swing.event.DocumentEvent; 33 39 import javax.swing.event.DocumentListener; 40 import javax.swing.filechooser.FileFilter; 34 41 import javax.swing.table.DefaultTableCellRenderer; 35 42 import javax.swing.table.DefaultTableModel; 36 43 37 44 import org.openstreetmap.josm.Main; 45 import org.openstreetmap.josm.data.CustomConfigurator; 38 46 import org.openstreetmap.josm.data.Preferences; 39 47 import org.openstreetmap.josm.data.Preferences.ListListSetting; … … 43 51 import org.openstreetmap.josm.data.Preferences.StringSetting; 44 52 import org.openstreetmap.josm.gui.ExtendedDialog; 53 import org.openstreetmap.josm.gui.actionsupport.LogShowDialog; 45 54 import org.openstreetmap.josm.gui.preferences.DefaultTabPreferenceSetting; 46 55 import org.openstreetmap.josm.gui.preferences.PreferenceSetting; … … 58 67 } 59 68 } 60 69 61 70 private AdvancedPreference() { 62 71 super("advanced", tr("Advanced Preferences"), tr("Setting Preference entries directly. Use with caution!")); … … 110 119 return changed; 111 120 } 112 121 122 private void markAsChanged() { 123 changed = true; 124 } 125 113 126 public void reset() { 114 127 value = defaultValue; … … 155 168 } 156 169 }); 157 158 Map<String, Setting> orig = Main.pref.getAllSettings(); 159 Map<String, Setting> defaults = Main.pref.getAllDefaults(); 160 orig.remove("osm-server.password"); 161 defaults.remove("osm-server.password"); 162 prepareData(orig, defaults); 170 readPreferences(Main.pref); 163 171 model = new AllSettingsTableModel(); 164 172 applyFilter(); … … 198 206 }); 199 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 200 287 list.addMouseListener(new MouseAdapter(){ 201 288 @Override public void mouseClicked(MouseEvent e) { … … 207 294 } 208 295 209 private void prepareData(Map<String, Setting> orig, Map<String, Setting> defaults) { 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 } else { 305 loaded = orig; 306 } 307 prepareData(loaded, orig, defaults); 308 } 309 310 private File[] askUserForCustomSettingsFiles(boolean saveFileFlag, String title) { 311 String dir = Main.pref.get("customsettings.lastDirectory"); 312 if (dir.length()==0) dir ="."; 313 314 JFileChooser fc = new JFileChooser(dir); 315 fc.setDialogTitle(title); 316 fc.setFileSelectionMode(JFileChooser.FILES_ONLY); 317 fc.setAcceptAllFileFilterUsed(false); 318 fc.setFileFilter(new FileFilter() { 319 @Override 320 public boolean accept(File f) { 321 return f.isDirectory() || f.getName().toLowerCase().endsWith(".xml"); 322 } 323 @Override 324 public String getDescription() { 325 return tr("JOSM custom settings files (*.xml)"); 326 } 327 }); 328 329 fc.setMultiSelectionEnabled(!saveFileFlag); 330 int result = saveFileFlag? fc.showSaveDialog(Main.parent) : fc.showOpenDialog(Main.parent); 331 if (result == JFileChooser.APPROVE_OPTION) { 332 if (!fc.getCurrentDirectory().getAbsolutePath().equals(dir)) { 333 Main.pref.put("customsettings.lastDirectory", fc.getCurrentDirectory().getAbsolutePath()); 334 } 335 File sel[] = fc.isMultiSelectionEnabled() ? fc.getSelectedFiles() : (new File[]{fc.getSelectedFile()}); 336 if (sel.length==1 && !sel[0].getName().contains(".")) sel[0]=new File(sel[0].getAbsolutePath()+".xml"); 337 return sel; 338 } 339 return new File[0]; 340 } 341 342 private void prepareData(Map<String, Setting> loaded, Map<String, Setting> orig, Map<String, Setting> defaults) { 210 343 data = new ArrayList<PrefEntry>(); 211 for (Entry<String, Setting> e : orig.entrySet()) {344 for (Entry<String, Setting> e : loaded.entrySet()) { 212 345 Setting value = e.getValue(); 346 Setting old = orig.get(e.getKey()); 213 347 Setting def = defaults.get(e.getKey()); 214 348 if (def == null) { … … 216 350 } 217 351 PrefEntry en = new PrefEntry(e.getKey(), value, def, false); 352 // after changes we have nondefault value. Value is changed if is not equal to old value 353 if ( !Preferences.isEqual(old, value) ) { 354 en.markAsChanged(); 355 } 218 356 data.add(en); 219 357 } 220 358 for (Entry<String, Setting> e : defaults.entrySet()) { 221 if (! orig.containsKey(e.getKey())) {359 if (!loaded.containsKey(e.getKey())) { 222 360 PrefEntry en = new PrefEntry(e.getKey(), e.getValue(), e.getValue(), true); 361 // after changes we have default value. So, value is changed if old value is not default 362 Setting old = orig.get(e.getKey()); 363 if ( old!=null ) { 364 en.markAsChanged(); 365 } 223 366 data.add(en); 224 367 } … … 264 407 265 408 private static class SettingCellRenderer extends DefaultTableCellRenderer { 409 private Color backgroundColor = Main.pref.getUIColor("Table.background"); 410 private Color changedColor = Main.pref.getColor( 411 marktr("Advanced Background: Changed"), 412 new Color(200,255,200)); 413 private Color foregroundColor = Main.pref.getUIColor("Table.foreground"); 414 private Color nonDefaultColor = Main.pref.getColor( 415 marktr("Advanced Background: NonDefalut"), 416 new Color(255,255,200)); 417 266 418 @Override 267 419 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { … … 272 424 Object val = setting.getValue(); 273 425 String display = val != null ? val.toString() : "<html><i><"+tr("unset")+"></i></html>"; 274 426 275 427 JLabel label = (JLabel)super.getTableCellRendererComponent(table, 276 428 display, isSelected, hasFocus, row, column); 429 430 label.setBackground(backgroundColor); 431 if (isSelected) { 432 label.setForeground(foregroundColor); 433 } 434 if(pe.isChanged()) { 435 label.setBackground(changedColor); 436 } else if(!pe.isDefault()) { 437 label.setBackground(nonDefaultColor); 438 } 439 277 440 if (!pe.isDefault()) { 278 441 label.setFont(label.getFont().deriveFont(Font.BOLD));
Note:
See TracChangeset
for help on using the changeset viewer.