]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/handler/CanvasCommandDelegate.java
Sync git svn branch with SVN repository r33176.
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / handler / CanvasCommandDelegate.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.diagram.handler;\r
13 \r
14 import org.eclipse.core.commands.AbstractHandler;\r
15 import org.eclipse.core.commands.ExecutionEvent;\r
16 import org.eclipse.core.commands.ExecutionException;\r
17 import org.eclipse.swt.widgets.Event;\r
18 import org.eclipse.ui.IWorkbenchPart;\r
19 import org.eclipse.ui.handlers.HandlerUtil;\r
20 import org.simantics.g2d.canvas.ICanvasContext;\r
21 import org.simantics.g2d.chassis.ICanvasChassis;\r
22 import org.simantics.scenegraph.g2d.events.EventDebugPolicy;\r
23 import org.simantics.scenegraph.g2d.events.EventQueue;\r
24 import org.simantics.scenegraph.g2d.events.command.Command;\r
25 import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
26 \r
27 /**\r
28  * A handler for the Eclipse Command framework that delegates ID of the command\r
29  * that triggered the event to the {@link EventQueue} of the\r
30  * {@link ICanvasContext} where the event occurred.\r
31  */\r
32 public class CanvasCommandDelegate extends AbstractHandler {\r
33 \r
34     private final boolean DEBUG = EventDebugPolicy.CANVAS_COMMAND_DELEGATION;\r
35 \r
36     @Override\r
37     public Object execute(ExecutionEvent event) throws ExecutionException {\r
38         Event e = ((Event)event.getTrigger());\r
39         CommandEvent ce = new CommandEvent(null, e.time, new Command(event.getCommand().getId()));\r
40 \r
41         if (DEBUG)\r
42             System.out.println(getClass().getSimpleName() + ": trigger event = " + e + " for command " + ce.command);\r
43 \r
44         if (e.widget instanceof ICanvasChassis) {\r
45             ICanvasChassis chassis = (ICanvasChassis) e.widget;\r
46             ICanvasContext canvas = chassis.getCanvasContext();\r
47             if (canvas != null) {\r
48                 if (DEBUG)\r
49                     System.out.println(getClass().getSimpleName() + ": sending command " + event.getCommand().getId() + " to " + chassis);\r
50                 canvas.getEventQueue().queueEvent( ce );\r
51             }\r
52         } else {\r
53             ICanvasContext canvas = null;\r
54             IWorkbenchPart part = HandlerUtil.getActiveEditor(event);\r
55             if (part != null)\r
56                 canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);\r
57             if (canvas == null) {\r
58                 part = HandlerUtil.getActivePart(event);\r
59                 if (part != null)\r
60                     canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);\r
61             }\r
62             if (canvas != null) {\r
63                 if (DEBUG)\r
64                     System.out.println(getClass().getSimpleName() + ": sending command " + event.getCommand().getId() + " to " + part);\r
65                 canvas.getEventQueue().queueEvent(ce);\r
66             }\r
67         }\r
68         return null;\r
69     }\r
70 \r
71 }