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.e4;
14 import javax.inject.Named;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.e4.core.di.annotations.CanExecute;
18 import org.eclipse.e4.core.di.annotations.Execute;
19 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
20 import org.eclipse.e4.ui.services.IServiceConstants;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
23 import org.simantics.g2d.canvas.ICanvasContext;
24 import org.simantics.scenegraph.g2d.events.EventDebugPolicy;
25 import org.simantics.scenegraph.g2d.events.EventQueue;
26 import org.simantics.scenegraph.g2d.events.command.Command;
27 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
28 import org.simantics.utils.ui.workbench.WorkbenchUtils;
31 * A handler for the Eclipse Command framework that delegates ID of the command
32 * that triggered the event to the {@link EventQueue} of the
33 * {@link ICanvasContext} where the event occurred.
35 public class CanvasCommandDelegate {
37 private final boolean DEBUG = EventDebugPolicy.CANVAS_COMMAND_DELEGATION;
39 private static final String ACTIVE_PART_PARAMETER = "org.simantics.diagram.commandparameter.canvasCommandHandlerActiveWithPart";
40 private static final String DELEGATE_PARAMETER = "org.simantics.diagram.commandparameter.canvasCommandDelegateParameter";
43 public boolean canExecute(@Named(ACTIVE_PART_PARAMETER) String activePartId, @Named(IServiceConstants.ACTIVE_PART) MPart activePart) {
44 if (activePart == null)
46 if (!(activePart.getObject() instanceof CompatibilityEditor))
48 CompatibilityEditor compatibilityEditor = (CompatibilityEditor) activePart.getObject();
49 IEditorPart part = compatibilityEditor.getEditor();
52 ICanvasContext ctx = part.getAdapter(ICanvasContext.class);
55 String id = compatibilityEditor.getReference().getId();
56 if (!(id.equals(activePartId)))
61 @SuppressWarnings("restriction")
63 public void execute(@Named(DELEGATE_PARAMETER) String commandId, @Named(IServiceConstants.ACTIVE_PART) MPart activePart) {
64 CommandEvent ce = new CommandEvent(null, System.currentTimeMillis(), new Command(commandId));
66 System.out.println(getClass().getSimpleName() + ": trigger event for command " + ce.command);
68 Object object = activePart.getObject();
70 IAdaptable part = null;
71 if (object instanceof CompatibilityEditor) {
72 CompatibilityEditor editor = (CompatibilityEditor) object;
73 part = editor.getPart();
74 } else if (object instanceof IAdaptable){
75 part = (IAdaptable) object;
77 ICanvasContext canvas = null;
79 canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);
81 part = WorkbenchUtils.getActiveWorkbenchPart();
83 canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);
87 System.out.println(getClass().getSimpleName() + ": sending command " + commandId + " to " + part);
88 canvas.getEventQueue().queueEvent(ce);