5fec36f7c1cb45aa789ba5ec26bc583f8c8f8208
[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.request.Read;
11 import org.simantics.document.linking.ontology.DocumentLink;
12 import org.simantics.layer0.Layer0;
13 import org.simantics.utils.ui.ExceptionUtils;
14 import org.simantics.workbench.search.ISearchService;
15 import org.simantics.workbench.search.SearchQuery;
16
17 public class SearchLinksAction implements ActionFactory {
18         
19         
20         
21         @Override
22         public Runnable create(Object target) {
23                 if(!(target instanceof Resource))
24                         return null;
25                 final Resource resource = (Resource)target;
26                 return new Runnable() {
27                         
28                         @Override
29                         public void run() {
30                                 try {
31                                         String name = Simantics.getSession().syncRequest(new Read<String>() {
32                                                 @Override
33                                                 public String perform(ReadGraph graph)
34                                                                 throws DatabaseException {
35                                                         Layer0 l0 = Layer0.getInstance(graph);
36                                                         String s = graph.getPossibleRelatedValue(resource, l0.HasLabel, Bindings.STRING);
37                                                         if (s == null)
38                                                                 s = graph.getRelatedValue(resource, l0.HasName, Bindings.STRING);
39                                                         return s;
40                                                 }
41                                         });
42                                         ISearchService searchService = (ISearchService) PlatformUI.getWorkbench().getService(ISearchService.class);
43                                         SearchQuery query = new SearchQuery(name);
44                                         query.setSearchFlag("Name", "on"); //$NON-NLS-1$ //$NON-NLS-2$
45                                         query.setSearchFlag(DocumentLink.URIs.SearchFunction, "on"); //$NON-NLS-1$
46                                         searchService.performQuery(query, ISearchService.ResultBrowser.VIEW, true);
47                                 } catch (DatabaseException e) {
48                                         ExceptionUtils.logAndShowError(Messages.SearchLinksAction_CannotPerformSearch, e);
49                                 }
50
51                         }
52                 };
53         }
54
55 }