]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/DefaultElementDoubleClickHandler.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / handlers / DefaultElementDoubleClickHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor.handlers;
13
14 import java.util.Set;
15
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;
39
40 /**
41  * Really simple support for activating property view from diagram when a single
42  * element is double clicked.
43  * 
44  * @author Tuukka Lehtonen
45  */
46 public class DefaultElementDoubleClickHandler extends AbstractCanvasParticipant {
47
48     @Dependency
49     Selection selection;
50
51     public boolean accept(IElement element) {
52         return true;
53     }
54     
55     @EventHandler(priority = -10)
56     public boolean handleDoubleClick(MouseDoubleClickedEvent me) {
57         
58         Set<IElement> sel = selection.getSelection(0);
59         if (sel.size() == 1) {
60                 
61             IElement e = sel.iterator().next();
62             if(!accept(e)) return false;
63             
64             final Object data = e.getHint(ElementHints.KEY_OBJECT);
65
66             final Display display = PlatformUI.getWorkbench().getDisplay();
67             if (display == null)
68                 return false;
69
70             // See if there's any highest-priority editor adapter that we could use for the object:
71             try {
72                 Boolean consumed = Simantics.getSession().sync(new UniqueRead<Boolean>() {
73                     @Override
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) );
80                             return true;
81                         } else if (data instanceof Resource) {
82                             PlatformUI.getWorkbench().getDisplay().asyncExec( revealView("org.simantics.browsing.ui.graph.propertyView") );
83                             return true;
84                         }
85                         return false;
86                     }
87                 });
88                 return consumed;
89             } catch (DatabaseException ex) {
90                 ErrorLogger.defaultLogError(ex);
91             }
92         }
93
94         return false;
95     }
96
97     public static Runnable revealView(final String viewIdPart) {
98         return new Runnable() {
99             @Override
100             public void run() {
101                 try {
102                     WorkbenchUtils.showView(viewIdPart, IWorkbenchPage.VIEW_VISIBLE);
103                 } catch (PartInitException e) {
104                     ExceptionUtils.logAndShowError(e);
105                 }
106             }
107         };
108     }
109
110     public static Runnable revealAndShowView(final String viewIdPart) {
111         return new Runnable() {
112             @Override
113             public void run() {
114                 try {
115                     WorkbenchUtils.showView(viewIdPart, IWorkbenchPage.VIEW_VISIBLE);
116
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);
121                 }
122             }
123         };
124     }
125
126     public static Runnable showView(final String viewIdPart) {
127         return new Runnable() {
128             @Override
129             public void run() {
130                 try {
131                     CommandUtil.showView(viewIdPart);
132                 } catch (ExecutionException e) {
133                     ExceptionUtils.logAndShowError(e);
134                 }
135             }
136         };
137     }
138
139     public static Runnable actionAsRunnable(final IAction action) {
140         return new Runnable() {
141             @Override
142             public void run() {
143                 action.run();
144             }
145         };
146     }
147
148 }