ref #6694 l10n of FeatureTreeEditor
[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.l10n.Messages;
33 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
34 import eu.etaxonomy.taxeditor.store.CdmStore;
35
36 /**
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
39 *
40 * @author pplitzner
41 * @date 21 Jul 2015
42 *
43 */
44
45 public class DefinedTermMenu extends CompoundContributionItem {
46
47
48 @Override
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$
54
55 dtMenuManager.setVisible(true);
56
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());
62
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());
70
71 List<TermType> ttList = new ArrayList<TermType>(EnumSet.allOf(TermType.class));
72 Collections.sort(ttList,new SortByTermTypeMessage());
73 for (TermType tt : ttList)
74 {
75 //skip Feature and NamedArea as they have already been added to top level
76 if(tt.equals(TermType.Feature) || tt.equals(TermType.NamedArea)){
77 continue;
78 }
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);
83 if(ici != null) {
84 otherMenuManager.add(ici);
85 }
86 }
87 }
88 }
89 return items.toArray(new IContributionItem[]{});
90 }
91
92 private IContributionItem addChildTermsToMenuManager(TermType termType) {
93
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);
100 }
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));
106
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);
112 if(item != null) {
113 dtMenuManager.add(item);
114 }
115 }
116 return dtMenuManager;
117 } else {
118 return null;
119 }
120
121 }
122
123 private CommandContributionItem createMenuItem(TermType termType) {
124
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());
128
129 CommandContributionItemParameter p = new CommandContributionItemParameter(
130 PlatformUI.getWorkbench(),
131 "", //$NON-NLS-1$
132 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor", //$NON-NLS-1$
133 params,
134 null,
135 null,
136 null,
137 termType.getMessage(),
138 "", //$NON-NLS-1$
139 "", //$NON-NLS-1$
140 SWT.PUSH,
141 "", //$NON-NLS-1$
142 true);
143
144 CommandContributionItem item = new CommandContributionItem(p);
145 return item;
146
147 }
148
149 private CommandContributionItem createFeatureTreeMenuItem() {
150
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$
154
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$
159 params,
160 null,
161 null,
162 null,
163 Messages.DefinedTermMenu_FEATURE_TREE,
164 "", //$NON-NLS-1$
165 "", //$NON-NLS-1$
166 SWT.PUSH,
167 "", //$NON-NLS-1$
168 true);
169
170
171
172 CommandContributionItem item = new CommandContributionItem(p);
173 return item;
174
175 }
176
177 private CommandContributionItem createDefaultMenuItem(TermType termType) {
178
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());
182
183 CommandContributionItemParameter p = new CommandContributionItemParameter(
184 PlatformUI.getWorkbench(),
185 "", //$NON-NLS-1$
186 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor", //$NON-NLS-1$
187 params,
188 null,
189 null,
190 null,
191 String.format(Messages.DefinedTermMenu_OTHER_S, termType.getMessage()),
192 "", //$NON-NLS-1$
193 "", //$NON-NLS-1$
194 SWT.PUSH,
195 "", //$NON-NLS-1$
196 true);
197
198
199
200 CommandContributionItem item = new CommandContributionItem(p);
201 return item;
202
203 }
204
205 private class SortByTermTypeMessage implements Comparator<TermType> {
206 @Override
207 public int compare(TermType t1, TermType t2) {
208 if (t1.equals(t2)){
209 return 0;
210 }
211 int result = t1.getMessage().compareTo(t2.getMessage());
212 if (result == 0){
213 return t1.compareTo(t2);
214 }
215 return result;
216 }
217 }
218
219
220 }