Changeset 10457 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2016-06-22T21:45:43+02:00 (8 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/actions
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/JoinAreasAction.java
r10446 r10457 500 500 if (ds != null) { 501 501 ds.setSelected(allWays); 502 Main.map.mapView.repaint();503 502 } 504 503 } else { -
trunk/src/org/openstreetmap/josm/actions/search/SearchAction.java
r10446 r10457 530 530 531 531 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 } 533 615 } 534 616 … … 540 622 private boolean canceled; 541 623 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) { 544 628 super(tr("Searching")); 545 629 this.ds = ds; … … 547 631 this.selection = selection; 548 632 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) { 552 637 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) { 553 648 final Collection<OsmPrimitive> selection = new HashSet<>(ds.getAllSelected()); 554 649 return new SearchTask(ds, setting, selection, new Predicate<OsmPrimitive>() { … … 557 652 return ds.isSelected(o); 558 653 } 559 } );654 }, resultReceiver); 560 655 } 561 656 … … 614 709 tr("Error"), 615 710 JOptionPane.ERROR_MESSAGE 616 617 711 ); 618 712 } … … 624 718 return; 625 719 } 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); 651 721 } 652 722 }
Note:
See TracChangeset
for help on using the changeset viewer.