- Timestamp:
- 2010-12-15T20:36:19+01:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java
r3707 r3726 7 7 8 8 import java.awt.Component; 9 import java.awt.Dimension; 9 10 import java.awt.GridBagLayout; 10 11 import java.awt.Image; … … 27 28 import java.util.LinkedList; 28 29 import java.util.List; 30 import java.util.Map; 29 31 import java.util.TreeSet; 30 32 … … 35 37 import javax.swing.JComponent; 36 38 import javax.swing.JLabel; 39 import javax.swing.JList; 37 40 import javax.swing.JOptionPane; 38 41 import javax.swing.JPanel; 39 42 import javax.swing.JTextField; 43 import javax.swing.ListCellRenderer; 40 44 import javax.swing.SwingUtilities; 41 45 … … 61 65 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList; 62 66 import org.openstreetmap.josm.gui.util.GuiHelper; 67 import org.openstreetmap.josm.gui.widgets.HtmlPanel; 63 68 import org.openstreetmap.josm.io.MirroredInputStream; 64 69 import org.openstreetmap.josm.tools.GBC; … … 344 349 public String display_values; 345 350 public String locale_display_values; 351 public String short_descriptions; 352 public String locale_short_descriptions; 346 353 public String default_; 347 354 public boolean delete_if_empty = false; … … 350 357 public boolean required = false; 351 358 359 private List<String> short_description_list; 352 360 private JComboBox combo; 353 private LinkedHashMap<String,String> lhm;361 private Map<String, PresetListEnty> lhm; 354 362 private Usage usage; 355 private StringoriginalValue;363 private PresetListEnty originalValue; 356 364 357 365 @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) { … … 363 371 String[] value_array = values.split(","); 364 372 String[] display_array; 373 String[] short_descriptions_array = null; 365 374 366 375 if(locale_display_values != null) { 367 display_array = locale_display_values.split(",");368 } else if (display_values != null) {369 display_array = display_values.split(",");376 display_array = splitEscaped(locale_display_values); 377 } else if (display_values != null) { 378 display_array = splitEscaped(display_values); 370 379 } else { 371 380 display_array = value_array; 372 381 } 373 382 374 if(use_last_as_default && def == null && lastValue.containsKey(key)) 375 { 383 if (locale_short_descriptions != null) { 384 short_descriptions_array = splitEscaped(locale_short_descriptions); 385 } else if (short_descriptions != null) { 386 short_descriptions_array = splitEscaped(short_descriptions); 387 } else if (short_description_list != null) { 388 short_descriptions_array = short_description_list.toArray(new String[0]); 389 } 390 391 if (use_last_as_default && def == null && lastValue.containsKey(key)) { 376 392 def = lastValue.get(key); 377 393 } … … 382 398 } 383 399 384 lhm = new LinkedHashMap<String,String>(); 385 if (!usage.hasUniqueValue() && !usage.unused()){ 386 lhm.put(DIFFERENT, DIFFERENT); 400 if (short_descriptions_array != null && short_descriptions_array.length != value_array.length) { 401 System.err.println(tr("Broken tagging preset \"{0}-{1}\" - number of items in short_descriptions must be the same as in values", key, text)); 402 short_descriptions_array = null; 403 } 404 405 lhm = new LinkedHashMap<String, PresetListEnty>(); 406 if (!usage.hasUniqueValue() && !usage.unused()) { 407 lhm.put(DIFFERENT, new PresetListEnty(DIFFERENT)); 387 408 } 388 409 for (int i=0; i<value_array.length; i++) { 389 lhm.put(value_array[i], (locale_display_values == null) 410 PresetListEnty e = new PresetListEnty(value_array[i]); 411 e.display_value = (locale_display_values == null) 390 412 ? (values_context == null ? tr(display_array[i]) 391 : trc(values_context, display_array[i])) : display_array[i]); 413 : trc(values_context, display_array[i])) : display_array[i]; 414 if (short_descriptions_array != null) { 415 e.short_description = locale_short_descriptions == null ? tr(short_descriptions_array[i]) 416 : short_descriptions_array[i]; 417 } 418 lhm.put(value_array[i], e); 392 419 } 393 420 if(!usage.unused()){ 394 421 for (String s : usage.values) { 395 422 if (!lhm.containsKey(s)) { 396 lhm.put(s, s);423 lhm.put(s, new PresetListEnty(s)); 397 424 } 398 425 } 399 426 } 400 427 if (def != null && !lhm.containsKey(def)) { 401 lhm.put(def, def); 402 } 403 if(!lhm.containsKey("")) { 404 lhm.put("", ""); 405 } 428 lhm.put(def, new PresetListEnty(def)); 429 } 430 lhm.put("", new PresetListEnty("")); 406 431 407 432 combo = new JComboBox(lhm.values().toArray()); 433 combo.setRenderer(new PresetComboListCellRenderer()); 408 434 combo.setEditable(editable); 409 435 combo.setMaximumRowCount(13); … … 414 440 415 441 if (usage.hasUniqueValue() && !usage.unused()){ 416 originalValue=usage.getFirst(); 417 combo.setSelectedItem(lhm.get(originalValue)); 442 originalValue=lhm.get(usage.getFirst()); 418 443 } 419 444 // use default only in case it is a totally new entry 420 445 else if(def != null && !usage.hadKeys()) { 421 combo.setSelectedItem(def); 422 originalValue=DIFFERENT; 446 originalValue=lhm.get(DIFFERENT); 423 447 } 424 448 else if(usage.unused()){ 425 combo.setSelectedItem(""); 426 originalValue=""; 449 originalValue=lhm.get(""); 427 450 } 428 451 else{ 429 combo.setSelectedItem(DIFFERENT);430 originalValue=DIFFERENT;431 }452 originalValue=lhm.get(DIFFERENT); 453 } 454 combo.setSelectedItem(originalValue); 432 455 433 456 if(locale_text == null) { … … 442 465 return true; 443 466 } 467 468 private static class PresetListEnty { 469 String value; 470 String display_value; 471 String short_description; 472 473 public String getListDisplay() { 474 if (value.equals(DIFFERENT)) 475 return "<b>"+DIFFERENT.replaceAll("<", "<").replaceAll(">", ">")+"</b>"; 476 477 if (value.equals("")) 478 return " "; 479 480 StringBuilder res = new StringBuilder("<b>"); 481 if (display_value != null) { 482 res.append(display_value); 483 } else { 484 res.append(value); 485 } 486 res.append("</b>"); 487 if (short_description != null) { 488 // wrap in table to restrict the text width 489 res.append("<br><table><td width='232'>(").append(short_description).append(")</td></table>"); 490 } 491 return res.toString(); 492 } 493 494 public PresetListEnty(String value) { 495 this.value = value; 496 this.display_value = value; 497 } 498 499 public PresetListEnty(String value, String display_value) { 500 this.value = value; 501 this.display_value = display_value; 502 } 503 504 // toString is mainly used to initialize the Editor 505 @Override 506 public String toString() { 507 if (value.equals(DIFFERENT)) 508 return DIFFERENT; 509 return display_value.replaceAll("<.*>", ""); // remove additional markup, e.g. <br> 510 } 511 } 512 513 private static class PresetComboListCellRenderer implements ListCellRenderer { 514 515 HtmlPanel lbl; 516 JComponent dummy = new JComponent() {}; 517 518 public PresetComboListCellRenderer() { 519 lbl = new HtmlPanel(); 520 } 521 522 public Component getListCellRendererComponent( 523 JList list, 524 Object value, 525 int index, 526 boolean isSelected, 527 boolean cellHasFocus) 528 { 529 if (isSelected) { 530 lbl.setBackground(list.getSelectionBackground()); 531 lbl.setForeground(list.getSelectionForeground()); 532 } else { 533 lbl.setBackground(list.getBackground()); 534 lbl.setForeground(list.getForeground()); 535 } 536 537 PresetListEnty item = (PresetListEnty) value; 538 String s = item.getListDisplay(); 539 lbl.setText(s); 540 // We do not want the editor to have the maximum height of all 541 // entries. Return a dummy with bogus height. 542 if (index == -1) { 543 dummy.setPreferredSize(new Dimension(lbl.getPreferredSize().width, 10)); 544 return dummy; 545 } 546 return lbl; 547 } 548 } 549 550 // allow escaped comma in comma separated list: 551 // "A\, B\, C,one\, two" --> ["A, B, C", "one, two"] 552 private static String[] splitEscaped(String s) { 553 String[] res = s.replaceAll("\\\\,", "\u0091").split(","); 554 for (int i=0; i<res.length; ++i) { 555 res[i] = res[i].replaceAll("\u0091", ","); 556 } 557 return res; 558 } 559 444 560 @Override public void addCommands(List<Tag> changedTags) { 445 561 Object obj = combo.getSelectedItem(); … … 453 569 { 454 570 for (String key : lhm.keySet()) { 455 String k = lhm.get(key) ;571 String k = lhm.get(key).toString(); 456 572 if (k != null && k.equals(display)) { 457 573 value=key; … … 466 582 467 583 // no change if same as before 468 if (value.equals(originalValue) || (originalValue == null && value.length() == 0)) return; 584 if (originalValue == null) { 585 if (value.length() == 0) 586 return; 587 } else if (value.equals(originalValue.toString())) 588 return; 469 589 470 590 if (delete_if_empty && value.length() == 0) { … … 474 594 lastValue.put(key, value); 475 595 } 596 System.err.print("change: "+key+" "+value); 476 597 changedTags.add(new Tag(key, value)); 477 598 } 599 600 public void setShort_description(String s) { 601 if (short_description_list == null) { 602 short_description_list = new ArrayList<String>(); 603 } 604 short_description_list.add(tr(s)); 605 } 606 478 607 @Override boolean requestFocusInWindow() {return combo.requestFocusInWindow();} 479 608 }
Note:
See TracChangeset
for help on using the changeset viewer.