1 /*******************************************************************************
2 * Copyright (c) 2007, 2016 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - refactoring (#6855)
12 *******************************************************************************/
13 package org.simantics.debug.ui.internal;
15 import java.util.function.BiConsumer;
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.handlers.HandlerUtil;
23 import org.simantics.Simantics;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.Session;
27 import org.simantics.db.common.request.ReadRequest;
28 import org.simantics.db.common.utils.NameUtils;
29 import org.simantics.db.exception.DatabaseException;
30 import org.simantics.debug.ui.ResourceSearch;
31 import org.simantics.debug.ui.SearchResourceDialog;
32 import org.simantics.ui.workbench.action.ChooseActionRequest;
33 import org.simantics.utils.Container;
34 import org.simantics.utils.ui.SWTUtils;
35 import org.simantics.utils.ui.action.IPriorityAction;
36 import org.simantics.utils.ui.workbench.WorkbenchUtils;
38 public class SearchResourceHandler extends AbstractHandler {
41 public Object execute(ExecutionEvent event) throws ExecutionException {
42 Shell shell = HandlerUtil.getActiveShellChecked(event);
43 Session session = Simantics.getSession();
44 SearchResourceDialog rld = new SearchResourceDialog(session, false, shell, "Open Resource");
45 rld.setResourceFilter(ResourceSearch.FILTER_ALL);
46 rld.setBlockOnOpen(true);
48 if (rld.getResult() == null)
51 String currentPerspectiveId = WorkbenchUtils.getCurrentPerspectiveId();
53 for (Object o : rld.getResult()) {
54 @SuppressWarnings("unchecked")
55 Container<Resource> rc = (Container<Resource>) o;
56 openPreferredEditor(shell, session, rc.get(), currentPerspectiveId);
62 private static void openPreferredEditor(Shell parent, Session s, Resource r, String defaultPerspective) {
63 findActions(s, r, defaultPerspective, (resourceName, actions) -> {
64 SWTUtils.asyncExec(parent, () -> {
65 IAction action = ChooseActionRequest.chooseAction(parent, actions, resourceName);
72 private static void findActions(Session s, Resource r, String defaultPerspective, BiConsumer<String, IPriorityAction[]> consumer) {
73 s.asyncRequest(new ReadRequest() {
75 public void run(ReadGraph g) throws DatabaseException {
76 String resourceName = NameUtils.getSafeName(g, r);
77 IPriorityAction[] actions = ChooseActionRequest.findActions(g, r, defaultPerspective);
79 consumer.accept(resourceName, actions);