]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/DefaultElementDoubleClickHandler.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / handlers / DefaultElementDoubleClickHandler.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.modeling.ui.diagramEditor.handlers;\r
13 \r
14 import java.util.Set;\r
15 \r
16 import org.eclipse.core.commands.ExecutionException;\r
17 import org.eclipse.jface.action.IAction;\r
18 import org.eclipse.swt.widgets.Display;\r
19 import org.eclipse.ui.IWorkbenchPage;\r
20 import org.eclipse.ui.PartInitException;\r
21 import org.eclipse.ui.PlatformUI;\r
22 import org.simantics.Simantics;\r
23 import org.simantics.browsing.ui.common.ErrorLogger;\r
24 import org.simantics.db.ReadGraph;\r
25 import org.simantics.db.Resource;\r
26 import org.simantics.db.common.request.UniqueRead;\r
27 import org.simantics.db.exception.DatabaseException;\r
28 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
29 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
30 import org.simantics.g2d.diagram.participant.Selection;\r
31 import org.simantics.g2d.element.ElementHints;\r
32 import org.simantics.g2d.element.IElement;\r
33 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
34 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;\r
35 import org.simantics.ui.workbench.action.ChooseActionRequest;\r
36 import org.simantics.utils.ui.ExceptionUtils;\r
37 import org.simantics.utils.ui.action.IPriorityAction;\r
38 import org.simantics.utils.ui.workbench.WorkbenchUtils;\r
39 \r
40 /**\r
41  * Really simple support for activating property view from diagram when a single\r
42  * element is double clicked.\r
43  * \r
44  * @author Tuukka Lehtonen\r
45  */\r
46 public class DefaultElementDoubleClickHandler extends AbstractCanvasParticipant {\r
47 \r
48     @Dependency\r
49     Selection selection;\r
50 \r
51     public boolean accept(IElement element) {\r
52         return true;\r
53     }\r
54     \r
55     @EventHandler(priority = -10)\r
56     public boolean handleDoubleClick(MouseDoubleClickedEvent me) {\r
57         \r
58         Set<IElement> sel = selection.getSelection(0);\r
59         if (sel.size() == 1) {\r
60                 \r
61             IElement e = sel.iterator().next();\r
62             if(!accept(e)) return false;\r
63             \r
64             final Object data = e.getHint(ElementHints.KEY_OBJECT);\r
65 \r
66             final Display display = PlatformUI.getWorkbench().getDisplay();\r
67             if (display == null)\r
68                 return false;\r
69 \r
70             // See if there's any highest-priority editor adapter that we could use for the object:\r
71             try {\r
72                 Boolean consumed = Simantics.getSession().sync(new UniqueRead<Boolean>() {\r
73                     @Override\r
74                     public Boolean perform(ReadGraph graph) throws DatabaseException {\r
75                         IPriorityAction[] actions = ChooseActionRequest.findActions(graph, null, data, "", false, false);\r
76                         IPriorityAction priorityAction = (IPriorityAction) ChooseActionRequest.chooseAction(null, actions, null, true);\r
77                         if (priorityAction != null && priorityAction.getPriority() >= 10) {\r
78                             //System.out.println("HIGH PRIORITY ACTION(" + priorityAction.getPriority() + "): " + priorityAction);\r
79                             display.asyncExec( actionAsRunnable( priorityAction) );\r
80                             return true;\r
81                         } else if (data instanceof Resource) {\r
82                             PlatformUI.getWorkbench().getDisplay().asyncExec( revealView("org.simantics.browsing.ui.graph.propertyView") );\r
83                             return true;\r
84                         }\r
85                         return false;\r
86                     }\r
87                 });\r
88                 return consumed;\r
89             } catch (DatabaseException ex) {\r
90                 ErrorLogger.defaultLogError(ex);\r
91             }\r
92         }\r
93 \r
94         return false;\r
95     }\r
96 \r
97     public static Runnable revealView(final String viewIdPart) {\r
98         return new Runnable() {\r
99             @Override\r
100             public void run() {\r
101                 try {\r
102                     WorkbenchUtils.showView(viewIdPart, IWorkbenchPage.VIEW_VISIBLE);\r
103                 } catch (PartInitException e) {\r
104                     ExceptionUtils.logAndShowError(e);\r
105                 }\r
106             }\r
107         };\r
108     }\r
109 \r
110     public static Runnable revealAndShowView(final String viewIdPart) {\r
111         return new Runnable() {\r
112             @Override\r
113             public void run() {\r
114                 try {\r
115                     WorkbenchUtils.showView(viewIdPart, IWorkbenchPage.VIEW_VISIBLE);\r
116 \r
117                     // Give the post selection listeners time to settle.\r
118                     PlatformUI.getWorkbench().getDisplay().timerExec( 500, showView(viewIdPart) );\r
119                 } catch (PartInitException e) {\r
120                     ExceptionUtils.logAndShowError(e);\r
121                 }\r
122             }\r
123         };\r
124     }\r
125 \r
126     public static Runnable showView(final String viewIdPart) {\r
127         return new Runnable() {\r
128             @Override\r
129             public void run() {\r
130                 try {\r
131                     CommandUtil.showView(viewIdPart);\r
132                 } catch (ExecutionException e) {\r
133                     ExceptionUtils.logAndShowError(e);\r
134                 }\r
135             }\r
136         };\r
137     }\r
138 \r
139     public static Runnable actionAsRunnable(final IAction action) {\r
140         return new Runnable() {\r
141             @Override\r
142             public void run() {\r
143                 action.run();\r
144             }\r
145         };\r
146     }\r
147 \r
148 }\r