- Timestamp:
- 2012-12-28T02:16:10+01:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java
r5624 r5639 12 12 import java.awt.Insets; 13 13 import java.awt.event.ActionEvent; 14 import java.awt.event.ActionListener; 14 15 import java.io.BufferedReader; 15 16 import java.io.File; … … 19 20 import java.io.Reader; 20 21 import java.io.UnsupportedEncodingException; 22 import java.text.NumberFormat; 23 import java.text.ParseException; 21 24 import java.util.ArrayList; 22 25 import java.util.Arrays; … … 34 37 import javax.swing.AbstractAction; 35 38 import javax.swing.Action; 39 import javax.swing.ButtonGroup; 36 40 import javax.swing.ImageIcon; 41 import javax.swing.JButton; 37 42 import javax.swing.JComponent; 38 43 import javax.swing.JLabel; … … 42 47 import javax.swing.JScrollPane; 43 48 import javax.swing.JTextField; 49 import javax.swing.JToggleButton; 44 50 import javax.swing.ListCellRenderer; 45 51 import javax.swing.ListModel; … … 393 399 public String originalValue; 394 400 public String use_last_as_default = "false"; 401 public String auto_increment; 395 402 public String length; 396 403 … … 411 418 if (!"false".equals(use_last_as_default) && lastValue.containsKey(key)) { 412 419 textField.setText(lastValue.get(key)); 420 } else if (auto_increment_selected != 0 && auto_increment != null) { 421 try { 422 textField.setText(Integer.toString(Integer.parseInt(lastValue.get(key)) + auto_increment_selected)); 423 } catch (NumberFormatException ex) { 424 // Ignore - cannot auto-increment if last was non-numeric 425 } 413 426 } else { 414 427 textField.setText(default_); … … 443 456 } 444 457 } 458 459 // if there's an auto_increment setting, then wrap the text field 460 // into a panel, appending a number of buttons. 461 // auto_increment has a format like -2,-1,1,2 462 // the text box being the first component in the panel is relied 463 // on in a rather ugly fashion further down. 464 if (auto_increment != null) { 465 ButtonGroup bg = new ButtonGroup(); 466 JPanel pnl = new JPanel(new GridBagLayout()); 467 pnl.add(value, GBC.std().fill(GBC.HORIZONTAL)); 468 469 // first, one button for each auto_increment value 470 for (final String ai : auto_increment.split(",")) { 471 JToggleButton aibutton = new JToggleButton(ai); 472 aibutton.setToolTipText(tr("Select auto-increment of {0} for this field", ai)); 473 aibutton.setMargin(new java.awt.Insets(0,0,0,0)); 474 bg.add(aibutton); 475 try { 476 // TODO there must be a better way to parse a number like "+3" than this. 477 final int buttonvalue = ((Number)NumberFormat.getIntegerInstance().parse(ai.replace("+", ""))).intValue(); 478 if (auto_increment_selected == buttonvalue) aibutton.setSelected(true); 479 aibutton.addActionListener(new ActionListener() { 480 public void actionPerformed(ActionEvent e) { 481 auto_increment_selected = buttonvalue; 482 } 483 }); 484 pnl.add(aibutton, GBC.std()); 485 } catch (ParseException x) { 486 System.err.println("Cannot parse auto-increment value of '" + ai + "' into an integer"); 487 } 488 } 489 490 // an invisible toggle button for "release" of the button group 491 final JToggleButton clearbutton = new JToggleButton("X"); 492 clearbutton.setVisible(false); 493 bg.add(clearbutton); 494 // and its visible counterpart. - this mechanism allows us to 495 // have *no* button selected after the X is clicked, instead 496 // of the X remaining selected 497 JButton releasebutton = new JButton("X"); 498 releasebutton.setToolTipText(tr("Cancel auto-increment for this field")); 499 releasebutton.setMargin(new java.awt.Insets(0,0,0,0)); 500 releasebutton.addActionListener(new ActionListener() { 501 public void actionPerformed(ActionEvent e) { 502 auto_increment_selected = 0; 503 clearbutton.setSelected(true); 504 } 505 }); 506 pnl.add(releasebutton, GBC.std().eol()); 507 value = pnl; 508 } 445 509 p.add(new JLabel(locale_text+":"), GBC.std().insets(0,0,10,0)); 446 510 p.add(value, GBC.eol().fill(GBC.HORIZONTAL)); … … 452 516 453 517 // return if unchanged 454 String v = (value instanceof JosmComboBox) 455 ? ((JosmComboBox) value).getEditor().getItem().toString() 456 : ((JTextField) value).getText(); 457 v = v.trim(); 458 459 if (!"false".equals(use_last_as_default)) { 460 lastValue.put(key, v); 461 } 462 if (v.equals(originalValue) || (originalValue == null && v.length() == 0)) 463 return; 464 465 changedTags.add(new Tag(key, v)); 518 String v = null; 519 if (value instanceof JosmComboBox) { 520 v = ((JosmComboBox) value).getEditor().getItem().toString(); 521 } else if (value instanceof JTextField) { 522 v = ((JTextField) value).getText(); 523 } else if (value instanceof JPanel) { 524 // this is what was alluded to with "ugly fashion" above. 525 v = ((JTextField) (((JPanel)value).getComponent(0))).getText(); 526 } else { 527 System.err.println("No 'last value' support for component " + value); 528 return; 529 } 530 531 v = v.trim(); 532 533 if (!"false".equals(use_last_as_default) || auto_increment != null) { 534 lastValue.put(key, v); 535 } 536 if (v.equals(originalValue) || (originalValue == null && v.length() == 0)) 537 return; 538 539 changedTags.add(new Tag(key, v)); 466 540 } 467 541 … … 1257 1331 public Match nameTemplateFilter; 1258 1332 private static final HashMap<String,String> lastValue = new HashMap<String,String>(); 1333 private static int auto_increment_selected = 0; 1259 1334 1260 1335 /**
Note:
See TracChangeset
for help on using the changeset viewer.