- Timestamp:
- 2008-04-07T02:19:33+02:00 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/PropertiesDialog.java
r582 r595 37 37 import javax.swing.JScrollPane; 38 38 import javax.swing.JTable; 39 import javax.swing.JTextField;40 39 import javax.swing.ListSelectionModel; 41 40 import javax.swing.table.DefaultTableCellRenderer; … … 129 128 panel.add(new JLabel(msg), BorderLayout.NORTH); 130 129 130 final TreeMap<String, TreeSet<String>> allData = createAutoCompletionInfo(true); 131 131 132 JPanel p = new JPanel(new GridBagLayout()); 132 133 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 135 140 p.add(new JLabel(tr("Key")), GBC.std()); 136 141 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); 140 157 p.add(new JLabel(tr("Value")), GBC.std()); 141 158 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) { 145 163 @Override public void selectInitialValue() { 146 value Field.requestFocusInWindow();147 value Field.getEditor().selectAll();164 values.requestFocusInWindow(); 165 values.getEditor().selectAll(); 148 166 } 149 167 }; 150 168 final JDialog dlg = optionPane.createDialog(Main.parent, tr("Change values?")); 151 169 152 value Field.getEditor().addActionListener(new ActionListener() {170 values.getEditor().addActionListener(new ActionListener() { 153 171 public void actionPerformed(ActionEvent e) { 154 172 dlg.setVisible(false); … … 157 175 }); 158 176 159 String oldValue = value Field.getEditor().getItem().toString();177 String oldValue = values.getEditor().getItem().toString(); 160 178 dlg.setVisible(true); 161 179 … … 163 181 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE || 164 182 (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION)) { 165 value Field.getEditor().setItem(oldValue);166 return; 167 } 168 169 String value = value Field.getEditor().getItem().toString();183 values.getEditor().setItem(oldValue); 184 return; 185 } 186 187 String value = values.getEditor().getItem().toString(); 170 188 if (value.equals(tr("<different>"))) 171 189 return; 172 190 if (value.equals("")) 173 191 value = null; // delete the key 174 String newkey = key Field.getText();192 String newkey = keys.getEditor().getItem().toString(); 175 193 if (newkey.equals("")) { 176 194 newkey = key; … … 214 232 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")), 215 233 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>>(); 217 298 for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) { 218 299 for (String key : osm.keySet()) { … … 227 308 } 228 309 } 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; 276 315 } 277 316
Note:
See TracChangeset
for help on using the changeset viewer.