Changeset 3531 in josm for trunk/src


Ignore:
Timestamp:
2010-09-16T10:27:46+02:00 (14 years ago)
Author:
stoecker
Message:

fix array preferences a bit

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r3530 r3531  
    601601    synchronized private void putCollectionDefault(String key, Collection<String> val) {
    602602        String s = null;
    603         if(val != null)
     603        for(String a : val)
    604604        {
    605             for(String a : val)
    606             {
    607                 if(s != null) {
    608                     s += "\u001e" + a;
    609                 } else {
    610                     s = a;
    611                 }
     605            if(s != null) {
     606                s += "\u001e" + a;
     607            } else {
     608                s = a;
    612609            }
    613610        }
     
    616613    synchronized public Collection<Collection<String>> getArray(String key,
    617614    Collection<Collection<String>> def) {
    618         if(def != null) {
    619             for(String k : getAllPrefixDefault(key + ".").keySet())
    620                 put(k, null);
    621             int num = 0;
    622             for(Collection<String> c : def)
    623                 putCollectionDefault(key+"."+num++, c);
    624         }
    625         String s = get(key+".0");
    626         if(s != null && s.length() != 0)
    627         {
    628             Collection<Collection<String>> col = new LinkedList<Collection<String>>();
    629             for(int num = 0; ; ++num) {
    630                 Collection<String> c = getCollection(key+"."+num, null);
    631                 if(c == null)
    632                     break;
    633                 col.add(c);
    634             }
    635             return col;
    636         }
    637         return def;
     615        if(def != null)
     616            putArrayDefault(key, def);
     617        key += ".";
     618        int num = 0;
     619        Collection<Collection<String>> col = new LinkedList<Collection<String>>();
     620        while(properties.containsKey(key+num))
     621            col.add(getCollection(key+num++, null));
     622        return num == 0 && def != null ? def : col;
    638623    }
    639624    synchronized public boolean putArray(String key, Collection<Collection<String>> val) {
    640625        boolean res = true;
     626        key += ".";
    641627        Collection<String> keys = getAllPrefix(key).keySet();
    642         key += ".";
    643628        if(val != null) {
    644             String s = null;
    645629            int num = 0;
    646630            for(Collection<String> c : val) {
     
    661645    }
    662646
     647    synchronized private void putArrayDefault(String key, Collection<Collection<String>> val) {
     648        key += ".";
     649        Collection<String> keys = getAllPrefixDefault(key).keySet();
     650        int num = 0;
     651        for(Collection<String> c : val) {
     652            keys.remove(key+num);
     653            putCollectionDefault(key+num++, c);
     654        }
     655        int l = key.length();
     656        for(String k : keys) {
     657            try {
     658              Integer.valueOf(k.substring(l));
     659              defaults.remove(k);
     660            } catch(Exception e) {
     661            }
     662        }
     663    }
     664
    663665    /**
    664666     * Updates system properties with the current values in the preferences.
  • trunk/src/org/openstreetmap/josm/gui/preferences/AdvancedPreference.java

    r3029 r3531  
    216216    }
    217217
     218    private void removePreference(final PreferenceTabbedPane gui, final JTable list) {
     219        if (list.getSelectedRowCount() == 0) {
     220            JOptionPane.showMessageDialog(
     221                    gui,
     222                    tr("Please select the row to delete."),
     223                    tr("Warning"),
     224                    JOptionPane.WARNING_MESSAGE
     225            );
     226            return;
     227        }
     228        for(int row: list.getSelectedRows()) {
     229            data.put((String) model.getValueAt(row, 0), "");
     230            model.setValueAt("", row, 1);
     231        }
     232    }
     233
     234    private void addPreference(final PreferenceTabbedPane gui) {
     235        String s[] = showEditDialog(gui, tr("Enter a new key/value pair"),
     236            null, null);
     237        if(s != null && !s[0].isEmpty() && !s[1].isEmpty()) {
     238            data.put(s[0], s[1]);
     239            dataToModel();
     240        }
     241    }
     242
    218243    private void editPreference(final PreferenceTabbedPane gui, final JTable list) {
    219244        if (list.getSelectedRowCount() != 1) {
     
    226251            return;
    227252        }
    228         String v = (String) JOptionPane.showInputDialog(
    229                 Main.parent,
    230                 tr("New value for {0}", model.getValueAt(list.getSelectedRow(), 0)),
    231                 tr("New value"),
    232                 JOptionPane.QUESTION_MESSAGE,
    233                 null,
    234                 null,
    235                 model.getValueAt(list.getSelectedRow(), 1)
    236         );
    237         if (v != null) {
    238             data.put((String) model.getValueAt(list.getSelectedRow(), 0), v);
    239             model.setValueAt(v, list.getSelectedRow(), 1);
    240         }
    241     }
    242 
    243     private void removePreference(final PreferenceTabbedPane gui, final JTable list) {
    244         if (list.getSelectedRowCount() == 0) {
    245             JOptionPane.showMessageDialog(
    246                     gui,
    247                     tr("Please select the row to delete."),
    248                     tr("Warning"),
    249                     JOptionPane.WARNING_MESSAGE
    250             );
    251             return;
    252         }
    253         for(int row: list.getSelectedRows()) {
    254             data.put((String) model.getValueAt(row, 0), "");
    255             model.setValueAt("", row, 1);
    256         }
    257     }
    258 
    259     private void addPreference(final PreferenceTabbedPane gui) {
     253        String key = (String)model.getValueAt(list.getSelectedRow(), 0);
     254        String value = data.get(key);
     255        if(value.isEmpty())
     256            value = defaults.get(key);
     257        String s[] = showEditDialog(gui, tr("Change a key/value pair"),
     258            key, value);
     259        if(s != null && !s[0].isEmpty()) {
     260            data.put(s[0], s[1]);
     261            if(!s[0].equals(key))
     262                data.put(key,"");
     263            dataToModel();
     264        }
     265    }
     266
     267    private String[] showEditDialog(final PreferenceTabbedPane gui, String title,
     268    String key, String value) {
    260269        JPanel p = new JPanel(new GridBagLayout());
    261270        p.add(new JLabel(tr("Key")), GBC.std().insets(0,0,5,0));
    262         JTextField key = new JTextField(10);
    263         JTextField value = new JTextField(10);
    264         p.add(key, GBC.eop().insets(5,0,0,0).fill(GBC.HORIZONTAL));
     271        JTextField tkey = new JTextField(key, 50);
     272        JTextField tvalue = new JTextField(value, 50);
     273        p.add(tkey, GBC.eop().insets(5,0,0,0).fill(GBC.HORIZONTAL));
    265274        p.add(new JLabel(tr("Value")), GBC.std().insets(0,0,5,0));
    266         p.add(value, GBC.eol().insets(5,0,0,0).fill(GBC.HORIZONTAL));
     275        /* TODO: Split value at "\u001e" and present a table with automatic added lines */
     276        p.add(tvalue, GBC.eol().insets(5,0,0,0).fill(GBC.HORIZONTAL));
    267277        int answer = JOptionPane.showConfirmDialog(
    268278                gui, p,
    269                 tr("Enter a new key/value pair"),
     279                title,
    270280                JOptionPane.OK_CANCEL_OPTION,
    271281                JOptionPane.PLAIN_MESSAGE
    272282        );
    273         if (answer == JOptionPane.OK_OPTION) {
    274             data.put(key.getText(), value.getText());
    275             model.addRow(new String[]{key.getText(), value.getText()});
    276         }
     283        if(answer == JOptionPane.OK_OPTION) {
     284            return new String[]{tkey.getText(), tvalue.getText()};
     285        }
     286        return null;
    277287    }
    278288}
Note: See TracChangeset for help on using the changeset viewer.