]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagramEditor/handlers/ToggleFocusabilityHandler.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagramEditor / handlers / 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;
13
14 import java.util.Map;
15
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.PlatformUI;
21 import org.eclipse.ui.commands.ICommandService;
22 import org.eclipse.ui.commands.IElementUpdater;
23 import org.eclipse.ui.menus.UIElement;
24 import org.simantics.g2d.diagram.DiagramHints;
25 import org.simantics.g2d.diagram.IDiagram;
26 import org.simantics.g2d.layers.ILayersEditor;
27 import org.simantics.modeling.ui.diagramEditor.DiagramEditor;
28 import org.simantics.utils.ui.workbench.WorkbenchUtils;
29
30 /**
31  * @author Tuukka Lehtonen
32  */
33 public class ToggleFocusabilityHandler extends AbstractHandler implements IElementUpdater {
34
35         @Override
36         public Object execute(ExecutionEvent event) throws ExecutionException {
37                 ILayersEditor le = getLayers();
38                 if (le != null) {
39                         boolean b = le.getIgnoreFocusSettings();
40                         le.setIgnoreFocusSettings( !b );
41
42                         ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
43                         service.refreshElements(event.getCommand().getId(), null);
44                 }
45                 return null;
46         }
47
48         @SuppressWarnings("rawtypes")
49         @Override
50         public void updateElement(UIElement element, Map parameters) {
51                 //System.out.println("updateElement: " + element);
52                 DiagramEditor editor = getEditor();
53                 if (editor != null) {
54                         //System.out.println("updateElement2: " + editor);
55                         ILayersEditor le = getLayers(editor);
56                         if (le != null) {
57                                 boolean bool = le.getIgnoreFocusSettings();
58                                 //System.out.println("get ignore focus setting: " + bool);
59                                 element.setChecked(bool);
60                                 element.setTooltip((bool ? "Deny" : "Allow") + " Focusing and Editing of Images");
61                                 return;
62                         }
63                 }
64         }
65
66         protected ILayersEditor getLayers() {
67                 DiagramEditor editor = getEditor();
68                 if (editor == null)
69                         return null;
70                 return getLayers(editor);
71         }
72
73         protected ILayersEditor getLayers(DiagramEditor editor) {
74                 // The diagram might not be available since the diagram editor loads it asynchronously.
75                 IDiagram diagram = (IDiagram) editor.getAdapter(IDiagram.class);
76                 if (diagram == null)
77                         return null;
78                 //System.out.println("getLayersEditor(" + diagram + ")");
79                 ILayersEditor le = diagram.getHint(DiagramHints.KEY_LAYERS_EDITOR);
80                 return le;
81         }
82
83         protected DiagramEditor getEditor() {
84                 IEditorPart editorPart = WorkbenchUtils.getActiveEditor();
85                 if (editorPart == null)
86                         return null;
87                 if (editorPart instanceof DiagramEditor) {
88                         DiagramEditor editor = (DiagramEditor) editorPart;
89                         return editor;
90                 }
91                 return null;
92         }
93
94 }