3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.editor
.definedterm
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collection
;
15 import java
.util
.Collections
;
16 import java
.util
.Comparator
;
17 import java
.util
.EnumSet
;
18 import java
.util
.HashMap
;
19 import java
.util
.List
;
23 import org
.eclipse
.jface
.action
.IContributionItem
;
24 import org
.eclipse
.jface
.action
.MenuManager
;
25 import org
.eclipse
.jface
.action
.Separator
;
26 import org
.eclipse
.swt
.SWT
;
27 import org
.eclipse
.ui
.PlatformUI
;
28 import org
.eclipse
.ui
.actions
.CompoundContributionItem
;
29 import org
.eclipse
.ui
.menus
.CommandContributionItem
;
30 import org
.eclipse
.ui
.menus
.CommandContributionItemParameter
;
32 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
35 * Menu used in the store plugin xml to dynamically generate menu (sub-menu) contribution items
36 * for term types which when clicked open the defined term editor for the chosen term type
43 public class DefinedTermMenu
extends CompoundContributionItem
{
47 protected IContributionItem
[] getContributionItems() {
48 Collection
<IContributionItem
> items
= new ArrayList
<IContributionItem
>();
49 MenuManager dtMenuManager
=
50 new MenuManager("Term Editor","eu.etaxonomy.taxeditor.store.definedTermEditorMenu");
52 dtMenuManager
.setVisible(true);
54 items
.add(dtMenuManager
);
55 List
<TermType
> ttList
= new ArrayList
<TermType
>(EnumSet
.allOf(TermType
.class));
56 Collections
.sort(ttList
,new SortByTermTypeMessage());
57 for (TermType tt
: ttList
)
59 // if term type has a parent, do not add it
60 // it will be added in the recursive call
61 if(tt
.getKindOf() == null) {
62 IContributionItem ici
= addChildTermsToMenuManager(tt
);
64 dtMenuManager
.add(ici
);
68 return items
.toArray(new IContributionItem
[]{});
71 private IContributionItem
addChildTermsToMenuManager(TermType termType
) {
73 //FIXME : need a better way to find out if a term type can be editable (ticket 3853)
74 if(termType
.getEmptyDefinedTermBase() != null) {
75 Set
<TermType
> children
= termType
.getGeneralizationOf();
76 // term type has no children, so create menu item
77 if(children
.isEmpty()) {
78 return createMenuItem(termType
);
80 // term type has children, so create sub menu
81 MenuManager dtMenuManager
=
82 new MenuManager(termType
.getMessage(),"eu.etaxonomy.taxeditor.store." + termType
.getKey() + "Menu");
83 dtMenuManager
.setVisible(true);
84 dtMenuManager
.add(createDefaultMenuItem(termType
));
86 Separator sep
= new Separator();
87 dtMenuManager
.add(sep
);
88 // add child items to the sub menu
89 for(TermType tt
: children
) {
90 IContributionItem item
= addChildTermsToMenuManager(tt
);
92 dtMenuManager
.add(item
);
102 private CommandContributionItem
createMenuItem(TermType termType
) {
104 Map
<String
, String
> params
= new HashMap
<String
, String
>();
105 params
.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
106 termType
.getUuid().toString());
108 CommandContributionItemParameter p
= new CommandContributionItemParameter(
109 PlatformUI
.getWorkbench(),
111 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
116 termType
.getMessage(),
123 CommandContributionItem item
= new CommandContributionItem(p
);
128 private CommandContributionItem
createDefaultMenuItem(TermType termType
) {
130 Map
<String
, String
> params
= new HashMap
<String
, String
>();
131 params
.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
132 termType
.getUuid().toString());
134 CommandContributionItemParameter p
= new CommandContributionItemParameter(
135 PlatformUI
.getWorkbench(),
137 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
142 "Other " + termType
.getMessage() + "s",
151 CommandContributionItem item
= new CommandContributionItem(p
);
156 private class SortByTermTypeMessage
implements Comparator
<TermType
> {
158 public int compare(TermType t1
, TermType t2
) {
159 return t1
.getMessage().compareTo(t2
.getMessage());