X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fhandler%2Fe4%2FCanvasCommandDelegate.java;fp=bundles%2Forg.simantics.diagram%2Fsrc%2Forg%2Fsimantics%2Fdiagram%2Fhandler%2Fe4%2FCanvasCommandDelegate.java;h=e69192addd408e77f9c26db3b205f7ba418703e9;hp=0e7e7dd34f74864701c1659f799a195b99d88a49;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/e4/CanvasCommandDelegate.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/e4/CanvasCommandDelegate.java index 0e7e7dd34..e69192add 100644 --- a/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/e4/CanvasCommandDelegate.java +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/handler/e4/CanvasCommandDelegate.java @@ -1,92 +1,92 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.diagram.handler.e4; - -import javax.inject.Named; - -import org.eclipse.core.runtime.IAdaptable; -import org.eclipse.e4.core.di.annotations.CanExecute; -import org.eclipse.e4.core.di.annotations.Execute; -import org.eclipse.e4.ui.model.application.ui.basic.MPart; -import org.eclipse.e4.ui.services.IServiceConstants; -import org.eclipse.ui.IEditorPart; -import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor; -import org.simantics.g2d.canvas.ICanvasContext; -import org.simantics.scenegraph.g2d.events.EventDebugPolicy; -import org.simantics.scenegraph.g2d.events.EventQueue; -import org.simantics.scenegraph.g2d.events.command.Command; -import org.simantics.scenegraph.g2d.events.command.CommandEvent; -import org.simantics.utils.ui.workbench.WorkbenchUtils; - -/** - * A handler for the Eclipse Command framework that delegates ID of the command - * that triggered the event to the {@link EventQueue} of the - * {@link ICanvasContext} where the event occurred. - */ -public class CanvasCommandDelegate { - - private final boolean DEBUG = EventDebugPolicy.CANVAS_COMMAND_DELEGATION; - - private static final String ACTIVE_PART_PARAMETER = "org.simantics.diagram.commandparameter.canvasCommandHandlerActiveWithPart"; - private static final String DELEGATE_PARAMETER = "org.simantics.diagram.commandparameter.canvasCommandDelegateParameter"; - - @CanExecute - public boolean canExecute(@Named(ACTIVE_PART_PARAMETER) String activePartId, @Named(IServiceConstants.ACTIVE_PART) MPart activePart) { - if (activePart == null) - return false; - if (!(activePart.getObject() instanceof CompatibilityEditor)) - return false; - CompatibilityEditor compatibilityEditor = (CompatibilityEditor) activePart.getObject(); - IEditorPart part = compatibilityEditor.getEditor(); - if (part == null) - return false; - ICanvasContext ctx = part.getAdapter(ICanvasContext.class); - if (ctx == null) - return false; - String id = compatibilityEditor.getReference().getId(); - if (!(id.equals(activePartId))) - return false; - return true; - } - - @SuppressWarnings("restriction") - @Execute - public void execute(@Named(DELEGATE_PARAMETER) String commandId, @Named(IServiceConstants.ACTIVE_PART) MPart activePart) { - CommandEvent ce = new CommandEvent(null, System.currentTimeMillis(), new Command(commandId)); - if (DEBUG) - System.out.println(getClass().getSimpleName() + ": trigger event for command " + ce.command); - - Object object = activePart.getObject(); - - IAdaptable part = null; - if (object instanceof CompatibilityEditor) { - CompatibilityEditor editor = (CompatibilityEditor) object; - part = editor.getPart(); - } else if (object instanceof IAdaptable){ - part = (IAdaptable) object; - } - ICanvasContext canvas = null; - if (part != null) - canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class); - if (canvas == null) { - part = WorkbenchUtils.getActiveWorkbenchPart(); - if (part != null) - canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class); - } - if (canvas != null) { - if (DEBUG) - System.out.println(getClass().getSimpleName() + ": sending command " + commandId + " to " + part); - canvas.getEventQueue().queueEvent(ce); - } - } - +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.diagram.handler.e4; + +import javax.inject.Named; + +import org.eclipse.core.runtime.IAdaptable; +import org.eclipse.e4.core.di.annotations.CanExecute; +import org.eclipse.e4.core.di.annotations.Execute; +import org.eclipse.e4.ui.model.application.ui.basic.MPart; +import org.eclipse.e4.ui.services.IServiceConstants; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor; +import org.simantics.g2d.canvas.ICanvasContext; +import org.simantics.scenegraph.g2d.events.EventDebugPolicy; +import org.simantics.scenegraph.g2d.events.EventQueue; +import org.simantics.scenegraph.g2d.events.command.Command; +import org.simantics.scenegraph.g2d.events.command.CommandEvent; +import org.simantics.utils.ui.workbench.WorkbenchUtils; + +/** + * A handler for the Eclipse Command framework that delegates ID of the command + * that triggered the event to the {@link EventQueue} of the + * {@link ICanvasContext} where the event occurred. + */ +public class CanvasCommandDelegate { + + private final boolean DEBUG = EventDebugPolicy.CANVAS_COMMAND_DELEGATION; + + private static final String ACTIVE_PART_PARAMETER = "org.simantics.diagram.commandparameter.canvasCommandHandlerActiveWithPart"; + private static final String DELEGATE_PARAMETER = "org.simantics.diagram.commandparameter.canvasCommandDelegateParameter"; + + @CanExecute + public boolean canExecute(@Named(ACTIVE_PART_PARAMETER) String activePartId, @Named(IServiceConstants.ACTIVE_PART) MPart activePart) { + if (activePart == null) + return false; + if (!(activePart.getObject() instanceof CompatibilityEditor)) + return false; + CompatibilityEditor compatibilityEditor = (CompatibilityEditor) activePart.getObject(); + IEditorPart part = compatibilityEditor.getEditor(); + if (part == null) + return false; + ICanvasContext ctx = part.getAdapter(ICanvasContext.class); + if (ctx == null) + return false; + String id = compatibilityEditor.getReference().getId(); + if (!(id.equals(activePartId))) + return false; + return true; + } + + @SuppressWarnings("restriction") + @Execute + public void execute(@Named(DELEGATE_PARAMETER) String commandId, @Named(IServiceConstants.ACTIVE_PART) MPart activePart) { + CommandEvent ce = new CommandEvent(null, System.currentTimeMillis(), new Command(commandId)); + if (DEBUG) + System.out.println(getClass().getSimpleName() + ": trigger event for command " + ce.command); + + Object object = activePart.getObject(); + + IAdaptable part = null; + if (object instanceof CompatibilityEditor) { + CompatibilityEditor editor = (CompatibilityEditor) object; + part = editor.getPart(); + } else if (object instanceof IAdaptable){ + part = (IAdaptable) object; + } + ICanvasContext canvas = null; + if (part != null) + canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class); + if (canvas == null) { + part = WorkbenchUtils.getActiveWorkbenchPart(); + if (part != null) + canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class); + } + if (canvas != null) { + if (DEBUG) + System.out.println(getClass().getSimpleName() + ": sending command " + commandId + " to " + part); + canvas.getEventQueue().queueEvent(ce); + } + } + } \ No newline at end of file