Search in sources :

Example 1 with SearchResultCollection

use of net.osmand.search.SearchUICore.SearchResultCollection in project OsmAnd-tools by osmandapp.

the class OsmExtractionUI method updateStatusField.

private void updateStatusField(final JTextField statusField) {
    popup = new JScrollPopupMenu();
    popup.setMaximumVisibleRows(25);
    popup.setFocusable(false);
    searchUICore.setOnResultsComplete(new Runnable() {

        @Override
        public void run() {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    updateSearchResult(statusField, searchUICore.getCurrentSearchResult(), true);
                }
            });
        }
    });
    statusField.addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent e) {
        // if(e.getOppositeComponent() != popup) {
        // popup.setVisible(false);
        // }
        }

        @Override
        public void focusGained(FocusEvent e) {
            popup.setFocusable(false);
            SearchSettings settings = searchUICore.getPhrase().getSettings().setOriginalLocation(new LatLon(mapPanel.getLatitude(), mapPanel.getLongitude()));
            settings = settings.setLang(DataExtractionSettings.getSettings().getSearchLocale(), false);
            searchUICore.updateSettings(settings);
        }
    });
    statusField.addKeyListener(new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            mapPanel.setStatusField(null);
            if (e.getKeyCode() == KeyEvent.VK_DOWN && popup.getComponentCount() > 0) {
                popup.setVisible(false);
                popup.setFocusable(true);
                Point p = statusField.getLocation();
                popup.show(e.getComponent(), p.x, p.y - 4);
                // popup.show();
                popup.requestFocus();
                return;
            }
        }

        @Override
        public void keyTyped(KeyEvent e) {
            mapPanel.setStatusField(null);
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                return;
            }
            String text = statusField.getText();
            int ps = statusField.getCaretPosition();
            if (e.getKeyChar() == '\b') {
            // nothing
            } else if (e.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
                if (ps >= text.length()) {
                    text += e.getKeyChar();
                } else {
                    text = text.substring(0, ps) + e.getKeyChar() + text.substring(ps);
                }
            }
            SearchSettings settings = searchUICore.getPhrase().getSettings();
            if (settings.getRadiusLevel() != 1) {
                searchUICore.updateSettings(settings.setRadiusLevel(1));
            }
            SearchResultCollection c = null;
            if (!text.contains("#map")) {
                c = searchUICore.search(text, true, null);
            }
            if (c != null) {
                updateSearchResult(statusField, c, false);
            }
        }
    });
    statusField.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            mapPanel.setStatusField(statusField);
            String txt = statusField.getText();
            int i = txt.indexOf("#map=");
            if (i != -1) {
                String[] vs = txt.substring(i + "#map=".length()).split("/");
                mapPanel.setLatLon(Float.parseFloat(vs[1]), Float.parseFloat(vs[2]));
                mapPanel.setZoom(Integer.parseInt(vs[0]));
            }
            mapPanel.refresh();
        }
    });
}
Also used : ActionEvent(java.awt.event.ActionEvent) KeyAdapter(java.awt.event.KeyAdapter) Point(java.awt.Point) FocusEvent(java.awt.event.FocusEvent) KeyEvent(java.awt.event.KeyEvent) LatLon(net.osmand.data.LatLon) ActionListener(java.awt.event.ActionListener) SearchSettings(net.osmand.search.core.SearchSettings) SearchResultCollection(net.osmand.search.SearchUICore.SearchResultCollection) FocusListener(java.awt.event.FocusListener)

Example 2 with SearchResultCollection

use of net.osmand.search.SearchUICore.SearchResultCollection 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 3 with SearchResultCollection

use of net.osmand.search.SearchUICore.SearchResultCollection 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 4 with SearchResultCollection

use of net.osmand.search.SearchUICore.SearchResultCollection 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 5 with SearchResultCollection

use of net.osmand.search.SearchUICore.SearchResultCollection 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)

Aggregations

SearchResultCollection (net.osmand.search.SearchUICore.SearchResultCollection)11 SearchResult (net.osmand.search.core.SearchResult)9 ArrayList (java.util.ArrayList)8 LatLon (net.osmand.data.LatLon)7 SearchPhrase (net.osmand.search.core.SearchPhrase)6 SearchSettings (net.osmand.search.core.SearchSettings)6 IOException (java.io.IOException)3 QuickSearchListItem (net.osmand.plus.search.listitems.QuickSearchListItem)3 Test (org.junit.Test)3 SpannableString (android.text.SpannableString)2 View (android.view.View)2 OnClickListener (android.view.View.OnClickListener)2 AdapterView (android.widget.AdapterView)2 ImageView (android.widget.ImageView)2 ListView (android.widget.ListView)2 TextView (android.widget.TextView)2 Point (java.awt.Point)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 List (java.util.List)2