Index tokenized lowercase versions of name and types for UI searches
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / actions / SearchLinksAction.java
1 package org.simantics.document.linking.actions;
2
3 import org.eclipse.ui.PlatformUI;
4 import org.simantics.Simantics;
5 import org.simantics.databoard.Bindings;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.layer0.adapter.ActionFactory;
10 import org.simantics.db.layer0.genericrelation.Dependencies;
11 import org.simantics.db.request.Read;
12 import org.simantics.document.linking.ontology.DocumentLink;
13 import org.simantics.layer0.Layer0;
14 import org.simantics.utils.ui.ExceptionUtils;
15 import org.simantics.workbench.search.ISearchService;
16 import org.simantics.workbench.search.SearchQuery;
17
18 public class SearchLinksAction implements ActionFactory {
19         
20         
21         
22         @Override
23         public Runnable create(Object target) {
24                 if(!(target instanceof Resource))
25                         return null;
26                 final Resource resource = (Resource)target;
27                 return new Runnable() {
28                         
29                         @Override
30                         public void run() {
31                                 try {
32                                         String name = Simantics.getSession().syncRequest(new Read<String>() {
33                                                 @Override
34                                                 public String perform(ReadGraph graph)
35                                                                 throws DatabaseException {
36                                                         Layer0 l0 = Layer0.getInstance(graph);
37                                                         String s = graph.getPossibleRelatedValue(resource, l0.HasLabel, Bindings.STRING);
38                                                         if (s == null)
39                                                                 s = graph.getRelatedValue(resource, l0.HasName, Bindings.STRING);
40                                                         return s;
41                                                 }
42                                         });
43                                         ISearchService searchService = (ISearchService) PlatformUI.getWorkbench().getService(ISearchService.class);
44                                         SearchQuery query = new SearchQuery(name);
45                                         query.setSearchFlag(Dependencies.FIELD_NAME_SEARCH, "on"); //$NON-NLS-1$
46                                         query.setSearchFlag(DocumentLink.URIs.SearchFunction, "on"); //$NON-NLS-1$
47                                         searchService.performQuery(query, ISearchService.ResultBrowser.VIEW, true);
48                                 } catch (DatabaseException e) {
49                                         ExceptionUtils.logAndShowError(Messages.SearchLinksAction_CannotPerformSearch, e);
50                                 }
51
52                         }
53                 };
54         }
55
56 }