Changeset 3214 in josm for trunk/src/org
- Timestamp:
- 2010-05-02T18:12:34+02:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java
r3210 r3214 26 26 import java.util.HashMap; 27 27 import java.util.HashSet; 28 import java.util.Iterator; 28 29 import java.util.List; 29 30 import java.util.Map; … … 81 82 import org.openstreetmap.josm.gui.tagging.TaggingPreset; 82 83 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox; 84 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList; 85 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem; 83 86 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager; 84 87 import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher; … … 148 151 private final Map<String, Map<String, Integer>> valueCount = new TreeMap<String, Map<String, Integer>>(); 149 152 150 Comparator<String> defaultKeyComparator = String.CASE_INSENSITIVE_ORDER; 151 Comparator<String> defaultValueComparator = String.CASE_INSENSITIVE_ORDER; 153 Comparator<AutoCompletionListItem> defaultACItemComparator = new Comparator<AutoCompletionListItem>() { 154 public int compare(AutoCompletionListItem o1, AutoCompletionListItem o2) { 155 return String.CASE_INSENSITIVE_ORDER.compare(o1.getValue(), o2.getValue()); 156 } 157 }; 152 158 153 159 private DataSetListenerAdapter dataChangedAdapter = new DataSetListenerAdapter(this); … … 191 197 192 198 AutoCompletionManager autocomplete = Main.main.getEditLayer().data.getAutoCompletionManager(); 193 199 List<AutoCompletionListItem> keyList = autocomplete.getKeys(); 200 Collections.sort(keyList, defaultACItemComparator); 201 194 202 final AutoCompletingComboBox keys = new AutoCompletingComboBox(); 195 keys.setPossible Items(autocomplete.getKeys(defaultKeyComparator));203 keys.setPossibleACItems(keyList); 196 204 keys.setEditable(true); 197 205 keys.setSelectedItem(key); … … 209 217 if (c instanceof JLabel) { 210 218 String str = null; 211 str=( String) value;219 str=((AutoCompletionListItem) value).getValue(); 212 220 if (valueCount.containsKey(objKey)){ 213 221 Map<String, Integer> m=valueCount.get(objKey); … … 223 231 }); 224 232 values.setEditable(true); 225 values.setPossibleItems(autocomplete.getValues(key, defaultValueComparator)); 233 234 List<AutoCompletionListItem> valueList = autocomplete.getValues(key); 235 Collections.sort(valueList, defaultACItemComparator); 236 237 values.setPossibleACItems(valueList); 226 238 Map<String, Integer> m=(Map<String, Integer>)propertyData.getValueAt(row, 1); 227 239 final String selection= m.size()!=1?tr("<different>"):m.entrySet().iterator().next().getKey(); … … 344 356 final AutoCompletingComboBox keys = new AutoCompletingComboBox(); 345 357 AutoCompletionManager autocomplete = Main.main.getEditLayer().data.getAutoCompletionManager(); 346 List<String> usedKeys = 347 new ArrayList<String>(autocomplete.getKeys(defaultKeyComparator)); 348 for (int i = 0; i < propertyData.getRowCount(); ++i) { 349 usedKeys.remove(propertyData.getValueAt(i, 0)); 350 } 351 keys.setPossibleItems(usedKeys); 358 List<AutoCompletionListItem> keyList = autocomplete.getKeys(); 359 360 // remove the object's tag keys from the list 361 Iterator<AutoCompletionListItem> iter = keyList.iterator(); 362 while (iter.hasNext()) { 363 AutoCompletionListItem item = iter.next(); 364 for (int i = 0; i < propertyData.getRowCount(); ++i) { 365 if (item.getValue().equals(propertyData.getValueAt(i, 0))) { 366 iter.remove(); 367 break; 368 } 369 } 370 } 371 372 Collections.sort(keyList, defaultACItemComparator); 373 keys.setPossibleACItems(keyList); 352 374 keys.setEditable(true); 353 375 … … 397 419 @Override public void focusGained(FocusEvent e) { 398 420 String key = keys.getEditor().getItem().toString(); 399 values.setPossibleItems(autocomplete.getValues(key, defaultValueComparator)); 421 422 List<AutoCompletionListItem> valueList = autocomplete.getValues(key); 423 Collections.sort(valueList, defaultACItemComparator); 424 425 values.setPossibleACItems(valueList); 400 426 objKey=key; 401 427 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java
r3210 r3214 306 306 public void focusGained(FocusEvent e) { 307 307 AutoCompletionList list = tfRole.getAutoCompletionList(); 308 list.clear(); 308 309 getLayer().data.getAutoCompletionManager().populateWithMemberRoles(list); 309 310 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberRoleCellEditor.java
r3210 r3214 43 43 String role = (String)value; 44 44 editor.setText(role); 45 autoCompletionList.clear(); 45 46 ds.getAutoCompletionManager().populateWithMemberRoles(autoCompletionList); 46 47 return editor; -
trunk/src/org/openstreetmap/josm/gui/tagging/TagTable.java
r3210 r3214 409 409 if (autocomplete == null) { 410 410 logger.warning("argument autocomplete should not be null. Aborting."); 411 Thread.dumpStack(); 411 412 return; 412 413 } -
trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletingComboBox.java
r3210 r3214 2 2 package org.openstreetmap.josm.gui.tagging.ac; 3 3 4 import java.awt.Component; 4 5 import java.awt.event.FocusEvent; 5 6 import java.awt.event.FocusListener; 6 7 import java.util.Collection; 7 8 9 import javax.swing.ComboBoxEditor; 8 10 import javax.swing.ComboBoxModel; 9 11 import javax.swing.DefaultComboBoxModel; 10 12 import javax.swing.JComboBox; 13 import javax.swing.JLabel; 14 import javax.swing.JList; 15 import javax.swing.ListCellRenderer; 11 16 import javax.swing.text.AttributeSet; 12 17 import javax.swing.text.BadLocationException; … … 41 46 42 47 @Override public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { 43 if (selecting || (offs == 0 && str.equals(getText(0, getLength()))))48 if (selecting || (offs == 0 && str.equals(getText(0, getLength())))) 44 49 return; 45 50 boolean initial = (offs == 0 && getLength() == 0 && str.length() > 1); … … 58 63 int end = start; 59 64 String curText = getText(0, size); 65 66 // if the text starts with a number we don't autocomplete 67 // 68 try { 69 Long.parseLong(str); 70 if (curText.length() == 0) { 71 // we don't autocomplete on numbers 72 return; 73 } 74 Long.parseLong(curText); 75 return; 76 } catch (NumberFormatException e) { 77 // either the new text or the current text isn't a number. We continue with 78 // autocompletion 79 } 80 60 81 // lookup and select a matching item 61 82 Object item = lookupItem(curText); 62 83 setSelectedItem(item); 63 if (initial) {84 if (initial) { 64 85 start = 0; 65 86 } 66 87 if (item != null) { 67 String newText = item.toString();68 if (!newText.equals(curText))88 String newText = ((AutoCompletionListItem) item).getValue(); 89 if (!newText.equals(curText)) 69 90 { 70 91 selecting = true; … … 89 110 private Object lookupItem(String pattern) { 90 111 ComboBoxModel model = comboBox.getModel(); 112 AutoCompletionListItem bestItem = null; 91 113 for (int i = 0, n = model.getSize(); i < n; i++) { 92 Object currentItem = model.getElementAt(i); 93 if (currentItem.toString().startsWith(pattern)) 94 return currentItem; 95 } 96 return null; 114 AutoCompletionListItem currentItem = (AutoCompletionListItem) model.getElementAt(i);; 115 if (currentItem.getValue().startsWith(pattern)) { 116 if (bestItem == null || currentItem.getPriority().compareTo(bestItem.getPriority()) > 0) { 117 bestItem = currentItem; 118 } 119 } 120 } 121 return bestItem; // may be null 97 122 } 98 123 } 99 124 100 125 public AutoCompletingComboBox() { 126 setRenderer(new AutoCompleteListCellRenderer()); 101 127 final JTextComponent editor = (JTextComponent) this.getEditor().getEditorComponent(); 102 128 editor.setDocument(new AutoCompletingComboBoxDocument(this)); … … 112 138 } 113 139 140 /** 141 * Convert the selected item into a String 142 * that can be edited in the editor component. 143 * 144 * @param editor the editor 145 * @param item excepts AutoCompletionListItem, String and null 146 */ 147 @Override public void configureEditor(ComboBoxEditor editor, Object item) { 148 if (item == null) { 149 editor.setItem(null); 150 } else if (item instanceof String) { 151 editor.setItem(item); 152 } else if (item instanceof AutoCompletionListItem) { 153 editor.setItem(((AutoCompletionListItem)item).getValue()); 154 } else 155 throw new IllegalArgumentException(); 156 } 157 158 /** 159 * Selects a given item in the ComboBox model 160 * @param item excepts AutoCompletionListItem, String and null 161 */ 162 @Override public void setSelectedItem(Object item) { 163 if (item == null) { 164 super.setSelectedItem(null); 165 } else if (item instanceof AutoCompletionListItem) { 166 super.setSelectedItem(item); 167 } else if (item instanceof String) { 168 String s = (String) item; 169 // find the string in the model or create a new item 170 for (int i=0; i< getModel().getSize(); i++) { 171 AutoCompletionListItem acItem = (AutoCompletionListItem) getModel().getElementAt(i); 172 if (s.equals(acItem.getValue())) { 173 super.setSelectedItem(acItem); 174 return; 175 } 176 } 177 super.setSelectedItem(new AutoCompletionListItem(s, AutoCompletionItemPritority.UNKNOWN)); 178 } else 179 throw new IllegalArgumentException(); 180 } 181 182 /** 183 * sets the items of the combobox to the given strings 184 */ 114 185 public void setPossibleItems(Collection<String> elems) { 115 186 DefaultComboBoxModel model = (DefaultComboBoxModel)this.getModel(); … … 117 188 model.removeAllElements(); 118 189 for (String elem : elems) { 190 model.addElement(new AutoCompletionListItem(elem, AutoCompletionItemPritority.UNKNOWN)); 191 } 192 this.getEditor().setItem(oldValue); 193 } 194 195 /** 196 * sets the items of the combobox to the given AutoCompletionListItems 197 */ 198 public void setPossibleACItems(Collection<AutoCompletionListItem> elems) { 199 DefaultComboBoxModel model = (DefaultComboBoxModel)this.getModel(); 200 Object oldValue = this.getEditor().getItem(); 201 model.removeAllElements(); 202 for (AutoCompletionListItem elem : elems) { 119 203 model.addElement(elem); 120 204 } 121 205 this.getEditor().setItem(oldValue); 122 206 } 207 123 208 124 209 protected boolean isAutocompleteEnabled() { … … 129 214 this.autocompleteEnabled = autocompleteEnabled; 130 215 } 216 217 /** 218 * ListCellRenderer for AutoCompletingComboBox 219 * renders an AutoCompletionListItem by showing only the string value part 220 */ 221 public class AutoCompleteListCellRenderer extends JLabel implements ListCellRenderer { 222 223 public AutoCompleteListCellRenderer() { 224 setOpaque(true); 225 } 226 227 public Component getListCellRendererComponent( 228 JList list, 229 Object value, 230 int index, 231 boolean isSelected, 232 boolean cellHasFocus) 233 { 234 if (isSelected) { 235 setBackground(list.getSelectionBackground()); 236 setForeground(list.getSelectionForeground()); 237 } else { 238 setBackground(list.getBackground()); 239 setForeground(list.getForeground()); 240 } 241 242 AutoCompletionListItem item = (AutoCompletionListItem) value; 243 setText(item.getValue()); 244 return this; 245 } 246 } 131 247 } -
trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionItemPritority.java
r3083 r3214 2 2 package org.openstreetmap.josm.gui.tagging.ac; 3 3 4 public enum AutoCompletionItemPritority implements Comparable<AutoCompletionItemPritority> { 5 6 /** Indicates that a value is in the current selection. */ 7 IS_IN_SELECTION, 4 /** 5 * Describes the priority of an item in an autocompletion list. 6 * The selected flag is currently only used in plugins. 7 * 8 * Instances of this class are not modifiable. 9 */ 10 public class AutoCompletionItemPritority implements Comparable<AutoCompletionItemPritority> { 8 11 9 12 /** … … 12 15 * usually not used by the user. 13 16 */ 14 IS_IN_STANDARD_AND_IN_DATASET, 17 public static AutoCompletionItemPritority IS_IN_STANDARD_AND_IN_DATASET = new AutoCompletionItemPritority(true, true, false); 18 19 /** 20 * Indicates that this is an arbitrary value from the data set, i.e. 21 * the value of a tag name=*. 22 */ 23 public static AutoCompletionItemPritority IS_IN_DATASET = new AutoCompletionItemPritority(true, false, false); 15 24 16 25 /** … … 18 27 * or a standard value for a given tag name (from the presets). 19 28 */ 20 IS_IN_STANDARD, 29 public static AutoCompletionItemPritority IS_IN_STANDARD = new AutoCompletionItemPritority(false, true, false); 30 31 /** 32 * Indicates that this is a value from a selected object. 33 */ 34 public static AutoCompletionItemPritority IS_IN_SELECTION = new AutoCompletionItemPritority(false, false, true); 35 36 /** Unknown priority. This is the lowest priority. */ 37 public static AutoCompletionItemPritority UNKNOWN = new AutoCompletionItemPritority(false, false, false); 38 39 private final boolean inDataSet; 40 private final boolean inStandard; 41 private final boolean selected; 42 43 public AutoCompletionItemPritority(boolean inDataSet, boolean inStandard, boolean selected) { 44 this.inDataSet = inDataSet; 45 this.inStandard = inStandard; 46 this.selected = selected; 47 } 48 49 public boolean isInDataSet() { 50 return inDataSet; 51 } 52 53 public boolean isInStandard() { 54 return inStandard; 55 } 56 57 public boolean isSelected() { 58 return selected; 59 } 21 60 22 61 /** 23 * I ndicates that this is an arbitrary value from the data set, i.e.24 * the value of a tag name=*.62 * Imposes an ordering on the priorities. 63 * Currently, being in the current DataSet is worth more than being in the Presets. 25 64 */ 26 IS_IN_DATASET, 65 public int compareTo(AutoCompletionItemPritority other) { 66 int sel = new Boolean(selected).compareTo(other.selected); 67 if (sel != 0) return sel; 27 68 28 /** Unknown priority. This is the lowest priority. */ 29 UNKNOWN 69 int ds = new Boolean(inDataSet).compareTo(other.inDataSet); 70 if (ds != 0) return ds; 71 72 int std = new Boolean(inStandard).compareTo(other.inStandard); 73 if (std != 0) return std; 74 75 return 0; 76 } 77 78 /** 79 * Merges two priorities. 80 * The resulting priority is always >= the original ones. 81 */ 82 public AutoCompletionItemPritority mergeWith(AutoCompletionItemPritority other) { 83 return new AutoCompletionItemPritority( 84 inDataSet || other.inDataSet, 85 inStandard || other.inStandard, 86 selected || other.selected); 87 } 88 89 @Override public String toString() { 90 return String.format("<Priority; inDataSet: %b, inStandard: %b, selected: %b>", inDataSet, inStandard, selected); 91 } 30 92 } -
trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionList.java
r3210 r3214 149 149 } 150 150 151 protected void appendOrUpdatePriority(AutoCompletionListItem to add) {152 AutoCompletionListItem item = valutToItemMap.get(to add.getValue());151 protected void appendOrUpdatePriority(AutoCompletionListItem toAdd) { 152 AutoCompletionListItem item = valutToItemMap.get(toAdd.getValue()); 153 153 if (item == null) { 154 154 // new item does not exist yet. Add it to the list 155 // 156 list.add(toadd); 157 valutToItemMap.put(toadd.getValue(), toadd); 155 list.add(toAdd); 156 valutToItemMap.put(toAdd.getValue(), toAdd); 158 157 } else { 159 // new item already exists. Update priority if necessary 160 161 // If it is both in the dataset and in the presets, update the priority. 162 final AutoCompletionItemPritority IS_IN_DATASET = AutoCompletionItemPritority.IS_IN_DATASET; 163 final AutoCompletionItemPritority IS_IN_STANDARD = AutoCompletionItemPritority.IS_IN_STANDARD; 164 if ((toadd.getPriority() == IS_IN_STANDARD && item.getPriority() == IS_IN_DATASET) || 165 (toadd.getPriority() == IS_IN_DATASET && item.getPriority() == IS_IN_STANDARD)) { 166 167 item.setPriority(AutoCompletionItemPritority.IS_IN_STANDARD_AND_IN_DATASET); 168 } else { 169 if (toadd.getPriority().compareTo(item.getPriority()) < 0) { 170 item.setPriority(toadd.getPriority()); 171 } 172 } 158 item.setPriority(item.getPriority().mergeWith(toAdd.getPriority())); 173 159 } 174 160 } … … 272 258 } 273 259 260 List<AutoCompletionListItem> getList() { 261 return Collections.unmodifiableList(list); 262 } 263 274 264 /** 275 265 * removes all elements from the auto completion list -
trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionListItem.java
r3210 r3214 112 112 public int compareTo(AutoCompletionListItem other) { 113 113 int ret = this.priority.compareTo(other.priority); 114 ret = -ret; // higher priority items come first in the list 114 115 if (ret != 0) 115 116 return ret; -
trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionManager.java
r3213 r3214 109 109 110 110 protected void cachePrimitives(Collection<? extends OsmPrimitive> primitives) { 111 if (tagCache == null) {112 // We are coming from a DataSetListener event and113 // rebuild has not been called yet, so do it now and114 // ignore the method parameter.115 rebuild();116 return;117 }118 111 for (OsmPrimitive primitive : primitives) { 119 112 cachePrimitiveTags(primitive); … … 204 197 } 205 198 206 public TreeSet<String> getKeys(Comparator<String> c) {207 TreeSet<String> ret = new TreeSet<String>(c);208 ret.addAll(getDataKeys());209 ret.addAll(getPresetKeys());210 return ret;211 }212 213 199 /** 214 200 * replies the auto completion values allowed for a specific key. Replies … … 226 212 } 227 213 228 public TreeSet<String> getValues(String key, Comparator<String> c) {229 TreeSet<String> ret = new TreeSet<String>(c);230 ret.addAll(getDataValues(key));231 ret.addAll(getPresetValues(key));232 return ret;233 }234 235 214 /** 236 215 * Replies the list of member roles … … 249 228 */ 250 229 public void populateWithMemberRoles(AutoCompletionList list) { 251 list.clear();252 230 list.add(getRoleCache(), AutoCompletionItemPritority.IS_IN_DATASET); 231 } 232 233 /** 234 * Populates the an {@see AutoCompletionList} with the currently cached 235 * tag keys 236 * 237 * @param list the list to populate 238 * @param append true to add the keys to the list; false, to replace the keys 239 * in the list by the keys in the cache 240 */ 241 public void populateWithKeys(AutoCompletionList list) { 242 list.add(getPresetKeys(), AutoCompletionItemPritority.IS_IN_STANDARD); 243 list.add(getDataKeys(), AutoCompletionItemPritority.IS_IN_DATASET); 253 244 } 254 245 … … 263 254 */ 264 255 public void populateWithTagValues(AutoCompletionList list, String key) { 256 list.add(getPresetValues(key), AutoCompletionItemPritority.IS_IN_STANDARD); 265 257 list.add(getDataValues(key), AutoCompletionItemPritority.IS_IN_DATASET); 266 list.add(getPresetValues(key), AutoCompletionItemPritority.IS_IN_STANDARD); 267 } 268 269 /** 270 * Populates the an {@see AutoCompletionList} with the currently cached 271 * tag keys 272 * 273 * @param list the list to populate 274 * @param append true to add the keys to the list; false, to replace the keys 275 * in the list by the keys in the cache 276 */ 277 public void populateWithKeys(AutoCompletionList list) { 278 list.add(getDataKeys(), AutoCompletionItemPritority.IS_IN_DATASET); 279 list.add(getPresetKeys(), AutoCompletionItemPritority.IS_IN_STANDARD); 258 } 259 260 public List<AutoCompletionListItem> getKeys() { 261 AutoCompletionList list = new AutoCompletionList(); 262 populateWithKeys(list); 263 return new ArrayList<AutoCompletionListItem>(list.getList()); 264 } 265 266 public List<AutoCompletionListItem> getValues(String key) { 267 AutoCompletionList list = new AutoCompletionList(); 268 populateWithTagValues(list, key); 269 return new ArrayList<AutoCompletionListItem>(list.getList()); 280 270 } 281 271 … … 286 276 287 277 public void primtivesAdded(PrimitivesAddedEvent event) { 278 if (dirty) 279 return; 288 280 cachePrimitives(event.getPrimitives()); 289 281 } … … 294 286 295 287 public void tagsChanged(TagsChangedEvent event) { 288 if (dirty) 289 return; 296 290 Map<String, String> newKeys = event.getPrimitive().getKeys(); 297 291 Map<String, String> oldKeys = event.getOriginalKeys();
Note:
See TracChangeset
for help on using the changeset viewer.