]> gerrit.simantics Code Review - simantics/platform.git/blob
62783b655224363d201ec30105e89dfb73cccca3
[simantics/platform.git] /
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.modeling.ui.diagramEditor;
13
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.action.ActionContributionItem;
16 import org.eclipse.jface.action.IContributionItem;
17 import org.eclipse.jface.action.ICoolBarManager;
18 import org.eclipse.jface.action.IToolBarManager;
19 import org.eclipse.jface.action.ToolBarContributionItem;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.IPartListener2;
23 import org.eclipse.ui.IWorkbenchPart;
24 import org.eclipse.ui.IWorkbenchPartReference;
25 import org.eclipse.ui.part.EditorActionBarContributor;
26 import org.simantics.g2d.canvas.Hints;
27 import org.simantics.g2d.canvas.ICanvasContext;
28 import org.simantics.g2d.canvas.IToolMode;
29 import org.simantics.modeling.ui.Activator;
30 import org.simantics.utils.datastructures.hints.HintListenerAdapter;
31 import org.simantics.utils.datastructures.hints.IHintContext.Key;
32 import org.simantics.utils.datastructures.hints.IHintListener;
33 import org.simantics.utils.datastructures.hints.IHintObservable;
34 import org.simantics.utils.threads.ThreadUtils;
35
36 /**
37  * @author Tuukka Lehtonen
38  */
39 public class DiagramViewerActionContributor extends EditorActionBarContributor {
40
41     private static final boolean DEBUG = false;
42
43     private IEditorPart    activePart;
44     private Display        display;
45
46     private ICanvasContext currentContext;
47
48     ModeAction             pointerAction;
49     ModeAction             connectAction;
50     IContributionItem      pointerItem;
51     IContributionItem      connectItem;
52     protected IToolBarManager        mgr;
53
54     IPartListener2 partListener = new IPartListener2() {
55         @Override
56         public void partVisible(IWorkbenchPartReference partRef) {
57         }
58         @Override
59         public void partOpened(IWorkbenchPartReference partRef) {
60         }
61         @Override
62         public void partInputChanged(IWorkbenchPartReference partRef) {
63         }
64         @Override
65         public void partHidden(IWorkbenchPartReference partRef) {
66         }
67         @Override
68         public void partDeactivated(IWorkbenchPartReference partRef) {
69             IWorkbenchPart part = partRef.getPart(false);
70             if (part == activePart) {
71                 setActiveEditor(null);
72             }
73         }
74         @Override
75         public void partClosed(IWorkbenchPartReference partRef) {
76         }
77         @Override
78         public void partBroughtToTop(IWorkbenchPartReference partRef) {
79         }
80         @Override
81         public void partActivated(IWorkbenchPartReference partRef) {
82         }
83     };
84
85     @Override
86     public void contributeToCoolBar(ICoolBarManager coolBarManager) {
87         IContributionItem item = coolBarManager.find("org.simantics.modeling.ui.diagramtoolbar");
88         if (item instanceof ToolBarContributionItem)
89             mgr = ((ToolBarContributionItem) item).getToolBarManager();
90         if (mgr == null)
91             return;
92
93         pointerAction = new ModeAction("org.simantics.modeling.ui.pointerMode", Hints.POINTERTOOL);
94         pointerAction.setText("Pointer Mode");
95         pointerAction.setImageDescriptor(Activator.POINTER_MODE);
96         connectAction = new ModeAction("org.simantics.modeling.ui.connectMode", Hints.CONNECTTOOL);
97         connectAction.setText("Connect Mode");
98         connectAction.setImageDescriptor(Activator.CONNECT_MODE);
99
100         pointerItem = new ActionContributionItem(pointerAction);
101         connectItem = new ActionContributionItem(connectAction);
102
103         mgr.appendToGroup("tool.additions", pointerItem);
104         mgr.appendToGroup("tool.additions", connectItem);
105         mgr.markDirty();
106
107         getPage().addPartListener(partListener);
108     }
109
110     @Override
111     public void dispose() {
112         getPage().removePartListener(partListener);
113
114         if (mgr != null) {
115             mgr.remove(connectItem);
116             mgr.remove(pointerItem);
117             connectItem.dispose();
118             pointerItem.dispose();
119             mgr.markDirty();
120             mgr.update(true);
121             if (activePart != null) {
122                 activePart.getEditorSite().getActionBars().updateActionBars();
123                 activePart = null;
124             }
125         }
126         setContext(null, null);
127         super.dispose();
128         setContext(null, null);
129         activePart = null;
130     }
131
132     public void setActiveEditor(IEditorPart part) {
133         if (DEBUG)
134             System.out.println("setActiveEditor: " + part);
135         if (part == activePart)
136             return;
137
138         this.activePart = part;
139         if (part != null) {
140             this.display = part.getSite().getShell().getDisplay();
141         }
142
143         ICanvasContext ctx = part != null ? (ICanvasContext) part.getAdapter(ICanvasContext.class) : null;
144         setContext(part, ctx);
145     }
146
147     private void setContext(IEditorPart part, ICanvasContext context) {
148         if (DEBUG)
149             System.out.println("setContext: " + part + "\nNEW CONTEXT: " + context + "\nPREVIOUS CONTEXT: " + currentContext);
150
151         ICanvasContext previous = currentContext;
152         currentContext = context;
153         if (previous != context) {
154             if (previous != null && !previous.isDisposed())
155                 previous.getHintStack().removeKeyHintListener(Hints.KEY_TOOL, TOOL_LISTENER);
156             if (context != null && !context.isDisposed())
157                 context.getHintStack().addKeyHintListener(Hints.KEY_TOOL, TOOL_LISTENER);
158         }
159
160         if (part != null && context != null && !context.isDisposed())
161             updateActionBars(part, (IToolMode) context.getHintStack().getHint(Hints.KEY_TOOL));
162         else
163             updateActionBars(null, null);
164     }
165
166     private void updateActionBars(IEditorPart part, IToolMode toolMode) {
167         if (DEBUG)
168             System.out.println("updateActionBars: " + part + ", " + toolMode);
169         updateToolMode(toolMode);
170         if (part != null)
171             part.getEditorSite().getActionBars().updateActionBars();
172     }
173
174     private void updateToolMode(IToolMode toolMode) {
175         if (DEBUG)
176             System.out.println("updateToolMode: " + toolMode);
177         if (toolMode != null) {
178             pointerAction.setEnabled(true);
179             connectAction.setEnabled(true);
180             if (toolMode == Hints.POINTERTOOL) {
181                 pointerAction.setChecked(true);
182                 connectAction.setChecked(false);
183             } else if (toolMode == Hints.CONNECTTOOL) {
184                 pointerAction.setChecked(false);
185                 connectAction.setChecked(true);
186             }
187         } else {
188             pointerAction.setEnabled(false);
189             connectAction.setEnabled(false);
190         }
191     }
192
193     IHintListener TOOL_LISTENER = new HintListenerAdapter() {
194         @Override
195         public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
196             final IToolMode mode = (IToolMode) newValue;
197             final IEditorPart part = activePart;
198             display.asyncExec(new Runnable() {
199                 @Override
200                 public void run() {
201                     if (display.isDisposed())
202                         return;
203                     if (activePart != part)
204                         return;
205                     updateActionBars(part, mode);
206                 }
207             });
208         }
209     };
210
211     public class ModeAction extends Action {
212
213         private IToolMode targetMode;
214
215         public ModeAction(String id, IToolMode targetMode) {
216             super(id, Action.AS_RADIO_BUTTON);
217             this.targetMode = targetMode;
218         }
219
220         @Override
221         public void run() {
222             final ICanvasContext context = currentContext;
223             if (context == null)
224                 return;
225
226             final IEditorPart part = activePart;
227             if (part == null)
228                 return;
229
230             if (context.isDisposed())
231                 return;
232
233             ThreadUtils.asyncExec(context.getThreadAccess(), new Runnable() {
234                 @Override
235                 public void run() {
236                     if (context.isDisposed())
237                         return;
238                     IToolMode toolMode = context.getHintStack().getHint(Hints.KEY_TOOL);
239                     if (!targetMode.equals(toolMode)) {
240                         if (DEBUG)
241                             System.out.println("changing tool mode to " + targetMode.getId());
242                         context.getDefaultHintContext().setHint(Hints.KEY_TOOL, targetMode);
243                     }
244                 }
245             });
246         }
247
248     }
249
250 }