Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ebe082069a15033114c9d2eecadbaa0d91d51758 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*******************************************************************************
 * Copyright (c) 2007, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Friederike Schertel <friederike@schertel.org> - Bug 478336
 ******************************************************************************/

package org.eclipse.ui.internal;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.expressions.EvaluationResult;
import org.eclipse.core.expressions.Expression;
import org.eclipse.core.expressions.ExpressionInfo;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
import org.eclipse.e4.ui.workbench.IPresentationEngine;
import org.eclipse.e4.ui.workbench.renderers.swt.StackRenderer;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.ui.ISources;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.handlers.HandlerUtil;

/**
 * Shows the View Menu
 * <p>
 * Replacement for: ShowViewMenuAction
 * </p>
 *
 * @since 3.3
 *
 */
public class ShowViewMenuHandler extends AbstractEvaluationHandler {

	private Expression enabledWhen;

	public ShowViewMenuHandler() {
		registerEnablement();
	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchPart part = HandlerUtil.getActivePart(event);
		if (part != null) {
			IWorkbenchPartSite site = part.getSite();
			if (site instanceof PartSite) {
				final MPart model = ((PartSite) site).getModel();
				Composite partContainer = (Composite) model.getWidget();
				if (partContainer != null) {
					Composite parent = partContainer.getParent();
					while (parent != null) {
						if (parent instanceof CTabFolder) {
							CTabFolder ctf = (CTabFolder) parent;
							final Control topRight = ctf.getTopRight();
							if (topRight instanceof Composite) {
								for (Control child : ((Composite) topRight).getChildren()) {
									if (child instanceof ToolBar && "ViewMenu".equals(child.getData())) { //$NON-NLS-1$
										ToolBar tb = (ToolBar) child;
										ToolItem ti = tb.getItem(0);
										Event sevent = new Event();
										sevent.type = SWT.Selection;
										sevent.widget = ti;
										ti.notifyListeners(SWT.Selection, sevent);
									}
								}
							}
							return null;
						}
						parent = parent.getParent();
					}

					MMenu menuModel = StackRenderer.getViewMenu(model);
					if (menuModel != null) {
						showStandaloneViewMenu(event, model, menuModel, partContainer);
					}
				}
			}
		}
		return null;
	}

	private void showStandaloneViewMenu(ExecutionEvent event, MPart model, MMenu menuModel, Composite partContainer) {
		Shell shell = partContainer.getShell();
		Menu menu = (Menu) menuModel.getWidget();
		if (menu == null) {
			IPresentationEngine engine = (IPresentationEngine) HandlerUtil.getVariable(event,
					IPresentationEngine.class.getName());
			menu = (Menu) engine.createGui(menuModel, shell, model.getContext());
			if (menu != null) {
				final Menu tmpMenu = menu;
				partContainer.addDisposeListener(e -> tmpMenu.dispose());
			}
		}

		Display display = menu.getDisplay();
		Point location = display.map(partContainer, null, partContainer.getLocation());
		Point size = partContainer.getSize();
		menu.setLocation(location.x + size.x, location.y);
		menu.setVisible(true);

		while (!menu.isDisposed() && menu.isVisible()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

		if (!(menu.getData() instanceof MenuManager)) {
			menu.dispose();
		}
	}

	@Override
	protected Expression getEnabledWhenExpression() {
		if (enabledWhen == null) {
			enabledWhen = new Expression() {
				@Override
				public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
					// IWorkbenchPart part = InternalHandlerUtil
					// .getActivePart(context);
					// if (part != null) {
					// PartPane pane = ((PartSite) part.getSite()).getPane();
					// if ((pane instanceof ViewPane)
					// && ((ViewPane) pane).hasViewMenu()) {
					return EvaluationResult.TRUE;
					// }
					// }
					// return EvaluationResult.FALSE;
				}

				@Override
				public void collectExpressionInfo(ExpressionInfo info) {
					info.addVariableNameAccess(ISources.ACTIVE_PART_NAME);
				}
			};
		}
		return enabledWhen;
	}

}

Back to the top