]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/handler/e4/CanvasCommandDelegate.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / handler / e4 / 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.e4;
13
14 import javax.inject.Named;
15
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;
29
30 /**
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.
34  */
35 public class CanvasCommandDelegate {
36
37     private final boolean DEBUG = EventDebugPolicy.CANVAS_COMMAND_DELEGATION;
38     
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";
41
42     @CanExecute
43     public boolean canExecute(@Named(ACTIVE_PART_PARAMETER) String activePartId, @Named(IServiceConstants.ACTIVE_PART) MPart activePart) {
44         if (activePart == null)
45             return false;
46         if (!(activePart.getObject() instanceof CompatibilityEditor))
47             return false;
48         CompatibilityEditor compatibilityEditor = (CompatibilityEditor) activePart.getObject();
49         IEditorPart part = compatibilityEditor.getEditor();
50         if (part == null)
51                 return false;
52         ICanvasContext ctx = part.getAdapter(ICanvasContext.class);
53         if (ctx == null)
54             return false;
55         String id = compatibilityEditor.getReference().getId();
56         if (!(id.equals(activePartId)))
57             return false;
58         return true;
59     }
60     
61     @SuppressWarnings("restriction")
62     @Execute
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));
65         if (DEBUG)
66             System.out.println(getClass().getSimpleName() + ": trigger event for command " + ce.command);
67
68         Object object = activePart.getObject();
69         
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; 
76         }
77         ICanvasContext canvas = null;
78         if (part != null)
79             canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);
80         if (canvas == null) {
81             part = WorkbenchUtils.getActiveWorkbenchPart();
82             if (part != null)
83                 canvas = (ICanvasContext) part.getAdapter(ICanvasContext.class);
84         }
85         if (canvas != null) {
86             if (DEBUG)
87                 System.out.println(getClass().getSimpleName() + ": sending command " + commandId + " to " + part);
88             canvas.getEventQueue().queueEvent(ce);
89         }
90     }
91
92 }