|
| 1 | +package org.scijava.search.template; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.net.URL; |
| 5 | +import java.util.LinkedHashMap; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +import org.scijava.Priority; |
| 11 | +import org.scijava.app.AppService; |
| 12 | +import org.scijava.plugin.Parameter; |
| 13 | +import org.scijava.plugin.Plugin; |
| 14 | +import org.scijava.search.SearchResult; |
| 15 | +import org.scijava.search.Searcher; |
| 16 | +import org.scijava.util.FileUtils; |
| 17 | + |
| 18 | +@Plugin(type = Searcher.class, priority = Priority.VERY_HIGH - 10) |
| 19 | +public class TemplateSearcher implements Searcher { |
| 20 | + |
| 21 | + @Parameter |
| 22 | + private AppService appService; |
| 23 | + |
| 24 | + @Override |
| 25 | + public String title() { |
| 26 | + return "Script templates"; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public List<SearchResult> search(String text, boolean fuzzy) { |
| 31 | + // Get list of all templates in possible template paths |
| 32 | + // templateService.getTemplates() ?? |
| 33 | + final String templatePath = "script_templates"; |
| 34 | + File baseDir = appService.getApp().getBaseDirectory(); |
| 35 | + |
| 36 | + Map<String, URL> templates = FileUtils.findResources(null, templatePath, baseDir); |
| 37 | + |
| 38 | + LinkedHashMap<String, URL> matches = new LinkedHashMap<>(); |
| 39 | + |
| 40 | + // Filter those templates with name matching text |
| 41 | + templates.entrySet().stream() // |
| 42 | + .filter(entry -> entry.getKey().toLowerCase().contains(text.toLowerCase())) |
| 43 | + .forEach(entry -> matches.put(entry.getKey(), entry.getValue())); |
| 44 | + |
| 45 | + // Wrap each template into a TemplateSearchResult |
| 46 | + return matches.entrySet().stream() // |
| 47 | + .map(entry -> new TemplateSearchResult(entry.getKey(), entry.getValue())) |
| 48 | + .collect(Collectors.toList()); |
| 49 | + } |
| 50 | +} |
0 commit comments