]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/e4/ToggleFocusabilityHandler.java
8eef0300be53300746e4ee4ef37d52d51da32f87
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / handlers / e4 / ToggleFocusabilityHandler.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Association for Decentralized Information Management in
3  * 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor.handlers.e4;
13
14 import org.eclipse.e4.core.contexts.Active;
15 import org.eclipse.e4.core.di.annotations.CanExecute;
16 import org.eclipse.e4.core.di.annotations.Execute;
17 import org.eclipse.e4.core.di.annotations.Optional;
18 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
19 import org.eclipse.e4.ui.model.application.ui.menu.MToolItem;
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
22 import org.simantics.g2d.diagram.DiagramHints;
23 import org.simantics.g2d.diagram.IDiagram;
24 import org.simantics.g2d.layers.ILayersEditor;
25 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;
26 import org.simantics.utils.ui.workbench.WorkbenchUtils;
27
28 /**
29  * @author Tuukka Lehtonen
30  */
31 public class ToggleFocusabilityHandler {
32
33     @CanExecute
34     public boolean canExecute(@Active MPart part) {
35         if (!(part.getObject() instanceof CompatibilityEditor))
36             return false;
37         CompatibilityEditor editor = (CompatibilityEditor) part.getObject();
38         if (!(editor.getPart() instanceof DiagramEditor))
39             return false;
40         return true;
41     }
42     
43         @Execute
44         public void execute(@Optional MToolItem toolItem) {
45                 ILayersEditor le = getLayers();
46                 if (le != null) {
47                         boolean b = le.getIgnoreFocusSettings();
48                         le.setIgnoreFocusSettings( !b );
49
50                         if (toolItem != null) {
51                             toolItem.setSelected(!b);
52                             toolItem.setTooltip((!b ? "Deny" : "Allow") + " Focusing and Editing of Images");
53                         }
54                 }
55         }
56
57         protected ILayersEditor getLayers() {
58                 DiagramEditor editor = getEditor();
59                 if (editor == null)
60                         return null;
61                 return getLayers(editor);
62         }
63
64         protected ILayersEditor getLayers(DiagramEditor editor) {
65                 // The diagram might not be available since the diagram editor loads it asynchronously.
66                 IDiagram diagram = (IDiagram) editor.getAdapter(IDiagram.class);
67                 if (diagram == null)
68                         return null;
69                 //System.out.println("getLayersEditor(" + diagram + ")");
70                 ILayersEditor le = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
71                 return le;
72         }
73
74         protected DiagramEditor getEditor() {
75                 IEditorPart editorPart = WorkbenchUtils.getActiveEditor();
76                 if (editorPart == null)
77                         return null;
78                 if (editorPart instanceof DiagramEditor) {
79                         DiagramEditor editor = (DiagramEditor) editorPart;
80                         return editor;
81                 }
82                 return null;
83         }
84
85 }