This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
/
ViewMenuDynamicContribution.java
137 lines (116 loc) · 4.89 KB
/
ViewMenuDynamicContribution.java
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
package goko.contribution;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.core.commands.ParameterizedCommand;
import org.eclipse.e4.core.commands.ECommandService;
import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.ui.di.AboutToHide;
import org.eclipse.e4.ui.di.AboutToShow;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.commands.MCommand;
import org.eclipse.e4.ui.model.application.commands.MCommandsFactory;
import org.eclipse.e4.ui.model.application.commands.MParameter;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.goko.core.log.GkLog;
public class ViewMenuDynamicContribution {
private static GkLog LOG = GkLog.getLogger(ViewMenuDynamicContribution.class);
public static final String VIEW_MENU_ENTRY_TAG = "view";
public static final String VIEW_NAME_PARAMETER = "org.goko.commands.toggleView.viewName";
@Inject
private EPartService partService;
@Inject
private EModelService modelService;
@Inject
@Optional
private MApplication application;
@Inject
private ECommandService commandService;
@Inject
@Optional
@Named("goko.menu.window")
private MMenu menu;
@AboutToHide
public void aboutToHide(List<MMenuElement> items) {
}
@AboutToShow
public void aboutToShow(List<MMenuElement> items) {
Collection<MPart> parts = partService.getParts();
Iterator<MPart> iterator = parts.iterator();
List<MMenuElement> children = new ArrayList<MMenuElement>();
List<String> existingChidlrenIds = new ArrayList<String>();
MMenu viewMenu = getViewMenu(); // Get existing items. Dirty hack against eclipse bug ?
for (MMenuElement windowsElement : viewMenu.getChildren()) {
existingChidlrenIds.add(windowsElement.getElementId());
}
List<MCommand> commands = modelService.findElements(application, "goko.command.toggleView", MCommand.class, null);
if(CollectionUtils.isNotEmpty(commands)){
MCommand modeledCommand = commands.get(0);
while (iterator.hasNext()) {
MPart mPart = iterator.next();
if(mPart.getTags().contains(VIEW_MENU_ENTRY_TAG)){
String menuItemId = "goko.menu.window.view.menuitem."+mPart.getElementId();
// Only creates the button if it doesn't exist yet
if(!existingChidlrenIds.contains(menuItemId)){
MHandledMenuItem item = MMenuFactory.INSTANCE.createHandledMenuItem();
item.setElementId(menuItemId);
item.setLabel(mPart.getLabel());
item.setTooltip(mPart.getLabel());
item.setIconURI(mPart.getIconURI());
item.setContributorURI("platform:/plugin/Goko");
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put(VIEW_NAME_PARAMETER, mPart.getElementId());
ParameterizedCommand command = commandService.createCommand("goko.command.toggleView", parameters);
MParameter parameter = MCommandsFactory.INSTANCE.createParameter();
parameter.setName(VIEW_NAME_PARAMETER);
parameter.setValue(mPart.getElementId());
item.getParameters().add(parameter);
item.setCommand(modeledCommand);
children.add(item);
}
}
}
Collections.sort(children, new MenuLabelComparator());
items.addAll(children);
//viewSubmenu.getChildren().addAll(children);
}
}
private MMenu getViewMenu(){
List<MMenuElement> mainMenus = application.getChildren().get(0).getMainMenu().getChildren();
for (MMenuElement mMenuElement : mainMenus) {
if(StringUtils.equals(mMenuElement.getElementId(), "goko.menu.window")){
MMenu windowsMenu = (MMenu) mMenuElement;
for (MMenuElement windowsElement : windowsMenu.getChildren()) {
if(StringUtils.equals(windowsElement.getElementId(), "goko.menu.window.view")){
return (MMenu) windowsElement;
}
}
}
}
return null;
}
}
class MenuLabelComparator implements Comparator<MMenuElement>{
/** (inheritDoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
@Override
public int compare(MMenuElement arg0, MMenuElement arg1) {
return StringUtils.defaultString(arg0.getLabel()).compareTo(arg1.getLabel());
}
}