Changeset 5071 in josm


Ignore:
Timestamp:
2012-03-11T15:02:31+01:00 (12 years ago)
Author:
simon04
Message:

fix #7497 - repair TaggingPresetSearch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetSearchDialog.java

    r4968 r5071  
    3636
    3737import org.openstreetmap.josm.Main;
     38import org.openstreetmap.josm.data.SelectionChangedListener;
     39import org.openstreetmap.josm.data.osm.DataSet;
    3840import org.openstreetmap.josm.data.osm.Node;
    3941import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    5254import org.openstreetmap.josm.gui.tagging.TaggingPreset.Text;
    5355
    54 public class TaggingPresetSearchDialog extends ExtendedDialog {
     56public class TaggingPresetSearchDialog extends ExtendedDialog implements SelectionChangedListener {
    5557
    5658    private static final int CLASSIFICATION_IN_FAVORITES = 300;
     
    205207    private JCheckBox ckSearchInTags;
    206208    private final EnumSet<PresetType> typesInSelection = EnumSet.noneOf(PresetType.class);
     209    private boolean typesInSelectionDirty = true;
    207210    private final List<PresetClasification> classifications = new ArrayList<PresetClasification>();
    208211    private ResultListModel lsResultModel = new ResultListModel();
     
    210213    private TaggingPresetSearchDialog() {
    211214        super(Main.parent, tr("Presets"), new String[] {tr("Select"), tr("Cancel")});
    212         getTypesInSelection();
     215        DataSet.addSelectionListener(this);
    213216
    214217        for (TaggingPreset preset: TaggingPresetPreference.taggingPresets) {
     
    221224
    222225        build();
    223         filterPresets("");
     226        filterPresets();
     227    }
     228
     229    @Override
     230    public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
     231        typesInSelectionDirty = true;
    224232    }
    225233
    226234    @Override
    227235    public ExtendedDialog showDialog() {
     236
     237        ckOnlyApplicable.setEnabled(!getTypesInSelection().isEmpty());
     238        ckOnlyApplicable.setSelected(!getTypesInSelection().isEmpty() && ONLY_APPLICABLE.get());
     239        edSearchText.setText("");
     240        filterPresets();
     241
    228242        super.showDialog();
    229         edSearchText.setText("");
    230243        lsResult.getSelectionModel().clearSelection();
    231244        return this;
     
    241254            @Override
    242255            public void removeUpdate(DocumentEvent e) {
    243                 filterPresets(edSearchText.getText());
     256                filterPresets();
    244257            }
    245258
    246259            @Override
    247260            public void insertUpdate(DocumentEvent e) {
    248                 filterPresets(edSearchText.getText());
     261                filterPresets();
    249262
    250263            }
     
    252265            @Override
    253266            public void changedUpdate(DocumentEvent e) {
    254                 filterPresets(edSearchText.getText());
     267                filterPresets();
    255268
    256269            }
     
    302315        ckOnlyApplicable.setText(tr("Show only applicable to selection"));
    303316        pnChecks.add(ckOnlyApplicable);
    304 
    305         if (typesInSelection.isEmpty()) {
    306             ckOnlyApplicable.setSelected(false);
    307             ckOnlyApplicable.setEnabled(false);
    308         } else {
    309             ckOnlyApplicable.setSelected(ONLY_APPLICABLE.get());
    310             ckOnlyApplicable.addItemListener(new ItemListener() {
    311                 @Override
    312                 public void itemStateChanged(ItemEvent e) {
    313                     filterPresets(edSearchText.getText());
    314                 }
    315             });
    316         }
     317        ckOnlyApplicable.addItemListener(new ItemListener() {
     318            @Override
     319            public void itemStateChanged(ItemEvent e) {
     320                filterPresets();
     321            }
     322        });
    317323
    318324        ckSearchInTags = new JCheckBox();
     
    322328            @Override
    323329            public void itemStateChanged(ItemEvent e) {
    324                 filterPresets(edSearchText.getText());
     330                filterPresets();
    325331            }
    326332        });
     
    352358     * @param text
    353359     */
    354     private void filterPresets(String text) {
     360    private void filterPresets() {
    355361        //TODO Save favorites to file
    356         text = text.toLowerCase();
     362        String text = edSearchText.getText().toLowerCase();
    357363
    358364        String[] groupWords;
     
    379385                    boolean found = false;
    380386                    for (PresetType type: preset.types) {
    381                         if (typesInSelection.contains(type)) {
     387                        if (getTypesInSelection().contains(type)) {
    382388                            found = true;
    383389                            break;
     
    427433    }
    428434
    429 
    430     private void getTypesInSelection() {
    431         for (OsmPrimitive primitive: Main.main.getCurrentDataSet().getSelected()) {
    432             if (primitive instanceof Node) {
    433                 typesInSelection.add(PresetType.NODE);
    434             } else if (primitive instanceof Way) {
    435                 typesInSelection.add(PresetType.WAY);
    436                 if (((Way)primitive).isClosed()) {
    437                     typesInSelection.add(PresetType.CLOSEDWAY);
    438                 }
    439             } else if (primitive instanceof Relation) {
    440                 typesInSelection.add(PresetType.RELATION);
    441             }
    442         }
     435    private EnumSet<PresetType> getTypesInSelection() {
     436        if (typesInSelectionDirty) {
     437            synchronized (typesInSelection) {
     438                typesInSelectionDirty = false;
     439                typesInSelection.clear();
     440                for (OsmPrimitive primitive : Main.main.getCurrentDataSet().getSelected()) {
     441                    if (primitive instanceof Node) {
     442                        typesInSelection.add(PresetType.NODE);
     443                    } else if (primitive instanceof Way) {
     444                        typesInSelection.add(PresetType.WAY);
     445                        if (((Way) primitive).isClosed()) {
     446                            typesInSelection.add(PresetType.CLOSEDWAY);
     447                        }
     448                    } else if (primitive instanceof Relation) {
     449                        typesInSelection.add(PresetType.RELATION);
     450                    }
     451                }
     452            }
     453        }
     454        return typesInSelection;
    443455    }
    444456
Note: See TracChangeset for help on using the changeset viewer.