1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.diagram.handler;
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;
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.
32 public class CanvasCommandDelegate extends AbstractHandler {
34 private final boolean DEBUG = EventDebugPolicy.CANVAS_COMMAND_DELEGATION;
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()));
42 System.out.println(getClass().getSimpleName() + ": trigger event = " + e + " for command " + ce.command);
44 if (e.widget instanceof ICanvasChassis) {
45 ICanvasChassis chassis = (ICanvasChassis) e.widget;
46 ICanvasContext canvas = chassis.getCanvasContext();
49 System.out.println(getClass().getSimpleName() + ": sending command " + event.getCommand().getId() + " to " + chassis);
50 canvas.getEventQueue().queueEvent( ce );
53 ICanvasContext canvas = null;
54 IWorkbenchPart part = HandlerUtil.getActiveEditor(event);
56 canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);
58 part = HandlerUtil.getActivePart(event);
60 canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);
64 System.out.println(getClass().getSimpleName() + ": sending command " + event.getCommand().getId() + " to " + part);
65 canvas.getEventQueue().queueEvent(ce);