Changeset 8979 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2015-11-02T21:19:19+01:00 (8 years ago)
Author:
simon04
Message:

see #9463 - Refactoring

  • Refactor SearchCompiler to work with Tagged instances
  • Extract CompileSearchTextDecorator from RelationListDialog
Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java

    r8971 r8979  
    2828import org.openstreetmap.josm.data.osm.Relation;
    2929import org.openstreetmap.josm.data.osm.RelationMember;
     30import org.openstreetmap.josm.data.osm.Tagged;
    3031import org.openstreetmap.josm.data.osm.Way;
    3132import org.openstreetmap.josm.gui.mappaint.Environment;
     
    233234
    234235    /**
    235      * Base class for all search operators.
     236     * Base class for all search criteria. If the criterion only depends on an object's tags,
     237     * inherit from {@link org.openstreetmap.josm.actions.search.SearchCompiler.TaggedMatch}.
    236238     */
    237239    public abstract static class Match implements Predicate<OsmPrimitive> {
    238240
     241        /**
     242         * Tests whether the primitive matches this criterion.
     243         * @param osm the primitive to test
     244         * @return true if the primitive matches this criterion
     245         */
    239246        public abstract boolean match(OsmPrimitive osm);
     247
     248        /**
     249         * Tests whether the tagged object matches this criterion.
     250         * @param tagged the tagged object to test
     251         * @return true if the tagged object matches this criterion
     252         */
     253        public boolean match(Tagged tagged) {
     254            return false;
     255        }
    240256
    241257        /**
     
    271287    }
    272288
     289    public abstract static class TaggedMatch extends Match {
     290
     291        @Override
     292        public abstract boolean match(Tagged tags);
     293
     294        @Override
     295        public final boolean match(OsmPrimitive osm) {
     296            return match((Tagged) osm);
     297        }
     298    }
     299
    273300    /**
    274301     * A unary search operator which may take data parameters.
     
    318345     * Matches every OsmPrimitive.
    319346     */
    320     public static class Always extends Match {
     347    public static class Always extends TaggedMatch {
    321348        /** The unique instance/ */
    322349        public static final Always INSTANCE = new Always();
    323350        @Override
    324         public boolean match(OsmPrimitive osm) {
     351        public boolean match(Tagged osm) {
    325352            return true;
    326353        }
     
    330357     * Never matches any OsmPrimitive.
    331358     */
    332     public static class Never extends Match {
    333         @Override
    334         public boolean match(OsmPrimitive osm) {
     359    public static class Never extends TaggedMatch {
     360        @Override
     361        public boolean match(Tagged osm) {
    335362            return false;
    336363        }
     
    351378
    352379        @Override
     380        public boolean match(Tagged osm) {
     381            return !match.match(osm);
     382        }
     383
     384        @Override
    353385        public String toString() {
    354386            return "!" + match;
     
    363395     * Matches if the value of the corresponding key is ''yes'', ''true'', ''1'' or ''on''.
    364396     */
    365     private static class BooleanMatch extends Match {
     397    private static class BooleanMatch extends TaggedMatch {
    366398        private final String key;
    367399        private final boolean defaultValue;
     
    373405
    374406        @Override
    375         public boolean match(OsmPrimitive osm) {
     407        public boolean match(Tagged osm) {
    376408            Boolean ret = OsmUtils.getOsmBoolean(osm.get(key));
    377409            if (ret == null)
     
    401433
    402434        @Override
     435        public boolean match(Tagged osm) {
     436            return lhs.match(osm) && rhs.match(osm);
     437        }
     438
     439        @Override
    403440        public String toString() {
    404441            return lhs + " && " + rhs;
     
    420457
    421458        @Override
     459        public boolean match(Tagged osm) {
     460            return lhs.match(osm) || rhs.match(osm);
     461        }
     462
     463        @Override
    422464        public String toString() {
    423465            return lhs + " || " + rhs;
     
    439481
    440482        @Override
     483        public boolean match(Tagged osm) {
     484            return lhs.match(osm) ^ rhs.match(osm);
     485        }
     486
     487        @Override
    441488        public String toString() {
    442489            return lhs + " ^ " + rhs;
     
    516563     * Matches objects with the given key-value pair.
    517564     */
    518     private static class KeyValue extends Match {
     565    private static class KeyValue extends TaggedMatch {
    519566        private final String key;
    520567        private final Pattern keyPattern;
     
    559606
    560607        @Override
    561         public boolean match(OsmPrimitive osm) {
     608        public boolean match(Tagged osm) {
    562609
    563610            if (keyPattern != null) {
     
    589636                String mv = null;
    590637
    591                 if ("timestamp".equals(key)) {
    592                     mv = DateUtils.fromTimestamp(osm.getRawTimestamp());
     638                if ("timestamp".equals(key) && osm instanceof OsmPrimitive) {
     639                    mv = DateUtils.fromTimestamp(((OsmPrimitive) osm).getRawTimestamp());
    593640                } else {
    594641                    mv = osm.get(key);
     
    623670    }
    624671
    625     public static class ValueComparison extends Match {
     672    public static class ValueComparison extends TaggedMatch {
    626673        private final String key;
    627674        private final String referenceValue;
     
    648695
    649696        @Override
    650         public boolean match(OsmPrimitive osm) {
     697        public boolean match(Tagged osm) {
    651698            final String currentValue = osm.get(key);
    652699            final int compareResult;
     
    676723     * Matches objects with the exact given key-value pair.
    677724     */
    678     public static class ExactKeyValue extends Match {
     725    public static class ExactKeyValue extends TaggedMatch {
    679726
    680727        private enum Mode {
     
    749796
    750797        @Override
    751         public boolean match(OsmPrimitive osm) {
     798        public boolean match(Tagged osm) {
    752799
    753800            if (!osm.hasKeys())
     
    806853     * Match a string in any tags (key or value), with optional regex and case insensitivity.
    807854     */
    808     private static class Any extends Match {
     855    private static class Any extends TaggedMatch {
    809856        private final String search;
    810857        private final Pattern searchRegex;
     
    833880
    834881        @Override
    835         public boolean match(OsmPrimitive osm) {
    836             if (!osm.hasKeys() && osm.getUser() == null)
     882        public boolean match(Tagged osm) {
     883            if (!osm.hasKeys())
    837884                return search.isEmpty();
    838885
  • trunk/src/org/openstreetmap/josm/gui/dialogs/RelationListDialog.java

    r8836 r8979  
    55
    66import java.awt.BorderLayout;
    7 import java.awt.Color;
    87import java.awt.Component;
    98import java.awt.event.ActionEvent;
    109import java.awt.event.KeyEvent;
    1110import java.awt.event.MouseEvent;
     11import java.beans.PropertyChangeEvent;
     12import java.beans.PropertyChangeListener;
    1213import java.util.ArrayList;
    1314import java.util.Arrays;
     
    2930import javax.swing.KeyStroke;
    3031import javax.swing.ListSelectionModel;
    31 import javax.swing.UIManager;
    32 import javax.swing.event.DocumentEvent;
    33 import javax.swing.event.DocumentListener;
    3432import javax.swing.event.ListSelectionEvent;
    3533import javax.swing.event.ListSelectionListener;
     
    7068import org.openstreetmap.josm.gui.util.GuiHelper;
    7169import org.openstreetmap.josm.gui.util.HighlightHelper;
     70import org.openstreetmap.josm.gui.widgets.CompileSearchTextDecorator;
    7271import org.openstreetmap.josm.gui.widgets.DisableShortcutsOnFocusGainedTextField;
    7372import org.openstreetmap.josm.gui.widgets.JosmTextField;
     
    283282        final JosmTextField f = new DisableShortcutsOnFocusGainedTextField();
    284283        f.setToolTipText(tr("Relation list filter"));
    285         f.getDocument().addDocumentListener(new DocumentListener() {
    286 
    287             private void setFilter() {
    288                 try {
    289                     f.setBackground(UIManager.getColor("TextField.background"));
    290                     f.setToolTipText(tr("Relation list filter"));
    291                     model.setFilter(SearchCompiler.compile(filter.getText()));
    292                 } catch (SearchCompiler.ParseError ex) {
    293                     f.setBackground(new Color(255, 224, 224));
    294                     f.setToolTipText(ex.getMessage());
    295                     model.setFilter(new SearchCompiler.Always());
    296                 }
    297             }
    298 
     284        final CompileSearchTextDecorator decorator = CompileSearchTextDecorator.decorate(f);
     285        f.addPropertyChangeListener("filter", new PropertyChangeListener() {
    299286            @Override
    300             public void insertUpdate(DocumentEvent e) {
    301                 setFilter();
    302             }
    303 
    304             @Override
    305             public void removeUpdate(DocumentEvent e) {
    306                 setFilter();
    307             }
    308 
    309             @Override
    310             public void changedUpdate(DocumentEvent e) {
    311                 setFilter();
     287            public void propertyChange(PropertyChangeEvent evt) {
     288                model.setFilter(decorator.getMatch());
    312289            }
    313290        });
Note: See TracChangeset for help on using the changeset viewer.