Search in sources :

Example 1 with SearchResult

use of net.osmand.search.core.SearchResult in project OsmAnd-tools by osmandapp.

the class OsmExtractionUI method updateSearchResult.

private void updateSearchResult(final JTextField statusField, SearchResultCollection res, boolean addMore) {
    popup.setVisible(false);
    popup.removeAll();
    if (res.getCurrentSearchResults().size() > 0 || addMore) {
        int count = 30;
        if (addMore) {
            JMenuItem mi = new JMenuItem();
            mi.setText("Results " + res.getCurrentSearchResults().size() + ", radius " + res.getPhrase().getRadiusLevel() + " (show more...)");
            mi.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    SearchSettings settings = searchUICore.getPhrase().getSettings();
                    searchUICore.updateSettings(settings.setRadiusLevel(settings.getRadiusLevel() + 1));
                    SearchResultCollection collection = searchUICore.search(statusField.getText(), true, null);
                    updateSearchResult(statusField, collection, false);
                }
            });
            popup.add(mi);
        }
        for (final SearchResult sr : res.getCurrentSearchResults()) {
            count--;
            if (count == 0) {
            // break;
            }
            JMenuItem mi = new JMenuItem();
            LatLon location = res.getPhrase().getLastTokenLocation();
            String locationString = "";
            if (sr.location != null) {
                locationString = ((int) MapUtils.getDistance(location, sr.location)) / 1000.f + " km";
            }
            if (!Algorithms.isEmpty(sr.localeRelatedObjectName)) {
                locationString += " " + sr.localeRelatedObjectName;
                if (sr.distRelatedObjectName != 0) {
                    locationString += " " + (int) (sr.distRelatedObjectName / 1000.f) + " km";
                }
            }
            if (sr.objectType == ObjectType.HOUSE) {
                if (sr.relatedObject instanceof Street) {
                    locationString += " " + ((Street) sr.relatedObject).getCity().getName();
                }
            }
            if (sr.objectType == ObjectType.LOCATION) {
                locationString += " " + osmandRegions.getCountryName(sr.location);
            }
            if (sr.object instanceof Amenity) {
                locationString += " " + ((Amenity) sr.object).getSubType();
                if (((Amenity) sr.object).isClosed()) {
                    locationString += " (CLOSED)";
                }
            }
            mi.setText(sr.localeName + " [" + sr.getFoundWordCount() + ", " + sr.objectType + "] " + locationString);
            mi.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    mapPanel.setStatusField(null);
                    if (sr.location != null) {
                        mapPanel.setLatLon(sr.location.getLatitude(), sr.location.getLongitude());
                        mapPanel.setZoom(sr.preferredZoom);
                    }
                    searchUICore.selectSearchResult(sr);
                    String txt = searchUICore.getPhrase().getText(true);
                    statusField.setText(txt);
                    searchUICore.search(txt, false, null);
                    statusField.requestFocus();
                // statusField.setCaretPosition(statusField.getText().length());
                }
            });
            popup.add(mi);
        }
        // .getCaret().getMagicCaretPosition();
        Point p = statusField.getLocation();
        if (popup.isVisible()) {
            popup.setVisible(true);
        } else {
            popup.show(statusField.getParent(), p.x, p.y + statusField.getHeight() + 4);
        // popup.show();
        }
    }
}
Also used : Amenity(net.osmand.data.Amenity) ActionEvent(java.awt.event.ActionEvent) SearchResult(net.osmand.search.core.SearchResult) Point(java.awt.Point) Point(java.awt.Point) LatLon(net.osmand.data.LatLon) ActionListener(java.awt.event.ActionListener) SearchSettings(net.osmand.search.core.SearchSettings) SearchResultCollection(net.osmand.search.SearchUICore.SearchResultCollection) Street(net.osmand.data.Street) JMenuItem(javax.swing.JMenuItem)

Example 2 with SearchResult

use of net.osmand.search.core.SearchResult in project Osmand by osmandapp.

the class QuickSearchDialogFragment method reloadCategoriesInternal.

private void reloadCategoriesInternal() {
    try {
        if (SearchUICore.isDebugMode()) {
            LOG.info("UI >> Start loading categories");
        }
        SearchResultCollection res = searchUICore.shallowSearch(SearchAmenityTypesAPI.class, "", null);
        if (res != null) {
            List<QuickSearchListItem> rows = new ArrayList<>();
            for (SearchResult sr : res.getCurrentSearchResults()) {
                rows.add(new QuickSearchListItem(app, sr));
            }
            if (OsmandPlugin.getEnabledPlugin(OsmandRasterMapsPlugin.class) != null) {
                rows.add(new QuickSearchButtonListItem(app, R.drawable.ic_world_globe_dark, app.getString(R.string.search_online_address), new OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        startOnlineSearch();
                        mainSearchFragment.getAdapter().clear();
                        updateTabbarVisibility(false);
                        openKeyboard();
                    }
                }));
            }
            rows.add(new QuickSearchButtonListItem(app, R.drawable.ic_action_search_dark, app.getString(R.string.custom_search), new OnClickListener() {

                @Override
                public void onClick(View v) {
                    PoiUIFilter filter = app.getPoiFilters().getCustomPOIFilter();
                    filter.clearFilter();
                    QuickSearchCustomPoiFragment.showDialog(QuickSearchDialogFragment.this, filter.getFilterId());
                }
            }));
            if (categoriesSearchFragment != null) {
                categoriesSearchFragment.updateListAdapter(rows, false);
            }
        }
        if (SearchUICore.isDebugMode()) {
            LOG.info("UI >> Categories loaded");
        }
    } catch (IOException e) {
        e.printStackTrace();
        app.showToastMessage(e.getMessage());
    }
}
Also used : QuickSearchButtonListItem(net.osmand.plus.search.listitems.QuickSearchButtonListItem) ArrayList(java.util.ArrayList) SearchResultCollection(net.osmand.search.SearchUICore.SearchResultCollection) QuickSearchListItem(net.osmand.plus.search.listitems.QuickSearchListItem) SearchMoreItemOnClickListener(net.osmand.plus.search.listitems.QuickSearchMoreListItem.SearchMoreItemOnClickListener) OnClickListener(android.view.View.OnClickListener) SearchResult(net.osmand.search.core.SearchResult) IOException(java.io.IOException) OsmandRasterMapsPlugin(net.osmand.plus.rastermaps.OsmandRasterMapsPlugin) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) PoiUIFilter(net.osmand.plus.poi.PoiUIFilter)

Example 3 with SearchResult

use of net.osmand.search.core.SearchResult in project Osmand by osmandapp.

the class QuickSearchDialogFragment method reloadCitiesInternal.

private void reloadCitiesInternal() {
    if (SearchUICore.isDebugMode()) {
        LOG.info("UI >> Start loading nearest cities");
    }
    startNearestCitySearch();
    runCoreSearch("", false, false, new SearchResultListener() {

        @Override
        public void searchStarted(SearchPhrase phrase) {
        }

        @Override
        public void publish(SearchResultCollection res, boolean append) {
        }

        @Override
        public boolean searchFinished(SearchPhrase phrase) {
            SearchResultCollection res = getResultCollection();
            if (SearchUICore.isDebugMode()) {
                LOG.info("UI >> Nearest cities found: " + (res != null ? res.getCurrentSearchResults().size() : 0));
            }
            final OsmandSettings settings = app.getSettings();
            List<QuickSearchListItem> rows = new ArrayList<>();
            if (SearchUICore.isDebugMode()) {
                LOG.info("UI >> Start last city searching (within nearests)");
            }
            SearchResult lastCity = null;
            if (res != null) {
                citiesLoaded = res.getCurrentSearchResults().size() > 0;
                final long lastCityId = settings.getLastSearchedCity();
                for (SearchResult sr : res.getCurrentSearchResults()) {
                    if (sr.objectType == ObjectType.CITY && ((City) sr.object).getId() == lastCityId) {
                        lastCity = sr;
                        break;
                    }
                }
            }
            if (SearchUICore.isDebugMode()) {
                LOG.info("UI >> Last city found: " + (lastCity != null ? lastCity.localeName : "-"));
            }
            final String lastCityName = lastCity == null ? settings.getLastSearchedCityName() : lastCity.localeName;
            if (!Algorithms.isEmpty(lastCityName)) {
                String selectStreets = app.getString(R.string.select_street);
                String inCityName = app.getString(R.string.shared_string_in_name, lastCityName);
                Spannable spannable = new SpannableString(selectStreets + " " + inCityName);
                boolean light = settings.isLightContent();
                spannable.setSpan(new ForegroundColorSpan(getResources().getColor(light ? R.color.icon_color : R.color.color_white)), selectStreets.length() + 1, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                final SearchResult lastCityFinal = lastCity;
                rows.add(new QuickSearchButtonListItem(app, R.drawable.ic_action_street_name, spannable, new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (lastCityFinal == null) {
                            final long lastCityId = settings.getLastSearchedCity();
                            final LatLon lastCityPoint = settings.getLastSearchedPoint();
                            if (lastCityId != -1 && lastCityPoint != null) {
                                startLastCitySearch(lastCityPoint);
                                if (SearchUICore.isDebugMode()) {
                                    LOG.info("UI >> Start last city searching (standalone)");
                                }
                                runCoreSearch("", false, false, new SearchResultListener() {

                                    boolean cityFound = false;

                                    @Override
                                    public void publish(SearchResultCollection res, boolean append) {
                                        if (res != null) {
                                            for (SearchResult sr : res.getCurrentSearchResults()) {
                                                if (sr.objectType == ObjectType.CITY && ((City) sr.object).getId() == lastCityId) {
                                                    if (SearchUICore.isDebugMode()) {
                                                        LOG.info("UI >> Last city found: " + sr.localeName);
                                                    }
                                                    cityFound = true;
                                                    completeQueryWithObject(sr);
                                                    break;
                                                }
                                            }
                                        }
                                    }

                                    @Override
                                    public void searchStarted(SearchPhrase phrase) {
                                    }

                                    @Override
                                    public boolean searchFinished(SearchPhrase phrase) {
                                        if (!cityFound) {
                                            replaceQueryWithText(lastCityName + " ");
                                        }
                                        return false;
                                    }
                                });
                                restoreSearch();
                            } else {
                                replaceQueryWithText(lastCityName + " ");
                            }
                        } else {
                            completeQueryWithObject(lastCityFinal);
                        }
                        openKeyboard();
                    }
                }));
            }
            rows.add(new QuickSearchButtonListItem(app, R.drawable.ic_action_building_number, app.getString(R.string.select_city), new OnClickListener() {

                @Override
                public void onClick(View v) {
                    searchEditText.setHint(R.string.type_city_town);
                    startCitySearch();
                    updateTabbarVisibility(false);
                    runCoreSearch("", false, false);
                    openKeyboard();
                }
            }));
            rows.add(new QuickSearchButtonListItem(app, R.drawable.ic_action_postcode, app.getString(R.string.select_postcode), new OnClickListener() {

                @Override
                public void onClick(View v) {
                    searchEditText.setHint(R.string.type_postcode);
                    startPostcodeSearch();
                    mainSearchFragment.getAdapter().clear();
                    updateTabbarVisibility(false);
                    openKeyboard();
                }
            }));
            rows.add(new QuickSearchButtonListItem(app, R.drawable.ic_action_marker_dark, app.getString(R.string.coords_search), new OnClickListener() {

                @Override
                public void onClick(View v) {
                    LatLon latLon = searchUICore.getSearchSettings().getOriginalLocation();
                    QuickSearchCoordinatesFragment.showDialog(QuickSearchDialogFragment.this, latLon.getLatitude(), latLon.getLongitude());
                }
            }));
            if (res != null) {
                rows.add(new QuickSearchHeaderListItem(app, app.getString(R.string.nearest_cities), true));
                int limit = 15;
                for (SearchResult sr : res.getCurrentSearchResults()) {
                    if (limit > 0) {
                        rows.add(new QuickSearchListItem(app, sr));
                    }
                    limit--;
                }
            }
            addressSearchFragment.updateListAdapter(rows, false);
            if (SearchUICore.isDebugMode()) {
                LOG.info("UI >> Nearest cities loaded");
            }
            return true;
        }
    });
    restoreSearch();
}
Also used : ForegroundColorSpan(android.text.style.ForegroundColorSpan) QuickSearchButtonListItem(net.osmand.plus.search.listitems.QuickSearchButtonListItem) QuickSearchHeaderListItem(net.osmand.plus.search.listitems.QuickSearchHeaderListItem) SearchResult(net.osmand.search.core.SearchResult) SpannableString(android.text.SpannableString) City(net.osmand.data.City) ImageView(android.widget.ImageView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) SearchPhrase(net.osmand.search.core.SearchPhrase) OsmandSettings(net.osmand.plus.OsmandSettings) SpannableString(android.text.SpannableString) LatLon(net.osmand.data.LatLon) SearchResultCollection(net.osmand.search.SearchUICore.SearchResultCollection) SearchMoreItemOnClickListener(net.osmand.plus.search.listitems.QuickSearchMoreListItem.SearchMoreItemOnClickListener) OnClickListener(android.view.View.OnClickListener) QuickSearchListItem(net.osmand.plus.search.listitems.QuickSearchListItem) ArrayList(java.util.ArrayList) List(java.util.List) Spannable(android.text.Spannable)

Example 4 with SearchResult

use of net.osmand.search.core.SearchResult in project Osmand by osmandapp.

the class QuickSearchDialogFragment method runCoreSearchInternal.

private void runCoreSearchInternal(String text, boolean showQuickResult, boolean searchMore, final SearchResultListener resultListener) {
    searchUICore.search(text, showQuickResult, new ResultMatcher<SearchResult>() {

        SearchResultCollection regionResultCollection = null;

        SearchCoreAPI regionResultApi = null;

        List<SearchResult> results = new ArrayList<>();

        @Override
        public boolean publish(final SearchResult object) {
            if (object.objectType == SEARCH_STARTED) {
                cancelPrev = false;
            }
            if (paused || cancelPrev) {
                if (results.size() > 0) {
                    app.runInUIThread(new Runnable() {

                        @Override
                        public void run() {
                            SearchResultCollection collection = getResultCollection();
                            if (collection != null) {
                                collection.addSearchResults(results, true, true);
                            }
                        }
                    });
                }
                return false;
            }
            switch(object.objectType) {
                case SEARCH_STARTED:
                    if (resultListener != null) {
                        app.runInUIThread(new Runnable() {

                            @Override
                            public void run() {
                                resultListener.searchStarted(object.requiredSearchPhrase);
                            }
                        });
                    }
                    break;
                case SEARCH_FINISHED:
                    app.runInUIThread(new Runnable() {

                        @Override
                        public void run() {
                            if (paused) {
                                return;
                            }
                            searching = false;
                            if (resultListener == null || resultListener.searchFinished(object.requiredSearchPhrase)) {
                                hideProgressBar();
                                addMoreButton(searchUICore.isSearchMoreAvailable(object.requiredSearchPhrase));
                            }
                        }
                    });
                    break;
                case FILTER_FINISHED:
                    if (resultListener != null) {
                        app.runInUIThread(new Runnable() {

                            @Override
                            public void run() {
                                resultListener.publish(searchUICore.getCurrentSearchResult(), false);
                            }
                        });
                    }
                    break;
                case SEARCH_API_FINISHED:
                    final SearchCoreAPI searchApi = (SearchCoreAPI) object.object;
                    final List<SearchResult> apiResults;
                    final SearchPhrase phrase = object.requiredSearchPhrase;
                    final SearchCoreAPI regionApi = regionResultApi;
                    final SearchResultCollection regionCollection = regionResultCollection;
                    final boolean hasRegionCollection = (searchApi == regionApi && regionCollection != null);
                    if (hasRegionCollection) {
                        apiResults = regionCollection.getCurrentSearchResults();
                    } else {
                        apiResults = results;
                    }
                    regionResultApi = null;
                    regionResultCollection = null;
                    results = new ArrayList<>();
                    showApiResults(searchApi, apiResults, phrase, hasRegionCollection, resultListener);
                    break;
                case SEARCH_API_REGION_FINISHED:
                    regionResultApi = (SearchCoreAPI) object.object;
                    final SearchPhrase regionPhrase = object.requiredSearchPhrase;
                    regionResultCollection = new SearchResultCollection(regionPhrase).addSearchResults(results, true, true);
                    showRegionResults(object.file, regionPhrase, regionResultCollection, resultListener);
                    break;
                case PARTIAL_LOCATION:
                    showLocationToolbar();
                    break;
                default:
                    results.add(object);
            }
            return true;
        }

        @Override
        public boolean isCancelled() {
            return paused || cancelPrev;
        }
    });
    if (!searchMore) {
        setResultCollection(null);
        if (!showQuickResult) {
            updateSearchResult(null, false);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) SearchResultCollection(net.osmand.search.SearchUICore.SearchResultCollection) SearchResult(net.osmand.search.core.SearchResult) ArrayList(java.util.ArrayList) List(java.util.List) SearchCoreAPI(net.osmand.search.core.SearchCoreAPI) SearchPhrase(net.osmand.search.core.SearchPhrase)

Example 5 with SearchResult

use of net.osmand.search.core.SearchResult in project Osmand by osmandapp.

the class QuickSearchDialogFragment method completeQueryWithObject.

public void completeQueryWithObject(SearchResult sr) {
    if (sr.object instanceof PoiType && ((PoiType) sr.object).isAdditional()) {
        PoiType additional = (PoiType) sr.object;
        AbstractPoiType parent = additional.getParentType();
        if (parent != null) {
            PoiUIFilter custom = app.getPoiFilters().getFilterById(PoiUIFilter.STD_PREFIX + parent.getKeyName());
            if (custom != null) {
                custom.clearFilter();
                custom.updateTypesToAccept(parent);
                custom.setFilterByName(additional.getKeyName().replace('_', ':').toLowerCase());
                SearchPhrase phrase = searchUICore.getPhrase();
                sr = new SearchResult(phrase);
                sr.localeName = custom.getName();
                sr.object = custom;
                sr.priority = SEARCH_AMENITY_TYPE_PRIORITY;
                sr.priorityDistance = 0;
                sr.objectType = ObjectType.POI_TYPE;
            }
        }
    }
    searchUICore.selectSearchResult(sr);
    if (addressSearch) {
        startAddressSearch();
        if (sr.objectType == ObjectType.CITY) {
            if (sr.relatedObject != null && sr.relatedObject instanceof BinaryMapIndexReader) {
                File f = ((BinaryMapIndexReader) sr.relatedObject).getFile();
                if (f != null) {
                    RegionAddressRepository region = app.getResourceManager().getRegionRepository(f.getName());
                    if (region != null) {
                        app.getSettings().setLastSearchedRegion(region.getFileName(), region.getEstimatedRegionCenter());
                        City city = (City) sr.object;
                        app.getSettings().setLastSearchedCity(city.getId(), sr.localeName, city.getLocation());
                    }
                }
            }
        }
    }
    String txt = searchUICore.getPhrase().getText(true);
    replaceQueryWithText(txt);
    if (sr.objectType == ObjectType.CITY) {
        openKeyboard();
    }
}
Also used : RegionAddressRepository(net.osmand.plus.resources.RegionAddressRepository) AbstractPoiType(net.osmand.osm.AbstractPoiType) PoiType(net.osmand.osm.PoiType) BinaryMapIndexReader(net.osmand.binary.BinaryMapIndexReader) SearchResult(net.osmand.search.core.SearchResult) City(net.osmand.data.City) SpannableString(android.text.SpannableString) AbstractPoiType(net.osmand.osm.AbstractPoiType) File(java.io.File) GPXFile(net.osmand.plus.GPXUtilities.GPXFile) SearchPhrase(net.osmand.search.core.SearchPhrase) PoiUIFilter(net.osmand.plus.poi.PoiUIFilter)

Aggregations

SearchResult (net.osmand.search.core.SearchResult)20 SearchPhrase (net.osmand.search.core.SearchPhrase)12 ArrayList (java.util.ArrayList)11 SearchResultCollection (net.osmand.search.SearchUICore.SearchResultCollection)9 LatLon (net.osmand.data.LatLon)8 SearchSettings (net.osmand.search.core.SearchSettings)7 SpannableString (android.text.SpannableString)6 PoiUIFilter (net.osmand.plus.poi.PoiUIFilter)5 QuickSearchListItem (net.osmand.plus.search.listitems.QuickSearchListItem)5 Bundle (android.os.Bundle)4 View (android.view.View)4 OnClickListener (android.view.View.OnClickListener)4 ImageView (android.widget.ImageView)4 TextView (android.widget.TextView)4 AdapterView (android.widget.AdapterView)3 ListView (android.widget.ListView)3 IOException (java.io.IOException)3 List (java.util.List)3 SearchMoreItemOnClickListener (net.osmand.plus.search.listitems.QuickSearchMoreListItem.SearchMoreItemOnClickListener)3 SuppressLint (android.annotation.SuppressLint)2