use of net.osmand.search.SearchUICore in project OsmAnd-tools by osmandapp.
the class OsmExtractionUI method createUI.
public void createUI() {
// $NON-NLS-1$
frame = new JFrame(Messages.getString("OsmExtractionUI.OSMAND_MAP_CREATOR"));
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// $NON-NLS-1$
log.error("Can't set look and feel", e);
}
frame.addWindowListener(new ExitListener());
Container content = frame.getContentPane();
frame.setFocusable(true);
mapPanel = new MapPanel(DataExtractionSettings.getSettings().getTilesDirectory());
mapPanel.setFocusable(true);
mapPanel.addMapLocationListener(this);
statusBarLabel = new JLabel();
osmandRegions = new OsmandRegions();
try {
File regions = new File("regions.ocbf");
if (!regions.exists()) {
InputStream is = OsmandRegions.class.getResourceAsStream("regions.ocbf");
FileOutputStream fous = new FileOutputStream(regions);
Algorithms.streamCopy(is, fous);
fous.close();
}
osmandRegions.prepareFile(regions.getAbsolutePath());
} catch (IOException e2) {
e2.printStackTrace();
log.error(e2.getMessage(), e2);
}
content.add(statusBarLabel, BorderLayout.SOUTH);
File workingDir = DataExtractionSettings.getSettings().getDefaultWorkingDir();
// $NON-NLS-1$ //$NON-NLS-2$
statusBarLabel.setText(workingDir == null ? Messages.getString("OsmExtractionUI.WORKING_DIR_UNSPECIFIED") : Messages.getString("OsmExtractionUI.WORKING_DIRECTORY") + workingDir.getAbsolutePath());
String bdir = DataExtractionSettings.getSettings().getBinaryFilesDir();
List<BinaryMapIndexReader> files = new ArrayList<>();
if (!Algorithms.isEmpty(bdir)) {
try {
File bdirFile = new File(bdir);
File cacheFile = new File(bdirFile, "indexes.cache");
CachedOsmandIndexes cache = new CachedOsmandIndexes();
if (cacheFile.exists()) {
cache.readFromFile(cacheFile, 2);
}
if (bdirFile.exists() && bdirFile.listFiles() != null) {
List<File> asList = Arrays.asList(Algorithms.getSortedFilesVersions(bdirFile));
ArrayList<File> sortedFiles = new ArrayList<>(asList);
// Collections.reverse(sortedFiles);
for (File obf : sortedFiles) {
if (!obf.isDirectory() && obf.getName().endsWith(".obf")) {
try {
// BinaryMapIndexReader bmir = new BinaryMapIndexReader(new RandomAccessFile(obf, "r"),
// obf);
BinaryMapIndexReader bmir = cache.getReader(obf);
RandomAccessFile raf = new RandomAccessFile(obf, "r");
BinaryMapIndexReader bmir2 = new BinaryMapIndexReader(raf, bmir);
files.add(bmir2);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
if (!files.isEmpty()) {
cache.writeToFile(cacheFile);
}
} catch (IOException e) {
e.printStackTrace();
}
}
String loc = DataExtractionSettings.getSettings().getSearchLocale();
if (loc.isEmpty()) {
loc = null;
}
searchUICore = new SearchUICore(MapPoiTypes.getDefault(), loc, false);
searchUICore.getSearchSettings().setOfflineIndexes(files);
searchUICore.init();
searchUICore.registerAPI(new SearchCoreFactory.SearchRegionByNameAPI());
// treePlaces = new JTree();
// treePlaces.setModel(new DefaultTreeModel(new DefaultMutableTreeNode(Messages.getString("OsmExtractionUI.REGION")), false)); //$NON-NLS-1$
// JSplitPane panelForTreeAndMap = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(treePlaces), mapPanel);
// panelForTreeAndMap.setResizeWeight(0.2);
// content.add(panelForTreeAndMap, BorderLayout.CENTER);
content.add(mapPanel, BorderLayout.CENTER);
JPanel bl = new JPanel();
bl.setLayout(new BoxLayout(bl, BoxLayout.PAGE_AXIS));
JPanel buttonsBar = createButtonsBar();
bl.add(buttonsBar);
final JTextField statusField = new JTextField();
mapPanel.setStatusField(statusField);
bl.add(statusField);
updateStatusField(statusField);
content.add(bl, BorderLayout.NORTH);
JMenuBar bar = new JMenuBar();
fillMenuWithActions(bar);
frame.setJMenuBar(bar);
}
use of net.osmand.search.SearchUICore in project Osmand by osmandapp.
the class QuickSearchDialogFragment method showInstance.
public static boolean showInstance(@NonNull MainActivity mainActivity, @NonNull String searchQuery, @Nullable Object object, boolean showCategories, @Nullable LatLon latLon) {
try {
Bundle bundle = new Bundle();
if (object != null) {
bundle.putBoolean(QUICK_SEARCH_RUN_SEARCH_FIRST_TIME_KEY, true);
String objectLocalizedName = searchQuery;
if (object instanceof PoiCategory) {
PoiCategory c = (PoiCategory) object;
objectLocalizedName = c.getTranslation();
SearchUICore searchUICore = mainActivity.getMyApplication().getSearchUICore().getCore();
SearchPhrase phrase = searchUICore.resetPhrase(objectLocalizedName + " ");
SearchResult sr = new SearchResult(phrase);
sr.localeName = objectLocalizedName;
sr.object = c;
sr.priority = SEARCH_AMENITY_TYPE_PRIORITY;
sr.priorityDistance = 0;
sr.objectType = ObjectType.POI_TYPE;
searchUICore.selectSearchResult(sr);
bundle.putBoolean(QUICK_SEARCH_PHRASE_DEFINED_KEY, true);
}
searchQuery = objectLocalizedName.trim() + " ";
} else if (!Algorithms.isEmpty(searchQuery)) {
bundle.putBoolean(QUICK_SEARCH_RUN_SEARCH_FIRST_TIME_KEY, true);
}
bundle.putString(QUICK_SEARCH_QUERY_KEY, searchQuery);
bundle.putBoolean(QUICK_SEARCH_SHOW_CATEGORIES_KEY, showCategories);
if (latLon != null) {
bundle.putDouble(QUICK_SEARCH_LAT_KEY, latLon.getLatitude());
bundle.putDouble(QUICK_SEARCH_LON_KEY, latLon.getLongitude());
}
QuickSearchDialogFragment fragment = new QuickSearchDialogFragment();
fragment.setArguments(bundle);
fragment.show(mainActivity.getSupportFragmentManager(), TAG);
return true;
} catch (RuntimeException e) {
return false;
}
}
use of net.osmand.search.SearchUICore in project Osmand by osmandapp.
the class QuickSearchDialogFragment method showInstance.
public static boolean showInstance(@NonNull MapActivity mapActivity, @NonNull String searchQuery, @Nullable Object object, QuickSearchType searchType, QuickSearchTab showSearchTab, @Nullable LatLon latLon) {
try {
if (mapActivity.isActivityDestroyed()) {
return false;
}
mapActivity.getMyApplication().logEvent(mapActivity, "search_open");
Bundle bundle = new Bundle();
if (object != null) {
bundle.putBoolean(QUICK_SEARCH_RUN_SEARCH_FIRST_TIME_KEY, true);
String objectLocalizedName = searchQuery;
if (object instanceof PoiCategory) {
PoiCategory c = (PoiCategory) object;
objectLocalizedName = c.getTranslation();
SearchUICore searchUICore = mapActivity.getMyApplication().getSearchUICore().getCore();
SearchPhrase phrase = searchUICore.resetPhrase(objectLocalizedName + " ");
SearchResult sr = new SearchResult(phrase);
sr.localeName = objectLocalizedName;
sr.object = c;
sr.priority = SEARCH_AMENITY_TYPE_PRIORITY;
sr.priorityDistance = 0;
sr.objectType = ObjectType.POI_TYPE;
searchUICore.selectSearchResult(sr);
bundle.putBoolean(QUICK_SEARCH_PHRASE_DEFINED_KEY, true);
} else if (object instanceof PoiUIFilter) {
PoiUIFilter filter = (PoiUIFilter) object;
objectLocalizedName = filter.getName();
SearchUICore searchUICore = mapActivity.getMyApplication().getSearchUICore().getCore();
SearchPhrase phrase = searchUICore.resetPhrase();
SearchResult sr = new SearchResult(phrase);
sr.localeName = objectLocalizedName;
sr.object = filter;
sr.priority = SEARCH_AMENITY_TYPE_PRIORITY;
sr.priorityDistance = 0;
sr.objectType = ObjectType.POI_TYPE;
searchUICore.selectSearchResult(sr);
bundle.putBoolean(QUICK_SEARCH_PHRASE_DEFINED_KEY, true);
}
searchQuery = objectLocalizedName.trim() + " ";
} else if (!Algorithms.isEmpty(searchQuery)) {
bundle.putBoolean(QUICK_SEARCH_RUN_SEARCH_FIRST_TIME_KEY, true);
}
bundle.putString(QUICK_SEARCH_QUERY_KEY, searchQuery);
bundle.putString(QUICK_SEARCH_SHOW_TAB_KEY, showSearchTab.name());
bundle.putString(QUICK_SEARCH_TYPE_KEY, searchType.name());
if (latLon != null) {
bundle.putDouble(QUICK_SEARCH_LAT_KEY, latLon.getLatitude());
bundle.putDouble(QUICK_SEARCH_LON_KEY, latLon.getLongitude());
}
QuickSearchDialogFragment fragment = new QuickSearchDialogFragment();
fragment.setArguments(bundle);
fragment.show(mapActivity.getSupportFragmentManager(), TAG);
return true;
} catch (RuntimeException e) {
return false;
}
}
use of net.osmand.search.SearchUICore in project Osmand by osmandapp.
the class QuickSearchListAdapter method getView.
@NonNull
@Override
public View getView(final int position, View convertView, @NonNull ViewGroup parent) {
final QuickSearchListItem listItem = getItem(position);
QuickSearchListItemType type = listItem.getType();
LinearLayout view;
if (type == QuickSearchListItemType.SEARCH_MORE) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.search_more_list_item, null);
} else {
view = (LinearLayout) convertView;
}
if (listItem.getSpannableName() != null) {
((TextView) view.findViewById(R.id.title)).setText(listItem.getSpannableName());
} else {
((TextView) view.findViewById(R.id.title)).setText(listItem.getName());
}
final QuickSearchMoreListItem searchMoreItem = (QuickSearchMoreListItem) listItem;
int emptyDescId = searchMoreItem.isSearchMoreAvailable() ? R.string.nothing_found_descr : R.string.modify_the_search_query;
((TextView) view.findViewById(R.id.empty_search_description)).setText(emptyDescId);
boolean emptySearchVisible = searchMoreItem.isEmptySearch() && !searchMoreItem.isInterruptedSearch();
boolean moreDividerVisible = emptySearchVisible && searchMoreItem.isSearchMoreAvailable();
view.findViewById(R.id.empty_search).setVisibility(emptySearchVisible ? View.VISIBLE : View.GONE);
view.findViewById(R.id.more_divider).setVisibility(moreDividerVisible ? View.VISIBLE : View.GONE);
SearchUICore searchUICore = app.getSearchUICore().getCore();
String textTitle = app.getString(R.string.nothing_found_in_radius) + " " + OsmAndFormatter.getFormattedDistance(searchUICore.getMinimalSearchRadius(searchUICore.getPhrase()), app);
((TextView) view.findViewById(R.id.empty_search_title)).setText(textTitle);
View increaseRadiusRow = view.findViewById(R.id.increase_radius_row);
increaseRadiusRow.setVisibility(searchMoreItem.isSearchMoreAvailable() ? View.VISIBLE : View.GONE);
increaseRadiusRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((QuickSearchMoreListItem) listItem).increaseRadiusOnClick();
}
});
if (!searchMoreItem.isOnlineSearch()) {
View onlineSearchRow = view.findViewById(R.id.online_search_row);
onlineSearchRow.setVisibility(View.VISIBLE);
onlineSearchRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
searchMoreItem.onlineSearchOnClick();
}
});
}
} else if (type == QuickSearchListItemType.BUTTON) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.search_custom_list_item, null);
} else {
view = (LinearLayout) convertView;
}
((ImageView) view.findViewById(R.id.imageView)).setImageDrawable(listItem.getIcon());
if (listItem.getSpannableName() != null) {
((TextView) view.findViewById(R.id.title)).setText(listItem.getSpannableName());
} else {
((TextView) view.findViewById(R.id.title)).setText(listItem.getName());
}
} else if (type == QuickSearchListItemType.SELECT_ALL) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.select_all_list_item, null);
} else {
view = (LinearLayout) convertView;
}
final CheckBox ch = (CheckBox) view.findViewById(R.id.toggle_item);
ch.setVisibility(View.VISIBLE);
ch.setChecked(selectAll);
ch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleCheckbox(position, ch);
}
});
} else if (type == QuickSearchListItemType.HEADER) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.search_header_list_item, null);
} else {
view = (LinearLayout) convertView;
}
view.findViewById(R.id.top_divider).setVisibility(((QuickSearchHeaderListItem) listItem).isShowTopDivider() ? View.VISIBLE : View.GONE);
if (listItem.getSpannableName() != null) {
((TextView) view.findViewById(R.id.title)).setText(listItem.getSpannableName());
} else {
((TextView) view.findViewById(R.id.title)).setText(listItem.getName());
}
} else if (type == QuickSearchListItemType.TOP_SHADOW) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.list_shadow_header, null);
} else {
view = (LinearLayout) convertView;
}
return view;
} else if (type == QuickSearchListItemType.BOTTOM_SHADOW) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.list_shadow_footer, null);
} else {
view = (LinearLayout) convertView;
}
return view;
} else {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) app.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (LinearLayout) inflater.inflate(R.layout.search_list_item, null);
} else {
view = (LinearLayout) convertView;
}
final CheckBox ch = (CheckBox) view.findViewById(R.id.toggle_item);
if (selectionMode) {
ch.setVisibility(View.VISIBLE);
ch.setChecked(selectedItems.contains(listItem));
ch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleCheckbox(position, ch);
}
});
} else {
ch.setVisibility(View.GONE);
}
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
TextView title = (TextView) view.findViewById(R.id.title);
TextView subtitle = (TextView) view.findViewById(R.id.subtitle);
imageView.setImageDrawable(listItem.getIcon());
String name = listItem.getName();
if (listItem.getSpannableName() != null) {
title.setText(listItem.getSpannableName());
} else {
title.setText(name);
}
String desc = listItem.getTypeName();
Object searchResultObject = listItem.getSearchResult().object;
if (searchResultObject instanceof AbstractPoiType) {
AbstractPoiType abstractPoiType = (AbstractPoiType) searchResultObject;
String[] synonyms = abstractPoiType.getSynonyms().split(";");
QuickSearchHelper searchHelper = app.getSearchUICore();
SearchUICore searchUICore = searchHelper.getCore();
String searchPhrase = searchUICore.getPhrase().getText(true);
SearchPhrase.NameStringMatcher nm = new SearchPhrase.NameStringMatcher(searchPhrase, CollatorStringMatcher.StringMatcherMode.CHECK_STARTS_FROM_SPACE);
if (!searchPhrase.isEmpty() && !nm.matches(abstractPoiType.getTranslation())) {
if (nm.matches(abstractPoiType.getEnTranslation())) {
desc = listItem.getTypeName() + " (" + abstractPoiType.getEnTranslation() + ")";
} else {
for (String syn : synonyms) {
if (nm.matches(syn)) {
desc = listItem.getTypeName() + " (" + syn + ")";
break;
}
}
}
}
}
boolean hasDesc = false;
if (!Algorithms.isEmpty(desc) && !desc.equals(name)) {
subtitle.setText(desc);
subtitle.setVisibility(View.VISIBLE);
hasDesc = true;
} else {
subtitle.setVisibility(View.GONE);
}
Drawable typeIcon = listItem.getTypeIcon();
ImageView group = (ImageView) view.findViewById(R.id.type_name_icon);
if (typeIcon != null && hasDesc) {
group.setImageDrawable(typeIcon);
group.setVisibility(View.VISIBLE);
} else {
group.setVisibility(View.GONE);
}
LinearLayout timeLayout = (LinearLayout) view.findViewById(R.id.time_layout);
TextView timeText = (TextView) view.findViewById(R.id.time);
ImageView timeIcon = (ImageView) view.findViewById(R.id.time_icon);
if (listItem.getSearchResult().object instanceof Amenity && ((Amenity) listItem.getSearchResult().object).getOpeningHours() != null) {
Amenity amenity = (Amenity) listItem.getSearchResult().object;
OpeningHoursParser.OpeningHours rs = OpeningHoursParser.parseOpenedHours(amenity.getOpeningHours());
if (rs != null) {
Calendar inst = Calendar.getInstance();
inst.setTimeInMillis(System.currentTimeMillis());
boolean worksNow = rs.isOpenedForTime(inst);
// 30 minutes later
inst.setTimeInMillis(System.currentTimeMillis() + 30 * 60 * 1000);
boolean worksLater = rs.isOpenedForTime(inst);
int colorId = worksNow ? worksLater ? R.color.color_ok : R.color.color_intermediate : R.color.color_warning;
timeLayout.setVisibility(View.VISIBLE);
timeIcon.setImageDrawable(app.getIconsCache().getIcon(R.drawable.ic_small_time, colorId));
timeText.setTextColor(app.getResources().getColor(colorId));
String rt = rs.getCurrentRuleTime(inst);
timeText.setText(rt == null ? "" : rt);
} else {
timeLayout.setVisibility(View.GONE);
}
} else {
timeLayout.setVisibility(View.GONE);
}
updateCompassVisibility(view, listItem);
}
view.setBackgroundColor(app.getResources().getColor(app.getSettings().isLightContent() ? R.color.bg_color_light : R.color.bg_color_dark));
View divider = view.findViewById(R.id.divider);
if (divider != null) {
if (position == getCount() - 1 || getItem(position + 1).getType() == QuickSearchListItemType.HEADER || getItem(position + 1).getType() == QuickSearchListItemType.BOTTOM_SHADOW) {
divider.setVisibility(View.GONE);
} else {
divider.setVisibility(View.VISIBLE);
if (getItem(position + 1).getType() == QuickSearchListItemType.SEARCH_MORE || type == QuickSearchListItemType.SELECT_ALL) {
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp1);
p.setMargins(0, 0, 0, 0);
divider.setLayoutParams(p);
} else {
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dp1);
p.setMargins(dp56, 0, 0, 0);
divider.setLayoutParams(p);
}
}
}
ViewCompat.setAccessibilityDelegate(view, accessibilityAssistant);
return view;
}
Aggregations