Changeset 595 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2008-04-07T02:19:33+02:00 (16 years ago)
Author:
framm
Message:
  • use auto completion for property editor; patch from Michael Bergbauer <michael@…>. Closes #661.
File:
1 edited

Legend:

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

    r582 r595  
    3737import javax.swing.JScrollPane;
    3838import javax.swing.JTable;
    39 import javax.swing.JTextField;
    4039import javax.swing.ListSelectionModel;
    4140import javax.swing.table.DefaultTableCellRenderer;
     
    129128                panel.add(new JLabel(msg), BorderLayout.NORTH);
    130129
     130                final TreeMap<String, TreeSet<String>> allData = createAutoCompletionInfo(true);
     131
    131132                JPanel p = new JPanel(new GridBagLayout());
    132133                panel.add(p, BorderLayout.CENTER);
    133                
    134                 final JTextField keyField = new JTextField(key);
     134
     135                final AutoCompleteComboBox keys = new AutoCompleteComboBox();
     136                keys.setPossibleItems(allData.keySet());
     137                keys.setEditable(true);
     138                keys.setSelectedItem(key);
     139
    135140                p.add(new JLabel(tr("Key")), GBC.std());
    136141                p.add(Box.createHorizontalStrut(10), GBC.std());
    137                 p.add(keyField, GBC.eol().fill(GBC.HORIZONTAL));
    138                                
    139                 final JComboBox valueField = (JComboBox) propertyData.getValueAt(row, 1);
     142                p.add(keys, GBC.eol().fill(GBC.HORIZONTAL));
     143
     144                final AutoCompleteComboBox values = new AutoCompleteComboBox();
     145                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();
     155                values.setSelectedItem(selection);
     156                values.getEditor().setItem(selection);
    140157                p.add(new JLabel(tr("Value")), GBC.std());
    141158                p.add(Box.createHorizontalStrut(10), GBC.std());
    142                 p.add(valueField, GBC.eol().fill(GBC.HORIZONTAL));
    143 
    144                 final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
     159                p.add(values, GBC.eol().fill(GBC.HORIZONTAL));
     160                addFocusAdapter(allData, keys, values);
     161
     162                final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
    145163                        @Override public void selectInitialValue() {
    146                                 valueField.requestFocusInWindow();
    147                                 valueField.getEditor().selectAll();
     164                                values.requestFocusInWindow();
     165                                values.getEditor().selectAll();
    148166                        }
    149167                };
    150168                final JDialog dlg = optionPane.createDialog(Main.parent, tr("Change values?"));
    151169
    152                 valueField.getEditor().addActionListener(new ActionListener() {
     170                values.getEditor().addActionListener(new ActionListener() {
    153171                        public void actionPerformed(ActionEvent e) {
    154172                                dlg.setVisible(false);
     
    157175                });
    158176
    159                 String oldValue = valueField.getEditor().getItem().toString();
     177                String oldValue = values.getEditor().getItem().toString();
    160178                dlg.setVisible(true);
    161179
     
    163181                if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE ||
    164182                                (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION)) {
    165                         valueField.getEditor().setItem(oldValue);
    166                         return;
    167                 }
    168 
    169                 String value = valueField.getEditor().getItem().toString();
     183                        values.getEditor().setItem(oldValue);
     184                        return;
     185                }
     186
     187                String value = values.getEditor().getItem().toString();
    170188                if (value.equals(tr("<different>")))
    171189                        return;
    172190                if (value.equals(""))
    173191                        value = null; // delete the key
    174                 String newkey = keyField.getText();
     192                String newkey = keys.getEditor().getItem().toString();
    175193                if (newkey.equals("")) {
    176194                        newkey = key;
     
    214232                p.add(new JLabel("<html>"+trn("This will change {0} object.","This will change {0} objects.", sel.size(),sel.size())+"<br><br>"+tr("Please select a key")),
    215233                                BorderLayout.NORTH);
    216                 final TreeMap<String,TreeSet<String>> allData = new TreeMap<String,TreeSet<String>>();
     234                final TreeMap<String, TreeSet<String>> allData = createAutoCompletionInfo(false);
     235                final AutoCompleteComboBox keys = new AutoCompleteComboBox();
     236                keys.setPossibleItems(allData.keySet());
     237                keys.setEditable(true);
     238               
     239                p.add(keys, BorderLayout.CENTER);
     240
     241                JPanel p2 = new JPanel(new BorderLayout());
     242                p.add(p2, BorderLayout.SOUTH);
     243                p2.add(new JLabel(tr("Please select a value")), BorderLayout.NORTH);
     244                final AutoCompleteComboBox values = new AutoCompleteComboBox();
     245                values.setEditable(true);
     246                p2.add(values, BorderLayout.CENTER);
     247
     248                JOptionPane pane = new JOptionPane(p, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
     249                        @Override public void selectInitialValue() {
     250                                keys.requestFocusInWindow();
     251                                keys.getEditor().selectAll();
     252                        }
     253                };
     254                pane.createDialog(Main.parent, tr("Change values?")).setVisible(true);
     255                if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
     256                        return;
     257                String key = keys.getEditor().getItem().toString();
     258                String value = values.getEditor().getItem().toString();
     259                if (value.equals(""))
     260                        return;
     261                Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, value));
     262                selectionChanged(sel); // update table
     263                Main.parent.repaint(); // repaint all - drawing could have been changed
     264        }
     265
     266        /**
     267         * @param allData
     268         * @param keys
     269         * @param values
     270         */
     271        private void addFocusAdapter(
     272                final TreeMap<String, TreeSet<String>> allData,
     273                final AutoCompleteComboBox keys, final AutoCompleteComboBox values) {
     274                // get the combo box' editor component
     275                JTextComponent editor = (JTextComponent)values.getEditor()
     276                        .getEditorComponent();
     277                // Refresh the values model when focus is gained
     278                editor.addFocusListener(new FocusAdapter() {
     279                        @Override public void focusGained(FocusEvent e) {
     280                                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);
     288                        }
     289                });
     290        }
     291
     292        /**
     293         * @return
     294         */
     295        private TreeMap<String, TreeSet<String>> createAutoCompletionInfo(
     296                boolean edit) {
     297                final TreeMap<String, TreeSet<String>> allData = new TreeMap<String, TreeSet<String>>();
    217298                for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) {
    218299                        for (String key : osm.keySet()) {
     
    227308                        }
    228309                }
    229                 for (int i = 0; i < propertyData.getRowCount(); ++i)
    230                         allData.remove(propertyData.getValueAt(i, 0));
    231                 final AutoCompleteComboBox keys = new AutoCompleteComboBox();
    232                 keys.setPossibleItems(allData.keySet());
    233                 keys.setEditable(true);
    234                
    235                 p.add(keys, BorderLayout.CENTER);
    236 
    237                 JPanel p2 = new JPanel(new BorderLayout());
    238                 p.add(p2, BorderLayout.SOUTH);
    239                 p2.add(new JLabel(tr("Please select a value")), BorderLayout.NORTH);
    240                 final AutoCompleteComboBox values = new AutoCompleteComboBox();
    241                 values.setEditable(true);
    242                 p2.add(values, BorderLayout.CENTER);
    243            
    244                 // get the combo box' editor component
    245                 JTextComponent editor = (JTextComponent) values.getEditor().getEditorComponent();
    246                 // Refresh the values model when focus is gained
    247                 editor.addFocusListener(new FocusAdapter() {
    248             @Override public void focusGained(FocusEvent e) {
    249                 String key = keys.getEditor().getItem().toString();
    250                                 Collection<String> newItems;
    251                 if (allData.containsKey(key)) {
    252                                         newItems = allData.get(key);
    253                                 } else {
    254                                         newItems = Collections.emptyList();
    255                                 }
    256                                 values.setPossibleItems(newItems);
    257             }
    258         });
    259 
    260                 JOptionPane pane = new JOptionPane(p, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
    261                         @Override public void selectInitialValue() {
    262                                 keys.requestFocusInWindow();
    263                                 keys.getEditor().selectAll();
    264                         }
    265                 };
    266                 pane.createDialog(Main.parent, tr("Change values?")).setVisible(true);
    267                 if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
    268                         return;
    269                 String key = keys.getEditor().getItem().toString();
    270                 String value = values.getEditor().getItem().toString();
    271                 if (value.equals(""))
    272                         return;
    273                 Main.main.undoRedo.add(new ChangePropertyCommand(sel, key, value));
    274                 selectionChanged(sel); // update table
    275                 Main.parent.repaint(); // repaint all - drawing could have been changed
     310                if (!edit) {
     311                        for (int i = 0; i < propertyData.getRowCount(); ++i)
     312                                allData.remove(propertyData.getValueAt(i, 0));
     313                }
     314                return allData;
    276315        }
    277316
Note: See TracChangeset for help on using the changeset viewer.