Changeset 10457 in josm


Ignore:
Timestamp:
2016-06-22T21:45:43+02:00 (3 years ago)
Author:
Don-vip
Message:

fix #13033 - JoinAreasActionTest: Do not set status line (patch by michael2402) - gsoc-core

Location:
trunk
Files:
3 edited

Legend:

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

    r10446 r10457  
    500500                if (ds != null) {
    501501                    ds.setSelected(allWays);
    502                     Main.map.mapView.repaint();
    503502                }
    504503            } else {
  • trunk/src/org/openstreetmap/josm/actions/search/SearchAction.java

    r10446 r10457  
    530530
    531531    static void search(SearchSetting s) {
    532         SearchTask.newSearchTask(s).run();
     532        SearchTask.newSearchTask(s, new SelectSearchReceiver()).run();
     533    }
     534
     535    /**
     536     * Performs the search specified by the search string {@code search} and the search mode {@code mode} and returns the result of the search.
     537     *
     538     * @param search the search string to use
     539     * @param mode the search mode to use
     540     * @return The result of the search.
     541     * @since 10457
     542     */
     543    public static Collection<OsmPrimitive> searchAndReturn(String search, SearchMode mode) {
     544        final SearchSetting searchSetting = new SearchSetting();
     545        searchSetting.text = search;
     546        searchSetting.mode = mode;
     547        CapturingSearchReceiver receiver = new CapturingSearchReceiver();
     548        SearchTask.newSearchTask(searchSetting, receiver).run();
     549        return receiver.result;
     550    }
     551
     552    /**
     553     * Interfaces implementing this may receive the result of the current search.
     554     * @author Michael Zangl
     555     * @since 10457
     556     */
     557    interface SearchReceiver {
     558        /**
     559         * Receive the search result
     560         * @param ds The data set searched on.
     561         * @param result The result collection, including the initial collection.
     562         * @param foundMatches The number of matches added to the result.
     563         * @param setting The setting used.
     564         */
     565        void receiveSearchResult(DataSet ds, Collection<OsmPrimitive> result, int foundMatches, SearchSetting setting);
     566    }
     567
     568    /**
     569     * Select the search result and display a status text for it.
     570     */
     571    private static class SelectSearchReceiver implements SearchReceiver {
     572
     573        @Override
     574        public void receiveSearchResult(DataSet ds, Collection<OsmPrimitive> result, int foundMatches, SearchSetting setting) {
     575            ds.setSelected(result);
     576            if (foundMatches == 0) {
     577                final String msg;
     578                final String text = Utils.shortenString(setting.text, MAX_LENGTH_SEARCH_EXPRESSION_DISPLAY);
     579                if (setting.mode == SearchMode.replace) {
     580                    msg = tr("No match found for ''{0}''", text);
     581                } else if (setting.mode == SearchMode.add) {
     582                    msg = tr("Nothing added to selection by searching for ''{0}''", text);
     583                } else if (setting.mode == SearchMode.remove) {
     584                    msg = tr("Nothing removed from selection by searching for ''{0}''", text);
     585                } else if (setting.mode == SearchMode.in_selection) {
     586                    msg = tr("Nothing found in selection by searching for ''{0}''", text);
     587                } else {
     588                    msg = null;
     589                }
     590                Main.map.statusLine.setHelpText(msg);
     591                JOptionPane.showMessageDialog(
     592                        Main.parent,
     593                        msg,
     594                        tr("Warning"),
     595                        JOptionPane.WARNING_MESSAGE
     596                );
     597            } else {
     598                Main.map.statusLine.setHelpText(tr("Found {0} matches", foundMatches));
     599            }
     600        }
     601    }
     602
     603    /**
     604     * This class stores the result of the search in a local variable.
     605     * @author Michael Zangl
     606     */
     607    private static final class CapturingSearchReceiver implements SearchReceiver {
     608        private Collection<OsmPrimitive> result;
     609
     610        @Override
     611        public void receiveSearchResult(DataSet ds, Collection<OsmPrimitive> result, int foundMatches,
     612                SearchSetting setting) {
     613                    this.result = result;
     614        }
    533615    }
    534616
     
    540622        private boolean canceled;
    541623        private int foundMatches;
    542 
    543         private SearchTask(DataSet ds, SearchSetting setting, Collection<OsmPrimitive> selection, Predicate<OsmPrimitive> predicate) {
     624        private SearchReceiver resultReceiver;
     625
     626        private SearchTask(DataSet ds, SearchSetting setting, Collection<OsmPrimitive> selection, Predicate<OsmPrimitive> predicate,
     627                SearchReceiver resultReceiver) {
    544628            super(tr("Searching"));
    545629            this.ds = ds;
     
    547631            this.selection = selection;
    548632            this.predicate = predicate;
    549         }
    550 
    551         static SearchTask newSearchTask(SearchSetting setting) {
     633            this.resultReceiver = resultReceiver;
     634        }
     635
     636        static SearchTask newSearchTask(SearchSetting setting, SearchReceiver resultReceiver) {
    552637            final DataSet ds = Main.getLayerManager().getEditDataSet();
     638            return newSearchTask(setting, ds, resultReceiver);
     639        }
     640
     641        /**
     642         * Create a new search task for the given search setting.
     643         * @param setting The setting to use
     644         * @param ds The data set to search on
     645         * @return A new search task.
     646         */
     647        private static SearchTask newSearchTask(SearchSetting setting, final DataSet ds, SearchReceiver resultReceiver) {
    553648            final Collection<OsmPrimitive> selection = new HashSet<>(ds.getAllSelected());
    554649            return new SearchTask(ds, setting, selection, new Predicate<OsmPrimitive>() {
     
    557652                    return ds.isSelected(o);
    558653                }
    559             });
     654            }, resultReceiver);
    560655        }
    561656
     
    614709                        tr("Error"),
    615710                        JOptionPane.ERROR_MESSAGE
    616 
    617711                );
    618712            }
     
    624718                return;
    625719            }
    626             ds.setSelected(selection);
    627             if (foundMatches == 0) {
    628                 final String msg;
    629                 final String text = Utils.shortenString(setting.text, MAX_LENGTH_SEARCH_EXPRESSION_DISPLAY);
    630                 if (setting.mode == SearchMode.replace) {
    631                     msg = tr("No match found for ''{0}''", text);
    632                 } else if (setting.mode == SearchMode.add) {
    633                     msg = tr("Nothing added to selection by searching for ''{0}''", text);
    634                 } else if (setting.mode == SearchMode.remove) {
    635                     msg = tr("Nothing removed from selection by searching for ''{0}''", text);
    636                 } else if (setting.mode == SearchMode.in_selection) {
    637                     msg = tr("Nothing found in selection by searching for ''{0}''", text);
    638                 } else {
    639                     msg = null;
    640                 }
    641                 Main.map.statusLine.setHelpText(msg);
    642                 JOptionPane.showMessageDialog(
    643                         Main.parent,
    644                         msg,
    645                         tr("Warning"),
    646                         JOptionPane.WARNING_MESSAGE
    647                 );
    648             } else {
    649                 Main.map.statusLine.setHelpText(tr("Found {0} matches", foundMatches));
    650             }
     720            resultReceiver.receiveSearchResult(ds, selection, foundMatches, setting);
    651721        }
    652722    }
  • trunk/test/unit/org/openstreetmap/josm/actions/JoinAreasActionTest.java

    r10436 r10457  
    66import java.io.IOException;
    77import java.io.InputStream;
     8import java.util.Collection;
    89
    910import org.junit.BeforeClass;
     
    1415import org.openstreetmap.josm.actions.search.SearchAction;
    1516import org.openstreetmap.josm.data.osm.DataSet;
     17import org.openstreetmap.josm.data.osm.OsmPrimitive;
     18import org.openstreetmap.josm.data.osm.Way;
    1619import org.openstreetmap.josm.gui.layer.Layer;
    1720import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1821import org.openstreetmap.josm.io.IllegalDataException;
    1922import org.openstreetmap.josm.io.OsmReader;
     23import org.openstreetmap.josm.tools.Utils;
    2024
    2125/**
     
    6569            Layer layer = new OsmDataLayer(ds, null, null);
    6670            Main.getLayerManager().addLayer(layer);
    67             try {
    68                 for (String ref : new String[]{"A", "B", "C", "D", "E"}) {
    69                     System.out.print("Joining ways " + ref);
    70                     SearchAction.search("type:way ref="+ref, SearchAction.SearchMode.replace);
    71                     assertEquals(2, ds.getSelectedWays().size());
    72                     Main.main.menu.joinAreas.join(ds.getSelectedWays());
    73                     assertEquals(1, ds.getSelectedWays().size());
    74                     System.out.println(" ==> OK");
    75                 }
    76             } finally {
    77                 // Ensure we clean the place before leaving, even if test fails.
    78                 Main.getLayerManager().removeLayer(layer);
     71            for (String ref : new String[]{"A", "B", "C", "D", "E"}) {
     72                System.out.print("Joining ways " + ref);
     73                Collection<OsmPrimitive> found = SearchAction.searchAndReturn("type:way ref="+ref, SearchAction.SearchMode.replace);
     74                assertEquals(2, found.size());
     75
     76                Main.main.menu.joinAreas.join(Utils.filteredCollection(found, Way.class));
     77
     78                Collection<OsmPrimitive> found2 = SearchAction.searchAndReturn("type:way ref="+ref, SearchAction.SearchMode.replace);
     79                assertEquals(1, found2.size());
     80                System.out.println(" ==> OK");
    7981            }
    8082        }
Note: See TracChangeset for help on using the changeset viewer.