Changeset 3210 in josm for trunk


Ignore:
Timestamp:
2010-04-26T23:25:10+02:00 (9 years ago)
Author:
bastiK
Message:

autocompletion rework; breaks tageditor plugin and possibly other plugins
(merged the 2 autocompletion systems; adds presets values for the properties dialog )

Location:
trunk/src/org/openstreetmap/josm
Files:
1 deleted
20 edited
3 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/corrector/ReverseWayTagCorrector.java

    r3190 r3210  
    1919import org.openstreetmap.josm.data.osm.RelationMember;
    2020import org.openstreetmap.josm.data.osm.Way;
     21
     22/**
     23 * A ReverseWayTagCorrector handles necessary corrections of tags
     24 * when a way is reversed. E.g. oneway=yes needs to be changed
     25 * to oneway=-1 and vice versa.
     26 *
     27 * The Corrector offers the automatic resolution in an dialog
     28 * for the user to confirm.
     29 */
    2130
    2231public class ReverseWayTagCorrector extends TagCorrector<Way> {
  • trunk/src/org/openstreetmap/josm/corrector/TagCorrection.java

    r3083 r3210  
    22package org.openstreetmap.josm.corrector;
    33
     4/**
     5 * TagCorrection reprepresents a change of a single
     6 * tag. Both key and value can be subject of this change.
     7 */
    48public class TagCorrection implements Correction {
    59
  • trunk/src/org/openstreetmap/josm/corrector/TagCorrector.java

    r3083 r3210  
    3434import org.openstreetmap.josm.tools.ImageProvider;
    3535
     36/**
     37 * Abstract base class for automatic tag corrections.
     38 *
     39 * Subclasses call applyCorrections() with maps of the requested
     40 * corrections and a dialog is pesented to the user to
     41 * confirm these changes.
     42 */
     43 
    3644public abstract class TagCorrector<P extends OsmPrimitive> {
    3745
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r3207 r3210  
    2828import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
    2929import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
     30import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
    3031import org.openstreetmap.josm.tools.Predicate;
    3132
     
    6869    public int getHighlightUpdateCount() {
    6970        return highlightUpdateCount;
     71    }
     72
     73    /**
     74     * Maintain a list of used tags for autocompletion
     75     */
     76    private AutoCompletionManager autocomplete;
     77
     78    public AutoCompletionManager getAutoCompletionManager() {
     79        if (autocomplete == null) {
     80            autocomplete = new AutoCompletionManager(this);
     81            addDataSetListener(autocomplete);
     82        }
     83        return autocomplete;
    7084    }
    7185
  • trunk/src/org/openstreetmap/josm/data/osm/event/DataSetListener.java

    r3083 r3210  
     1package org.openstreetmap.josm.data.osm.event;
    12/*
    23 *  JOSMng - a Java Open Street Map editor, the next generation.
     
    1920 *  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
    2021 */
    21 
    22 package org.openstreetmap.josm.data.osm.event;
    2322
    2423/**
  • trunk/src/org/openstreetmap/josm/gui/conflict/tags/RelationMemberConflictResolver.java

    r3083 r3210  
    3333import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3434import org.openstreetmap.josm.gui.JMultilineLabel;
    35 import org.openstreetmap.josm.gui.tagging.AutoCompletingTextField;
    36 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache;
     35import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
    3736import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
     37import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
    3838import org.openstreetmap.josm.tools.ImageProvider;
    3939
     
    183183
    184184    public void prepareForEditing() {
    185         AutoCompletionCache.getCacheForLayer(Main.main.getEditLayer()).initFromDataSet();
    186185        AutoCompletionList acList = new AutoCompletionList();
    187         AutoCompletionCache.getCacheForLayer(Main.main.getEditLayer()).populateWithMemberRoles(acList);
     186        Main.main.getEditLayer().data.getAutoCompletionManager().populateWithMemberRoles(acList);
    188187        tfRole.setAutoCompletionList(acList);
    189188        AutoCompletingTextField editor = (AutoCompletingTextField) tblResolver.getColumnModel().getColumn(2).getCellEditor();
     
    192191        }
    193192        AutoCompletionList acList2 = new AutoCompletionList();
    194         AutoCompletionCache.getCacheForLayer(Main.main.getEditLayer()).populateWithKeys(acList2, false /* don'tappend */);
     193        Main.main.getEditLayer().data.getAutoCompletionManager().populateWithKeys(acList2);
    195194        tfKey.setAutoCompletionList(acList2);
    196195    }
  • trunk/src/org/openstreetmap/josm/gui/conflict/tags/RelationMemberConflictResolverColumnModel.java

    r3083 r3210  
    88
    99import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
    10 import org.openstreetmap.josm.gui.tagging.AutoCompletingTextField;
     10import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
    1111
    1212public class RelationMemberConflictResolverColumnModel extends DefaultTableColumnModel{
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java

    r3202 r3210  
    2323import java.util.Collection;
    2424import java.util.Collections;
     25import java.util.Comparator;
    2526import java.util.HashMap;
    2627import java.util.HashSet;
     
    7980import org.openstreetmap.josm.gui.preferences.TaggingPresetPreference;
    8081import org.openstreetmap.josm.gui.tagging.TaggingPreset;
    81 import org.openstreetmap.josm.gui.widgets.AutoCompleteComboBox;
     82import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox;
     83import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
    8284import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
    8385import org.openstreetmap.josm.tools.GBC;
     
    145147
    146148    private final Map<String, Map<String, Integer>> valueCount = new TreeMap<String, Map<String, Integer>>();
    147     private final ListOfUsedTags listOfUsedTags = new ListOfUsedTags();
     149
     150    Comparator<String> defaultKeyComparator = String.CASE_INSENSITIVE_ORDER;
     151    Comparator<String> defaultValueComparator = String.CASE_INSENSITIVE_ORDER;
    148152
    149153    private DataSetListenerAdapter dataChangedAdapter = new DataSetListenerAdapter(this);
     
    151155    @Override
    152156    public void showNotify() {
    153         DatasetEventManager.getInstance().addDatasetListener(listOfUsedTags, FireMode.IMMEDIATELY);
    154157        DatasetEventManager.getInstance().addDatasetListener(dataChangedAdapter, FireMode.IN_EDT_CONSOLIDATED);
    155         listOfUsedTags.rebuildNecessary();
    156158        SelectionEventManager.getInstance().addSelectionListener(this, FireMode.IN_EDT_CONSOLIDATED);
    157159        MapView.addEditLayerChangeListener(this);
     
    161163    @Override
    162164    public void hideNotify() {
    163         DatasetEventManager.getInstance().removeDatasetListener(listOfUsedTags);
    164165        DatasetEventManager.getInstance().removeDatasetListener(dataChangedAdapter);
    165166        SelectionEventManager.getInstance().removeSelectionListener(this);
     
    189190        panel.add(p, BorderLayout.CENTER);
    190191
    191         final AutoCompleteComboBox keys = new AutoCompleteComboBox();
    192         keys.setPossibleItems(listOfUsedTags.getUsedKeys());
     192        AutoCompletionManager autocomplete = Main.main.getEditLayer().data.getAutoCompletionManager();
     193
     194        final AutoCompletingComboBox keys = new AutoCompletingComboBox();
     195        keys.setPossibleItems(autocomplete.getKeys(defaultKeyComparator));
    193196        keys.setEditable(true);
    194197        keys.setSelectedItem(key);
     
    198201        p.add(keys, GBC.eol().fill(GBC.HORIZONTAL));
    199202
    200         final AutoCompleteComboBox values = new AutoCompleteComboBox();
     203        final AutoCompletingComboBox values = new AutoCompletingComboBox();
    201204        values.setRenderer(new DefaultListCellRenderer() {
    202205            @Override public Component getListCellRendererComponent(JList list,
     
    220223        });
    221224        values.setEditable(true);
    222         values.setPossibleItems(listOfUsedTags.getUsedValues(key));
     225        values.setPossibleItems(autocomplete.getValues(key, defaultValueComparator));
    223226        Map<String, Integer> m=(Map<String, Integer>)propertyData.getValueAt(row, 1);
    224227        final String selection= m.size()!=1?tr("<different>"):m.entrySet().iterator().next().getKey();
     
    228231        p.add(Box.createHorizontalStrut(10), GBC.std());
    229232        p.add(values, GBC.eol().fill(GBC.HORIZONTAL));
    230         addFocusAdapter(row, keys, values);
     233        addFocusAdapter(row, keys, values, autocomplete);
    231234
    232235        final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
     
    339342                "This will change up to {0} objects.", sel.size(),sel.size())
    340343                +"<br><br>"+tr("Please select a key")), BorderLayout.NORTH);
    341         final AutoCompleteComboBox keys = new AutoCompleteComboBox();
    342         List<String> usedKeys = new ArrayList<String>(listOfUsedTags.getUsedKeys());
     344        final AutoCompletingComboBox keys = new AutoCompletingComboBox();
     345        AutoCompletionManager autocomplete = Main.main.getEditLayer().data.getAutoCompletionManager();
     346        List<String> usedKeys =
     347                new ArrayList<String>(autocomplete.getKeys(defaultKeyComparator));
    343348        for (int i = 0; i < propertyData.getRowCount(); ++i) {
    344349            usedKeys.remove(propertyData.getValueAt(i, 0));
     
    352357        p.add(p2, BorderLayout.SOUTH);
    353358        p2.add(new JLabel(tr("Please select a value")), BorderLayout.NORTH);
    354         final AutoCompleteComboBox values = new AutoCompleteComboBox();
     359        final AutoCompletingComboBox values = new AutoCompletingComboBox();
    355360        values.setEditable(true);
    356361        p2.add(values, BorderLayout.CENTER);
    357362
    358         addFocusAdapter(-1, keys, values);
     363        FocusAdapter focus = addFocusAdapter(-1, keys, values, autocomplete);
     364        // fire focus event in advance or otherwise the popup list will be too small at first
     365        focus.focusGained(null);
     366
    359367        JOptionPane pane = new JOptionPane(p, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION){
    360368            @Override public void selectInitialValue() {
     
    381389     * @param values
    382390     */
    383     private void addFocusAdapter(final int row, final AutoCompleteComboBox keys, final AutoCompleteComboBox values) {
     391    private FocusAdapter addFocusAdapter(final int row, final AutoCompletingComboBox keys, final AutoCompletingComboBox values, final AutoCompletionManager autocomplete) {
    384392        // get the combo box' editor component
    385393        JTextComponent editor = (JTextComponent)values.getEditor()
    386394        .getEditorComponent();
    387395        // Refresh the values model when focus is gained
    388         editor.addFocusListener(new FocusAdapter() {
     396        FocusAdapter focus = new FocusAdapter() {
    389397            @Override public void focusGained(FocusEvent e) {
    390398                String key = keys.getEditor().getItem().toString();
    391                 values.setPossibleItems(listOfUsedTags.getUsedValues(key));
     399                values.setPossibleItems(autocomplete.getValues(key, defaultValueComparator));
    392400                objKey=key;
    393401            }
    394         });
     402        };
     403        editor.addFocusListener(focus);
     404        return focus;
    395405    }
    396406    private String objKey;
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/GenericRelationEditor.java

    r3202 r3210  
    6666import org.openstreetmap.josm.gui.help.HelpUtil;
    6767import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    68 import org.openstreetmap.josm.gui.tagging.AutoCompletingTextField;
    6968import org.openstreetmap.josm.gui.tagging.TagEditorPanel;
    70 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache;
     69import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
    7170import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
     71import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
    7272import org.openstreetmap.josm.tools.ImageProvider;
    7373import org.openstreetmap.josm.tools.Shortcut;
     
    111111        setRememberWindowGeometry(getClass().getName() + ".geometry",
    112112                WindowGeometry.centerInWindow(Main.parent, new Dimension(700, 650)));
    113 
    114         // initialize the autocompletion infrastructure
    115         //
    116         AutoCompletionCache.getCacheForLayer(getLayer()).initFromDataSet();
    117113
    118114        // init the various models
     
    310306                    public void focusGained(FocusEvent e) {
    311307                        AutoCompletionList list = tfRole.getAutoCompletionList();
    312                         AutoCompletionCache.getCacheForLayer(Main.main.getEditLayer()).populateWithMemberRoles(list);
     308                        getLayer().data.getAutoCompletionManager().populateWithMemberRoles(list);
    313309                    }
    314310                }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberRoleCellEditor.java

    r3083 r3210  
    1010
    1111import org.openstreetmap.josm.Main;
    12 import org.openstreetmap.josm.gui.tagging.AutoCompletingTextField;
    13 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache;
     12import org.openstreetmap.josm.data.osm.DataSet;
     13import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
     14import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
    1415import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
    1516
     
    1920
    2021    private AutoCompletingTextField editor = null;
     22    private DataSet ds;
    2123
    2224    /** user input is matched against this list of auto completion items */
     
    2628     * constructor
    2729     */
    28     public MemberRoleCellEditor() {
     30    public MemberRoleCellEditor(DataSet ds) {
     31        this.ds = ds;
    2932        editor = new AutoCompletingTextField();
    3033        autoCompletionList = new AutoCompletionList();
     
    4043        String role = (String)value;
    4144        editor.setText(role);
    42         AutoCompletionCache.getCacheForLayer(Main.main.getEditLayer()).populateWithMemberRoles(autoCompletionList);
     45        ds.getAutoCompletionManager().populateWithMemberRoles(autoCompletionList);
    4346        return editor;
    4447    }
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTable.java

    r3083 r3210  
    4848     */
    4949    public MemberTable(OsmDataLayer layer, MemberTableModel model) {
    50         super(model, new MemberTableColumnModel(), model.getSelectionModel());
     50        super(model, new MemberTableColumnModel(layer.data), model.getSelectionModel());
    5151        this.layer = layer;
    5252        model.addMemberModelListener(this);
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableColumnModel.java

    r3083 r3210  
    77import javax.swing.table.TableColumn;
    88
     9import org.openstreetmap.josm.data.osm.DataSet;
     10
    911public class MemberTableColumnModel extends DefaultTableColumnModel {
    1012
    11     public MemberTableColumnModel() {
     13    public MemberTableColumnModel(DataSet ds) {
    1214        TableColumn col = null;
    1315
     
    1820        col.setPreferredWidth(100);
    1921        col.setCellRenderer(new MemberTableRoleCellRenderer());
    20         col.setCellEditor(new MemberRoleCellEditor());
     22        col.setCellEditor(new MemberRoleCellEditor(ds));
    2123        addColumn(col);
    2224
  • trunk/src/org/openstreetmap/josm/gui/preferences/TaggingPresetPreference.java

    r2946 r3210  
    2121import org.openstreetmap.josm.gui.tagging.TaggingPresetMenu;
    2222import org.openstreetmap.josm.gui.tagging.TaggingPresetSeparator;
    23 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache;
     23import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
    2424import org.openstreetmap.josm.tools.GBC;
    2525
     
    8888        else
    8989        {
    90             AutoCompletionCache.cachePresets(taggingPresets);
     90            AutoCompletionManager.cachePresets(taggingPresets);
    9191            HashMap<TaggingPresetMenu,JMenu> submenus = new HashMap<TaggingPresetMenu,JMenu>();
    9292            for (final TaggingPreset p : taggingPresets)
  • trunk/src/org/openstreetmap/josm/gui/tagging/TagCellEditor.java

    r3083 r3210  
    99import javax.swing.table.TableCellEditor;
    1010
    11 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache;
     11import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
    1212import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
     13import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
    1314
    1415/**
     
    2627
    2728    /** the cache of auto completion items derived from the current JOSM data set */
    28     protected AutoCompletionCache acCache = null;
     29    protected AutoCompletionManager autocomplete = null;
    2930
    3031    /** user input is matched against this list of auto completion items */
     
    3637    public TagCellEditor() {
    3738        editor = new AutoCompletingTextField();
    38         acCache = new AutoCompletionCache();
    3939    }
    4040
     
    5858        // add the list of keys in the current data set
    5959        //
    60         acCache.populateWithKeys(autoCompletionList, true);
     60        autocomplete.populateWithKeys(autoCompletionList);
    6161
    6262        // remove the keys already present in the current tag model
     
    8383            return;
    8484        }
    85         acCache.populateWithTagValues(autoCompletionList, forKey, false);
     85        autoCompletionList.clear();
     86        autocomplete.populateWithTagValues(autoCompletionList, forKey);
    8687    }
    8788
     
    137138    }
    138139
    139     public void setAutoCompletionCache(AutoCompletionCache acCache) {
    140         this.acCache = acCache;
     140    public void setAutoCompletionManager(AutoCompletionManager autocomplete) {
     141        this.autocomplete = autocomplete;
    141142    }
    142143
  • trunk/src/org/openstreetmap/josm/gui/tagging/TagEditorPanel.java

    r3141 r3210  
    1414
    1515import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    16 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache;
    1716import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
     17import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
    1818import org.openstreetmap.josm.tools.CheckParameterUtil;
    1919
     
    3333    private TagTable tagTable;
    3434
    35     private AutoCompletionCache acCache;
     35    private AutoCompletionManager autocomplete;
    3636    private AutoCompletionList acList;
    3737
     
    141141    public void initAutoCompletion(OsmDataLayer layer) throws IllegalArgumentException{
    142142        CheckParameterUtil.ensureParameterNotNull(layer, "layer");
    143         // initialize the autocompletion infrastructure
    144         //
    145         acCache = AutoCompletionCache.getCacheForLayer(layer);
    146         acCache.initFromDataSet();
     143
     144        autocomplete = layer.data.getAutoCompletionManager();
    147145        acList = new AutoCompletionList();
    148146
    149147        TagCellEditor editor = ((TagCellEditor) tagTable.getColumnModel().getColumn(0).getCellEditor());
    150         editor.setAutoCompletionCache(acCache);
     148        editor.setAutoCompletionManager(autocomplete);
    151149        editor.setAutoCompletionList(acList);
    152150        editor = ((TagCellEditor) tagTable.getColumnModel().getColumn(1).getCellEditor());
    153         editor.setAutoCompletionCache(acCache);
     151        editor.setAutoCompletionManager(autocomplete);
    154152        editor.setAutoCompletionList(acList);
    155153    }
  • trunk/src/org/openstreetmap/josm/gui/tagging/TagTable.java

    r3141 r3210  
    4141
    4242import org.openstreetmap.josm.gui.dialogs.relation.RunnableAction;
    43 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache;
     43import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
    4444import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
    4545import org.openstreetmap.josm.tools.ImageProvider;
     
    406406    }
    407407
    408     public void setAutoCompletionCache(AutoCompletionCache acCache) {
    409         if (acCache == null) {
    410             logger.warning("argument acCache should not be null. Aborting.");
     408    public void setAutoCompletionManager(AutoCompletionManager autocomplete) {
     409        if (autocomplete == null) {
     410            logger.warning("argument autocomplete should not be null. Aborting.");
    411411            return;
    412412        }
    413413        if (editor != null) {
    414             editor.setAutoCompletionCache(acCache);
     414            editor.setAutoCompletionManager(autocomplete);
    415415        }
    416416    }
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r3024 r3210  
    5151import org.openstreetmap.josm.gui.layer.Layer;
    5252import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    53 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache;
     53import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
    5454import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPritority;
    5555import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
     56import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
    5657import org.openstreetmap.josm.io.MirroredInputStream;
    5758import org.openstreetmap.josm.tools.GBC;
     
    8283            if (layer == null) return;
    8384            AutoCompletionList list  = new AutoCompletionList();
    84             AutoCompletionCache.getCacheForLayer(Main.main.getEditLayer()).populateWithTagValues(list, key, false);
     85            Main.main.getEditLayer().data.getAutoCompletionManager().populateWithTagValues(list, key);
    8586            field.setAutoCompletionList(list);
    8687        }
     
    696697            return null;
    697698        OsmDataLayer layer = Main.main.getEditLayer();
    698         if (layer != null) {
    699             AutoCompletionCache.getCacheForLayer(layer).initFromDataSet();
    700         }
    701699        PresetPanel p = new PresetPanel();
    702700        LinkedList<Item> l = new LinkedList<Item>();
  • trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletingComboBox.java

    r3208 r3210  
    11// License: GPL. Copyright 2007 by Immanuel Scholz and others
    2 package org.openstreetmap.josm.gui.widgets;
     2package org.openstreetmap.josm.gui.tagging.ac;
    33
    44import java.awt.event.FocusEvent;
     
    1717 * @author guilhem.bonnefille@gmail.com
    1818 */
    19 public class AutoCompleteComboBox extends JComboBox {
     19public class AutoCompletingComboBox extends JComboBox {
    2020
    2121    private boolean autocompleteEnabled = true;
     
    2626     * Inspired by http://www.orbital-computer.de/JComboBox/
    2727     */
    28     class AutoCompleteComboBoxDocument extends PlainDocument {
     28    class AutoCompletingComboBoxDocument extends PlainDocument {
    2929        private JComboBox comboBox;
    3030        private boolean selecting = false;
    3131
    32         public AutoCompleteComboBoxDocument(final JComboBox comboBox) {
     32        public AutoCompletingComboBoxDocument(final JComboBox comboBox) {
    3333            this.comboBox = comboBox;
    3434        }
     
    9898    }
    9999
    100     public AutoCompleteComboBox() {
     100    public AutoCompletingComboBox() {
    101101        final JTextComponent editor = (JTextComponent) this.getEditor().getEditorComponent();
    102         editor.setDocument(new AutoCompleteComboBoxDocument(this));
     102        editor.setDocument(new AutoCompletingComboBoxDocument(this));
    103103        editor.addFocusListener(
    104104                new FocusListener() {
  • trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletingTextField.java

    r3208 r3210  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.gui.tagging;
     2package org.openstreetmap.josm.gui.tagging.ac;
    33
    44import java.awt.Component;
  • trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionList.java

    r3083 r3210  
     1// License: GPL. For details, see LICENSE file.
    12package org.openstreetmap.josm.gui.tagging.ac;
    23
     
    297298        System.out.println("---------------------------------");
    298299        for (AutoCompletionListItem item: list) {
    299             System.out.println(item.getValue());
     300            System.out.println(item);
    300301        }
    301302        System.out.println("---------------------------------");
  • trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionListItem.java

    r3083 r3210  
    7272    @Override public String toString() {
    7373        StringBuilder sb = new StringBuilder();
    74         sb.append("<AutoCompletionItemPritority: ");
    75         sb.append("value='");
     74        sb.append("<val='");
    7675        sb.append(value);
    7776        sb.append("',");
    78         sb.append("priority='");
    7977        sb.append(priority.toString());
    80         sb.append("'>");
     78        sb.append(">");
    8179        return sb.toString();
    8280    }
  • trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionManager.java

    r3208 r3210  
     1// License: GPL. For details, see LICENSE file.
    12package org.openstreetmap.josm.gui.tagging.ac;
    23
    34import java.util.ArrayList;
    45import java.util.Collection;
    5 import java.util.HashMap;
     6import java.util.Comparator;
     7import java.util.Collections;
    68import java.util.HashSet;
    79import java.util.List;
     10import java.util.Map;
     11import java.util.Map.Entry;
    812import java.util.Set;
     13import java.util.TreeSet;
    914import java.util.logging.Logger;
    1015
     
    1318import org.openstreetmap.josm.data.osm.Relation;
    1419import org.openstreetmap.josm.data.osm.RelationMember;
     20import org.openstreetmap.josm.data.osm.DataSet;
     21import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
     22import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
     23import org.openstreetmap.josm.data.osm.event.DataSetListener;
     24import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
     25import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
     26import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
     27import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
     28import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
     29import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
    1530import org.openstreetmap.josm.gui.MapView;
    1631import org.openstreetmap.josm.gui.layer.Layer;
     
    2035
    2136/**
    22  * AutoCompletionCache temporarily holds a cache of keys with a list of
     37 * AutoCompletionManager holds a cache of keys with a list of
    2338 * possible auto completion values for each key.
    2439 *
    25  * The cache can initialize itself from the current JOSM data set such that
     40 * Each DataSet is assigned one AutoCompletionManager instance such that
    2641 * <ol>
    2742 *   <li>any key used in a tag in the data set is part of the key list in the cache</li>
     
    3550 * field is not efficient enough, hence this cache.
    3651 *
     52 * TODO: respect the relation type for member role autocompletion
    3753 */
    38 public class AutoCompletionCache {
    39     @SuppressWarnings("unused")
    40     static private final Logger logger = Logger.getLogger(AutoCompletionCache.class.getName());
    41 
    42     private static HashMap<OsmDataLayer, AutoCompletionCache> caches;
    43 
    44     static {
    45         caches = new HashMap<OsmDataLayer, AutoCompletionCache>();
    46         MapView.addLayerChangeListener(new MapView.LayerChangeListener() {
    47             public void activeLayerChange(Layer oldLayer, Layer newLayer) {
    48                 // do nothing
    49             }
    50 
    51             public void layerAdded(Layer newLayer) {
    52                 // do noting
    53             }
    54 
    55             public void layerRemoved(Layer oldLayer) {
    56                 if (oldLayer instanceof OsmDataLayer) {
    57                     caches.remove(oldLayer);
    58                 }
    59             }
    60         }
    61         );
    62     }
    63 
    64     static public AutoCompletionCache getCacheForLayer(OsmDataLayer layer) {
    65         AutoCompletionCache cache = caches.get(layer);
    66         if (cache == null) {
    67             cache = new AutoCompletionCache(layer);
    68             caches.put(layer, cache);
    69         }
    70         return cache;
    71     }
    72 
    73     /** the cached tags given by a tag key and a list of values for this tag */
    74     private MultiMap<String, String> tagCache;
    75     /**  the layer this cache is built for */
    76     private OsmDataLayer layer;
    77     /** the same as tagCache but for the preset keys and values */
    78     private static MultiMap<String, String> presetTagCache = new MultiMap<String, String>();
    79     /** the cached list of member roles */
    80     private  Set<String> roleCache;
    81 
    82     /**
    83      * constructor
    84      */
    85     public AutoCompletionCache(OsmDataLayer layer) {
     54public class AutoCompletionManager implements DataSetListener {
     55
     56    /** If the dirty flag is set true, a rebuild is necessary. */
     57    protected boolean dirty;
     58    /** The data set that is managed */
     59    protected DataSet ds;
     60
     61    /**
     62     * the cached tags given by a tag key and a list of values for this tag
     63     * only accessed by getTagCache(), rebuild() and cachePrimitiveTags()
     64     * use getTagCache() accessor
     65     */
     66    protected MultiMap<String, String> tagCache;
     67    /**
     68     * the same as tagCache but for the preset keys and values
     69     * can be accessed directly
     70     */
     71    protected static MultiMap<String, String> presetTagCache = new MultiMap<String, String>();
     72    /**
     73     * the cached list of member roles
     74     * only accessed by getRoleCache(), rebuild() and cacheRelationMemberRoles()
     75     * use getRoleCache() accessor
     76     */
     77    protected  Set<String> roleCache;
     78
     79    public AutoCompletionManager(DataSet ds) {
     80        this.ds = ds;
     81        dirty = true;
     82    }
     83
     84    protected MultiMap<String, String> getTagCache() {
     85        if (dirty) {
     86            rebuild();
     87            dirty = false;
     88        }
     89        return tagCache;
     90    }
     91
     92    protected Set<String> getRoleCache() {
     93        if (dirty) {
     94            rebuild();
     95            dirty = false;
     96        }
     97        return roleCache;
     98    }
     99
     100    /**
     101     * initializes the cache from the primitives in the dataset
     102     *
     103     */
     104    protected void rebuild() {
    86105        tagCache = new MultiMap<String, String>();
    87106        roleCache = new HashSet<String>();
    88         this.layer = layer;
    89     }
    90 
    91     public AutoCompletionCache() {
    92         this(null);
     107        cachePrimitives(ds.allNonDeletedCompletePrimitives());
     108    }
     109
     110    protected void cachePrimitives(Collection<? extends OsmPrimitive> primitives) {
     111        for (OsmPrimitive primitive : primitives) {
     112            cachePrimitiveTags(primitive);
     113            if (primitive instanceof Relation) {
     114                cacheRelationMemberRoles((Relation) primitive);
     115            }
     116        }
    93117    }
    94118
     
    99123     * @param primitive an OSM primitive
    100124     */
    101     protected void cachePrimitive(OsmPrimitive primitive) {
     125    protected void cachePrimitiveTags(OsmPrimitive primitive) {
    102126        for (String key: primitive.keySet()) {
    103127            String value = primitive.get(key);
     
    113137    protected void cacheRelationMemberRoles(Relation relation){
    114138        for (RelationMember m: relation.getMembers()) {
    115             if (m.hasRole() && !roleCache.contains(m.getRole())) {
     139            if (m.hasRole()) {
    116140                roleCache.add(m.getRole());
    117141            }
    118         }
    119     }
    120 
    121     /**
    122      * initializes the cache from the primitives in the dataset of
    123      * {@see #layer}
    124      *
    125      */
    126     public void initFromDataSet() {
    127         tagCache = new MultiMap<String, String>();
    128         if (layer == null)
    129             return;
    130         Collection<OsmPrimitive> ds = layer.data.allNonDeletedPrimitives();
    131         for (OsmPrimitive primitive : ds) {
    132             cachePrimitive(primitive);
    133         }
    134         for (Relation relation : layer.data.getRelations()) {
    135             if (relation.isIncomplete() || relation.isDeleted()) {
    136                 continue;
    137             }
    138             cacheRelationMemberRoles(relation);
    139142        }
    140143    }
     
    187190     */
    188191    protected List<String> getDataKeys() {
    189         return new ArrayList<String>(tagCache.keySet());
     192        return new ArrayList<String>(getTagCache().keySet());
    190193    }
    191194
     
    194197    }
    195198
     199    public TreeSet<String> getKeys(Comparator<String> c) {
     200        TreeSet<String> ret = new TreeSet<String>(c);
     201        ret.addAll(getDataKeys());
     202        ret.addAll(getPresetKeys());
     203        return ret;
     204    }
     205
    196206    /**
    197207     * replies the auto completion values allowed for a specific key. Replies
     
    202212     */
    203213    protected List<String> getDataValues(String key) {
    204         return new ArrayList<String>(tagCache.getValues(key));
     214        return new ArrayList<String>(getTagCache().getValues(key));
    205215    }
    206216
     
    209219    }
    210220
     221    public TreeSet<String> getValues(String key, Comparator<String> c) {
     222        TreeSet<String> ret = new TreeSet<String>(c);
     223        ret.addAll(getDataValues(key));
     224        ret.addAll(getPresetValues(key));
     225        return ret;
     226    }
     227
    211228    /**
    212229     * Replies the list of member roles
     
    215232     */
    216233    public List<String> getMemberRoles() {
    217         return new ArrayList<String>(roleCache);
     234        return new ArrayList<String>(getRoleCache());
    218235    }
    219236
     
    226243    public void populateWithMemberRoles(AutoCompletionList list) {
    227244        list.clear();
    228         list.add(roleCache, AutoCompletionItemPritority.IS_IN_DATASET);
     245        list.add(getRoleCache(), AutoCompletionItemPritority.IS_IN_DATASET);
    229246    }
    230247
     
    238255     * in the list by the tag values
    239256     */
    240     public void populateWithTagValues(AutoCompletionList list, String key, boolean append) {
    241         if (!append) {
    242             list.clear();
    243         }
     257    public void populateWithTagValues(AutoCompletionList list, String key) {
    244258        list.add(getDataValues(key), AutoCompletionItemPritority.IS_IN_DATASET);
    245259        list.add(getPresetValues(key), AutoCompletionItemPritority.IS_IN_STANDARD);
     
    254268     * in the list by the keys in the cache
    255269     */
    256     public void populateWithKeys(AutoCompletionList list, boolean append) {
    257         if (!append) {
    258             list.clear();
    259         }
     270    public void populateWithKeys(AutoCompletionList list) {
    260271        list.add(getDataKeys(), AutoCompletionItemPritority.IS_IN_DATASET);
    261272        list.add(getPresetKeys(), AutoCompletionItemPritority.IS_IN_STANDARD);
    262273    }
     274
     275    /*********************************************************
     276     * Implementation of the DataSetListener interface
     277     *
     278     **/
     279
     280    public void primtivesAdded(PrimitivesAddedEvent event) {
     281        cachePrimitives(event.getPrimitives());
     282    }
     283
     284    public void primtivesRemoved(PrimitivesRemovedEvent event) {
     285        dirty = true;
     286    }
     287
     288    public void tagsChanged(TagsChangedEvent event) {
     289        Map<String, String> newKeys = event.getPrimitive().getKeys();
     290        Map<String, String> oldKeys = event.getOriginalKeys();
     291
     292        if (!newKeys.keySet().containsAll(oldKeys.keySet())) {
     293            // Some keys removed, might be the last instance of key, rebuild necessary
     294            dirty = true;
     295        } else {
     296            for (Entry<String, String> oldEntry: oldKeys.entrySet()) {
     297                if (!oldEntry.getValue().equals(newKeys.get(oldEntry.getKey()))) {
     298                    // Value changed, might be last instance of value, rebuild necessary
     299                    dirty = true;
     300                    return;
     301                }
     302            }
     303            cachePrimitives(Collections.singleton(event.getPrimitive()));
     304        }
     305    }
     306
     307    public void nodeMoved(NodeMovedEvent event) {/* ignored */}
     308
     309    public void wayNodesChanged(WayNodesChangedEvent event) {/* ignored */}
     310
     311    public void relationMembersChanged(RelationMembersChangedEvent event) {
     312        dirty = true; // TODO: not necessary to rebuid if a member is added
     313    }
     314
     315    public void otherDatasetChange(AbstractDatasetChangedEvent event) {/* ignored */}
     316
     317    public void dataChanged(DataChangedEvent event) {
     318        dirty = true;
     319    }
    263320}
  • trunk/src/org/openstreetmap/josm/gui/widgets/HistoryComboBox.java

    r3083 r3210  
    66import javax.swing.text.JTextComponent;
    77
    8 public class HistoryComboBox extends AutoCompleteComboBox {
     8import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox;
     9
     10public class HistoryComboBox extends AutoCompletingComboBox {
    911    private ComboBoxHistory model;
    1012
Note: See TracChangeset for help on using the changeset viewer.