1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.editor.definedterm.e4;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Collections;
|
14
|
import java.util.Comparator;
|
15
|
import java.util.EnumSet;
|
16
|
import java.util.List;
|
17
|
import java.util.Set;
|
18
|
|
19
|
import org.eclipse.e4.ui.di.AboutToShow;
|
20
|
import org.eclipse.e4.ui.model.application.commands.MCommand;
|
21
|
import org.eclipse.e4.ui.model.application.commands.MCommandsFactory;
|
22
|
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
|
23
|
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
|
24
|
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
|
25
|
import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
|
26
|
|
27
|
import eu.etaxonomy.cdm.model.term.TermType;
|
28
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
29
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
30
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
31
|
|
32
|
/**
|
33
|
*
|
34
|
* Menu to dynamically generate menu (sub-menu) contribution items
|
35
|
* for term types which when clicked open the defined term editor for the chosen term type
|
36
|
*
|
37
|
* @author pplitzner
|
38
|
* @since Aug 23, 2017
|
39
|
*
|
40
|
*/
|
41
|
public class DefinedTermMenuE4 {
|
42
|
|
43
|
private static final String COMMAND_ID = "eu.etaxonomy.taxeditor.store.openDefinedTermEditor";
|
44
|
|
45
|
/** {@inheritDoc} */
|
46
|
@AboutToShow
|
47
|
public void aboutToShow(List<MMenuElement> items) {
|
48
|
|
49
|
if(CdmStore.isActive()){
|
50
|
|
51
|
//add Feature and NamedArea to top level
|
52
|
addChildTermsToMenuManager(items, TermType.Feature, true);
|
53
|
addChildTermsToMenuManager(items, TermType.NamedArea);
|
54
|
|
55
|
MMenu otherMenu = MMenuFactory.INSTANCE.createMenu();
|
56
|
otherMenu.setLabel(Messages.DefinedTermMenu_OTHERS);
|
57
|
items.add(otherMenu);
|
58
|
|
59
|
items.add(MMenuFactory.INSTANCE.createMenuSeparator());
|
60
|
|
61
|
List<TermType> ttList = new ArrayList<TermType>(EnumSet.allOf(TermType.class));
|
62
|
Collections.sort(ttList,new SortByTermTypeMessage());
|
63
|
for (TermType tt : ttList)
|
64
|
{
|
65
|
//skip Feature and NamedArea as they have already been added to top level
|
66
|
if(tt.equals(TermType.Feature) || tt.equals(TermType.NamedArea)){
|
67
|
continue;
|
68
|
}
|
69
|
// if term type has a parent, do not add it
|
70
|
// it will be added in the recursive call
|
71
|
if(tt.getKindOf() == null) {
|
72
|
addChildTermsToMenuManager(otherMenu.getChildren(), tt);
|
73
|
}
|
74
|
}
|
75
|
}
|
76
|
}
|
77
|
|
78
|
private void addChildTermsToMenuManager(List<MMenuElement> items, TermType termType) {
|
79
|
addChildTermsToMenuManager(items, termType, false);
|
80
|
}
|
81
|
|
82
|
private void addChildTermsToMenuManager(List<MMenuElement> items, TermType termType, boolean skipChildren) {
|
83
|
|
84
|
//FIXME : need a better way to find out if a term type can be editable (ticket 3853)
|
85
|
if(termType.getEmptyDefinedTermBase() != null) {
|
86
|
Set<TermType> children = termType.getGeneralizationOf();
|
87
|
// term type has no children, so create menu item
|
88
|
if(skipChildren || children.isEmpty()) {
|
89
|
items.add(createMenuItem(termType));
|
90
|
}
|
91
|
else{
|
92
|
MMenu subMenu = MMenuFactory.INSTANCE.createMenu();
|
93
|
subMenu.setLabel(termType.getMessage(PreferencesUtil.getGlobalLanguage()));
|
94
|
items.add(subMenu);
|
95
|
// term type has children, so create sub menu
|
96
|
subMenu.getChildren().add(createDefaultMenuItem(termType));
|
97
|
|
98
|
subMenu.getChildren().add(MMenuFactory.INSTANCE.createMenuSeparator());
|
99
|
|
100
|
// add child items to the sub menu
|
101
|
for(TermType tt : children) {
|
102
|
addChildTermsToMenuManager(subMenu.getChildren(), tt);
|
103
|
}
|
104
|
}
|
105
|
}
|
106
|
|
107
|
}
|
108
|
|
109
|
private MHandledMenuItem createMenuItem(TermType termType) {
|
110
|
|
111
|
MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
|
112
|
menuItem.setLabel(termType.getMessage());
|
113
|
MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
|
114
|
mCommand.setElementId(COMMAND_ID);
|
115
|
|
116
|
//set params
|
117
|
menuItem.getTransientData().put(COMMAND_ID+".termTypeUuid", termType.getUuid());
|
118
|
|
119
|
menuItem.setCommand(mCommand);
|
120
|
return menuItem;
|
121
|
}
|
122
|
|
123
|
private MHandledMenuItem createDefaultMenuItem(TermType termType) {
|
124
|
|
125
|
MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
|
126
|
menuItem.setLabel(String.format(Messages.DefinedTermMenu_OTHER_S, termType.getMessage()));
|
127
|
MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
|
128
|
mCommand.setElementId(COMMAND_ID);
|
129
|
|
130
|
//set params
|
131
|
menuItem.getTransientData().put(COMMAND_ID+".termTypeUuid", termType.getUuid());
|
132
|
|
133
|
menuItem.setCommand(mCommand);
|
134
|
|
135
|
return menuItem;
|
136
|
}
|
137
|
|
138
|
private class SortByTermTypeMessage implements Comparator<TermType> {
|
139
|
@Override
|
140
|
public int compare(TermType t1, TermType t2) {
|
141
|
if (t1.equals(t2)){
|
142
|
return 0;
|
143
|
}
|
144
|
int result = t1.getMessage().compareTo(t2.getMessage());
|
145
|
if (result == 0){
|
146
|
return t1.compareTo(t2);
|
147
|
}
|
148
|
return result;
|
149
|
}
|
150
|
}
|
151
|
|
152
|
|
153
|
}
|