blob: ca8b3516e1259e25dae9328958f5eafa31048d93 (
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
|
/*******************************************************************************
* Copyright (C) 2019 Thomas Wolf <thomas.wolf@paranor.ch>
*
* All rights reserved. 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
*******************************************************************************/
package org.eclipse.egit.ui.internal.components;
import java.util.Collection;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
/**
* Specialized {@link Action} intended to be used in a
* {@link org.eclipse.jface.action.IContributionManager IContributionManager}
* for actions with a drop-down menu. In a tool bar, selecting the button itself
* will by default also show the menu; if that is not desired, override
* {@link #runWithEvent(Event)} or {@link #run()}.
*/
public abstract class DropDownMenuAction extends Action
implements IWorkbenchAction, IMenuCreator {
private Menu controlMenu;
private Menu subMenu;
private boolean showMenu;
/**
* Creates a new {@link DropDownMenuAction}.
*
* @param title
* for the action
*/
public DropDownMenuAction(String title) {
super(title, IAction.AS_DROP_DOWN_MENU);
}
@Override
public void run() {
showMenu = true;
}
@Override
public void runWithEvent(Event event) {
if (!isEnabled()) {
return;
}
// Show the menu also when the button is clicked, unless run() is
// overridden (and not called via super).
showMenu = false;
run();
Widget widget = event.widget;
if (showMenu && (widget instanceof ToolItem)) {
ToolItem item = (ToolItem) widget;
Rectangle bounds = item.getBounds();
event.detail = SWT.ARROW;
event.x = bounds.x;
event.y = bounds.y + bounds.height;
item.notifyListeners(SWT.Selection, event);
}
}
@Override
public IMenuCreator getMenuCreator() {
return this;
}
private Menu fillMenu(Menu m) {
for (IAction action : getActions()) {
ActionContributionItem item = new ActionContributionItem(action);
item.fill(m, -1);
}
return m;
}
private Menu dispose(Menu m) {
if (m != null) {
if (!m.isDisposed()) {
m.dispose();
}
}
return null;
}
@Override
public Menu getMenu(Menu parent) {
subMenu = dispose(subMenu);
subMenu = fillMenu(new Menu(parent));
return subMenu;
}
@Override
public Menu getMenu(Control parent) {
controlMenu = dispose(controlMenu);
controlMenu = fillMenu(new Menu(parent));
return controlMenu;
}
/**
* Obtains the actions to display in the drop-down menu.
*
* @return the actions
*/
@NonNull
protected abstract Collection<IAction> getActions();
@Override
public void dispose() {
controlMenu = dispose(controlMenu);
subMenu = dispose(subMenu);
}
}
|