2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
.definedterm
.e4
;
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
;
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
;
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
;
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
41 public class DefinedTermMenuE4
{
43 private static final String COMMAND_ID
= "eu.etaxonomy.taxeditor.store.openDefinedTermEditor";
47 public void aboutToShow(List
<MMenuElement
> items
) {
49 if(CdmStore
.isActive()){
51 //add Feature and NamedArea to top level
52 // addChildTermsToMenuManager(items, TermType.Feature, true);
53 // addChildTermsToMenuManager(items, TermType.NamedArea);
55 MMenu otherMenu
= MMenuFactory
.INSTANCE
.createMenu();
56 otherMenu
.setLabel(Messages
.DefinedTermMenu_Vocabularies
);
59 items
.add(MMenuFactory
.INSTANCE
.createMenuSeparator());
61 List
<TermType
> ttList
= new ArrayList
<TermType
>(EnumSet
.allOf(TermType
.class));
62 Collections
.sort(ttList
,new SortByTermTypeMessage());
63 for (TermType tt
: ttList
)
65 //skip Feature and NamedArea as they have already been added to top level
66 // if(tt.equals(TermType.Feature) || tt.equals(TermType.NamedArea)){
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 || tt
.equals(TermType
.Character
)) {// special case for Character because subtype of Feature
73 addChildTermsToMenuManager(otherMenu
.getChildren(), tt
);
79 private void addChildTermsToMenuManager(List
<MMenuElement
> items
, TermType termType
) {
80 addChildTermsToMenuManager(items
, termType
, false);
83 private void addChildTermsToMenuManager(List
<MMenuElement
> items
, TermType termType
, boolean skipChildren
) {
85 //FIXME : need a better way to find out if a term type can be editable (ticket 3853)
86 if(termType
.getEmptyDefinedTermBase() != null) {
87 Set
<TermType
> children
= termType
.getGeneralizationOf();
88 // term type has no children, so create menu item
89 if(skipChildren
|| children
.isEmpty()) {
90 items
.add(createMenuItem(termType
));
93 MMenu subMenu
= MMenuFactory
.INSTANCE
.createMenu();
94 subMenu
.setLabel(termType
.getLabel(PreferencesUtil
.getGlobalLanguage()));
96 // term type has children, so create sub menu
97 subMenu
.getChildren().add(createDefaultMenuItem(termType
));
99 subMenu
.getChildren().add(MMenuFactory
.INSTANCE
.createMenuSeparator());
101 // add child items to the sub menu
102 for(TermType tt
: children
) {
103 addChildTermsToMenuManager(subMenu
.getChildren(), tt
);
110 private MHandledMenuItem
createMenuItem(TermType termType
) {
112 MHandledMenuItem menuItem
= MMenuFactory
.INSTANCE
.createHandledMenuItem();
113 menuItem
.setLabel(termType
.getLabel());
114 MCommand mCommand
= MCommandsFactory
.INSTANCE
.createCommand();
115 mCommand
.setElementId(COMMAND_ID
);
118 menuItem
.getTransientData().put(COMMAND_ID
+".termTypeUuid", termType
.getUuid());
120 menuItem
.setCommand(mCommand
);
124 private MHandledMenuItem
createDefaultMenuItem(TermType termType
) {
126 MHandledMenuItem menuItem
= MMenuFactory
.INSTANCE
.createHandledMenuItem();
127 menuItem
.setLabel(String
.format(Messages
.DefinedTermMenu_OTHER_S
, termType
.getLabel()));
128 MCommand mCommand
= MCommandsFactory
.INSTANCE
.createCommand();
129 mCommand
.setElementId(COMMAND_ID
);
132 menuItem
.getTransientData().put(COMMAND_ID
+".termTypeUuid", termType
.getUuid());
134 menuItem
.setCommand(mCommand
);
139 private class SortByTermTypeMessage
implements Comparator
<TermType
> {
141 public int compare(TermType t1
, TermType t2
) {
145 int result
= t1
.getLabel().compareTo(t2
.getLabel());
147 return t1
.compareTo(t2
);