1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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 *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor.handlers;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.jface.action.IAction;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.ui.IWorkbenchPage;
20 import org.eclipse.ui.PartInitException;
21 import org.eclipse.ui.PlatformUI;
22 import org.simantics.Simantics;
23 import org.simantics.browsing.ui.common.ErrorLogger;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.common.request.UniqueRead;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
29 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
30 import org.simantics.g2d.diagram.participant.Selection;
31 import org.simantics.g2d.element.ElementHints;
32 import org.simantics.g2d.element.IElement;
33 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
34 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;
35 import org.simantics.ui.workbench.action.ChooseActionRequest;
36 import org.simantics.utils.ui.ExceptionUtils;
37 import org.simantics.utils.ui.action.IPriorityAction;
38 import org.simantics.utils.ui.workbench.WorkbenchUtils;
41 * Really simple support for activating property view from diagram when a single
42 * element is double clicked.
44 * @author Tuukka Lehtonen
46 public class DefaultElementDoubleClickHandler extends AbstractCanvasParticipant {
51 public boolean accept(IElement element) {
55 @EventHandler(priority = -10)
56 public boolean handleDoubleClick(MouseDoubleClickedEvent me) {
58 Set<IElement> sel = selection.getSelection(0);
59 if (sel.size() == 1) {
61 IElement e = sel.iterator().next();
62 if(!accept(e)) return false;
64 final Object data = e.getHint(ElementHints.KEY_OBJECT);
66 final Display display = PlatformUI.getWorkbench().getDisplay();
70 // See if there's any highest-priority editor adapter that we could use for the object:
72 Boolean consumed = Simantics.getSession().sync(new UniqueRead<Boolean>() {
74 public Boolean perform(ReadGraph graph) throws DatabaseException {
75 IPriorityAction[] actions = ChooseActionRequest.findActions(graph, null, data, "", false, false);
76 IPriorityAction priorityAction = (IPriorityAction) ChooseActionRequest.chooseAction(null, actions, null, true);
77 if (priorityAction != null && priorityAction.getPriority() >= 10) {
78 //System.out.println("HIGH PRIORITY ACTION(" + priorityAction.getPriority() + "): " + priorityAction);
79 display.asyncExec( actionAsRunnable( priorityAction) );
81 } else if (data instanceof Resource) {
82 PlatformUI.getWorkbench().getDisplay().asyncExec( revealView("org.simantics.browsing.ui.graph.propertyView") );
89 } catch (DatabaseException ex) {
90 ErrorLogger.defaultLogError(ex);
97 public static Runnable revealView(final String viewIdPart) {
98 return new Runnable() {
102 WorkbenchUtils.showView(viewIdPart, IWorkbenchPage.VIEW_VISIBLE);
103 } catch (PartInitException e) {
104 ExceptionUtils.logAndShowError(e);
110 public static Runnable revealAndShowView(final String viewIdPart) {
111 return new Runnable() {
115 WorkbenchUtils.showView(viewIdPart, IWorkbenchPage.VIEW_VISIBLE);
117 // Give the post selection listeners time to settle.
118 PlatformUI.getWorkbench().getDisplay().timerExec( 500, showView(viewIdPart) );
119 } catch (PartInitException e) {
120 ExceptionUtils.logAndShowError(e);
126 public static Runnable showView(final String viewIdPart) {
127 return new Runnable() {
131 CommandUtil.showView(viewIdPart);
132 } catch (ExecutionException e) {
133 ExceptionUtils.logAndShowError(e);
139 public static Runnable actionAsRunnable(final IAction action) {
140 return new Runnable() {