5acacce6c03100d083026b86898febc4f53254a7
[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 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
33 import eu.etaxonomy.taxeditor.store.CdmStore;
34
35 /**
36 * Menu used in the store plugin xml to dynamically generate menu (sub-menu) contribution items
37 * for term types which when clicked open the defined term editor for the chosen term type
38 *
39 * @author pplitzner
40 * @date 21 Jul 2015
41 *
42 */
43
44 public class DefinedTermMenu extends CompoundContributionItem {
45
46
47 @Override
48 protected IContributionItem[] getContributionItems() {
49 Collection<IContributionItem> items = new ArrayList<IContributionItem>();
50 if(CdmStore.isActive()){
51 MenuManager dtMenuManager =
52 new MenuManager("Term Editor","eu.etaxonomy.taxeditor.store.definedTermEditorMenu");
53
54 dtMenuManager.setVisible(true);
55
56 items.add(dtMenuManager);
57 //add Feature and NamedArea to top level
58 dtMenuManager.add(addChildTermsToMenuManager(TermType.Feature));
59 dtMenuManager.add(addChildTermsToMenuManager(TermType.NamedArea));
60 dtMenuManager.add(new Separator());
61
62 MenuManager otherMenuManager =
63 new MenuManager("Others","eu.etaxonomy.taxeditor.store.term.other.menu");
64 otherMenuManager.setVisible(true);
65 dtMenuManager.add(otherMenuManager);
66
67 List<TermType> ttList = new ArrayList<TermType>(EnumSet.allOf(TermType.class));
68 Collections.sort(ttList,new SortByTermTypeMessage());
69 for (TermType tt : ttList)
70 {
71 //skip Feature and NamedArea as they have already been added to top level
72 if(tt.equals(TermType.Feature) || tt.equals(TermType.NamedArea)){
73 continue;
74 }
75 // if term type has a parent, do not add it
76 // it will be added in the recursive call
77 if(tt.getKindOf() == null) {
78 IContributionItem ici = addChildTermsToMenuManager(tt);
79 if(ici != null) {
80 otherMenuManager.add(ici);
81 }
82 }
83 }
84 }
85 return items.toArray(new IContributionItem[]{});
86 }
87
88 private IContributionItem addChildTermsToMenuManager(TermType termType) {
89
90 //FIXME : need a better way to find out if a term type can be editable (ticket 3853)
91 if(termType.getEmptyDefinedTermBase() != null) {
92 Set<TermType> children = termType.getGeneralizationOf();
93 // term type has no children, so create menu item
94 if(children.isEmpty()) {
95 return createMenuItem(termType);
96 }
97 // term type has children, so create sub menu
98 MenuManager dtMenuManager =
99 new MenuManager(termType.getMessage(PreferencesUtil.getGlobalLanguage()),"eu.etaxonomy.taxeditor.store." + termType.getKey() + "Menu");
100 dtMenuManager.setVisible(true);
101 dtMenuManager.add(createDefaultMenuItem(termType));
102
103 Separator sep = new Separator();
104 dtMenuManager.add(sep);
105 // add child items to the sub menu
106 for(TermType tt : children) {
107 IContributionItem item = addChildTermsToMenuManager(tt);
108 if(item != null) {
109 dtMenuManager.add(item);
110 }
111 }
112 return dtMenuManager;
113 } else {
114 return null;
115 }
116
117 }
118
119 private CommandContributionItem createMenuItem(TermType termType) {
120
121 Map<String, String> params = new HashMap<String, String>();
122 params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
123 termType.getUuid().toString());
124
125 CommandContributionItemParameter p = new CommandContributionItemParameter(
126 PlatformUI.getWorkbench(),
127 "",
128 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
129 params,
130 null,
131 null,
132 null,
133 termType.getMessage(),
134 "",
135 "",
136 SWT.PUSH,
137 "",
138 true);
139
140 CommandContributionItem item = new CommandContributionItem(p);
141 return item;
142
143 }
144
145 private CommandContributionItem createDefaultMenuItem(TermType termType) {
146
147 Map<String, String> params = new HashMap<String, String>();
148 params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
149 termType.getUuid().toString());
150
151 CommandContributionItemParameter p = new CommandContributionItemParameter(
152 PlatformUI.getWorkbench(),
153 "",
154 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
155 params,
156 null,
157 null,
158 null,
159 "Other " + termType.getMessage() + "s",
160 "",
161 "",
162 SWT.PUSH,
163 "",
164 true);
165
166
167
168 CommandContributionItem item = new CommandContributionItem(p);
169 return item;
170
171 }
172
173 private class SortByTermTypeMessage implements Comparator<TermType> {
174 @Override
175 public int compare(TermType t1, TermType t2) {
176 if (t1.equals(t2)){
177 return 0;
178 }
179 int result = t1.getMessage().compareTo(t2.getMessage());
180 if (result == 0){
181 return t1.compareTo(t2);
182 }
183 return result;
184 }
185 }
186
187
188 }