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
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collection
;
14 import java
.util
.Collections
;
15 import java
.util
.Comparator
;
16 import java
.util
.EnumSet
;
17 import java
.util
.HashMap
;
18 import java
.util
.List
;
22 import org
.eclipse
.jface
.action
.IContributionItem
;
23 import org
.eclipse
.jface
.action
.MenuManager
;
24 import org
.eclipse
.jface
.action
.Separator
;
25 import org
.eclipse
.swt
.SWT
;
26 import org
.eclipse
.ui
.PlatformUI
;
27 import org
.eclipse
.ui
.actions
.CompoundContributionItem
;
28 import org
.eclipse
.ui
.menus
.CommandContributionItem
;
29 import org
.eclipse
.ui
.menus
.CommandContributionItemParameter
;
31 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
32 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
33 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
34 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
37 * Menu used in the store plugin xml to dynamically generate menu (sub-menu) contribution items
38 * for term types which when clicked open the defined term editor for the chosen term type
45 public class DefinedTermMenu
extends CompoundContributionItem
{
49 protected IContributionItem
[] getContributionItems() {
50 Collection
<IContributionItem
> items
= new ArrayList
<IContributionItem
>();
51 if(CdmStore
.isActive()){
52 MenuManager dtMenuManager
=
53 new MenuManager(Messages
.DefinedTermMenu_TERM_EDITOR
,"eu.etaxonomy.taxeditor.store.definedTermEditorMenu"); //$NON-NLS-2$
55 dtMenuManager
.setVisible(true);
57 items
.add(dtMenuManager
);
58 //add Feature and NamedArea to top level
59 dtMenuManager
.add(addChildTermsToMenuManager(TermType
.Feature
));
60 dtMenuManager
.add(addChildTermsToMenuManager(TermType
.NamedArea
));
61 dtMenuManager
.add(new Separator());
63 MenuManager otherMenuManager
=
64 new MenuManager(Messages
.DefinedTermMenu_OTHERS
,"eu.etaxonomy.taxeditor.store.term.other.menu"); //$NON-NLS-2$
65 otherMenuManager
.setVisible(true);
66 dtMenuManager
.add(otherMenuManager
);
67 //FIXME E4: This should be removed during e4 migration. dynamic menu should be declared in model fragment
68 dtMenuManager
.add(new Separator());
69 dtMenuManager
.add(createFeatureTreeMenuItem());
71 List
<TermType
> ttList
= new ArrayList
<TermType
>(EnumSet
.allOf(TermType
.class));
72 Collections
.sort(ttList
,new SortByTermTypeMessage());
73 for (TermType tt
: ttList
)
75 //skip Feature and NamedArea as they have already been added to top level
76 if(tt
.equals(TermType
.Feature
) || tt
.equals(TermType
.NamedArea
)){
79 // if term type has a parent, do not add it
80 // it will be added in the recursive call
81 if(tt
.getKindOf() == null) {
82 IContributionItem ici
= addChildTermsToMenuManager(tt
);
84 otherMenuManager
.add(ici
);
89 return items
.toArray(new IContributionItem
[]{});
92 private IContributionItem
addChildTermsToMenuManager(TermType termType
) {
94 //FIXME : need a better way to find out if a term type can be editable (ticket 3853)
95 if(termType
.getEmptyDefinedTermBase() != null) {
96 Set
<TermType
> children
= termType
.getGeneralizationOf();
97 // term type has no children, so create menu item
98 if(children
.isEmpty()) {
99 return createMenuItem(termType
);
101 // term type has children, so create sub menu
102 MenuManager dtMenuManager
=
103 new MenuManager(termType
.getMessage(PreferencesUtil
.getGlobalLanguage()),"eu.etaxonomy.taxeditor.store." + termType
.getKey() + Messages
.DefinedTermMenu_MENU
); //$NON-NLS-1$
104 dtMenuManager
.setVisible(true);
105 dtMenuManager
.add(createDefaultMenuItem(termType
));
107 Separator sep
= new Separator();
108 dtMenuManager
.add(sep
);
109 // add child items to the sub menu
110 for(TermType tt
: children
) {
111 IContributionItem item
= addChildTermsToMenuManager(tt
);
113 dtMenuManager
.add(item
);
116 return dtMenuManager
;
123 private CommandContributionItem
createMenuItem(TermType termType
) {
125 Map
<String
, String
> params
= new HashMap
<String
, String
>();
126 params
.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid", //$NON-NLS-1$
127 termType
.getUuid().toString());
129 CommandContributionItemParameter p
= new CommandContributionItemParameter(
130 PlatformUI
.getWorkbench(),
132 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor", //$NON-NLS-1$
137 termType
.getMessage(),
144 CommandContributionItem item
= new CommandContributionItem(p
);
149 private CommandContributionItem
createFeatureTreeMenuItem() {
151 Map
<String
, String
> params
= new HashMap
<String
, String
>();
152 params
.put("eu.etaxonomy.taxeditor.workbench.commandparameter.partName", //$NON-NLS-1$
153 "eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditor"); //$NON-NLS-1$
155 CommandContributionItemParameter p
= new CommandContributionItemParameter(
156 PlatformUI
.getWorkbench(),
157 "eu.etaxonomy.taxeditor.featuretree.commandContributionItemParameter", //$NON-NLS-1$
158 "eu.etaxonomy.taxeditor.command.openPart", //$NON-NLS-1$
163 Messages
.DefinedTermMenu_FEATURE_TREE
,
172 CommandContributionItem item
= new CommandContributionItem(p
);
177 private CommandContributionItem
createDefaultMenuItem(TermType termType
) {
179 Map
<String
, String
> params
= new HashMap
<String
, String
>();
180 params
.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid", //$NON-NLS-1$
181 termType
.getUuid().toString());
183 CommandContributionItemParameter p
= new CommandContributionItemParameter(
184 PlatformUI
.getWorkbench(),
186 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor", //$NON-NLS-1$
191 String
.format(Messages
.DefinedTermMenu_OTHER_S
, termType
.getMessage()),
200 CommandContributionItem item
= new CommandContributionItem(p
);
205 private class SortByTermTypeMessage
implements Comparator
<TermType
> {
207 public int compare(TermType t1
, TermType t2
) {
211 int result
= t1
.getMessage().compareTo(t2
.getMessage());
213 return t1
.compareTo(t2
);