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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.handler;
\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
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
32 public class CanvasCommandDelegate extends AbstractHandler {
\r
34 private final boolean DEBUG = EventDebugPolicy.CANVAS_COMMAND_DELEGATION;
\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
42 System.out.println(getClass().getSimpleName() + ": trigger event = " + e + " for command " + ce.command);
\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
49 System.out.println(getClass().getSimpleName() + ": sending command " + event.getCommand().getId() + " to " + chassis);
\r
50 canvas.getEventQueue().queueEvent( ce );
\r
53 ICanvasContext canvas = null;
\r
54 IWorkbenchPart part = HandlerUtil.getActiveEditor(event);
\r
56 canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);
\r
57 if (canvas == null) {
\r
58 part = HandlerUtil.getActivePart(event);
\r
60 canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);
\r
62 if (canvas != null) {
\r
64 System.out.println(getClass().getSimpleName() + ": sending command " + event.getCommand().getId() + " to " + part);
\r
65 canvas.getEventQueue().queueEvent(ce);
\r