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
;
34 * Menu used in the store plugin xml 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
42 public class DefinedTermMenu
extends CompoundContributionItem
{
46 protected IContributionItem
[] getContributionItems() {
47 Collection
<IContributionItem
> items
= new ArrayList
<IContributionItem
>();
48 MenuManager dtMenuManager
=
49 new MenuManager("Term Editor","eu.etaxonomy.taxeditor.store.definedTermEditorMenu");
51 dtMenuManager
.setVisible(true);
53 items
.add(dtMenuManager
);
54 List
<TermType
> ttList
= new ArrayList
<TermType
>(EnumSet
.allOf(TermType
.class));
55 Collections
.sort(ttList
,new SortByTermTypeMessage());
56 for (TermType tt
: ttList
)
58 // if term type has a parent, do not add it
59 // it will be added in the recursive call
60 if(tt
.getKindOf() == null) {
61 IContributionItem ici
= addChildTermsToMenuManager(tt
);
63 dtMenuManager
.add(ici
);
67 return items
.toArray(new IContributionItem
[]{});
70 private IContributionItem
addChildTermsToMenuManager(TermType termType
) {
72 //FIXME : need a better way to find out if a term type can be editable (ticket 3853)
73 if(termType
.getEmptyDefinedTermBase() != null) {
74 Set
<TermType
> children
= termType
.getGeneralizationOf();
75 // term type has no children, so create menu item
76 if(children
.isEmpty()) {
77 return createMenuItem(termType
);
79 // term type has children, so create sub menu
80 MenuManager dtMenuManager
=
81 new MenuManager(termType
.getMessage(),"eu.etaxonomy.taxeditor.store." + termType
.getKey() + "Menu");
82 dtMenuManager
.setVisible(true);
83 dtMenuManager
.add(createDefaultMenuItem(termType
));
85 Separator sep
= new Separator();
86 dtMenuManager
.add(sep
);
87 // add child items to the sub menu
88 for(TermType tt
: children
) {
89 IContributionItem item
= addChildTermsToMenuManager(tt
);
91 dtMenuManager
.add(item
);
101 private CommandContributionItem
createMenuItem(TermType termType
) {
103 Map
<String
, String
> params
= new HashMap
<String
, String
>();
104 params
.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
105 termType
.getUuid().toString());
107 CommandContributionItemParameter p
= new CommandContributionItemParameter(
108 PlatformUI
.getWorkbench(),
110 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
115 termType
.getMessage(),
122 CommandContributionItem item
= new CommandContributionItem(p
);
127 private CommandContributionItem
createDefaultMenuItem(TermType termType
) {
129 Map
<String
, String
> params
= new HashMap
<String
, String
>();
130 params
.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
131 termType
.getUuid().toString());
133 CommandContributionItemParameter p
= new CommandContributionItemParameter(
134 PlatformUI
.getWorkbench(),
136 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
141 "Other " + termType
.getMessage() + "s",
150 CommandContributionItem item
= new CommandContributionItem(p
);
155 private class SortByTermTypeMessage
implements Comparator
<TermType
> {
157 public int compare(TermType t1
, TermType t2
) {
158 return t1
.getMessage().compareTo(t2
.getMessage());