1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.diagramEditor;
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;
37 * @author Tuukka Lehtonen
39 public class DiagramViewerActionContributor extends EditorActionBarContributor {
41 private static final boolean DEBUG = false;
43 private IEditorPart activePart;
44 private Display display;
46 private ICanvasContext currentContext;
48 ModeAction pointerAction;
49 ModeAction connectAction;
50 IContributionItem pointerItem;
51 IContributionItem connectItem;
52 protected IToolBarManager mgr;
54 IPartListener2 partListener = new IPartListener2() {
56 public void partVisible(IWorkbenchPartReference partRef) {
59 public void partOpened(IWorkbenchPartReference partRef) {
62 public void partInputChanged(IWorkbenchPartReference partRef) {
65 public void partHidden(IWorkbenchPartReference partRef) {
68 public void partDeactivated(IWorkbenchPartReference partRef) {
69 IWorkbenchPart part = partRef.getPart(false);
70 if (part == activePart) {
71 setActiveEditor(null);
75 public void partClosed(IWorkbenchPartReference partRef) {
78 public void partBroughtToTop(IWorkbenchPartReference partRef) {
81 public void partActivated(IWorkbenchPartReference partRef) {
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();
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);
100 pointerItem = new ActionContributionItem(pointerAction);
101 connectItem = new ActionContributionItem(connectAction);
103 mgr.appendToGroup("tool.additions", pointerItem);
104 mgr.appendToGroup("tool.additions", connectItem);
107 getPage().addPartListener(partListener);
111 public void dispose() {
112 getPage().removePartListener(partListener);
115 mgr.remove(connectItem);
116 mgr.remove(pointerItem);
117 connectItem.dispose();
118 pointerItem.dispose();
121 if (activePart != null) {
122 activePart.getEditorSite().getActionBars().updateActionBars();
126 setContext(null, null);
128 setContext(null, null);
132 public void setActiveEditor(IEditorPart part) {
134 System.out.println("setActiveEditor: " + part);
135 if (part == activePart)
138 this.activePart = part;
140 this.display = part.getSite().getShell().getDisplay();
143 ICanvasContext ctx = part != null ? (ICanvasContext) part.getAdapter(ICanvasContext.class) : null;
144 setContext(part, ctx);
147 private void setContext(IEditorPart part, ICanvasContext context) {
149 System.out.println("setContext: " + part + "\nNEW CONTEXT: " + context + "\nPREVIOUS CONTEXT: " + currentContext);
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);
160 if (part != null && context != null && !context.isDisposed())
161 updateActionBars(part, (IToolMode) context.getHintStack().getHint(Hints.KEY_TOOL));
163 updateActionBars(null, null);
166 private void updateActionBars(IEditorPart part, IToolMode toolMode) {
168 System.out.println("updateActionBars: " + part + ", " + toolMode);
169 updateToolMode(toolMode);
171 part.getEditorSite().getActionBars().updateActionBars();
174 private void updateToolMode(IToolMode toolMode) {
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);
188 pointerAction.setEnabled(false);
189 connectAction.setEnabled(false);
193 IHintListener TOOL_LISTENER = new HintListenerAdapter() {
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() {
201 if (display.isDisposed())
203 if (activePart != part)
205 updateActionBars(part, mode);
211 public class ModeAction extends Action {
213 private IToolMode targetMode;
215 public ModeAction(String id, IToolMode targetMode) {
216 super(id, Action.AS_RADIO_BUTTON);
217 this.targetMode = targetMode;
222 final ICanvasContext context = currentContext;
226 final IEditorPart part = activePart;
230 if (context.isDisposed())
233 ThreadUtils.asyncExec(context.getThreadAccess(), new Runnable() {
236 if (context.isDisposed())
238 IToolMode toolMode = context.getHintStack().getHint(Hints.KEY_TOOL);
239 if (!targetMode.equals(toolMode)) {
241 System.out.println("changing tool mode to " + targetMode.getId());
242 context.getDefaultHintContext().setHint(Hints.KEY_TOOL, targetMode);