]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/e4/ToggleFocusabilityHandler.java
Merge "Imported ModuleRepository.flush from Java"
[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 javax.inject.Inject;
15 import javax.inject.Named;
16
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;
35
36 /**
37  * @author Tuukka Lehtonen
38  */
39 @SuppressWarnings("restriction")
40 public class ToggleFocusabilityHandler {
41
42         private static final String HANDLED_ITEM_ID = "fi.vtt.apros.ui.diagram.handledtoolitem.enableimageeditability";
43
44         @Inject
45         private EModelService modelService;
46
47         private void updateStateForPart(MPart part) {
48                 IWorkbenchPart wbPart = tryGetWorkbenchPart(part);
49                 if (wbPart != null) {
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());
55                         }
56                 }
57         }
58
59         // tracks the active part
60         @Inject
61         @Optional
62         public void receiveActivePart(@Named(IServiceConstants.ACTIVE_PART) MPart activePart) {
63                 updateStateForPart(activePart);
64         }
65
66         @CanExecute
67         public boolean canExecute(@Active MPart part) {
68                 return tryGetWorkbenchPart(part) instanceof DiagramEditor;
69         }
70
71         @Execute
72         public void execute(@Optional MToolItem toolItem) {
73                 ILayersEditor le = getLayers();
74                 if (le != null) {
75                         boolean newValue = !le.getIgnoreFocusSettings();
76                         le.setIgnoreFocusSettings( newValue );
77                         if (toolItem != null) {
78                                 setToolItemState(toolItem, newValue);
79                         }
80                 }
81         }
82
83         private void setToolItemState(MToolItem item, boolean ignoreFocusSettings) {
84                 item.setSelected(ignoreFocusSettings);
85                 item.setTooltip((ignoreFocusSettings ? "Deny" : "Allow") + " Focusing and Editing of Images");
86         }
87
88         protected ILayersEditor getLayers() {
89                 DiagramEditor editor = getEditor();
90                 if (editor == null)
91                         return null;
92                 return getLayers(editor);
93         }
94
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);
98                 if (diagram == null)
99                         return null;
100                 //System.out.println("getLayersEditor(" + diagram + ")");
101                 ILayersEditor le = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
102                 return le;
103         }
104
105         protected DiagramEditor getEditor() {
106                 IEditorPart editorPart = WorkbenchUtils.getActiveEditor();
107                 if (editorPart == null)
108                         return null;
109                 if (editorPart instanceof DiagramEditor) {
110                         DiagramEditor editor = (DiagramEditor) editorPart;
111                         return editor;
112                 }
113                 return null;
114         }
115
116         private IWorkbenchPart tryGetWorkbenchPart(MPart part) {
117                 if (part == null)
118                         return null;
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; 
125                 }
126                 return null;
127         }
128
129 }