Changeset 12859 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2017-09-14T21:35:25+02:00 (7 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 6 added
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java
r12846 r12859 77 77 import org.openstreetmap.josm.data.preferences.ListProperty; 78 78 import org.openstreetmap.josm.data.preferences.StringProperty; 79 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem; 79 80 import org.openstreetmap.josm.gui.ExtendedDialog; 80 81 import org.openstreetmap.josm.gui.IExtendedDialog; … … 83 84 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles; 84 85 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox; 85 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;86 86 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager; 87 87 import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset; … … 112 112 private String objKey; 113 113 114 static final Comparator<AutoCompletion ListItem> DEFAULT_AC_ITEM_COMPARATOR =114 static final Comparator<AutoCompletionItem> DEFAULT_AC_ITEM_COMPARATOR = 115 115 (o1, o2) -> String.CASE_INSENSITIVE_ORDER.compare(o1.getValue(), o2.getValue()); 116 116 … … 393 393 private final String key; 394 394 private final transient Map<String, Integer> m; 395 private final transient Comparator<AutoCompletion ListItem> usedValuesAwareComparator;396 397 private final transient ListCellRenderer<AutoCompletion ListItem> cellRenderer = new ListCellRenderer<AutoCompletionListItem>() {395 private final transient Comparator<AutoCompletionItem> usedValuesAwareComparator; 396 397 private final transient ListCellRenderer<AutoCompletionItem> cellRenderer = new ListCellRenderer<AutoCompletionItem>() { 398 398 private final DefaultListCellRenderer def = new DefaultListCellRenderer(); 399 399 @Override 400 public Component getListCellRendererComponent(JList<? extends AutoCompletion ListItem> list,401 AutoCompletion ListItem value, int index, boolean isSelected, boolean cellHasFocus) {400 public Component getListCellRendererComponent(JList<? extends AutoCompletionItem> list, 401 AutoCompletionItem value, int index, boolean isSelected, boolean cellHasFocus) { 402 402 Component c = def.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 403 403 if (c instanceof JLabel) { … … 447 447 448 448 AutoCompletionManager autocomplete = AutoCompletionManager.of(MainApplication.getLayerManager().getEditLayer().data); 449 List<AutoCompletionListItem> keyList = autocomplete.getKeys(); 450 keyList.sort(DEFAULT_AC_ITEM_COMPARATOR); 449 List<AutoCompletionItem> keyList = autocomplete.getTagKeys(DEFAULT_AC_ITEM_COMPARATOR); 451 450 452 451 keys = new AutoCompletingComboBox(key); 453 keys.setPossibleA CItems(keyList);452 keys.setPossibleAcItems(keyList); 454 453 keys.setEditable(true); 455 454 keys.setSelectedItem(key); … … 460 459 p.add(keys, GBC.eol().fill(GBC.HORIZONTAL)); 461 460 462 List<AutoCompletionListItem> valueList = autocomplete.getValues(getAutocompletionKeys(key)); 463 valueList.sort(usedValuesAwareComparator); 461 List<AutoCompletionItem> valueList = autocomplete.getTagValues(getAutocompletionKeys(key), usedValuesAwareComparator); 464 462 465 463 final String selection = m.size() != 1 ? tr("<different>") : m.entrySet().iterator().next().getKey(); … … 469 467 470 468 values.setEditable(true); 471 values.setPossibleA CItems(valueList);469 values.setPossibleAcItems(valueList); 472 470 values.setSelectedItem(selection); 473 471 values.getEditor().setItem(selection); … … 630 628 * @return The created adapter 631 629 */ 632 protected FocusAdapter addFocusAdapter(final AutoCompletionManager autocomplete, final Comparator<AutoCompletion ListItem> comparator) {630 protected FocusAdapter addFocusAdapter(final AutoCompletionManager autocomplete, final Comparator<AutoCompletionItem> comparator) { 633 631 // get the combo box' editor component 634 632 final JTextComponent editor = values.getEditorComponent(); … … 639 637 String key = keys.getEditor().getItem().toString(); 640 638 641 List<AutoCompletionListItem> valueList = autocomplete.getValues(getAutocompletionKeys(key)); 642 valueList.sort(comparator); 639 List<AutoCompletionItem> valueList = autocomplete.getTagValues(getAutocompletionKeys(key), comparator); 643 640 Logging.trace("Focus gained by {0}, e={1}", values, e); 644 values.setPossibleA CItems(valueList);641 values.setPossibleAcItems(valueList); 645 642 values.getEditor().selectAll(); 646 643 objKey = key; … … 692 689 693 690 cacheRecentTags(); 694 AutoCompletionManager autocomplete = AutoCompletionManager.of(Main Application.getLayerManager().getEditLayer().data);695 List<AutoCompletion ListItem> keyList = autocomplete.getKeys();691 AutoCompletionManager autocomplete = AutoCompletionManager.of(Main.main.getEditDataSet()); 692 List<AutoCompletionItem> keyList = autocomplete.getTagKeys(DEFAULT_AC_ITEM_COMPARATOR); 696 693 697 694 // remove the object's tag keys from the list 698 695 keyList.removeIf(item -> containsDataKey(item.getValue())); 699 696 700 keyList.sort(DEFAULT_AC_ITEM_COMPARATOR); 701 keys.setPossibleACItems(keyList); 697 keys.setPossibleAcItems(keyList); 702 698 keys.setEditable(true); 703 699 -
trunk/src/org/openstreetmap/josm/gui/preferences/projection/CustomProjectionChoice.java
r12846 r12859 24 24 import org.openstreetmap.josm.data.projection.ProjectionConfigurationException; 25 25 import org.openstreetmap.josm.data.projection.Projections; 26 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem; 26 27 import org.openstreetmap.josm.gui.ExtendedDialog; 27 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem;28 28 import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator; 29 29 import org.openstreetmap.josm.gui.widgets.HistoryComboBox; … … 65 65 input = new JosmTextField(30); 66 66 cbInput = new HistoryComboBox(); 67 cbInput.setPrototypeDisplayValue(new AutoCompletion ListItem("xxxx"));67 cbInput.setPrototypeDisplayValue(new AutoCompletionItem("xxxx")); 68 68 cbInput.setEditor(new BasicComboBoxEditor() { 69 69 @Override -
trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletingComboBox.java
r12846 r12859 10 10 import java.util.Collection; 11 11 import java.util.Locale; 12 import java.util.stream.Collectors; 12 13 13 14 import javax.swing.ComboBoxEditor; … … 23 24 import javax.swing.text.StyleConstants; 24 25 26 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem; 27 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionPriority; 25 28 import org.openstreetmap.josm.gui.MainApplication; 26 29 import org.openstreetmap.josm.gui.MapFrame; … … 35 38 * @since 272 36 39 */ 37 public class AutoCompletingComboBox extends JosmComboBox<AutoCompletion ListItem> {40 public class AutoCompletingComboBox extends JosmComboBox<AutoCompletionItem> { 38 41 39 42 private boolean autocompleteEnabled = true; … … 85 88 */ 86 89 class AutoCompletingComboBoxDocument extends PlainDocument { 87 private final JosmComboBox<AutoCompletion ListItem> comboBox;90 private final JosmComboBox<AutoCompletionItem> comboBox; 88 91 private boolean selecting; 89 92 … … 92 95 * @param comboBox the combobox 93 96 */ 94 AutoCompletingComboBoxDocument(final JosmComboBox<AutoCompletion ListItem> comboBox) {97 AutoCompletingComboBoxDocument(final JosmComboBox<AutoCompletionItem> comboBox) { 95 98 this.comboBox = comboBox; 96 99 } … … 159 162 } 160 163 if (item != null) { 161 String newText = ((AutoCompletion ListItem) item).getValue();164 String newText = ((AutoCompletionItem) item).getValue(); 162 165 if (!newText.equals(curText)) { 163 166 selecting = true; … … 190 193 191 194 private Object lookupItem(String pattern, boolean match) { 192 ComboBoxModel<AutoCompletion ListItem> model = comboBox.getModel();193 AutoCompletion ListItem bestItem = null;195 ComboBoxModel<AutoCompletionItem> model = comboBox.getModel(); 196 AutoCompletionItem bestItem = null; 194 197 for (int i = 0, n = model.getSize(); i < n; i++) { 195 AutoCompletion ListItem currentItem = model.getElementAt(i);198 AutoCompletionItem currentItem = model.getElementAt(i); 196 199 if (currentItem.getValue().equals(pattern)) 197 200 return currentItem; … … 219 222 */ 220 223 public AutoCompletingComboBox(String prototype) { 221 super(new AutoCompletion ListItem(prototype));224 super(new AutoCompletionItem(prototype)); 222 225 setRenderer(new AutoCompleteListCellRenderer()); 223 226 final JTextComponent editorComponent = this.getEditorComponent(); … … 241 244 */ 242 245 @Override 246 @SuppressWarnings("deprecation") 243 247 public void configureEditor(ComboBoxEditor cbEditor, Object item) { 244 248 if (item == null) { … … 246 250 } else if (item instanceof String) { 247 251 cbEditor.setItem(item); 252 } else if (item instanceof AutoCompletionItem) { 253 cbEditor.setItem(((AutoCompletionItem) item).getValue()); 248 254 } else if (item instanceof AutoCompletionListItem) { 249 cbEditor.setItem(((AutoCompletionListItem) item).get Value());255 cbEditor.setItem(((AutoCompletionListItem) item).getItem().getValue()); 250 256 } else 251 257 throw new IllegalArgumentException("Unsupported item: "+item); … … 254 260 /** 255 261 * Selects a given item in the ComboBox model 256 * @param item excepts AutoCompletion ListItem, String and null262 * @param item excepts AutoCompletionItem, String and null 257 263 */ 258 264 @Override 265 @SuppressWarnings("deprecation") 259 266 public void setSelectedItem(Object item) { 260 267 if (item == null) { 261 268 super.setSelectedItem(null); 269 } else if (item instanceof AutoCompletionItem) { 270 super.setSelectedItem(item); 262 271 } else if (item instanceof AutoCompletionListItem) { 263 super.setSelectedItem( item);272 super.setSelectedItem(((AutoCompletionListItem) item).getItem()); 264 273 } else if (item instanceof String) { 265 274 String s = (String) item; 266 275 // find the string in the model or create a new item 267 276 for (int i = 0; i < getModel().getSize(); i++) { 268 AutoCompletion ListItem acItem = getModel().getElementAt(i);277 AutoCompletionItem acItem = getModel().getElementAt(i); 269 278 if (s.equals(acItem.getValue())) { 270 279 super.setSelectedItem(acItem); … … 272 281 } 273 282 } 274 super.setSelectedItem(new AutoCompletion ListItem(s, AutoCompletionItemPriority.UNKNOWN));283 super.setSelectedItem(new AutoCompletionItem(s, AutoCompletionPriority.UNKNOWN)); 275 284 } else { 276 285 throw new IllegalArgumentException("Unsupported item: "+item); … … 283 292 */ 284 293 public void setPossibleItems(Collection<String> elems) { 285 DefaultComboBoxModel<AutoCompletion ListItem> model = (DefaultComboBoxModel<AutoCompletionListItem>) this.getModel();294 DefaultComboBoxModel<AutoCompletionItem> model = (DefaultComboBoxModel<AutoCompletionItem>) this.getModel(); 286 295 Object oldValue = this.getEditor().getItem(); // Do not use getSelectedItem(); (fix #8013) 287 296 model.removeAllElements(); 288 297 for (String elem : elems) { 289 model.addElement(new AutoCompletion ListItem(elem, AutoCompletionItemPriority.UNKNOWN));298 model.addElement(new AutoCompletionItem(elem, AutoCompletionPriority.UNKNOWN)); 290 299 } 291 300 // disable autocomplete to prevent unnecessary actions in AutoCompletingComboBoxDocument#insertString … … 298 307 * Sets the items of the combobox to the given {@code AutoCompletionListItem}s. 299 308 * @param elems AutoCompletionListItem items 300 */ 309 * @deprecated to be removed end of 2017. Use {@link #setPossibleAcItems(Collection)} instead 310 */ 311 @Deprecated 301 312 public void setPossibleACItems(Collection<AutoCompletionListItem> elems) { 302 DefaultComboBoxModel<AutoCompletionListItem> model = (DefaultComboBoxModel<AutoCompletionListItem>) this.getModel(); 313 setPossibleAcItems(elems.stream().map(AutoCompletionListItem::getItem).collect(Collectors.toList())); 314 } 315 316 /** 317 * Sets the items of the combobox to the given {@code AutoCompletionItem}s. 318 * @param elems AutoCompletionItem items 319 * @since 12859 320 */ 321 public void setPossibleAcItems(Collection<AutoCompletionItem> elems) { 322 DefaultComboBoxModel<AutoCompletionItem> model = (DefaultComboBoxModel<AutoCompletionItem>) this.getModel(); 303 323 Object oldValue = getSelectedItem(); 304 324 Object editorOldValue = this.getEditor().getItem(); 305 325 model.removeAllElements(); 306 for (AutoCompletion ListItem elem : elems) {326 for (AutoCompletionItem elem : elems) { 307 327 model.addElement(elem); 308 328 } … … 359 379 * renders an AutoCompletionListItem by showing only the string value part 360 380 */ 361 public static class AutoCompleteListCellRenderer extends JLabel implements ListCellRenderer<AutoCompletion ListItem> {381 public static class AutoCompleteListCellRenderer extends JLabel implements ListCellRenderer<AutoCompletionItem> { 362 382 363 383 /** … … 370 390 @Override 371 391 public Component getListCellRendererComponent( 372 JList<? extends AutoCompletion ListItem> list,373 AutoCompletion ListItem item,392 JList<? extends AutoCompletionItem> list, 393 AutoCompletionItem item, 374 394 int index, 375 395 boolean isSelected, -
trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletingTextField.java
r12846 r12859 99 99 if (autoCompletionList.getFilteredSize() > 0 && !Objects.equals(str, noAutoCompletionString)) { 100 100 // there are matches. Insert the new text and highlight the auto completed suffix 101 String matchingString = autoCompletionList.getFilteredItem (0).getValue();101 String matchingString = autoCompletionList.getFilteredItemAt(0).getValue(); 102 102 remove(0, getLength()); 103 103 super.insertString(0, matchingString, a); -
trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionItemPriority.java
r10378 r12859 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.gui.tagging.ac; 3 4 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionPriority; 3 5 4 6 /** … … 8 10 * Instances of this class are not modifiable. 9 11 * @since 1762 12 * @deprecated to be removed end of 2017. Use {@link AutoCompletionPriority} instead 10 13 */ 14 @Deprecated 11 15 public class AutoCompletionItemPriority implements Comparable<AutoCompletionItemPriority> { 12 16 … … 16 20 * usually not used by the user. 17 21 */ 18 public static final AutoCompletionItemPriority IS_IN_STANDARD_AND_IN_DATASET = new AutoCompletionItemPriority(true, true, false); 22 public static final AutoCompletionItemPriority IS_IN_STANDARD_AND_IN_DATASET = new AutoCompletionItemPriority( 23 AutoCompletionPriority.IS_IN_STANDARD_AND_IN_DATASET); 19 24 20 25 /** … … 22 27 * the value of a tag name=*. 23 28 */ 24 public static final AutoCompletionItemPriority IS_IN_DATASET = new AutoCompletionItemPriority( true, false, false);29 public static final AutoCompletionItemPriority IS_IN_DATASET = new AutoCompletionItemPriority(AutoCompletionPriority.IS_IN_DATASET); 25 30 26 31 /** … … 28 33 * or a standard value for a given tag name (from the presets). 29 34 */ 30 public static final AutoCompletionItemPriority IS_IN_STANDARD = new AutoCompletionItemPriority( false, true, false);35 public static final AutoCompletionItemPriority IS_IN_STANDARD = new AutoCompletionItemPriority(AutoCompletionPriority.IS_IN_STANDARD); 31 36 32 37 /** 33 38 * Indicates that this is a value from a selected object. 34 39 */ 35 public static final AutoCompletionItemPriority IS_IN_SELECTION = new AutoCompletionItemPriority( false, false, true);40 public static final AutoCompletionItemPriority IS_IN_SELECTION = new AutoCompletionItemPriority(AutoCompletionPriority.IS_IN_SELECTION); 36 41 37 42 /** Unknown priority. This is the lowest priority. */ 38 public static final AutoCompletionItemPriority UNKNOWN = new AutoCompletionItemPriority( false, false, false);43 public static final AutoCompletionItemPriority UNKNOWN = new AutoCompletionItemPriority(AutoCompletionPriority.UNKNOWN); 39 44 40 45 private static final int NO_USER_INPUT = Integer.MAX_VALUE; 41 46 42 private final int userInput; 43 private final boolean inDataSet; 44 private final boolean inStandard; 45 private final boolean selected; 47 private final AutoCompletionPriority priority; 46 48 47 49 /** … … 56 58 */ 57 59 public AutoCompletionItemPriority(boolean inDataSet, boolean inStandard, boolean selected, Integer userInput) { 58 this.inDataSet = inDataSet; 59 this.inStandard = inStandard; 60 this.selected = selected; 61 this.userInput = userInput == null ? NO_USER_INPUT : userInput; 60 this(new AutoCompletionPriority(inDataSet, inStandard, selected, userInput)); 62 61 } 63 62 … … 74 73 75 74 /** 75 * Constructs a new {@code AutoCompletionItemPriority} from an existing {@link AutoCompletionPriority}. 76 * @param other {@code AutoCompletionPriority} to convert 77 * @since 12859 78 */ 79 public AutoCompletionItemPriority(AutoCompletionPriority other) { 80 this.priority = other; 81 } 82 83 /** 76 84 * Determines if the item is found in the currently active data layer. 77 85 * @return {@code true} if the item is found in the currently active data layer 78 86 */ 79 87 public boolean isInDataSet() { 80 return inDataSet;88 return priority.isInDataSet(); 81 89 } 82 90 … … 86 94 */ 87 95 public boolean isInStandard() { 88 return inStandard;96 return priority.isInStandard(); 89 97 } 90 98 … … 94 102 */ 95 103 public boolean isSelected() { 96 return selected;104 return priority.isSelected(); 97 105 } 98 106 … … 104 112 */ 105 113 public Integer getUserInput() { 106 return userInput == NO_USER_INPUT ? null : userInput;114 return priority.getUserInput(); 107 115 } 108 116 … … 113 121 @Override 114 122 public int compareTo(AutoCompletionItemPriority other) { 115 int ui = Integer.compare(other.userInput, userInput); 116 if (ui != 0) 117 return ui; 118 119 int sel = Boolean.compare(selected, other.selected); 120 if (sel != 0) 121 return sel; 122 123 int ds = Boolean.compare(inDataSet, other.inDataSet); 124 if (ds != 0) 125 return ds; 126 127 int std = Boolean.compare(inStandard, other.inStandard); 128 if (std != 0) 129 return std; 130 131 return 0; 123 return priority.compareTo(other.priority); 132 124 } 133 125 … … 139 131 */ 140 132 public AutoCompletionItemPriority mergeWith(AutoCompletionItemPriority other) { 141 return new AutoCompletionItemPriority( 142 inDataSet || other.inDataSet, 143 inStandard || other.inStandard, 144 selected || other.selected, 145 Math.min(userInput, other.userInput)); 133 return new AutoCompletionItemPriority(priority.mergeWith(other.priority)); 146 134 } 147 135 148 136 @Override 149 137 public String toString() { 150 return String.format("<Priority; userInput: %s, inDataSet: %b, inStandard: %b, selected: %b>", 151 userInput == NO_USER_INPUT ? "no" : Integer.toString(userInput), inDataSet, inStandard, selected); 138 return priority.toString(); 139 } 140 141 /** 142 * Returns the underlying priority. 143 * @return the underlying priority 144 * @since 12859 145 */ 146 public AutoCompletionPriority getPriority() { 147 return priority; 152 148 } 153 149 } -
trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionList.java
r10137 r12859 5 5 import java.util.Collection; 6 6 import java.util.Collections; 7 import java.util.HashMap;8 7 import java.util.List; 9 import java.util.Map; 8 import java.util.Set; 9 import java.util.stream.Collectors; 10 10 11 11 import javax.swing.JTable; 12 12 import javax.swing.table.AbstractTableModel; 13 13 14 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem; 15 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionPriority; 16 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionSet; 14 17 import org.openstreetmap.josm.tools.CheckParameterUtil; 15 18 16 19 /** 17 * AutoCompletionList manages a list of {@link AutoCompletionListItem}s.20 * AutoCompletionList manages a graphical list of {@link AutoCompletionItem}s. 18 21 * 19 22 * The list is sorted, items with higher priority first, then according to lexicographic order 20 * on the value of the {@link AutoCompletion ListItem}.23 * on the value of the {@link AutoCompletionItem}. 21 24 * 22 * AutoCompletionList maintains two views on the list of {@link AutoCompletion ListItem}s.25 * AutoCompletionList maintains two views on the list of {@link AutoCompletionItem}s. 23 26 * <ol> 24 27 * <li>the bare, unfiltered view which includes all items</li> … … 33 36 34 37 /** the bare list of AutoCompletionItems */ 35 private final transient List<AutoCompletionListItem>list;38 private final transient AutoCompletionSet list; 36 39 /** the filtered list of AutoCompletionItems */ 37 private final transient ArrayList<AutoCompletion ListItem> filtered;40 private final transient ArrayList<AutoCompletionItem> filtered; 38 41 /** the filter expression */ 39 42 private String filter; 40 /** map from value to priority */41 private final transient Map<String, AutoCompletionListItem> valutToItemMap;42 43 43 44 /** … … 45 46 */ 46 47 public AutoCompletionList() { 47 list = new A rrayList<>();48 list = new AutoCompletionSet(); 48 49 filtered = new ArrayList<>(); 49 valutToItemMap = new HashMap<>();50 50 } 51 51 … … 81 81 82 82 /** 83 * adds an AutoCompletionListItemto the list. Only adds the item if it83 * adds an {@link AutoCompletionListItem} to the list. Only adds the item if it 84 84 * is not null and if not in the list yet. 85 85 * 86 86 * @param item the item 87 */ 87 * @deprecated To be removed end of 2017. Use {@link #add(AutoCompletionItem)} instead 88 */ 89 @Deprecated 88 90 public void add(AutoCompletionListItem item) { 89 91 if (item == null) 90 92 return; 91 appendOrUpdatePriority(item); 92 sort(); 93 filter(); 94 } 95 96 /** 97 * adds another AutoCompletionList to this list. An item is only 93 add(item.getItem()); 94 } 95 96 /** 97 * adds an {@link AutoCompletionItem} to the list. Only adds the item if it 98 * is not null and if not in the list yet. 99 * 100 * @param item the item 101 * @since 12859 102 */ 103 public void add(AutoCompletionItem item) { 104 if (item != null && list.add(item)) { 105 filter(); 106 } 107 } 108 109 /** 110 * adds another {@link AutoCompletionList} to this list. An item is only 98 111 * added it is not null and if it does not exist in the list yet. 99 112 * … … 103 116 public void add(AutoCompletionList other) { 104 117 CheckParameterUtil.ensureParameterNotNull(other, "other"); 105 for (AutoCompletionListItem item : other.list) { 106 appendOrUpdatePriority(item); 107 } 108 sort(); 109 filter(); 110 } 111 112 /** 113 * adds a list of AutoCompletionListItem to this list. Only items which 118 add(other.list); 119 } 120 121 /** 122 * adds a colleciton of {@link AutoCompletionItem} to this list. An item is only 123 * added it is not null and if it does not exist in the list yet. 124 * 125 * @param collection auto completion collection; must not be null 126 * @throws IllegalArgumentException if other is null 127 * @since 12859 128 */ 129 public void add(Collection<AutoCompletionItem> collection) { 130 CheckParameterUtil.ensureParameterNotNull(collection, "collection"); 131 if (list.addAll(collection)) { 132 filter(); 133 } 134 } 135 136 /** 137 * adds a list of {@link AutoCompletionListItem} to this list. Only items which 114 138 * are not null and which do not exist yet in the list are added. 115 139 * 116 140 * @param other a list of AutoCompletionListItem; must not be null 117 141 * @throws IllegalArgumentException if other is null 118 */ 142 * @deprecated to be removed end of 2017. Use {@link #add(Collection)} instead 143 */ 144 @Deprecated 119 145 public void add(List<AutoCompletionListItem> other) { 120 146 CheckParameterUtil.ensureParameterNotNull(other, "other"); 121 for (AutoCompletionListItem toadd : other) { 122 appendOrUpdatePriority(toadd); 123 } 124 sort(); 125 filter(); 147 add(other.stream().map(AutoCompletionListItem::getItem).collect(Collectors.toList())); 126 148 } 127 149 … … 132 154 * @param values a list of strings to add 133 155 * @param priority the priority to use 134 */ 156 * @deprecated to be removed end of 2017. Use {@link #add(Collection, AutoCompletionPriority)} instead 157 */ 158 @Deprecated 135 159 public void add(Collection<String> values, AutoCompletionItemPriority priority) { 136 if (values == null) 137 return; 138 for (String value : values) { 139 if (value == null) { 140 continue; 141 } 142 AutoCompletionListItem item = new AutoCompletionListItem(value, priority); 143 appendOrUpdatePriority(item); 144 145 } 146 sort(); 147 filter(); 160 add(values, priority.getPriority()); 161 } 162 163 /** 164 * adds a list of strings to this list. Only strings which 165 * are not null and which do not exist yet in the list are added. 166 * 167 * @param values a list of strings to add 168 * @param priority the priority to use 169 * @since 12859 170 */ 171 public void add(Collection<String> values, AutoCompletionPriority priority) { 172 if (values != null && list.addAll(values, priority)) { 173 filter(); 174 } 148 175 } 149 176 … … 153 180 */ 154 181 public void addUserInput(Collection<String> values) { 155 if (values == null) 156 return; 157 int i = 0; 158 for (String value : values) { 159 if (value != null) { 160 appendOrUpdatePriority( 161 new AutoCompletionListItem(value, new AutoCompletionItemPriority(false, false, false, i++))); 162 } 163 } 164 sort(); 165 filter(); 166 } 167 168 protected void appendOrUpdatePriority(AutoCompletionListItem toAdd) { 169 AutoCompletionListItem item = valutToItemMap.get(toAdd.getValue()); 170 if (item == null) { 171 // new item does not exist yet. Add it to the list 172 list.add(toAdd); 173 valutToItemMap.put(toAdd.getValue(), toAdd); 174 } else { 175 item.setPriority(item.getPriority().mergeWith(toAdd.getPriority())); 182 if (values != null && list.addUserInput(values)) { 183 filter(); 176 184 } 177 185 } … … 183 191 * @param item the item to check 184 192 * @return true, if item is in the list; false, otherwise 185 */ 193 * @deprecated to be removed end of 2017. Use {@link #contains(AutoCompletionItem)} instead 194 */ 195 @Deprecated 186 196 public boolean contains(AutoCompletionListItem item) { 187 197 if (item == null) 188 198 return false; 199 return contains(item.getItem()); 200 } 201 202 /** 203 * checks whether a specific item is already in the list. Matches for the 204 * the value <strong>and</strong> the priority of the item 205 * 206 * @param item the item to check 207 * @return true, if item is in the list; false, otherwise 208 * @since 12859 209 */ 210 public boolean contains(AutoCompletionItem item) { 189 211 return list.contains(item); 190 212 } … … 198 220 */ 199 221 public boolean contains(String value) { 200 if (value == null) 201 return false; 202 for (AutoCompletionListItem item: list) { 203 if (item.getValue().equals(value)) 204 return true; 205 } 206 return false; 222 return list.contains(value); 207 223 } 208 224 209 225 /** 210 226 * removes the auto completion item with key <code>key</code> 211 * @param key the key;227 * @param key the key 212 228 */ 213 229 public void remove(String key) { 214 if (key == null) 215 return; 216 for (int i = 0; i < list.size(); i++) { 217 AutoCompletionListItem item = list.get(i); 218 if (item.getValue().equals(key)) { 219 list.remove(i); 220 return; 221 } 222 } 223 } 224 225 /** 226 * sorts the list 227 */ 228 protected void sort() { 229 Collections.sort(list); 230 if (key != null) { 231 list.remove(key); 232 } 230 233 } 231 234 … … 235 238 // Collections.copy throws an exception "Source does not fit in dest" 236 239 filtered.ensureCapacity(list.size()); 237 for (AutoCompletionListItem item: list) { 238 filtered.add(item); 239 } 240 filtered.addAll(list); 240 241 return; 241 242 } … … 244 245 // value to the list of filtered values 245 246 // 246 for (AutoCompletionListItem item : list) { 247 if (item.getValue().startsWith(filter)) { 248 filtered.add(item); 249 } 250 } 247 list.stream().filter(e -> e.getValue().startsWith(filter)).forEach(filtered::add); 251 248 fireTableDataChanged(); 252 249 } … … 258 255 */ 259 256 public int getFilteredSize() { 260 return this.filtered.size();257 return filtered.size(); 261 258 } 262 259 … … 267 264 * 268 265 * @throws IndexOutOfBoundsException if idx is out of bounds 269 */ 266 * @deprecated to be removed end of 2017. Use {@link #getFilteredItemAt(int)} instead 267 */ 268 @Deprecated 270 269 public AutoCompletionListItem getFilteredItem(int idx) { 271 if (idx < 0 || idx >= getFilteredSize()) 272 throw new IndexOutOfBoundsException("idx out of bounds. idx=" + idx); 270 return new AutoCompletionListItem(getFilteredItemAt(idx)); 271 } 272 273 /** 274 * replies the idx-th item from the list of filtered items 275 * @param idx the index; must be in the range 0 <= idx < {@link #getFilteredSize()} 276 * @return the item 277 * 278 * @throws IndexOutOfBoundsException if idx is out of bounds 279 * @since 12859 280 */ 281 public AutoCompletionItem getFilteredItemAt(int idx) { 273 282 return filtered.get(idx); 274 283 } 275 284 276 List<AutoCompletionListItem> getList() {285 AutoCompletionSet getSet() { 277 286 return list; 278 287 } 279 288 280 List<AutoCompletionListItem> getUnmodifiableList() {281 return Collections.unmodifiable List(list);289 Set<AutoCompletionItem> getUnmodifiableSet() { 290 return Collections.unmodifiableSet(list); 282 291 } 283 292 284 293 /** 285 294 * removes all elements from the auto completion list 286 *287 295 */ 288 296 public void clear() { 289 valutToItemMap.clear();290 297 list.clear(); 291 298 fireTableDataChanged(); … … 299 306 @Override 300 307 public int getRowCount() { 301 302 308 return list == null ? 0 : getFilteredSize(); 303 309 } … … 305 311 @Override 306 312 public Object getValueAt(int rowIndex, int columnIndex) { 307 return list == null ? null : getFilteredItem (rowIndex);313 return list == null ? null : getFilteredItemAt(rowIndex); 308 314 } 309 315 } -
trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionListItem.java
r10378 r12859 2 2 package org.openstreetmap.josm.gui.tagging.ac; 3 3 4 import org.openstreetmap.josm. tools.CheckParameterUtil;4 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem; 5 5 6 6 /** … … 15 15 * 16 16 * The value is a string which will be displayed in the auto completion list. 17 * 17 * @deprecated To be removed end of 2017. Use {@link AutoCompletionItem} instead 18 18 */ 19 @Deprecated 19 20 public class AutoCompletionListItem implements Comparable<AutoCompletionListItem> { 20 21 21 /** the pritority of this item */ 22 private AutoCompletionItemPriority priority; 23 /** the value of this item */ 24 private String value; 22 /** the item */ 23 private final AutoCompletionItem item; 25 24 26 25 /** … … 30 29 */ 31 30 public AutoCompletionListItem(String value, AutoCompletionItemPriority priority) { 32 this.value = value; 33 this.priority = priority; 31 this.item = new AutoCompletionItem(value, priority.getPriority()); 34 32 } 35 33 … … 39 37 */ 40 38 public AutoCompletionListItem(String value) { 41 this.value = value; 42 priority = AutoCompletionItemPriority.UNKNOWN; 39 this.item = new AutoCompletionItem(value); 43 40 } 44 41 … … 47 44 */ 48 45 public AutoCompletionListItem() { 49 value = ""; 50 priority = AutoCompletionItemPriority.UNKNOWN; 46 this.item = new AutoCompletionItem(); 47 } 48 49 /** 50 * Constructs a new {@code AutoCompletionListItem} from an existing {@link AutoCompletionItem}. 51 * @param other {@code AutoCompletionItem} to convert 52 * @since 12859 53 */ 54 public AutoCompletionListItem(AutoCompletionItem other) { 55 this.item = other; 51 56 } 52 57 … … 56 61 */ 57 62 public AutoCompletionItemPriority getPriority() { 58 return priority;63 return new AutoCompletionItemPriority(item.getPriority()); 59 64 } 60 65 … … 64 69 */ 65 70 public void setPriority(AutoCompletionItemPriority priority) { 66 this.priority = priority;71 item.setPriority(priority.getPriority()); 67 72 } 68 73 … … 72 77 */ 73 78 public String getValue() { 74 return value;79 return item.getValue(); 75 80 } 76 81 … … 81 86 */ 82 87 public void setValue(String value) { 83 CheckParameterUtil.ensureParameterNotNull(value, "value"); 84 this.value = value; 88 item.setValue(value); 85 89 } 86 90 87 91 @Override 88 92 public String toString() { 89 StringBuilder sb = new StringBuilder(); 90 sb.append("<val='") 91 .append(value) 92 .append("',") 93 .append(priority) 94 .append('>'); 95 return sb.toString(); 93 return item.toString(); 96 94 } 97 95 98 96 @Override 99 97 public int hashCode() { 100 final int prime = 31; 101 int result = 1; 102 result = prime * result 103 + ((priority == null) ? 0 : priority.hashCode()); 104 result = prime * result + ((value == null) ? 0 : value.hashCode()); 105 return result; 98 return item.hashCode(); 106 99 } 107 100 … … 113 106 return false; 114 107 if (obj instanceof String) 115 return obj.equals( value);108 return obj.equals(item.getValue()); 116 109 if (getClass() != obj.getClass()) 117 110 return false; 118 111 final AutoCompletionListItem other = (AutoCompletionListItem) obj; 119 if (priority == null) { 120 if (other.priority != null) 121 return false; 122 } else if (!priority.equals(other.priority)) 123 return false; 124 if (value == null) { 125 if (other.value != null) 126 return false; 127 } else if (!value.equals(other.value)) 128 return false; 129 return true; 112 return item.equals(other.item); 130 113 } 131 114 132 115 @Override 133 116 public int compareTo(AutoCompletionListItem other) { 134 int ret = other.priority.compareTo(priority); // higher priority items come first in the list 135 if (ret != 0) 136 return ret; 137 else 138 return this.value.compareTo(other.value); 117 return item.compareTo(other.item); 118 } 119 120 /** 121 * Returns the underlying item. 122 * @return the underlying item 123 * @since 12859 124 */ 125 public AutoCompletionItem getItem() { 126 return item; 139 127 } 140 128 } -
trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionManager.java
r12758 r12859 6 6 import java.util.Collection; 7 7 import java.util.Collections; 8 import java.util.Comparator; 8 9 import java.util.HashMap; 9 10 import java.util.HashSet; … … 15 16 import java.util.Set; 16 17 import java.util.function.Function; 18 import java.util.stream.Collectors; 17 19 18 20 import org.openstreetmap.josm.data.osm.DataSet; … … 29 31 import org.openstreetmap.josm.data.osm.event.TagsChangedEvent; 30 32 import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent; 33 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem; 34 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionPriority; 35 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionSet; 31 36 import org.openstreetmap.josm.gui.MainApplication; 32 37 import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent; … … 46 51 * possible auto completion values for each key. 47 52 * 48 * Each DataSet isassigned one AutoCompletionManager instance such that53 * Each DataSet can be assigned one AutoCompletionManager instance such that 49 54 * <ol> 50 55 * <li>any key used in a tag in the data set is part of the key list in the cache</li> 51 * <li>any value used in a tag for a specific key is part of the autocompletion list of 52 * this key</li> 56 * <li>any value used in a tag for a specific key is part of the autocompletion list of this key</li> 53 57 * </ol> 54 58 * … … 271 275 272 276 /** 273 * Populates the {@link AutoCompletionList} with the currently cached 274 * member roles. 277 * Populates the {@link AutoCompletionList} with the currently cached member roles. 275 278 * 276 279 * @param list the list to populate 277 280 */ 278 281 public void populateWithMemberRoles(AutoCompletionList list) { 279 list.add(TaggingPresets.getPresetRoles(), AutoCompletion ItemPriority.IS_IN_STANDARD);280 list.add(getRoleCache(), AutoCompletion ItemPriority.IS_IN_DATASET);282 list.add(TaggingPresets.getPresetRoles(), AutoCompletionPriority.IS_IN_STANDARD); 283 list.add(getRoleCache(), AutoCompletionPriority.IS_IN_DATASET); 281 284 } 282 285 … … 297 300 for (TaggingPreset tp : presets) { 298 301 if (tp.roles != null) { 299 list.add(Utils.transform(tp.roles.roles, (Function<Role, String>) x -> x.key), AutoCompletion ItemPriority.IS_IN_STANDARD);302 list.add(Utils.transform(tp.roles.roles, (Function<Role, String>) x -> x.key), AutoCompletionPriority.IS_IN_STANDARD); 300 303 } 301 304 } 302 list.add(r.getMemberRoles(), AutoCompletion ItemPriority.IS_IN_DATASET);305 list.add(r.getMemberRoles(), AutoCompletionPriority.IS_IN_DATASET); 303 306 } else { 304 307 populateWithMemberRoles(list); … … 312 315 */ 313 316 public void populateWithKeys(AutoCompletionList list) { 314 list.add(TaggingPresets.getPresetKeys(), AutoCompletion ItemPriority.IS_IN_STANDARD);315 list.add(new AutoCompletion ListItem("source", AutoCompletionItemPriority.IS_IN_STANDARD));316 list.add(getDataKeys(), AutoCompletion ItemPriority.IS_IN_DATASET);317 list.add(TaggingPresets.getPresetKeys(), AutoCompletionPriority.IS_IN_STANDARD); 318 list.add(new AutoCompletionItem("source", AutoCompletionPriority.IS_IN_STANDARD)); 319 list.add(getDataKeys(), AutoCompletionPriority.IS_IN_DATASET); 317 320 list.addUserInput(getUserInputKeys()); 318 321 } 319 322 320 323 /** 321 * Populates the an {@link AutoCompletionList} with the currently cached 322 * values for a tag 324 * Populates the an {@link AutoCompletionList} with the currently cached values for a tag 323 325 * 324 326 * @param list the list to populate … … 330 332 331 333 /** 332 * Populates the an {@link AutoCompletionList} with the currently cached 333 * values for some given tags 334 * Populates the an {@link AutoCompletionList} with the currently cached values for some given tags 334 335 * 335 336 * @param list the list to populate … … 338 339 public void populateWithTagValues(AutoCompletionList list, List<String> keys) { 339 340 for (String key : keys) { 340 list.add(TaggingPresets.getPresetValues(key), AutoCompletion ItemPriority.IS_IN_STANDARD);341 list.add(getDataValues(key), AutoCompletion ItemPriority.IS_IN_DATASET);341 list.add(TaggingPresets.getPresetValues(key), AutoCompletionPriority.IS_IN_STANDARD); 342 list.add(getDataValues(key), AutoCompletionPriority.IS_IN_DATASET); 342 343 list.addUserInput(getUserInputValues(key)); 343 344 } … … 347 348 * Returns the currently cached tag keys. 348 349 * @return a list of tag keys 349 */ 350 * @deprecated to be removed end of 2017. Use {@link #getTagKeys()} instead 351 */ 352 @Deprecated 350 353 public List<AutoCompletionListItem> getKeys() { 351 AutoCompletionList list = new AutoCompletionList(); 352 populateWithKeys(list); 353 return list.getList(); 354 return getTagKeys().stream().map(AutoCompletionListItem::new).collect(Collectors.toList()); 354 355 } 355 356 … … 358 359 * @param key the tag key 359 360 * @return a list of tag values 360 */ 361 * @deprecated to be removed end of 2017. Use {@link #getTagValues(String)} instead 362 */ 363 @Deprecated 361 364 public List<AutoCompletionListItem> getValues(String key) { 362 return get Values(Arrays.asList(key));365 return getTagValues(key).stream().map(AutoCompletionListItem::new).collect(Collectors.toList()); 363 366 } 364 367 … … 367 370 * @param keys the tag keys 368 371 * @return a list of tag values 369 */ 372 * @deprecated to be removed end of 2017. Use {@link #getTagValues(List)} instead 373 */ 374 @Deprecated 370 375 public List<AutoCompletionListItem> getValues(List<String> keys) { 376 return getTagValues(keys).stream().map(AutoCompletionListItem::new).collect(Collectors.toList()); 377 } 378 379 private static List<AutoCompletionItem> setToList(AutoCompletionSet set, Comparator<AutoCompletionItem> comparator) { 380 List<AutoCompletionItem> list = set.stream().collect(Collectors.toList()); 381 list.sort(comparator); 382 return list; 383 } 384 385 /** 386 * Returns the currently cached tag keys. 387 * @return a set of tag keys 388 * @since 12859 389 */ 390 public AutoCompletionSet getTagKeys() { 391 AutoCompletionList list = new AutoCompletionList(); 392 populateWithKeys(list); 393 return list.getSet(); 394 } 395 396 /** 397 * Returns the currently cached tag keys. 398 * @param comparator the custom comparator used to sort the list 399 * @return a list of tag keys 400 * @since 12859 401 */ 402 public List<AutoCompletionItem> getTagKeys(Comparator<AutoCompletionItem> comparator) { 403 return setToList(getTagKeys(), comparator); 404 } 405 406 /** 407 * Returns the currently cached tag values for a given tag key. 408 * @param key the tag key 409 * @return a set of tag values 410 * @since 12859 411 */ 412 public AutoCompletionSet getTagValues(String key) { 413 return getTagValues(Arrays.asList(key)); 414 } 415 416 /** 417 * Returns the currently cached tag values for a given tag key. 418 * @param key the tag key 419 * @param comparator the custom comparator used to sort the list 420 * @return a list of tag values 421 * @since 12859 422 */ 423 public List<AutoCompletionItem> getTagValues(String key, Comparator<AutoCompletionItem> comparator) { 424 return setToList(getTagValues(key), comparator); 425 } 426 427 /** 428 * Returns the currently cached tag values for a given list of tag keys. 429 * @param keys the tag keys 430 * @return a set of tag values 431 * @since 12859 432 */ 433 public AutoCompletionSet getTagValues(List<String> keys) { 371 434 AutoCompletionList list = new AutoCompletionList(); 372 435 populateWithTagValues(list, keys); 373 return list.getList(); 374 } 375 376 /********************************************************* 436 return list.getSet(); 437 } 438 439 /** 440 * Returns the currently cached tag values for a given list of tag keys. 441 * @param keys the tag keys 442 * @param comparator the custom comparator used to sort the list 443 * @return a set of tag values 444 * @since 12859 445 */ 446 public List<AutoCompletionItem> getTagValues(List<String> keys, Comparator<AutoCompletionItem> comparator) { 447 return setToList(getTagValues(keys), comparator); 448 } 449 450 /* 377 451 * Implementation of the DataSetListener interface 378 452 * 379 * */453 */ 380 454 381 455 @Override -
trunk/src/org/openstreetmap/josm/gui/tagging/ac/package-info.java
r8624 r12859 2 2 3 3 /** 4 * Provides classes for handling autocompletion of tags.4 * Provides GUI classes for handling autocompletion of tags. 5 5 */ 6 6 package org.openstreetmap.josm.gui.tagging.ac; -
trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/Combo.java
r12846 r12859 4 4 import javax.swing.JPanel; 5 5 6 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionPriority; 6 7 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField; 7 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPriority;8 8 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList; 9 9 import org.openstreetmap.josm.gui.widgets.JosmComboBox; … … 64 64 AutoCompletionList acList = tf.getAutoCompletionList(); 65 65 if (acList != null) { 66 acList.add(getDisplayValues(), AutoCompletion ItemPriority.IS_IN_STANDARD);66 acList.add(getDisplayValues(), AutoCompletionPriority.IS_IN_STANDARD); 67 67 } 68 68 combobox.setEditor(tf); -
trunk/src/org/openstreetmap/josm/gui/widgets/ComboBoxHistory.java
r9983 r12859 9 9 import javax.swing.DefaultComboBoxModel; 10 10 11 import org.openstreetmap.josm. gui.tagging.ac.AutoCompletionListItem;11 import org.openstreetmap.josm.data.tagging.ac.AutoCompletionItem; 12 12 13 13 /** 14 14 * A data model for {@link HistoryComboBox} 15 15 */ 16 class ComboBoxHistory extends DefaultComboBoxModel<AutoCompletion ListItem> implements Iterable<AutoCompletionListItem> {16 class ComboBoxHistory extends DefaultComboBoxModel<AutoCompletionItem> implements Iterable<AutoCompletionItem> { 17 17 18 18 private final int maxSize; … … 31 31 */ 32 32 public void addElement(String s) { 33 addElement(new AutoCompletion ListItem(s));33 addElement(new AutoCompletionItem(s)); 34 34 } 35 35 … … 39 39 */ 40 40 @Override 41 public void addElement(AutoCompletion ListItem o) {41 public void addElement(AutoCompletionItem o) { 42 42 String newEntry = o.getValue(); 43 43 … … 64 64 65 65 @Override 66 public Iterator<AutoCompletion ListItem> iterator() {67 return new Iterator<AutoCompletion ListItem>() {66 public Iterator<AutoCompletionItem> iterator() { 67 return new Iterator<AutoCompletionItem>() { 68 68 69 69 private int position = -1; … … 80 80 81 81 @Override 82 public AutoCompletion ListItem next() {82 public AutoCompletionItem next() { 83 83 if (!hasNext()) 84 84 throw new NoSuchElementException(); … … 102 102 103 103 /** 104 * Returns the {@link AutoCompletion ListItem} items as strings104 * Returns the {@link AutoCompletionItem} items as strings 105 105 * @return a list of strings 106 106 */ 107 107 public List<String> asStringList() { 108 108 List<String> list = new ArrayList<>(maxSize); 109 for (AutoCompletion ListItem item : this) {109 for (AutoCompletionItem item : this) { 110 110 list.add(item.getValue()); 111 111 }
Note:
See TracChangeset
for help on using the changeset viewer.