Changeset 611 in josm for trunk


Ignore:
Timestamp:
2008-04-18T00:47:08+02:00 (16 years ago)
Author:
framm
Message:
  • change the behaviour of property drop-downs to list the USED as well as the EXISTING values. Patch by Michael Bergbauer <michael@…>
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/PropertiesDialog.java

    r595 r611  
    1 // License: GPL. Copyright 2007 by Immanuel Scholz and others
     1// License: GPL. See LICENSE file for details.
     2
    23package org.openstreetmap.josm.gui.dialogs;
    34
     
    2930import javax.swing.Box;
    3031import javax.swing.DefaultComboBoxModel;
     32import javax.swing.DefaultListCellRenderer;
    3133import javax.swing.JButton;
    3234import javax.swing.JComboBox;
    3335import javax.swing.JDialog;
    3436import javax.swing.JLabel;
     37import javax.swing.JList;
    3538import javax.swing.JOptionPane;
    3639import javax.swing.JPanel;
     
    112115        }
    113116
     117        private final Map<String, Map<String, Integer>> valueCount = new TreeMap<String, Map<String, Integer>>();
    114118        /**
    115119         * Edit the value in the properties table row
     
    118122        void propertyEdit(int row) {
    119123                String key = propertyData.getValueAt(row, 0).toString();
     124                objKey=key;
    120125                Collection<OsmPrimitive> sel = Main.ds.getSelected();
    121126                if (sel.isEmpty()) {
     
    143148
    144149                final AutoCompleteComboBox values = new AutoCompleteComboBox();
     150                values.setRenderer(new DefaultListCellRenderer() {
     151                        @Override public Component getListCellRendererComponent(JList list,  Object value, int index, boolean isSelected,  boolean cellHasFocus) {
     152                            Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
     153                            if (c instanceof JLabel) {
     154                                String str = null;
     155                                        str=(String) value;
     156                                        if (valueCount.containsKey(objKey)){
     157                                                Map<String, Integer> m=valueCount.get(objKey);
     158                                                if (m.containsKey(str)) {
     159                                                        str+="("+m.get(str)+")";
     160                                                        c.setFont(c.getFont().deriveFont(Font.ITALIC+Font.BOLD));
     161                                                }
     162                                        }
     163                                    ((JLabel)c).setText(str);
     164                            }
     165                            return c;
     166                        }
     167                });
    145168                values.setEditable(true);
    146                 Collection<String> newItems;
    147                 if (allData.containsKey(key)) {
    148                         newItems = allData.get(key);
    149                 } else {
    150                         newItems = Collections.emptyList();
    151                 }
    152                 values.setPossibleItems(newItems);
    153                 values.setSelectedItem(null);
    154                 final String selection= ((JComboBox)propertyData.getValueAt(row, 1)).getEditor().getItem().toString();
     169                updateListData(key, allData, values);
     170                Map<String, Integer> m=(Map<String, Integer>)propertyData.getValueAt(row, 1);
     171                final String selection= m.size()!=1?"<different>":m.entrySet().iterator().next().getKey();
    155172                values.setSelectedItem(selection);
    156173                values.getEditor().setItem(selection);
     
    158175                p.add(Box.createHorizontalStrut(10), GBC.std());
    159176                p.add(values, GBC.eol().fill(GBC.HORIZONTAL));
    160                 addFocusAdapter(allData, keys, values);
     177                addFocusAdapter(row, allData, keys, values);
    161178
    162179                final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
     
    203220                }
    204221
    205                         selectionChanged(sel); // update whole table
     222                selectionChanged(sel); // update whole table
    206223                Main.parent.repaint(); // repaint all - drawing could have been changed
    207224        }
     225
     226        /**
     227     * @param row
     228     * @param key
     229     * @param allData
     230     * @param values
     231     */
     232    private void updateListData(String key, final TreeMap<String, TreeSet<String>> allData, final AutoCompleteComboBox values) {
     233            Collection<String> newItems;
     234            if (allData.containsKey(key)) {
     235                        newItems = allData.get(key);
     236                } else {
     237                        newItems = Collections.emptyList();
     238                }
     239                values.setPossibleItems(newItems);
     240    }
    208241
    209242        /**
     
    246279                p2.add(values, BorderLayout.CENTER);
    247280
     281                addFocusAdapter(-1, allData, keys, values);
    248282                JOptionPane pane = new JOptionPane(p, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
    249283                        @Override public void selectInitialValue() {
     
    269303         * @param values
    270304         */
    271         private void addFocusAdapter(
    272                 final TreeMap<String, TreeSet<String>> allData,
    273                 final AutoCompleteComboBox keys, final AutoCompleteComboBox values) {
     305        private void addFocusAdapter(final int row, final TreeMap<String, TreeSet<String>> allData,final AutoCompleteComboBox keys, final AutoCompleteComboBox values) {
    274306                // get the combo box' editor component
    275307                JTextComponent editor = (JTextComponent)values.getEditor()
     
    279311                        @Override public void focusGained(FocusEvent e) {
    280312                                String key = keys.getEditor().getItem().toString();
    281                                 Collection<String> newItems;
    282                                 if (allData.containsKey(key)) {
    283                                         newItems = allData.get(key);
    284                                 } else {
    285                                         newItems = Collections.emptyList();
    286                                 }
    287                                 values.setPossibleItems(newItems);
     313                                updateListData(key, allData, values);
     314                                objKey=key;
    288315                        }
    289316                });
    290317        }
    291 
     318        private String objKey;
    292319        /**
    293320         * @return
     
    334361                }
    335362                @Override public Class<?> getColumnClass(int columnIndex) {
    336                         return columnIndex == 1 ? Relation.class : String.class;
     363                        return String.class;
    337364                }
    338365        };
     
    402429                                Component c = super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
    403430                                if (c instanceof JLabel) {
    404                                         String str = ((JComboBox) value).getEditor().getItem().toString();
     431                                        String str = null;
     432                                        switch (column) {
     433                                        case 0:
     434                                                str = (String) value;
     435                                        break;
     436                                        case 1:
     437                                                Map<String, Integer> v = (Map<String,Integer>) value;
     438                                                if (v.size()!=1) {
     439                                                        str="<different>";     
     440                                                        c.setFont(c.getFont().deriveFont(Font.ITALIC));
     441                                                } else {
     442                                                        str=v.entrySet().iterator().next().getKey();
     443                                                }
     444                                        break;
     445                                        }
    405446                                        ((JLabel)c).setText(str);
    406                                         if (str.equals(tr("<different>")))
    407                                                 c.setFont(c.getFont().deriveFont(Font.ITALIC));
    408447                                }
    409448                                return c;
     
    498537                propertyData.setRowCount(0);
    499538
    500                 Map<String, Integer> valueCount = new HashMap<String, Integer>();
    501                 TreeMap<String, Collection<String>> props = new TreeMap<String, Collection<String>>();
     539                Map<String, Integer> keyCount = new HashMap<String, Integer>();
     540                valueCount.clear();
    502541                for (OsmPrimitive osm : newSelection) {
    503542                        for (Entry<String, String> e : osm.entrySet()) {
    504                                 Collection<String> value = props.get(e.getKey());
    505                                 if (value == null) {
    506                                         value = new TreeSet<String>();
    507                                         props.put(e.getKey(), value);
    508                                 }
    509                                 value.add(e.getValue());
    510                                 valueCount.put(e.getKey(), valueCount.containsKey(e.getKey()) ? valueCount.get(e.getKey())+1 : 1);
    511                         }
    512                 }
    513                 for (Entry<String, Collection<String>> e : props.entrySet()) {
    514             JComboBox value = new JComboBox(e.getValue().toArray());
    515             value.setEditable(true);
    516             value.getEditor().setItem(e.getValue().size() > 1 || valueCount.get(e.getKey()) != newSelection.size() ? tr("<different>") : e.getValue().iterator().next());
    517             propertyData.addRow(new Object[]{e.getKey(), value});
     543                                keyCount.put(e.getKey(), keyCount.containsKey(e.getKey()) ? keyCount.get(e.getKey())+1 : 1);
     544                                if (valueCount.containsKey(e.getKey())) {
     545                                        Map<String, Integer> v = valueCount.get(e.getKey());
     546                                        v.put(e.getValue(), v.containsKey(e.getValue())? v.get(e.getValue())+1 : 1 );
     547                                } else {
     548                                        TreeMap<String,Integer> v = new TreeMap<String, Integer>();
     549                                        v.put(e.getValue(), 1);
     550                                        valueCount.put(e.getKey(), v);
     551                                }
     552                        }
     553                }
     554                for (Entry<String, Map<String, Integer>> e : valueCount.entrySet()) {
     555                        int count=0;
     556                        for (Entry<String, Integer> e1: e.getValue().entrySet()) {
     557//                              System.out.println(e.getKey()+" Entry "+e1.getKey()+" value "+e1.getValue());
     558                                count+=e1.getValue();
     559                        }
     560                        if (count < newSelection.size()) {
     561                                e.getValue().put("", newSelection.size()-count);
     562                        }
     563                        propertyData.addRow(new Object[]{e.getKey(), e.getValue()});
    518564                }
    519565               
     
    536582                                                }
    537583                                                value.add(m.role);
    538                                                 valueCountM.put(r, valueCount.containsKey(r) ? valueCount.get(r)+1 : 1);
     584                                                valueCountM.put(r, keyCount.containsKey(r) ? keyCount.get(r)+1 : 1);
    539585                                        }
    540586                                }
Note: See TracChangeset for help on using the changeset viewer.