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();
}
}
}
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());
}
}
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();
}
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);
}
}
}
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();
}
}
Aggregations