1 /*******************************************************************************
2 * Copyright (c) 2013 Association for Decentralized Information Management in
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 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor.handlers.e4;
14 import javax.inject.Inject;
15 import javax.inject.Named;
17 import org.eclipse.core.runtime.IAdaptable;
18 import org.eclipse.e4.core.contexts.Active;
19 import org.eclipse.e4.core.di.annotations.CanExecute;
20 import org.eclipse.e4.core.di.annotations.Execute;
21 import org.eclipse.e4.core.di.annotations.Optional;
22 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23 import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
24 import org.eclipse.e4.ui.model.application.ui.menu.MToolItem;
25 import org.eclipse.e4.ui.services.IServiceConstants;
26 import org.eclipse.e4.ui.workbench.modeling.EModelService;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.IWorkbenchPart;
29 import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
30 import org.simantics.g2d.diagram.DiagramHints;
31 import org.simantics.g2d.diagram.IDiagram;
32 import org.simantics.g2d.layers.ILayersEditor;
33 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;
34 import org.simantics.utils.ui.workbench.WorkbenchUtils;
37 * @author Tuukka Lehtonen
39 @SuppressWarnings("restriction")
40 public class ToggleFocusabilityHandler {
42 private static final String HANDLED_ITEM_ID = "fi.vtt.apros.ui.diagram.handledtoolitem.enableimageeditability";
45 private EModelService modelService;
47 private void updateStateForPart(MPart part) {
48 IWorkbenchPart wbPart = tryGetWorkbenchPart(part);
50 MWindow win = modelService.getTopLevelWindowFor(part);
51 MToolItem item = (MToolItem) modelService.find(HANDLED_ITEM_ID, win);
52 ILayersEditor le = getLayers(wbPart);
53 if (item != null && le != null) {
54 setToolItemState(item, le.getIgnoreFocusSettings());
59 // tracks the active part
62 public void receiveActivePart(@Named(IServiceConstants.ACTIVE_PART) MPart activePart) {
63 updateStateForPart(activePart);
67 public boolean canExecute(@Active MPart part) {
68 return tryGetWorkbenchPart(part) instanceof DiagramEditor;
72 public void execute(@Optional MToolItem toolItem) {
73 ILayersEditor le = getLayers();
75 boolean newValue = !le.getIgnoreFocusSettings();
76 le.setIgnoreFocusSettings( newValue );
77 if (toolItem != null) {
78 setToolItemState(toolItem, newValue);
83 private void setToolItemState(MToolItem item, boolean ignoreFocusSettings) {
84 item.setSelected(ignoreFocusSettings);
85 item.setTooltip((ignoreFocusSettings ? "Deny" : "Allow") + " Focusing and Editing of Images");
88 protected ILayersEditor getLayers() {
89 DiagramEditor editor = getEditor();
92 return getLayers(editor);
95 protected ILayersEditor getLayers(IAdaptable editor) {
96 // The diagram might not be available since the diagram editor loads it asynchronously.
97 IDiagram diagram = (IDiagram) editor.getAdapter(IDiagram.class);
100 //System.out.println("getLayersEditor(" + diagram + ")");
101 ILayersEditor le = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
105 protected DiagramEditor getEditor() {
106 IEditorPart editorPart = WorkbenchUtils.getActiveEditor();
107 if (editorPart == null)
109 if (editorPart instanceof DiagramEditor) {
110 DiagramEditor editor = (DiagramEditor) editorPart;
116 private IWorkbenchPart tryGetWorkbenchPart(MPart part) {
119 Object obj = part.getObject();
120 if (obj instanceof CompatibilityEditor) {
121 CompatibilityEditor editor = (CompatibilityEditor) obj;
122 return editor.getPart();
123 } else if (obj instanceof IWorkbenchPart) {
124 return (IWorkbenchPart) obj;