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