Add @Override
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / editor / definedterm / DefinedTermMenuFactory.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.editor.definedterm;
12
13 import java.util.ArrayList;
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.menus.CommandContributionItem;
27 import org.eclipse.ui.menus.CommandContributionItemParameter;
28 import org.eclipse.ui.menus.ExtensionContributionFactory;
29 import org.eclipse.ui.menus.IContributionRoot;
30 import org.eclipse.ui.services.IServiceLocator;
31
32 import eu.etaxonomy.cdm.model.common.TermType;
33
34 /**
35 * Menu factory 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
37 *
38 * @author c.mathew
39 * @date 18 Jul 2013
40 *
41 */
42
43 public class DefinedTermMenuFactory extends ExtensionContributionFactory {
44
45 @Override
46 public void createContributionItems(IServiceLocator serviceLocator,
47 IContributionRoot additions) {
48 MenuManager dtMenuManager =
49 new MenuManager("Term Editor","eu.etaxonomy.taxeditor.store.definedTermEditorMenu");
50
51 dtMenuManager.setVisible(true);
52
53 additions.addContributionItem(dtMenuManager, null);
54 List<TermType> ttList = new ArrayList<TermType>(EnumSet.allOf(TermType.class));
55 Collections.sort(ttList,new SortByTermTypeMessage());
56 for (TermType tt : ttList)
57 {
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, serviceLocator);
62 if(ici != null) {
63 dtMenuManager.add(ici);
64 }
65 }
66 }
67 }
68
69 private IContributionItem addChildTermsToMenuManager(TermType termType, IServiceLocator serviceLocator) {
70
71 //FIXME : need a better way to find out if a term type can be editable (ticket 3853)
72 if(termType.getEmptyDefinedTermBase() != null) {
73 Set<TermType> children = termType.getGeneralizationOf();
74 // term type has no children, so create menu item
75 if(children.isEmpty()) {
76 return createMenuItem(termType, serviceLocator);
77 }
78 // term type has children, so create sub menu
79 MenuManager dtMenuManager =
80 new MenuManager(termType.getMessage(),"eu.etaxonomy.taxeditor.store." + termType.getKey() + "Menu");
81 dtMenuManager.setVisible(true);
82 dtMenuManager.add(createDefaultMenuItem(termType, serviceLocator));
83
84 Separator sep = new Separator();
85 dtMenuManager.add(sep);
86 // add child items to the sub menu
87 for(TermType tt : children) {
88 IContributionItem item = addChildTermsToMenuManager(tt,serviceLocator);
89 if(item != null) {
90 dtMenuManager.add(item);
91 }
92 }
93 return dtMenuManager;
94 } else {
95 return null;
96 }
97
98 }
99
100 private CommandContributionItem createMenuItem(TermType termType, IServiceLocator serviceLocator) {
101
102 Map<String, String> params = new HashMap<String, String>();
103 params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
104 termType.getUuid().toString());
105
106 CommandContributionItemParameter p = new CommandContributionItemParameter(
107 serviceLocator,
108 "",
109 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
110 params,
111 null,
112 null,
113 null,
114 termType.getMessage(),
115 "",
116 "",
117 SWT.PUSH,
118 "",
119 true);
120
121 CommandContributionItem item = new CommandContributionItem(p);
122 return item;
123
124 }
125
126 private CommandContributionItem createDefaultMenuItem(TermType termType, IServiceLocator serviceLocator) {
127
128 Map<String, String> params = new HashMap<String, String>();
129 params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
130 termType.getUuid().toString());
131
132 CommandContributionItemParameter p = new CommandContributionItemParameter(
133 serviceLocator,
134 "",
135 "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
136 params,
137 null,
138 null,
139 null,
140 "Other " + termType.getMessage() + "s",
141 "",
142 "",
143 SWT.PUSH,
144 "",
145 true);
146
147
148
149 CommandContributionItem item = new CommandContributionItem(p);
150 return item;
151
152 }
153
154 private class SortByTermTypeMessage implements Comparator<TermType> {
155 @Override
156 public int compare(TermType t1, TermType t2) {
157 return t1.getMessage().compareTo(t2.getMessage());
158 }
159 }
160
161
162 }