Changeset 2048 in josm for trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java
- Timestamp:
- 2009-09-04T10:49:53+02:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java
r2017 r2048 18 18 import java.util.Collection; 19 19 import java.util.HashMap; 20 import java.util.HashSet;21 20 import java.util.LinkedHashMap; 22 21 import java.util.LinkedList; 23 22 import java.util.List; 24 import java.util. Set;23 import java.util.TreeSet; 25 24 26 25 import javax.swing.AbstractAction; … … 45 44 import org.openstreetmap.josm.gui.ExtendedDialog; 46 45 import org.openstreetmap.josm.gui.QuadStateCheckBox; 46 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 47 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache; 48 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPritority; 49 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList; 47 50 import org.openstreetmap.josm.io.MirroredInputStream; 48 51 import org.openstreetmap.josm.tools.GBC; … … 66 69 67 70 public static abstract class Item { 71 protected void initAutoCompletionField(AutoCompletingTextField field, String key) { 72 OsmDataLayer layer = Main.main.getEditLayer(); 73 if (layer == null) return; 74 AutoCompletionCache cache = AutoCompletionCache.getCacheForLayer(layer); 75 AutoCompletionList list = new AutoCompletionList(); 76 cache.populateWithValues(list, false /* don't append */); 77 field.setAutoCompletionList(list); 78 } 79 68 80 public boolean focus = false; 69 81 abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel); … … 73 85 74 86 public static class Usage { 75 Set<String> values; 76 Boolean hadKeys = false; 77 Boolean hadEmpty = false; 78 public Boolean allSimilar() 79 { 87 TreeSet<String> values; 88 boolean hadKeys = false; 89 boolean hadEmpty = false; 90 public boolean hasUniqueValue() { 80 91 return values.size() == 1 && !hadEmpty; 81 92 } 82 public Boolean unused() 83 {93 94 public boolean unused() { 84 95 return values.size() == 0; 85 96 } 86 public String getFirst() 87 { 88 return (String)(values.toArray()[0]); 89 } 90 public Boolean hadKeys() 91 { 97 public String getFirst() { 98 return values.first(); 99 } 100 101 public boolean hadKeys() { 92 102 return hadKeys; 93 103 } … … 98 108 static Usage determineTextUsage(Collection<OsmPrimitive> sel, String key) { 99 109 Usage returnValue = new Usage(); 100 returnValue.values = new HashSet<String>();110 returnValue.values = new TreeSet<String>(); 101 111 for (OsmPrimitive s : sel) { 102 112 String v = s.get(key); … … 106 116 returnValue.hadEmpty = true; 107 117 } 108 returnValue.hadKeys = returnValue.hadKeys | s.hasKeys();118 returnValue.hadKeys = ! returnValue.values.isEmpty() | returnValue.hadEmpty; 109 119 } 110 120 return returnValue; … … 114 124 115 125 Usage returnValue = new Usage(); 116 returnValue.values = new HashSet<String>();126 returnValue.values = new TreeSet<String>(); 117 127 for (OsmPrimitive s : sel) { 118 128 returnValue.values.add(OsmUtils.getNamedOsmBoolean(s.get(key))); … … 133 143 private JComponent value; 134 144 145 135 146 @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) { 136 147 137 148 // find out if our key is already used in the selection. 138 149 Usage usage = determineTextUsage(sel, key); 139 if (usage.unused()) 140 {141 value = new JTextField();150 if (usage.unused()){ 151 AutoCompletingTextField textField = new AutoCompletingTextField(); 152 initAutoCompletionField(textField, key); 142 153 if (use_last_as_default && lastValue.containsKey(key)) { 143 ((JTextField)value).setText(lastValue.get(key));154 textField.setText(lastValue.get(key)); 144 155 } else { 145 ((JTextField)value).setText(default_); 146 } 156 textField.setText(default_); 157 } 158 value = textField; 147 159 originalValue = null; 148 } else if (usage. allSimilar()) {160 } else if (usage.hasUniqueValue()) { 149 161 // all objects use the same value 150 value = new JTextField();151 for (String s : usage.values) {152 ((JTextField) value).setText(s);153 }154 originalValue = ((JTextField)value).getText();162 AutoCompletingTextField textField = new AutoCompletingTextField(); 163 initAutoCompletionField(textField, key); 164 textField.setText(usage.getFirst()); 165 value = textField; 166 originalValue = usage.getFirst(); 155 167 } else { 156 168 // the objects have different values 157 value = new JComboBox(usage.values.toArray()); 158 ((JComboBox)value).setEditable(true); 159 ((JComboBox)value).getEditor().setItem(DIFFERENT); 169 AutoCompletingTextField textField = new AutoCompletingTextField(); 170 initAutoCompletionField(textField, key); 171 JComboBox comboBox = new JComboBox(usage.values.toArray()); 172 comboBox.setEditable(true); 173 comboBox.setEditor(textField); 174 comboBox.getEditor().setItem(DIFFERENT); 175 value=comboBox; 160 176 originalValue = DIFFERENT; 161 177 } … … 300 316 301 317 lhm = new LinkedHashMap<String,String>(); 302 if (!usage.allSimilar() && !usage.unused()) 303 { 318 if (!usage.hasUniqueValue() && !usage.unused()){ 304 319 lhm.put(DIFFERENT, DIFFERENT); 305 320 } … … 309 324 tr(display_array[i]) : display_array[i]); 310 325 } 311 if(!usage.unused()) 312 { 326 if(!usage.unused()){ 313 327 for (String s : usage.values) { 314 328 if (!lhm.containsKey(s)) { … … 326 340 combo = new JComboBox(lhm.values().toArray()); 327 341 combo.setEditable(editable); 328 if (usage.allSimilar() && !usage.unused()) 329 { 342 AutoCompletingTextField tf = new AutoCompletingTextField(); 343 initAutoCompletionField(tf, key); 344 tf.getAutoCompletionList().add(Arrays.asList(display_array), AutoCompletionItemPritority.IS_IN_STANDARD); 345 combo.setEditor(tf); 346 347 if (usage.hasUniqueValue() && !usage.unused()){ 330 348 originalValue=usage.getFirst(); 331 349 combo.setSelectedItem(lhm.get(originalValue)); 332 350 } 333 351 // use default only in case it is a totally new entry 334 else if(default_ != null && !usage.hadKeys()) 335 { 352 else if(default_ != null && !usage.hadKeys()) { 336 353 combo.setSelectedItem(default_); 337 354 originalValue=DIFFERENT; 338 355 } 339 else if(usage.unused()) 340 { 356 else if(usage.unused()){ 341 357 combo.setSelectedItem(""); 342 358 originalValue=""; 343 359 } 344 else 345 { 360 else{ 346 361 combo.setSelectedItem(DIFFERENT); 347 362 originalValue=DIFFERENT; … … 628 643 if (data == null) 629 644 return null; 645 OsmDataLayer layer = Main.main.getEditLayer(); 646 if (layer != null) { 647 AutoCompletionCache.getCacheForLayer(layer).initFromDataSet(); 648 } 630 649 PresetPanel p = new PresetPanel(); 631 650 LinkedList<Item> l = new LinkedList<Item>(); 632 if(types != null) 633 { 651 if(types != null){ 634 652 JPanel pp = new JPanel(); 635 for(String t : types) 636 { 653 for(String t : types){ 637 654 JLabel la = new JLabel(ImageProvider.get("Mf_" + t)); 638 655 la.setToolTipText(tr("Elements of type {0} are supported.", tr(t))); … … 642 659 } 643 660 644 for (Item i : data) 645 { 661 for (Item i : data){ 646 662 if(i instanceof Link) { 647 663 l.add(i); 648 } else 649 { 664 } else { 650 665 if(i.addToPanel(p, selected)) { 651 666 p.hasElements = true; … … 683 698 true); 684 699 contentConstraints = GBC.eol().fill().insets(5,10,5,0); 685 setupDialog(content, new String[] {"ok.png", "cancel.png" }); 700 setButtonIcons(new String[] {"ok.png", "cancel.png" }); 701 setContent(content); 702 setupDialog(); 686 703 buttons.get(0).setEnabled(!disableApply); 687 704 buttons.get(0).setToolTipText(title);
Note:
See TracChangeset
for help on using the changeset viewer.