Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / editor / definedterm / DefinedTermMenu.java
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;
11
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;
19 import java.util.Map;
20 import java.util.Set;
21
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;
30
31 import eu.etaxonomy.cdm.model.common.TermType;
32
33 /**
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
36 *
37 * @author pplitzner
38 * @date 21 Jul 2015
39 *
40 */
41
42 public class DefinedTermMenu extends CompoundContributionItem {
43
44
45 @Override
46 protected IContributionItem[] getContributionItems() {
47 Collection<IContributionItem> items = new ArrayList<IContributionItem>();
48 MenuManager dtMenuManager =
49 new MenuManager("Term Editor","eu.etaxonomy.taxeditor.store.definedTermEditorMenu");
50
51 dtMenuManager.setVisible(true);
52
53 items.add(dtMenuManager);
54 //add Feature and NamedArea to top level
55 dtMenuManager.add(addChildTermsToMenuManager(TermType.Feature));
56 dtMenuManager.add(addChildTermsToMenuManager(TermType.NamedArea));
57
58 MenuManager otherMenuManager =
59 new MenuManager("Others","eu.etaxonomy.taxeditor.store.term.other.menu");
60 otherMenuManager.setVisible(true);
61 dtMenuManager.add(otherMenuManager);
62
63 List<TermType> ttList = new ArrayList<TermType>(EnumSet.allOf(TermType.class));
64 Collections.sort(ttList,new SortByTermTypeMessage());
65 for (TermType tt : ttList)
66 {
67 //skip Feature and NamedArea as they have already been added to top level
68 if(tt.equals(TermType.Feature) || tt.equals(TermType.NamedArea)){
69 continue;
70 }
71 // if term type has a parent, do not add it
72 // it will be added in the recursive call
73 if(tt.getKindOf() == null) {
74 IContributionItem ici = addChildTermsToMenuManager(tt);
75 if(ici != null) {
76 otherMenuManager.add(ici);
77 }
78 }
79 }
80 return items.toArray(new IContributionItem[]{});
81 }
82
83 private IContributionItem addChildTermsToMenuManager(TermType termType) {
84
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(children.isEmpty()) {
90 return createMenuItem(termType);
91 }
92 // term type has children, so create sub menu
93 MenuManager dtMenuManager =
94 new MenuManager(termType.getMessage(),"eu.etaxonomy.taxeditor.store." + termType.getKey() + "Menu");
95 dtMenuManager.setVisible(true);
96 dtMenuManager.add(createDefaultMenuItem(termType));
97
98 Separator sep = new Separator();
99 dtMenuManager.add(sep);
100 // add child items to the sub menu
101 for(TermType tt : children) {
102 IContributionItem item = addChildTermsToMenuManager(tt);
103 if(item != null) {
104 dtMenuManager.add(item);
105 }
106 }
107 return dtMenuManager;
108 } else {
109 return null;
110 }
111
112 }
113
114 private CommandContributionItem createMenuItem(TermType termType) {
115
116 Map<String, String> params = new HashMap<String, String>();
117 params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
118 termType.getUuid().toString());
119
120 CommandContributionItemParameter p = new CommandContributionItemParameter(
121 PlatformUI.getWorkbench(),
122 "",
123 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
124 params,
125 null,
126 null,
127 null,
128 termType.getMessage(),
129 "",
130 "",
131 SWT.PUSH,
132 "",
133 true);
134
135 CommandContributionItem item = new CommandContributionItem(p);
136 return item;
137
138 }
139
140 private CommandContributionItem createDefaultMenuItem(TermType termType) {
141
142 Map<String, String> params = new HashMap<String, String>();
143 params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
144 termType.getUuid().toString());
145
146 CommandContributionItemParameter p = new CommandContributionItemParameter(
147 PlatformUI.getWorkbench(),
148 "",
149 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
150 params,
151 null,
152 null,
153 null,
154 "Other " + termType.getMessage() + "s",
155 "",
156 "",
157 SWT.PUSH,
158 "",
159 true);
160
161
162
163 CommandContributionItem item = new CommandContributionItem(p);
164 return item;
165
166 }
167
168 private class SortByTermTypeMessage implements Comparator<TermType> {
169 @Override
170 public int compare(TermType t1, TermType t2) {
171 return t1.getMessage().compareTo(t2.getMessage());
172 }
173 }
174
175
176 }